MediaWiki r115614 - Code Review

Jump to: navigation, search
Repository:MediaWiki
Revision:r115613‎ | r115614 (on ViewVC)‎ | r115615 >
Date:18:46, 16 July 2012
Author:ashley
Status:new
Tags:
Comment:
MultiUpload: make this work under MW 1.19+.

Includes code from http://trac.wikia-code.com/changeset/55331

Also includes an old ShoutWiki patch (the getMaxUploadFiles() function) to allow different user groups to upload different amount of files via the special page.
Right now it's hard-coded (staff can upload 40 files at once, sysops 20, autoconfirmed 10 and other registered users 5), so it should be generalized, but it's still a rather useful patch, which is why I'm including it.
Modified paths:

Diff [purge]

Index: trunk/extensions/MultiUpload/MultiUpload.body.php
@@ -13,7 +13,7 @@
1414 public $mUploadHasBeenShown;
1515 public $mSessionKeys;
1616
17 - // status messagse for multiple files
 17+ // status messages for multiple files
1818 public $mWarnings;
1919 public $mSuccesses;
2020 public $mErrors;
@@ -24,11 +24,9 @@
2525 * @param WebRequest $request Data posted.
2626 */
2727 public function __construct( $request = null ) {
28 - global $wgRequest;
29 -
3028 SpecialPage::__construct( 'MultipleUpload', 'upload' );
3129
32 - $this->loadRequest( is_null( $request ) ? $wgRequest : $request );
 30+ $this->loadRequest();
3331 $this->mUploadHasBeenShown = false;
3432 $this->mSessionKeys = array();
3533 $this->mWarnings = array();
@@ -37,12 +35,33 @@
3836 }
3937
4038 /**
 39+ * ShoutWiki change -- gets and returns the amount of files a user can
 40+ * upload at once.
 41+ *
 42+ * @return Integer: amount of files the user can upload at once
 43+ */
 44+ public static function getMaxUploadFiles() {
 45+ global $wgUser, $wgMaxUploadFiles;
 46+ $groups = $wgUser->getEffectiveGroups();
 47+ if( in_array( 'staff', $groups ) ) {
 48+ $wgMaxUploadFiles = 40;
 49+ } elseif( in_array( 'sysop', $groups ) ) {
 50+ $wgMaxUploadFiles = 20;
 51+ } elseif( in_array( 'autoconfirmed', $groups ) ) {
 52+ $wgMaxUploadFiles = 10;
 53+ } else {
 54+ $wgMaxUploadFiles = 5;
 55+ }
 56+ return $wgMaxUploadFiles;
 57+ }
 58+
 59+ /**
4160 * Initialize instance variables from request and create an Upload handler
4261 *
4362 * @param WebRequest $request The request to extract variables from
4463 */
45 - protected function loadRequest( $request ) {
46 - global $wgUser, $wgMaxUploadFiles;
 64+ protected function loadRequest() {
 65+ $request = $this->getRequest();
4766
4867 // let's make the parent happy
4968 wfSuppressWarnings();
@@ -50,16 +69,16 @@
5170 wfRestoreWarnings();
5271 // Guess the desired name from the filename if not provided
5372 $this->mDesiredDestNames = array();
54 - $this->mUploads = array();
 73+ $this->mUploads = array();
5574
5675 // deal with session keys, if we have some pick the first one, for now
5776 $vals = $request->getValues();
58 - $fromsession = false;
 77+ $fromSession = false;
5978 foreach ( $vals as $k => $v ) {
60 - if ( preg_match( "@^wpSessionKey@", $k ) ) {
 79+ if ( preg_match( '@^wpSessionKey@', $k ) ) {
6180 $request->setVal( 'wpSessionKey', $v );
62 - $fromsession = true;
63 - $filenum = preg_replace( "@wpSessionKey@", '', $k );
 81+ $fromSession = true;
 82+ $filenum = preg_replace( '@wpSessionKey@', '', $k );
6483 $request->setVal( 'wpDestFile', $request->getVal( 'wpDestFile' . $filenum ) );
6584 $up = UploadBase::createFromRequest( $request );
6685 $this->mUploads[] = $up;
@@ -73,8 +92,8 @@
7493 && ( $request->getCheck( 'wpUpload' )
7594 || $request->getCheck( 'wpUploadIgnoreWarning' ) );
7695
77 - if ( !$fromsession ) {
78 - for ( $i = 0; $i < $wgMaxUploadFiles; $i++ ) {
 96+ if ( !$fromSession ) {
 97+ for ( $i = 0; $i < MultipleUpload::getMaxUploadFiles(); $i++ ) {
7998 $this->mDesiredDestNames[$i] = $request->getText( 'wpDestFile'. $i );
8099 if( !$this->mDesiredDestNames[$i] && $request->getFileName( 'wpUploadFile' . $i ) !== null ) {
81100 $this->mDesiredDestNames[$i] = $request->getFileName( 'wpUploadFile' . $i );
@@ -88,13 +107,13 @@
89108 $_FILES['wpUploadFile'] = $_FILES['wpUploadFile' . $i];
90109 wfRestoreWarnings();
91110 $up = UploadBase::createFromRequest( $request );
92 - if ($up) {
 111+ if ( $up ) {
93112 $this->mUploads[] = $up;
94113 }
95114 }
96115 }
97116 $this->mDesiredDestName = $this->mDesiredDestNames[0];
98 - $this->mUpload= $this->mUploads[0];
 117+ $this->mUpload = $this->mUploads[0];
99118 }
100119
101120 function showUploadForm( $form ) {
@@ -113,8 +132,6 @@
114133 * @return UploadForm
115134 */
116135 protected function getUploadForm( $message = '', $sessionKeys = array(), $hideIgnoreWarning = false ) {
117 - global $wgOut, $wgMaxUploadFiles;
118 -
119136 # Initialize form
120137 $options = array(
121138 'watch' => $this->getWatchCheck(),
@@ -125,9 +142,9 @@
126143 'textaftersummary' => $this->uploadFormTextAfterSummary,
127144 );
128145 foreach ( $this->mSessionKeys as $f => $key ) {
129 - $options['sessionkey'. $f] = $key;
 146+ $options['sessionkey' . $f] = $key;
130147 }
131 - for ( $i = 0; $i < $wgMaxUploadFiles; $i++ ) {
 148+ for ( $i = 0; $i < MultipleUpload::getMaxUploadFiles(); $i++ ) {
132149 $options['destfile' . $i] = $this->mDesiredDestNames[$i];
133150 }
134151 $form = new MultiUploadForm( $options );
@@ -150,7 +167,7 @@
151168 $uploadFooter = wfMsgNoTrans( 'uploadfooter' );
152169 if ( $uploadFooter != '-' && !wfEmptyMsg( 'uploadfooter', $uploadFooter ) ) {
153170 $form->addPostText( '<div id="mw-upload-footer-message">'
154 - . $wgOut->parse( $uploadFooter ) . "</div>\n" );
 171+ . $this->getOutput()->parse( $uploadFooter ) . "</div>\n" );
155172 }
156173
157174 return $form;
@@ -160,35 +177,33 @@
161178 * Shows the "view X deleted revivions link"
162179 */
163180 protected function showViewDeletedLinks() {
164 - global $wgMaxUploadFiles;
165 - for ( $i = 0; $i < $wgMaxUploadFiles; $i++ ) {
 181+ for ( $i = 0; $i < MultipleUpload::getMaxUploadFiles(); $i++ ) {
166182 $this->showViewDeletedLinksInner( $this->mDesiredDestNames[$i] );
167183 }
168184 }
169185
170186 protected function showViewDeletedLinksInner( $name ) {
171 - global $wgOut, $wgUser;
172 -
173187 $title = Title::makeTitleSafe( NS_FILE, $this->mDesiredDestName );
174188 // Show a subtitle link to deleted revisions (to sysops et al only)
175189 if( $title instanceof Title ) {
176190 $count = $title->isDeleted();
177 - if ( $count > 0 && $wgUser->isAllowed( 'deletedhistory' ) ) {
 191+ $user = $this->getUser();
 192+ if ( $count > 0 && $user->isAllowed( 'deletedhistory' ) ) {
178193 $link = wfMsgExt(
179 - $wgUser->isAllowed( 'delete' ) ? 'thisisdeleted' : 'viewdeleted',
 194+ $user->isAllowed( 'delete' ) ? 'thisisdeleted' : 'viewdeleted',
180195 array( 'parse', 'replaceafter' ),
181 - $wgUser->getSkin()->linkKnown(
 196+ Linker::linkKnown(
182197 SpecialPage::getTitleFor( 'Undelete', $title->getPrefixedText() ),
183198 wfMsgExt( 'restorelink', array( 'parsemag', 'escape' ), $count )
184199 )
185200 );
186 - $wgOut->addHTML( "<div id=\"contentSub2\">{$link}</div>" );
 201+ $this->getOutput()->addHTML( "<div id=\"contentSub2\">{$link}</div>" );
187202 }
188203 }
189204
190205 // Show the relevant lines from deletion log (for still deleted files only)
191206 if( $title instanceof Title && $title->isDeletedQuick() && !$title->exists() ) {
192 - $this->showDeletionLog( $wgOut, $title->getPrefixedText() );
 207+ $this->showDeletionLog( $this->getOutput(), $title->getPrefixedText() );
193208 }
194209 }
195210
@@ -201,8 +216,6 @@
202217 * warnings and the should continue processing like there was no warning
203218 */
204219 protected function showUploadWarning( $warnings ) {
205 - global $wgUser;
206 -
207220 # If there are no warnings, or warnings we can ignore, return early.
208221 # mDestWarningAck is set when some javascript has shown the warning
209222 # to the user. mForReUpload is set when the user clicks the "upload a
@@ -216,8 +229,6 @@
217230
218231 $sessionKey = $this->mUpload->stashSession();
219232
220 - $sk = $wgUser->getSkin();
221 -
222233 $warningHtml = '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n"
223234 . '<ul class="warning">';
224235 foreach( $warnings as $warning => $args ) {
@@ -257,12 +268,12 @@
258269 * @param $message String
259270 */
260271 protected function showUploadError( $message ) {
261 - $err ='<ul><li>';
 272+ $err = '<ul><li>';
262273 $file = $this->mLocalFile;
263274 if ( $file ) {
264275 $t = $this->mLocalFile->getTitle();
265276 if ( $t ) {
266 - $err .= $t->getFullText() . ":";
 277+ $err .= $t->getFullText() . ':';
267278 }
268279 }
269280 $err .= $message . "</li></ul>\n";
@@ -274,38 +285,42 @@
275286 * Checks are made in SpecialUpload::execute()
276287 */
277288 protected function processUpload() {
278 - global $wgMaxUploadFiles, $wgOut;
279 -
280 - for ( $i = 0; $i < $wgMaxUploadFiles; $i++ ) {
 289+ for ( $i = 0; $i < MultipleUpload::getMaxUploadFiles(); $i++ ) {
281290 if ( isset( $this->mUploads[$i] ) ) {
282291 $this->mUpload = $this->mUploads[$i];
283 - $this->mUploadSuccessful = false; // reset
284 - parent::processUpload();
285 - if ( $this->mUploadSuccessful ) {
286 - $this->mSuccesses[] = "<ul><li><a href='" . $this->mLocalFile->getTitle()->getFullURL() .
287 - "' target='new'>{$this->mLocalFile->getTitle()->getFullText()}: " .
288 - wfMsg( 'multiupload-fileuploaded' ) . '</a></li></ul>';
 292+ $title = $this->mUpload->getTitle();
 293+ if ( !empty( $title ) ) {
 294+ $this->mUploadSuccessful = false; // reset
 295+ parent::processUpload();
 296+ if ( $this->mUploadSuccessful ) {
 297+ $this->mSuccesses[] = "<ul><li><a href='" . $this->mLocalFile->getTitle()->getFullURL() .
 298+ "' target='new'>{$this->mLocalFile->getTitle()->getFullText()}: " .
 299+ wfMsg( 'multiupload-fileuploaded' ) . '</a></li></ul>';
 300+ }
289301 }
290302 }
291303 }
 304+
 305+ $out = $this->getOutput();
 306+
292307 // clear out the redirects
293 - $wgOut->redirect( '' );
 308+ $out->redirect( '' );
294309
295310 // tell the good news first
296311 if ( sizeof( $this->mSuccesses ) > 0 ) {
297 - $wgOut->addHTML( '<h2>' . wfMsgHtml( 'successfulupload' ) . "</h2>\n" );
298 - $wgOut->addHTML( implode( $this->mSuccesses ) );
 312+ $out->addHTML( '<h2>' . wfMsgHtml( 'multiupload-successful-upload' ) . "</h2>\n" );
 313+ $out->addHTML( implode( $this->mSuccesses ) );
299314 }
300315
301316 // the bad news
302317 if ( sizeof( $this->mErrors ) > 0 ) {
303 - $wgOut->addHTML( '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" );
304 - $wgOut->addHTML( implode( $this->mErrors ) );
 318+ $out->addHTML( '<h2>' . wfMsgHtml( 'uploaderror' ) . "</h2>\n" );
 319+ $out->addHTML( implode( $this->mErrors ) );
305320 }
306321
307322 // the hopefully recoverable news
308323 if ( sizeof( $this->mWarnings ) > 0 || sizeof( $this->mErrors ) > 0 ) {
309 - $wgOut->addHTML( '<br /><br /><hr />' ); // visually separate the form from the errors/successes
 324+ $out->addHTML( '<br /><br /><hr />' ); // visually separate the form from the errors/successes
310325 $form = $this->getUploadForm( implode( $this->mWarnings ), $this->mSessionKeys, /* $hideIgnoreWarning */ true );
311326 $form->setSubmitText( wfMsg( 'upload-tryagain' ) );
312327 $form->addButton( 'wpUploadIgnoreWarning', wfMsg( 'ignorewarning' ) );
@@ -321,9 +336,8 @@
322337 * @return Boolean: success
323338 */
324339 protected function unsaveUploadedFile() {
325 - global $wgMaxUploadFiles;
326340 $ret = true;
327 - for ( $i = 0; $i < $wgMaxUploadFiles; $i++ ) {
 341+ for ( $i = 0; $i < MultipleUpload::getMaxUploadFiles(); $i++ ) {
328342 if ( isset( $this->mUploads[$i] ) ) {
329343 $this->mUpload = $this->mUploads[$i];
330344 // return false if even one of them failed
@@ -337,9 +351,9 @@
338352 * Construct a warning and a gallery from an array of duplicate files.
339353 * Override because the original doesn't say which file is a dupe
340354 */
341 - public static function getDupeWarning( $dupes, $dupetitle = null ) {
 355+ public static function getDupeWarning( $dupes, $dupeTitle = null ) {
342356 $result = parent::getDupeWarning( $dupes );
343 - return preg_replace( "@<li>@", "<li>{$dupetitle->getText()}", $result );
 357+ return preg_replace( '@<li>@', "<li>{$dupeTitle->getText()}", $result );
344358 }
345359
346360 }
@@ -352,20 +366,63 @@
353367 protected $mDestFiles;
354368 protected $mSessionKeys;
355369
356 - public function __construct( $options = array() ) {
 370+ public function __construct( $options = array(), $context = null, $messagePrefix = 'multiupload' ) {
357371 // basically we want to map filenames to session keys here somehow
358 - global $wgMaxUploadFiles;
359372 $this->mDestFiles = array();
360 - for( $i = 0; $i < $wgMaxUploadFiles; $i++ ) {
 373+ for( $i = 0; $i < MultipleUpload::getMaxUploadFiles(); $i++ ) {
361374 $this->mDestFiles[$i] = $options['destfile'. $i];
362375 }
363376 $this->mSessionKeys = array();
364377 foreach ( $options as $k => $v ) {
365 - if ( preg_match( "@^sessionkey@", $k ) ) {
 378+ if ( preg_match( '@^sessionkey@', $k ) ) {
366379 $this->mSessionKeys[$k] = $v;
367380 }
368381 }
369 - parent::__construct( $options );
 382+ // In an ideal world, this would work:
 383+ #parent::__construct( $options, $context, $messagePrefix );
 384+ // But MediaWiki is MediaWiki, so of course you can't pass the third
 385+ // parameter to UploadForm::__construct(), but instead it hardcodes it
 386+ // to 'upload'...great. So, we have to duplicate that function here. :(
 387+ $this->mWatch = !empty( $options['watch'] );
 388+ $this->mForReUpload = !empty( $options['forreupload'] );
 389+ $this->mSessionKey = isset( $options['sessionkey'] )
 390+ ? $options['sessionkey'] : '';
 391+ $this->mHideIgnoreWarning = !empty( $options['hideignorewarning'] );
 392+ $this->mDestWarningAck = !empty( $options['destwarningack'] );
 393+ $this->mDestFile = isset( $options['destfile'] ) ? $options['destfile'] : '';
 394+
 395+ $this->mComment = isset( $options['description'] ) ?
 396+ $options['description'] : '';
 397+
 398+ $this->mTextTop = isset( $options['texttop'] )
 399+ ? $options['texttop'] : '';
 400+
 401+ $this->mTextAfterSummary = isset( $options['textaftersummary'] )
 402+ ? $options['textaftersummary'] : '';
 403+
 404+ $sourceDescriptor = $this->getSourceSection();
 405+ $descriptor = $sourceDescriptor
 406+ + $this->getDescriptionSection()
 407+ + $this->getOptionsSection();
 408+
 409+ wfRunHooks( 'UploadFormInitDescriptor', array( &$descriptor ) );
 410+ $this->setMessagePrefix( 'multiupload' );
 411+ HTMLForm::__construct( $descriptor, $context, 'multiupload' ); // here's the change
 412+
 413+ # Set some form properties
 414+ $this->setSubmitText( wfMsg( 'uploadbtn' ) );
 415+ $this->setSubmitName( 'wpUpload' );
 416+ # Used message keys: 'accesskey-upload', 'tooltip-upload'
 417+ $this->setSubmitTooltip( 'upload' );
 418+ $this->setId( 'mw-upload-form' );
 419+
 420+ # Build a list of IDs for javascript insertion
 421+ $this->mSourceIds = array();
 422+ foreach ( $sourceDescriptor as $field ) {
 423+ if ( !empty( $field['id'] ) ) {
 424+ $this->mSourceIds[] = $field['id'];
 425+ }
 426+ }
370427 }
371428
372429 protected function getDescriptionSection() {
@@ -382,9 +439,7 @@
383440 * @return array Descriptor array
384441 */
385442 protected function getSourceSection() {
386 - global $wgLang, $wgUser, $wgRequest, $wgMaxUploadFiles;
387 -
388 - if ( sizeof( $this->mSessionKeys ) > 0) {
 443+ if ( sizeof( $this->mSessionKeys ) > 0 ) {
389444 $data = array(
390445 'wpSourceType' => array(
391446 'type' => 'hidden',
@@ -409,9 +464,9 @@
410465 return $data;
411466 }
412467
413 - $canUploadByUrl = UploadFromUrl::isEnabled() && $wgUser->isAllowed( 'upload_by_url' );
 468+ $canUploadByUrl = UploadFromUrl::isEnabled() && $this->getUser()->isAllowed( 'upload_by_url' );
414469 $radio = $canUploadByUrl;
415 - $selectedSourceType = strtolower( $wgRequest->getText( 'wpSourceType', 'File' ) );
 470+ $selectedSourceType = strtolower( $this->getRequest()->getText( 'wpSourceType', 'File' ) );
416471
417472 $descriptor = array();
418473 if ( $this->mTextTop ) {
@@ -423,7 +478,7 @@
424479 );
425480 }
426481
427 - for ( $i = 0; $i < $wgMaxUploadFiles; $i++ ) {
 482+ for ( $i = 0; $i < MultipleUpload::getMaxUploadFiles(); $i++ ) {
428483 $descriptor['UploadFile' . $i] = array(
429484 'class' => 'UploadSourceField',
430485 'section' => 'source',
@@ -456,7 +511,7 @@
457512 'radio' => &$radio,
458513 'help' => wfMsgExt( 'upload-maxfilesize',
459514 array( 'parseinline', 'escapenoentities' ),
460 - $wgLang->formatSize( $wgMaxUploadSize )
 515+ $this->getLang()->formatSize( $wgMaxUploadSize )
461516 ) . ' ' . wfMsgHtml( 'upload_source_url' ),
462517 'checked' => $selectedSourceType == 'url',
463518 );
@@ -471,7 +526,7 @@
472527 'raw' => true,
473528 'help' => wfMsgExt( 'upload-maxfilesize',
474529 array( 'parseinline', 'escapenoentities' ),
475 - $wgLang->formatSize(
 530+ $this->getLang()->formatSize(
476531 wfShorthandToInteger( ini_get( 'upload_max_filesize' ) )
477532 )
478533 ) . ' ' . wfMsgHtml( 'upload_source_file' ),
@@ -483,10 +538,9 @@
484539 * Add upload JavaScript to $wgOut
485540 */
486541 protected function addUploadJS() {
487 - global $wgScriptPath, $wgMaxUploadFiles, $wgFileExtensions;
 542+ global $wgScriptPath, $wgFileExtensions;
488543
489544 global $wgUseAjax, $wgAjaxUploadDestCheck, $wgAjaxLicensePreview, $wgEnableAPI;
490 - global $wgOut;
491545
492546 $useAjaxDestCheck = $wgUseAjax && $wgAjaxUploadDestCheck;
493547 $useAjaxLicensePreview = $wgUseAjax && $wgAjaxLicensePreview && $wgEnableAPI;
@@ -501,19 +555,19 @@
502556 'wgUploadSourceIds' => $this->mSourceIds,
503557 );
504558
505 - $wgOut->addScript( Skin::makeVariablesScript( $scriptVars ) );
 559+ $out = $this->getOutput();
 560+ $out->addScript( Skin::makeVariablesScript( $scriptVars ) );
506561
507562 // For <charinsert> support
508 - $wgOut->addScriptFile( 'edit.js' );
 563+ $out->addScriptFile( 'edit.js' );
509564
510565 // changed
511 - $wgOut->addScriptFile( "$wgScriptPath/extensions/MultiUpload/multiupload.js" );
 566+ $out->addScriptFile( "$wgScriptPath/extensions/MultiUpload/multiupload.js" );
512567 $newscriptVars = array(
513 - 'wgMaxUploadFiles' => $wgMaxUploadFiles,
 568+ 'wgMaxUploadFiles' => MultipleUpload::getMaxUploadFiles(),
514569 'wgFileExtensions' => $wgFileExtensions
515570 );
516 - $wgOut->addScript( Skin::makeVariablesScript( $newscriptVars ) );
 571+ $out->addScript( Skin::makeVariablesScript( $newscriptVars ) );
517572 }
518573
519 -}
520 -
 574+}
\ No newline at end of file
Index: trunk/extensions/MultiUpload/MultiUpload.i18n.php
@@ -30,6 +30,10 @@
3131 'multiupload-toolbox' => 'Upload multiple files',
3232 'multiupload-no-files' => 'Please select at least one file to upload',
3333 'multiupload-blank' => 'No file selected',
 34+ 'multiupload-source' => 'Source files',
 35+ 'multiupload-description' => 'File description', // duplicate of upload-description
 36+ 'multiupload-options' => 'Upload options', // duplicate of upload-options
 37+ 'multiupload-successful-upload' => 'Successful upload',
3438 );
3539
3640 /** Message documentation (Message documentation)
Index: trunk/extensions/MultiUpload/MultiUpload.php
@@ -21,7 +21,7 @@
2222 'path' => __FILE__,
2323 'name' => 'MultipleUpload',
2424 'author' => 'Travis Derouin',
25 - 'version' => '2.0',
 25+ 'version' => '2.1',
2626 'descriptionmsg' => 'multiupload-desc',
2727 'url' => 'https://www.mediawiki.org/wiki/Extension:MultiUpload',
2828 );