Index: trunk/extensions/ImportFreeImages/README |
— | — | @@ -3,6 +3,7 @@ |
4 | 4 | |
5 | 5 | == License == |
6 | 6 | |
| 7 | +Copyright 2010 Bryan Tong Minh |
7 | 8 | Copyright 2006 Travis Derouin, wikiHow |
8 | 9 | |
9 | 10 | This program is free software; you can redistribute it and/or modify |
— | — | @@ -25,13 +26,13 @@ |
26 | 27 | |
27 | 28 | == Pre-requisites == |
28 | 29 | |
29 | | -This extension was tested with Mediawiki 1.8.2. |
| 30 | +This extension was tested with Mediawiki 1.16. |
30 | 31 | |
31 | 32 | == Configuration & Installation == |
32 | 33 | |
33 | 34 | * Get the source via [http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/ImportFreeImages SVN] |
34 | 35 | * You have to have already set up uploads to work properly on your wiki. |
35 | | -* You may have to manully create the wgTmpDirectory (typically wiki/images/tmp) |
| 36 | +* You may have to manually create the wgTmpDirectory (typically wiki/images/tmp) |
36 | 37 | * Download and install [http://sourceforge.net/projects/phpflickr phpflickr 2.0] and put it in the directory extensions/ImportFreeImages (so you should have a directory wiki/extensions/phpFlickr-2.0.0). |
37 | 38 | * Add this line to your LocalSettings.php |
38 | 39 | <pre> |
— | — | @@ -46,7 +47,7 @@ |
47 | 48 | |
48 | 49 | == Bugs and enhancements == |
49 | 50 | |
50 | | -Bugs or feature requests can be sent to the author at travis @ wikihow.com. |
| 51 | +Bugs or feature requests can be posted to bugzilla: https://bugzilla.wikimedia.org |
51 | 52 | |
52 | 53 | Other ideas offering radio buttons and checkboxes to allow users to customize which licenses are searched and how the results are sorted. Other API sources could be added to complement the flickr functionality. |
53 | 54 | |
Index: trunk/extensions/ImportFreeImages/UploadFreeImage.php |
— | — | @@ -0,0 +1,97 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class UploadFreeImage extends UploadFromUrl { |
| 5 | + /** |
| 6 | + * Hook to UploadCreateOnRequest. |
| 7 | + * |
| 8 | + * This class processes wpSourceType=IFI |
| 9 | + */ |
| 10 | + public static function onUploadCreateFromRequest( $type, &$className ) { |
| 11 | + if ( $type == 'IFI' ) { |
| 12 | + $className = 'UploadFreeImage'; |
| 13 | + // Stop processing |
| 14 | + return false; |
| 15 | + } |
| 16 | + return true; |
| 17 | + } |
| 18 | + |
| 19 | + public static function isEnabled() { return true; } |
| 20 | + |
| 21 | + /** |
| 22 | + * A valid request requires wpFlickrId to be set |
| 23 | + */ |
| 24 | + public static function isValidRequest( $request ) { |
| 25 | + return (bool)$request->getVal( 'wpFlickrId' ); |
| 26 | + } |
| 27 | + |
| 28 | + public function initializeFromRequest( &$request ) { |
| 29 | + return $this->initialize( |
| 30 | + $request->getText( 'wpDestFile' ), |
| 31 | + self::getUrl( $request->getText( 'wpFlickrId' ), $request->getText( 'wpSize' ) ), |
| 32 | + false |
| 33 | + ); |
| 34 | + } |
| 35 | + public static function getUrl( $flickrId, $requestedSize ) { |
| 36 | + if ( !$requestedSize ) |
| 37 | + return false; |
| 38 | + |
| 39 | + $ifi = new ImportFreeImages(); |
| 40 | + $sizes = $ifi->getSizes( $flickrId ); |
| 41 | + |
| 42 | + foreach ( $sizes as $size ) { |
| 43 | + if ( $size['label'] === $requestedSize ) { |
| 44 | + return $size['source']; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * UI hook to add an extra text box to the upload form |
| 53 | + */ |
| 54 | + public static function onUploadFormSourceDescriptors( &$descriptor, &$radio, $selectedSourceType ) { |
| 55 | + global $wgRequest; |
| 56 | + if ( $wgRequest->getVal( 'wpSourceType' ) != 'IFI' || !$wgRequest->getCheck( 'wpFlickrId' ) ) |
| 57 | + return true; |
| 58 | + |
| 59 | + // We entered here from Special:ImportFreeImages, so kill all other source selections |
| 60 | + foreach ( $descriptor as $name => $value ) { |
| 61 | + if ( isset( $value['section'] ) && $value['section'] == 'source' ) |
| 62 | + unset( $descriptor[$name] ); |
| 63 | + } |
| 64 | + |
| 65 | + $ifi = new ImportFreeImages(); |
| 66 | + $sizes = $ifi->getSizes( $wgRequest->getText( 'wpFlickrId' ) ); |
| 67 | + |
| 68 | + $options = array(); |
| 69 | + foreach ( $sizes as $size ) { |
| 70 | + $label = wfMsgExt( 'importfreeimages_size_' . strtolower( $size['label'] ), 'parseinline' ); |
| 71 | + $options[$label] = $size['label']; |
| 72 | + } |
| 73 | + |
| 74 | + $descriptor['Size'] = array( |
| 75 | + 'type' => 'radio', |
| 76 | + 'section' => 'source', |
| 77 | + 'name' => 'Size', |
| 78 | + 'options' => $options |
| 79 | + ); |
| 80 | + $descriptor['wpFlickrId'] = array( |
| 81 | + 'type' => 'hidden', |
| 82 | + 'name' => 'wpFlickrId', |
| 83 | + 'default' => $wgRequest->getText( 'wpFlickrId' ), |
| 84 | + ); |
| 85 | + $descriptor['wpSourceType'] = array( |
| 86 | + 'type' => 'hidden', |
| 87 | + 'name' => 'wpSourceType', |
| 88 | + 'default' => 'IFI', |
| 89 | + ); |
| 90 | + |
| 91 | + // Stop running further hooks |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + public static function onUploadFormInitDescriptor( &$descriptor ) { |
| 96 | + return true; |
| 97 | + } |
| 98 | +} |
\ No newline at end of file |
Property changes on: trunk/extensions/ImportFreeImages/UploadFreeImage.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 99 | + native |
Index: trunk/extensions/ImportFreeImages/ImportFreeImages.body.php |
— | — | @@ -1,353 +1,92 @@ |
2 | 2 | <?php |
3 | | -// Sanity check - check for MediaWiki environment... |
4 | | -if( !defined( 'MEDIAWIKI' ) ) { |
5 | | - die( "This is an extension to the MediaWiki package and cannot be run standalone.\n" ); |
6 | | -} |
7 | 3 | |
8 | | -class ImportFreeImages extends SpecialPage { |
9 | | - |
10 | | - /** |
11 | | - * Constructor |
12 | | - */ |
| 4 | +class ImportFreeImages { |
| 5 | + public $resultsPerPage; |
| 6 | + public $resultsPerRow; |
| 7 | + public $thumbType; |
| 8 | + |
13 | 9 | public function __construct() { |
14 | | - parent::__construct( 'ImportFreeImages'/*class*/, 'upload'/*restriction*/ ); |
| 10 | + # Load settings |
| 11 | + global $wgIFI_FlickrAPIKey, $wgIFI_CreditsTemplate, $wgIFI_GetOriginal, |
| 12 | + $wgIFI_PromptForFilename, $wgIFI_phpFlickr, $wgIFI_ResultsPerRow, |
| 13 | + $wgIFI_ResultsPerPage, $wgIFI_FlickrLicense, $wgIFI_FlickrSort, |
| 14 | + $wgIFI_FlickrSearchBy, $wgIFI_AppendRandomNumber, $wgIFI_ThumbType; |
| 15 | + |
| 16 | + $this->apiKey = $wgIFI_FlickrAPIKey; |
| 17 | + $this->creditsTemplate = $wgIFI_CreditsTemplate; |
| 18 | + $this->shouldGetOriginal = $wgIFI_GetOriginal; |
| 19 | + $this->promptForFilename = $wgIFI_PromptForFilename; |
| 20 | + $this->phpFlickrFile = $wgIFI_phpFlickr; |
| 21 | + |
| 22 | + $this->resultsPerRow = $wgIFI_ResultsPerRow; |
| 23 | + $this->resultsPerPage = $wgIFI_ResultsPerPage; |
| 24 | + $this->licenses = is_array( $wgIFI_FlickrLicense ) ? |
| 25 | + $wgIFI_FlickrLicense : explode( ',', $wgIFI_FlickrLicense ); |
| 26 | + $this->sortBy = $wgIFI_FlickrSort; |
| 27 | + $this->searchBy = $wgIFI_FlickrSearchBy; |
| 28 | + $this->appendRandomNumber = $wgIFI_AppendRandomNumber; |
| 29 | + $this->thumbType = $wgIFI_ThumbType; |
| 30 | + |
| 31 | + $this->load(); |
15 | 32 | } |
16 | | - |
17 | | - /** |
18 | | - * Show the special page |
19 | | - * |
20 | | - * @param $par Mixed: parameter passed to the page or null |
21 | | - */ |
22 | | - public function execute( $par ) { |
23 | | - global $wgUser, $wgOut, $wgRequest, $wgIFI_FlickrAPIKey, $wgEnableUploads; |
24 | | - global $wgIFI_ResultsPerPage, $wgIFI_FlickrSort, $wgIFI_FlickrLicense, $wgIFI_ResultsPerRow; |
25 | | - global $wgIFI_PromptForFilename, $wgIFI_FlickrSearchBy, $wgIFI_ThumbType, $wgIFphpFlickr; |
26 | | - |
27 | | - wfLoadExtensionMessages( 'ImportFreeImages' ); |
28 | | - |
29 | | - $this->setHeaders(); |
30 | | - |
31 | | - wfSetupSession(); |
32 | | - if( !is_readable( $wgIFphpFlickr ) ) { |
33 | | - $wgOut->errorpage( 'error', 'importfreeimages_nophpflickr' ); |
34 | | - return; |
35 | | - } |
36 | | - require_once( $wgIFphpFlickr ); |
37 | | - |
38 | | - $importPage = SpecialPage::getTitleFor( 'ImportFreeImages' ); |
39 | | - |
40 | | - if( empty( $wgIFI_FlickrAPIKey ) ) { |
41 | | - // error - need to set $wgIFI_FlickrAPIKey to use this extension |
42 | | - $wgOut->errorpage( 'error', 'importfreeimages_noapikey' ); |
43 | | - return; |
44 | | - } |
45 | | - |
46 | | - $f = new phpFlickr($wgIFI_FlickrAPIKey); |
47 | | - |
48 | | - # a lot of this code is duplicated from SpecialUpload, should be refactored |
49 | | - # Check uploading enabled |
50 | | - if( !$wgEnableUploads ) { |
51 | | - $wgOut->showErrorPage( 'uploaddisabled', 'uploaddisabledtext' ); |
52 | | - return; |
53 | | - } |
54 | | - |
55 | | - # Check that the user has 'upload' right and is logged in |
56 | | - if( !$wgUser->isAllowed( 'upload' ) ) { |
57 | | - if( !$wgUser->isLoggedIn() ) { |
58 | | - $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' ); |
59 | | - } else { |
60 | | - $wgOut->permissionRequired( 'upload' ); |
61 | | - } |
62 | | - return; |
63 | | - } |
64 | | - |
65 | | - # Check blocks |
66 | | - if( $wgUser->isBlocked() ) { |
67 | | - $wgOut->blockedPage(); |
68 | | - return; |
69 | | - } |
70 | | - |
71 | | - # Show a message if the database is in read-only mode |
72 | | - if( wfReadOnly() ) { |
73 | | - $wgOut->readOnlyPage(); |
74 | | - return; |
75 | | - } |
76 | | - |
77 | | - $import = $wgRequest->getVal( 'url', '' ); |
78 | | - if( $wgRequest->wasPosted() && $import != '' ) { |
79 | | - if( $this->wfIFI_handleUpload( $f, $import ) ) |
80 | | - return; |
81 | | - $wgOut->addHTML('<hr/>'); |
82 | | - } |
83 | | - |
84 | | - $q = $wgRequest->getText( 'q' ); |
85 | | - |
86 | | - global $wgScript; |
87 | | - $wgOut->addHTML( wfMsg( 'importfreeimages_description' ) . "<br /><br /> |
88 | | - <form method=GET action=\"$wgScript\">".wfMsg('search'). |
89 | | - Xml::hidden( 'title', $importPage->getPrefixedDBkey() ) . |
90 | | - ": <input type=text name=q value='" . htmlspecialchars($q) . "'><input type=submit value=".wfMsg('search')."></form>"); |
91 | | - |
92 | | - if( $q != '' ) { |
93 | | - $page = $wgRequest->getInt( 'p', 1 ); |
94 | | - $q = $wgRequest->getVal( 'q' ); |
95 | | - // TODO: get the right licenses |
96 | | - $photos = $f->photos_search( |
97 | | - array( |
98 | | - $wgIFI_FlickrSearchBy => $q, |
| 33 | + |
| 34 | + protected function suppressStrictWarnings() { |
| 35 | + // Hack around with E_STRICT because phpFlickr is written for PHP4 ... |
| 36 | + $this->oldLevel = error_reporting(); |
| 37 | + error_reporting( $this->oldLevel ^ E_STRICT ); |
| 38 | + } |
| 39 | + protected function restoreStrictWarnings() { |
| 40 | + error_reporting( $this->oldLevel ); |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + protected function load() { |
| 45 | + if ( !file_exists( $this->phpFlickrFile ) ) |
| 46 | + throw new MWException( 'phpFlickr can not be found at ' . $this->phpFlickrFile ); |
| 47 | + |
| 48 | + $this->suppressStrictWarnings(); |
| 49 | + |
| 50 | + require_once( $this->phpFlickrFile ); |
| 51 | + if ( !$this->apiKey ) |
| 52 | + throw new MWException( 'No Flickr API key found' ); |
| 53 | + $this->flickr = new phpFlickr( $this->apiKey ); |
| 54 | + |
| 55 | + $this->restoreStrictWarnings(); |
| 56 | + } |
| 57 | + |
| 58 | + public function searchPhotos( $query, $page ) { |
| 59 | + $this->suppressStrictWarnings(); |
| 60 | + $result = $this->flickr->photos_search( |
| 61 | + array( |
| 62 | + $this->searchBy => $query, |
99 | 63 | 'tag_mode' => 'any', |
100 | 64 | 'page' => $page, |
101 | | - 'per_page' => $wgIFI_ResultsPerPage, |
102 | | - 'license' => $wgIFI_FlickrLicense, |
103 | | - 'sort' => $wgIFI_FlickrSort |
104 | | - ) |
105 | | - ); |
106 | | - |
107 | | - if( $photos == null || !is_array($photos) || sizeof($photos) == 0 || !isset($photos['photo']) |
108 | | - || !is_array($photos['photo']) || sizeof($photos['photo']) == 0 ) { |
109 | | - $wgOut->addHTML( wfMsg( 'importfreeimages_nophotosfound', htmlspecialchars( $q ) ) ); |
110 | | - return; |
111 | | - } |
112 | | - $sk = $wgUser->getSkin(); |
113 | | - $wgOut->addHTML(" |
114 | | - <table cellpadding=4> |
115 | | - <form method='POST' name='uploadphotoform' action='" . $importPage->escapeFullURL() . "'> |
116 | | - <input type=hidden name='url' value=''> |
117 | | - <input type=hidden name='id' value=''> |
118 | | - <input type=hidden name='action' value='submit'> |
119 | | - <input type=hidden name='owner' value=''> |
120 | | - <input type=hidden name='name' value=''> |
121 | | - <input type=hidden name='ititle' value=''> |
122 | | - <input type=hidden name='token' value='" . $wgUser->editToken() . "'> |
123 | | - <input type=hidden name='q' value='" . htmlspecialchars($q) . "'> |
124 | | - <script type='text/javascript'> |
125 | | - function s2( url, id, owner, name, ititle ) { |
126 | | - document.uploadphotoform.url.value = url; |
127 | | - document.uploadphotoform.id.value = id; |
128 | | - document.uploadphotoform.owner.value = owner; |
129 | | - document.uploadphotoform.name.value = name; |
130 | | - document.uploadphotoform.ititle.value = ititle; |
131 | | - if( " . ($wgIFI_PromptForFilename ? "true" : "false") . " ) { |
132 | | - document.uploadphotoform.ititle.value = prompt(" . Xml::encodeJsVar( wfMsg('importfreeimages_promptuserforfilename') ) . ", ititle); |
133 | | - if( document.uploadphotoform.ititle.value == '' ) { |
134 | | - document.uploadphotoform.ititle.value = ititle; |
135 | | - } |
136 | | - } |
137 | | - document.uploadphotoform.submit(); |
138 | | - } |
139 | | - </script>"); |
140 | | - $ownermsg = wfMsg( 'importfreeimages_owner' ); |
141 | | - $importmsg = wfMsg( 'importfreeimages_importthis' ); |
142 | | - $i = 0; |
143 | | - foreach( $photos['photo'] as $photo ) { |
144 | | - $owner = $f->people_getInfo( $photo['owner'] ); |
145 | | - |
146 | | - $owner_esc = htmlspecialchars( $photo['owner'], ENT_QUOTES ); |
147 | | - $id_esc = htmlspecialchars( $photo['id'], ENT_QUOTES ); |
148 | | - $title_esc = htmlspecialchars( $photo['title'], ENT_QUOTES ); |
149 | | - $username_esc = htmlspecialchars( $owner['username'], ENT_QUOTES ); |
150 | | - $thumb_esc = htmlspecialchars( "http://farm{$photo['farm']}.static.flickr.com/{$photo['server']}/{$photo['id']}_{$photo['secret']}_{$wgIFI_ThumbType}.jpg", ENT_QUOTES ); |
151 | | - |
152 | | - $owner_js = Xml::encodeJsVar( $photo['owner'] ); |
153 | | - $id_js = Xml::encodeJsVar( $photo['id'] ); |
154 | | - $title_js = Xml::encodeJsVar( $photo['title'] ); |
155 | | - $username_js = Xml::encodeJsVar( $owner['username'] ); |
156 | | - $url_js = Xml::encodeJsVar( "http://farm{$photo['farm']}.static.flickr.com/{$photo['server']}/{$photo['id']}_{$photo['secret']}.jpg" ); |
157 | | - |
158 | | - if( $i % $wgIFI_ResultsPerRow == 0 ) $wgOut->addHTML("<tr>"); |
159 | | - $wgOut->addHTML( " |
160 | | - <td align='center' style='padding-top: 15px; border-bottom: 1px solid #ccc;'> |
161 | | - <font size=-2><a href='http://www.flickr.com/photos/$owner_esc/$id_esc/'>$title_esc</a> |
162 | | - <br />$ownermsg: <a href='http://www.flickr.com/people/$owner_esc/'>$username_esc</a> |
163 | | - <br /><img src='$thumb_esc' /> |
164 | | - <br />(<a href='#' onclick='s2($url_js, $id_js, $owner_js, $username_js, $title_js);'>$importmsg</a>)</font> |
165 | | - </td> |
166 | | - " ); |
167 | | - if( $i % $wgIFI_ResultsPerRow == ($wgIFI_ResultsPerRow - 1) ) $wgOut->addHTML("</tr>"); |
168 | | - $i++; |
169 | | - } |
170 | | - |
171 | | - $wgOut->addHTML("</form></table>"); |
172 | | - if( $wgIFI_ResultsPerPage * $page < $photos['total'] ) { |
173 | | - $page++; |
174 | | - $wgOut->addHTML("<br />" . $sk->makeLinkObj($importPage, wfMsg('importfreeimages_next', $wgIFI_ResultsPerPage), "p=$page&q=" . urlencode($q) ) ); |
175 | | - } |
176 | | - } |
| 65 | + 'per_page' => $this->resultsPerPage, |
| 66 | + 'license' => implode( ',', $this->licenses ), |
| 67 | + 'sort' => $this->sortBy, |
| 68 | + ) |
| 69 | + ); |
| 70 | + $this->restoreStrictWarnings(); |
| 71 | + |
| 72 | + if ( !$result || !is_array( $result ) || !isset( $result['photo'] ) ) |
| 73 | + return false; |
| 74 | + return $result; |
177 | 75 | } |
178 | | - |
179 | | - /** |
180 | | - * Shows a custom upload warning |
181 | | - * @param $u UploadFromFile object |
182 | | - * @param $warning Mixed: warning message (MediaWiki:Fileexists plus some other stuff) |
183 | | - */ |
184 | | - function wfIFI_uploadWarning( $u, $warning ) { |
185 | | - global $wgOut, $wgUseCopyrightUpload; |
186 | | - |
187 | | - wfLoadExtensionMessages( 'ImportFreeImages' ); |
188 | | - |
189 | | - $form = new UploadForm(); |
190 | | - $form->mUpload = $u; |
191 | | - $form->uploadWarning(); |
192 | | - return; |
193 | | - |
194 | | - $u->mSessionKey = $u->stashSession(); |
195 | | - if( !$u->mSessionKey ) { |
196 | | - # Couldn't save file; an error has been displayed so let's go. |
197 | | - return; |
198 | | - } |
199 | | - |
200 | | - $wgOut->addHTML( "<h2>" . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" ); |
201 | | - $wgOut->addHTML( "<ul class='warning'>{$warning}</ul><br />\n" ); |
202 | | - |
203 | | - $save = wfMsgHtml( 'savefile' ); |
204 | | - $reupload = wfMsgHtml( 'reupload' ); |
205 | | - $iw = wfMsgWikiHtml( 'ignorewarning' ); |
206 | | - $reup = wfMsgWikiHtml( 'reuploaddesc' ); |
207 | | - $titleObj = Title::makeTitle( NS_SPECIAL, 'Upload' ); |
208 | | - $action = $titleObj->escapeLocalURL( 'action=submit' ); |
209 | | - if ( $wgUseCopyrightUpload ){ |
210 | | - $copyright = '<input type="hidden" name="wpUploadCopyStatus" value="' . htmlspecialchars( $u->mUploadCopyStatus ) . '" /> |
211 | | - <input type="hidden" name="wpUploadSource" value="' . htmlspecialchars( $u->mUploadSource ) . '" />'; |
212 | | - } else { |
213 | | - $copyright = ''; |
214 | | - } |
215 | | - |
216 | | - $wgOut->addHTML( " |
217 | | - <form id='uploadwarning' method='post' enctype='multipart/form-data' action='$action'> |
218 | | - <input type='hidden' name='wpIgnoreWarning' value='1' /> |
219 | | - <input type='hidden' name='wpSessionKey' value=\"" . htmlspecialchars( $u->mSessionKey ) . "\" /> |
220 | | - <input type='hidden' name='wpUploadDescription' value=\"" . htmlspecialchars( $u->mUploadDescription ) . "\" /> |
221 | | - <input type='hidden' name='wpLicense' value=\"" . htmlspecialchars( $u->mLicense ) . "\" /> |
222 | | - <input type='hidden' name='wpDestFile' value=\"" . htmlspecialchars( $u->mDestFile ) . "\" /> |
223 | | - <input type='hidden' name='wpWatchu' value=\"" . htmlspecialchars( intval( $u->mWatchu ) ) . "\" /> |
224 | | - {$copyright} |
225 | | - <table border='0'> |
226 | | - <tr> |
227 | | - <tr> |
228 | | - <td align='right'> |
229 | | - <input tabindex='2' type='submit' name='wpUpload' value=\"$save\" /> |
230 | | - </td> |
231 | | - <td align='left'>$iw</td> |
232 | | - </tr> |
233 | | - </tr> |
234 | | - </table></form>\n" . wfMsg( 'importfreeimages_returntoform', $_SERVER["HTTP_REFERER"] ) ); |
235 | | - // $_SERVER["HTTP_REFERER"]; -- javascript.back wasn't working for some reason... hmph. |
| 76 | + |
| 77 | + public function getOwnerInfo( $owner ) { |
| 78 | + $this->suppressStrictWarnings(); |
| 79 | + $result = $this->flickr->people_getInfo( $owner ); |
| 80 | + $this->restoreStrictWarnings(); |
| 81 | + return $result; |
236 | 82 | } |
237 | | - |
238 | | - /** |
239 | | - * Return values: |
240 | | - * true: Don't show query form, because |
241 | | - * either everything worked |
242 | | - * or something is so wrong that it makes no sense to continue |
243 | | - * false: Temporary error (e.g. proposed pagename is protected against creation), |
244 | | - * show query again so user has a chance to retry. |
245 | | - */ |
246 | | - function wfIFI_handleUpload( $f, $import ) { |
247 | | - global $wgRequest, $wgUser, $wgOut, $wgTmpDirectory; |
248 | | - global $wgIFI_GetOriginal, $wgIFI_CreditsTemplate, $wgIFI_AppendRandomNumber; |
249 | | - |
250 | | - wfLoadExtensionMessages( 'ImportFreeImages' ); |
251 | | - |
252 | | - # Check token, to preven Cross Site Request Forgeries |
253 | | - $token = $wgRequest->getVal( 'token' ); |
254 | | - if( !$wgUser->matchEditToken( $token ) ) { |
255 | | - $wgOut->addWikiMsg( 'sessionfailure' ); |
256 | | - return false; |
257 | | - } |
258 | | - |
259 | | - $id = $wgRequest->getVal( 'id' ); |
260 | | - $ititle = $wgRequest->getVal( 'ititle' ); |
261 | | - $owner = $wgRequest->getVal( 'owner' ); |
262 | | - $name = $wgRequest->getVal( 'name' ); |
263 | | - |
264 | | - if( $wgIFI_GetOriginal ) { |
265 | | - // get URL of original :1 |
266 | | - |
267 | | - $sizes = $f->photos_getSizes( $id ); |
268 | | - $original = ''; |
269 | | - foreach( $sizes as $size ) { |
270 | | - if( $size['label'] == 'Original' ) { |
271 | | - $original = $size['source']; |
272 | | - $import = $size['source']; |
273 | | - } else if( $size['label'] == 'Large' ) { |
274 | | - $large = $size['source']; |
275 | | - } |
276 | | - } |
277 | | - // sometimes Large is returned but no Original! |
278 | | - if( $original == '' && $large != '' ) |
279 | | - $import = $large; |
280 | | - } |
281 | | - |
282 | | - if( !preg_match( '/^http:\/\/farm[0-9]+\.static\.flickr\.com\/.*\.(jpg|gif|png)$/', $import, $matches ) ) { |
283 | | - $wgOut->showErrorPage( 'error', 'importfreeimages_invalidurl', array( wfEscapeWikiText( $import ) ) ); |
284 | | - return true; |
285 | | - } |
286 | | - $fileext = '.' . $matches[1]; |
287 | | - |
288 | | - // store the contents of the file |
289 | | - $pageContents = file_get_contents($import); |
290 | | - $tempname = tempnam( $wgTmpDirectory, 'flickr' ); |
291 | | - $r = fopen( $tempname, 'wb' ); |
292 | | - if( $r === FALSE ) { |
293 | | - # Could not open temporary file to write in |
294 | | - $wgOut->errorPage( 'upload-file-error', 'upload-file-error-text' ); |
295 | | - return true; |
296 | | - } |
297 | | - $size = fwrite( $r, $pageContents ); |
298 | | - fclose( $r ); |
299 | | - |
300 | | - $info = $f->photos_getInfo( $id ); |
301 | | - $name_wiki = wfEscapeWikiText( $name ); |
302 | | - if( !empty( $wgIFI_CreditsTemplate ) ) { |
303 | | - $owner_wiki = wfEscapeWikiText( $owner ); |
304 | | - $id_wiki = wfEscapeWikiText( $id ); |
305 | | - $caption = "{{" . $wgIFI_CreditsTemplate . intval( $info['license'] ) . "|1=$id_wiki|2=$owner_wiki|3=$name_wiki}}"; |
306 | | - } else { |
307 | | - // TODO: this is totally wrong: The whole message should be configurable, we shouldn't include arbitrary templates |
308 | | - // additionally, the license information is not correct (we are not guaranteed to get "CC by 2.0" images only) |
309 | | - $caption = wfMsgForContent('importfreeimages_filefromflickr', $ititle, "http://www.flickr.com/people/" . urlencode($owner) . " " . $name_wiki) . " <nowiki>$import</nowiki>. {{CC by 2.0}} "; |
310 | | - $caption = trim($caption); |
311 | | - } |
312 | | - |
313 | | - $filename = $ititle . ($wgIFI_AppendRandomNumber ? "-" . rand(0, 9999) : "") . $fileext; |
314 | | - $filename = preg_replace('/ +/', ' ', $filename); |
315 | | - /** |
316 | | - * Filter out illegal characters, and try to make a legible name |
317 | | - * out of it. We'll strip some silently that Title would die on. |
318 | | - * This is taken from SpecialUpload::internalProcessUploads() |
319 | | - */ |
320 | | - $filename = preg_replace ( "/[^".Title::legalChars()."]|:/", '-', $filename ); |
321 | | - $nt = Title::makeTitleSafe( NS_IMAGE, $filename ); |
322 | | - if( is_null( $nt ) ) { |
323 | | - $wgOut->showErrorPage( 'error', 'illegalfilename', array( wfEscapeWikiText( $filename ) ) ); |
324 | | - return false; |
325 | | - } |
326 | | - $u = new UploadFromFile(); |
327 | | - $u->initialize($filename, $tempname, $size, true); |
328 | | - |
329 | | - if( $nt->getArticleID() > 0 ) { |
330 | | - $sk = $wgUser->getSkin(); |
331 | | - $dlink = $sk->makeKnownLinkObj( $t ); |
332 | | - $warning = '<li>'.wfMsgExt( 'fileexists', '', $dlink ).'</li>'; |
333 | | - |
334 | | - // use our own upload warning as we dont have a 'reupload' feature |
335 | | - $this->wfIFI_uploadWarning( $u, $warning ); |
336 | | - return true; |
337 | | - } elseif( !$nt->userCan( 'create' ) ) { |
338 | | - $wgOut->showPermissionsErrorPage( $nt->getUserPermissionsErrors( 'create', $wgUser ) ); |
339 | | - return false; |
340 | | - } else { |
341 | | - $status = $u->performUpload(wfMsg('importfreeimages_comment'), $caption, false, $wgUser); |
342 | | - if ( !$status->isGood() ) { |
343 | | - $form = new UploadForm(); |
344 | | - $form->mUppload = $u; |
345 | | - $form->uploadError( $wgOut->parse( $status->getWikiText() ) ); |
346 | | - } |
347 | | - // Success, redirect to description page |
348 | | - wfRunHooks( 'SpecialUploadComplete', array( &$this ) ); |
349 | | - $local = $u->getLocalFile(); |
350 | | - $wgOut->redirect( $local->getTitle()->getFullURL() ); |
351 | | - } |
| 83 | + |
| 84 | + public function getSizes( $id ) { |
| 85 | + // [{'label': 'Large/Original', 'source': 'url', ...}, ...] |
| 86 | + $this->suppressStrictWarnings(); |
| 87 | + $result = $this->flickr->photos_getSizes( $id ); |
| 88 | + $this->restoreStrictWarnings(); |
| 89 | + return $result; |
352 | 90 | } |
353 | 91 | |
354 | | -} // class |
| 92 | +} |
| 93 | + |
Index: trunk/extensions/ImportFreeImages/ImportFreeImages.i18n.php |
— | — | @@ -27,6 +27,13 @@ |
28 | 28 | 'importfreeimages_returntoform' => 'Or, click <a href=\'$1\'>here</a> to return to your search results', |
29 | 29 | 'importfreeimages_nophpflickr' => 'You have not installed phpFlickr, please set $wgIFphpFlickr in your LocalSettings.php.', |
30 | 30 | 'importfreeimages_comment' => "Importing image from Flickr", |
| 31 | + |
| 32 | + 'importfreeimages_size_square' => 'Square', |
| 33 | + 'importfreeimages_size_thumbnail' => 'Thumbnail', |
| 34 | + 'importfreeimages_size_small' => 'Small', |
| 35 | + 'importfreeimages_size_medium' => 'Medium', |
| 36 | + 'importfreeimages_size_large' => 'Large', |
| 37 | + 'importfreeimages_size_original' => 'Original', |
31 | 38 | ); |
32 | 39 | |
33 | 40 | /** Message documentation (Message documentation) |
Index: trunk/extensions/ImportFreeImages/ImportFreeImages.php |
— | — | @@ -17,7 +17,7 @@ |
18 | 18 | $wgIFI_CreditsTemplate = 'flickr'; // use this to format the image content with some key parameters |
19 | 19 | $wgIFI_GetOriginal = true; // import the original version of the photo |
20 | 20 | $wgIFI_PromptForFilename = true; // prompt the user through javascript for the destination filename |
21 | | -$wgIFphpFlickr = 'phpFlickr-2.2.0/phpFlickr.php'; // Path to your phpFlickr file |
| 21 | +$wgIFI_phpFlickr = 'phpFlickr-2.2.0/phpFlickr.php'; // Path to your phpFlickr file |
22 | 22 | |
23 | 23 | $wgIFI_ResultsPerPage = 20; |
24 | 24 | $wgIFI_ResultsPerRow = 4; |
— | — | @@ -34,8 +34,8 @@ |
35 | 35 | $wgExtensionCredits['specialpage'][] = array( |
36 | 36 | 'path' => __FILE__, |
37 | 37 | 'name' => 'ImportFreeImages', |
38 | | - 'author' => 'Travis Derouin', |
39 | | - 'version' => '1.1', |
| 38 | + 'author' => array( 'Travis Derouin', 'Bryan Tong Minh' ), |
| 39 | + 'version' => '2.0', |
40 | 40 | 'description' => 'Provides a way of importing properly licensed photos from flickr.', |
41 | 41 | 'descriptionmsg' => 'importfreeimages-desc', |
42 | 42 | 'url' => 'http://www.mediawiki.org/wiki/Extension:ImportFreeImages', |
— | — | @@ -44,8 +44,15 @@ |
45 | 45 | // Set up the new special page |
46 | 46 | $dir = dirname(__FILE__) . '/'; |
47 | 47 | $wgAutoloadClasses['ImportFreeImages'] = $dir . 'ImportFreeImages.body.php'; |
| 48 | +$wgAutoloadClasses['SpecialImportFreeImages'] = $dir . 'SpecialImportFreeImages.php'; |
| 49 | +$wgAutoloadClasses['UploadFreeImage'] = $dir . 'UploadFreeImage.php'; |
48 | 50 | $wgExtensionMessagesFiles['ImportFreeImages'] = $dir . 'ImportFreeImages.i18n.php'; |
49 | 51 | $wgExtensionAliasesFiles['ImportFreeImages'] = $dir . 'ImportFreeImages.alias.php'; |
50 | | -$wgSpecialPages['ImportFreeImages'] = 'ImportFreeImages'; |
| 52 | + |
| 53 | +$wgSpecialPages['ImportFreeImages'] = 'SpecialImportFreeImages'; |
51 | 54 | // Special page group for MW 1.13+ |
52 | 55 | $wgSpecialPageGroups['ImportFreeImages'] = 'media'; |
| 56 | + |
| 57 | + |
| 58 | +$wgHooks['UploadCreateFromRequest'][] = 'UploadFreeImage::onUploadCreateFromRequest'; |
| 59 | +$wgHooks['UploadFormSourceDescriptors'][] = 'UploadFreeImage::onUploadFormSourceDescriptors'; |
Index: trunk/extensions/ImportFreeImages/SpecialImportFreeImages.php |
— | — | @@ -0,0 +1,149 @@ |
| 2 | +<?php |
| 3 | +// Sanity check - check for MediaWiki environment... |
| 4 | +if( !defined( 'MEDIAWIKI' ) ) { |
| 5 | + die( "This is an extension to the MediaWiki package and cannot be run standalone.\n" ); |
| 6 | +} |
| 7 | + |
| 8 | +class SpecialImportFreeImages extends SpecialPage { |
| 9 | + |
| 10 | + /** |
| 11 | + * Constructor |
| 12 | + */ |
| 13 | + public function __construct() { |
| 14 | + parent::__construct( 'ImportFreeImages'/*class*/, 'upload'/*restriction*/ ); |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * Show the special page |
| 19 | + * |
| 20 | + * @param $par Mixed: parameter passed to the page or null |
| 21 | + */ |
| 22 | + public function execute( $par ) { |
| 23 | + global $wgUser, $wgOut, $wgRequest, $wgEnableUploads; |
| 24 | + |
| 25 | + $this->setHeaders(); |
| 26 | + $this->outputHeader(); |
| 27 | + |
| 28 | + # a lot of this code is duplicated from SpecialUpload, should be refactored |
| 29 | + # Check uploading enabled |
| 30 | + if( !$wgEnableUploads ) { |
| 31 | + $wgOut->showErrorPage( 'uploaddisabled', 'uploaddisabledtext' ); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + # Check that the user has 'upload' right and is logged in |
| 36 | + if( !$wgUser->isAllowed( 'upload' ) ) { |
| 37 | + if( !$wgUser->isLoggedIn() ) { |
| 38 | + $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' ); |
| 39 | + } else { |
| 40 | + $wgOut->permissionRequired( 'upload' ); |
| 41 | + } |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + # Check blocks |
| 46 | + if( $wgUser->isBlocked() ) { |
| 47 | + $wgOut->blockedPage(); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + # Show a message if the database is in read-only mode |
| 52 | + if( wfReadOnly() ) { |
| 53 | + $wgOut->readOnlyPage(); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + $this->showForm(); |
| 58 | + $this->showResult(); |
| 59 | + |
| 60 | + |
| 61 | + } |
| 62 | + protected function showForm() { |
| 63 | + global $wgOut, $wgScript, $wgRequest; |
| 64 | + |
| 65 | + $wgOut->addHTML( Html::rawElement( 'fieldset', array(), |
| 66 | + Html::element( 'legend', array(), wfMsg( 'importfreeimages' ) ) . "\n" . |
| 67 | + wfMsgExt( 'importfreeimages_description', 'parse' ) . "\n" . |
| 68 | + Html::rawElement( 'form', array( 'action' => $wgScript ), |
| 69 | + Html::element( 'input', array( |
| 70 | + 'type' => 'hidden', |
| 71 | + 'name' => 'title', |
| 72 | + 'value' => $this->getTitle()->getPrefixedText(), |
| 73 | + ) ) . "\n" . |
| 74 | + Html::element( 'input', array( |
| 75 | + 'type' => 'text', |
| 76 | + 'name' => 'q', |
| 77 | + 'size' => '40', |
| 78 | + 'value' => $wgRequest->getText( 'q' ), |
| 79 | + ) ) . "\n" . |
| 80 | + Html::element( 'input', array( |
| 81 | + 'type' => 'submit', |
| 82 | + 'value' => wfMsg( 'search' ) |
| 83 | + ) ) |
| 84 | + ) |
| 85 | + ) ); |
| 86 | + } |
| 87 | + |
| 88 | + protected function showResult() { |
| 89 | + global $wgRequest, $wgUser, $wgOut; |
| 90 | + |
| 91 | + $page = $wgRequest->getInt( 'p', 1 ); |
| 92 | + $q = $wgRequest->getVal( 'q' ); |
| 93 | + if ( !$q ) |
| 94 | + return; |
| 95 | + |
| 96 | + $ifi = new ImportFreeImages(); |
| 97 | + // TODO: get the right licenses |
| 98 | + $photos = $ifi->searchPhotos( $q, $page ); |
| 99 | + if ( !$photos ) { |
| 100 | + $wgOut->addHTML( wfMsg( 'importfreeimages_nophotosfound', htmlspecialchars( $q ) ) ); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + $sk = $wgUser->getSkin(); |
| 105 | + $wgOut->addHTML( '<table cellpadding="4" id="mw-ifi-result">' ); |
| 106 | + |
| 107 | + $specialUploadTitle = SpecialPage::getTitleFor( 'Upload' ); |
| 108 | + |
| 109 | + $ownermsg = wfMsg( 'importfreeimages_owner' ); |
| 110 | + $importmsg = wfMsg( 'importfreeimages_importthis' ); |
| 111 | + $i = 0; |
| 112 | + foreach( $photos['photo'] as $photo ) { |
| 113 | + $owner = $ifi->getOwnerInfo( $photo['owner'] ); |
| 114 | + |
| 115 | + $owner_esc = htmlspecialchars( $photo['owner'], ENT_QUOTES ); |
| 116 | + $id_esc = htmlspecialchars( $photo['id'], ENT_QUOTES ); |
| 117 | + $title_esc = htmlspecialchars( $photo['title'], ENT_QUOTES ); |
| 118 | + $username_esc = htmlspecialchars( $owner['username'], ENT_QUOTES ); |
| 119 | + $thumb_esc = htmlspecialchars( "http://farm{$photo['farm']}.static.flickr.com/{$photo['server']}/{$photo['id']}_{$photo['secret']}_{$ifi->thumbType}.jpg", ENT_QUOTES ); |
| 120 | + $link = $specialUploadTitle->escapeLocalUrl( array( 'wpSourceType' => 'IFI', 'wpFlickrId' => $photo['id'] ) ); |
| 121 | + |
| 122 | + if ( $i % $ifi->resultsPerRow == 0 ) |
| 123 | + $wgOut->addHTML( '<tr>' ); |
| 124 | + |
| 125 | + # TODO: Fix nasty HTML generation |
| 126 | + $wgOut->addHTML( " |
| 127 | + <td align='center' style='padding-top: 15px; border-bottom: 1px solid #ccc;'> |
| 128 | + <font size=-2><a href='http://www.flickr.com/photos/$owner_esc/$id_esc/'>$title_esc</a> |
| 129 | + <br />$ownermsg: <a href='http://www.flickr.com/people/$owner_esc/'>$username_esc</a> |
| 130 | + <br /><img src='$thumb_esc' /> |
| 131 | + <br />(<a href='$link'>$importmsg</a>)</font> |
| 132 | + </td> |
| 133 | + " ); |
| 134 | + |
| 135 | + if ( $i % $ifi->resultsPerRow == ($ifi->resultsPerRow - 1) ) |
| 136 | + $wgOut->addHTML( '</tr>' ); |
| 137 | + |
| 138 | + $i++; |
| 139 | + } |
| 140 | + |
| 141 | + $wgOut->addHTML( '</table>' ); |
| 142 | + |
| 143 | + if ( $ifi->resultsPerPage * $page < $photos['total'] ) { |
| 144 | + $page++; |
| 145 | + $wgOut->addHTML("<br />" . $sk->makeLinkObj( $this->getTitle(), |
| 146 | + wfMsg('importfreeimages_next', $ifi->resultsPerPage), "p=$page&q=" . urlencode($q) ) ); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | +} // class |
Property changes on: trunk/extensions/ImportFreeImages/SpecialImportFreeImages.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 151 | + native |