r96249 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96248‎ | r96249 | r96250 >
Date:19:28, 4 September 2011
Author:ialex
Status:resolved (Comments)
Tags:
Comment:
Use local context instead of global variables
Modified paths:
  • /trunk/phase3/includes/specials/SpecialUpload.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/specials/SpecialUpload.php
@@ -35,11 +35,7 @@
3636 * @param $request WebRequest : data posted.
3737 */
3838 public function __construct( $request = null ) {
39 - global $wgRequest;
40 -
4139 parent::__construct( 'Upload', 'upload' );
42 -
43 - $this->loadRequest( is_null( $request ) ? $wgRequest : $request );
4440 }
4541
4642 /** Misc variables **/
@@ -83,13 +79,9 @@
8480
8581 /**
8682 * Initialize instance variables from request and create an Upload handler
87 - *
88 - * @param $request WebRequest: the request to extract variables from
8983 */
90 - protected function loadRequest( $request ) {
91 - global $wgUser;
92 -
93 - $this->mRequest = $request;
 84+ protected function loadRequest() {
 85+ $this->mRequest = $request = $this->getRequest();
9486 $this->mSourceType = $request->getVal( 'wpSourceType', 'file' );
9587 $this->mUpload = UploadBase::createFromRequest( $request );
9688 $this->mUploadClicked = $request->wasPosted()
@@ -108,7 +100,7 @@
109101 $this->mDestWarningAck = $request->getText( 'wpDestFileWarningAck' );
110102 $this->mIgnoreWarning = $request->getCheck( 'wpIgnoreWarning' )
111103 || $request->getCheck( 'wpUploadIgnoreWarning' );
112 - $this->mWatchthis = $request->getBool( 'wpWatchthis' ) && $wgUser->isLoggedIn();
 104+ $this->mWatchthis = $request->getBool( 'wpWatchthis' ) && $this->getUser()->isLoggedIn();
113105 $this->mCopyrightStatus = $request->getText( 'wpUploadCopyStatus' );
114106 $this->mCopyrightSource = $request->getText( 'wpUploadSource' );
115107
@@ -125,7 +117,7 @@
126118 // with their submissions, as that's new in 1.16.
127119 $this->mTokenOk = true;
128120 } else {
129 - $this->mTokenOk = $wgUser->matchEditToken( $token );
 121+ $this->mTokenOk = $this->getUser()->matchEditToken( $token );
130122 }
131123
132124 $this->uploadFormTextTop = '';
@@ -148,43 +140,42 @@
149141 * Special page entry point
150142 */
151143 public function execute( $par ) {
152 - global $wgUser, $wgOut;
 144+ global $wgGroupPermissions;
153145
154146 $this->setHeaders();
155147 $this->outputHeader();
156148
157149 # Check uploading enabled
158150 if( !UploadBase::isEnabled() ) {
159 - $wgOut->showErrorPage( 'uploaddisabled', 'uploaddisabledtext' );
160 - return;
 151+ throw new ErrorPageError( 'uploaddisabled', 'uploaddisabledtext' );
161152 }
162153
163154 # Check permissions
164 - global $wgGroupPermissions;
165 - $permissionRequired = UploadBase::isAllowed( $wgUser );
 155+ $user = $this->getUser();
 156+ $permissionRequired = UploadBase::isAllowed( $user );
166157 if( $permissionRequired !== true ) {
167 - if( !$wgUser->isLoggedIn() && ( $wgGroupPermissions['user']['upload']
 158+ if( !$user->isLoggedIn() && ( $wgGroupPermissions['user']['upload']
168159 || $wgGroupPermissions['autoconfirmed']['upload'] ) ) {
169160 // Custom message if logged-in users without any special rights can upload
170 - $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
 161+ throw new ErrorPageError( 'uploadnologin', 'uploadnologintext' );
171162 } else {
172 - $wgOut->permissionRequired( $permissionRequired );
 163+ throw new PermissionsError( $permissionRequired );
173164 }
174 - return;
175165 }
176166
177167 # Check blocks
178 - if( $wgUser->isBlocked() ) {
179 - $wgOut->blockedPage();
 168+ if( $user->isBlocked() ) {
 169+ $this->getOutput()->blockedPage();
180170 return;
181171 }
182172
183173 # Check whether we actually want to allow changing stuff
184174 if( wfReadOnly() ) {
185 - $wgOut->readOnlyPage();
186 - return;
 175+ throw new ReadOnlyError;
187176 }
188177
 178+ $this->loadRequest();
 179+
189180 # Unsave the temporary file in case this was a cancelled upload
190181 if ( $this->mCancelUpload ) {
191182 if ( !$this->unsaveUploadedFile() ) {
@@ -231,8 +222,7 @@
232223 if ( $form instanceof HTMLForm ) {
233224 $form->show();
234225 } else {
235 - global $wgOut;
236 - $wgOut->addHTML( $form );
 226+ $this->getOutput()->addHTML( $form );
237227 }
238228
239229 }
@@ -246,8 +236,6 @@
247237 * @return UploadForm
248238 */
249239 protected function getUploadForm( $message = '', $sessionKey = '', $hideIgnoreWarning = false ) {
250 - global $wgOut;
251 -
252240 # Initialize form
253241 $form = new UploadForm( array(
254242 'watch' => $this->getWatchCheck(),
@@ -260,7 +248,7 @@
261249 'texttop' => $this->uploadFormTextTop,
262250 'textaftersummary' => $this->uploadFormTextAfterSummary,
263251 'destfile' => $this->mDesiredDestName,
264 - ) );
 252+ ), $this->getContext() );
265253 $form->setTitle( $this->getTitle() );
266254
267255 # Check the token, but only if necessary
@@ -298,7 +286,7 @@
299287 $uploadFooter = wfMessage( 'uploadfooter' );
300288 if ( !$uploadFooter->isDisabled() ) {
301289 $form->addPostText( '<div id="mw-upload-footer-message">'
302 - . $wgOut->parse( $uploadFooter->plain() ) . "</div>\n" );
 290+ . $this->getOutput()->parse( $uploadFooter->plain() ) . "</div>\n" );
303291 }
304292
305293 return $form;
@@ -309,23 +297,22 @@
310298 * Shows the "view X deleted revivions link""
311299 */
312300 protected function showViewDeletedLinks() {
313 - global $wgOut, $wgUser;
314 -
315301 $title = Title::makeTitleSafe( NS_FILE, $this->mDesiredDestName );
 302+ $user = $this->getUser();
316303 // Show a subtitle link to deleted revisions (to sysops et al only)
317 - if( $title instanceof Title && $wgUser->isAllowed( 'deletedhistory' ) && !$wgUser->isBlocked() ) {
318 - $canViewSuppress = $wgUser->isAllowed( 'suppressrevision' );
 304+ if( $title instanceof Title && $user->isAllowed( 'deletedhistory' ) && !$user->isBlocked() ) {
 305+ $canViewSuppress = $user->isAllowed( 'suppressrevision' );
319306 $count = $title->isDeleted( $canViewSuppress );
320307 if ( $count > 0 ) {
321308 $link = wfMsgExt(
322 - $wgUser->isAllowed( 'delete' ) ? 'thisisdeleted' : 'viewdeleted',
 309+ $user->isAllowed( 'delete' ) ? 'thisisdeleted' : 'viewdeleted',
323310 array( 'parse', 'replaceafter' ),
324 - $this->getSkin()->linkKnown(
 311+ Linker::linkKnown(
325312 SpecialPage::getTitleFor( 'Undelete', $title->getPrefixedText() ),
326313 wfMsgExt( 'restorelink', array( 'parsemag', 'escape' ), $count )
327314 )
328315 );
329 - $wgOut->addHTML( "<div id=\"contentSub2\">{$link}</div>" );
 316+ $this->getOutput()->addHTML( "<div id=\"contentSub2\">{$link}</div>" );
330317 }
331318 }
332319 }
@@ -423,12 +410,10 @@
424411 * Checks are made in SpecialUpload::execute()
425412 */
426413 protected function processUpload() {
427 - global $wgUser, $wgOut;
428 -
429414 // Fetch the file if required
430415 $status = $this->mUpload->fetchFile();
431416 if( !$status->isOK() ) {
432 - $this->showUploadError( $wgOut->parse( $status->getWikiText() ) );
 417+ $this->showUploadError( $this->getOutput()->parse( $status->getWikiText() ) );
433418 return;
434419 }
435420
@@ -450,7 +435,7 @@
451436 }
452437
453438 // Verify permissions for this title
454 - $permErrors = $this->mUpload->verifyTitlePermissions( $wgUser );
 439+ $permErrors = $this->mUpload->verifyTitlePermissions( $this->getUser() );
455440 if( $permErrors !== true ) {
456441 $code = array_shift( $permErrors[0] );
457442 $this->showRecoverableUploadError( wfMsgExt( $code,
@@ -475,16 +460,16 @@
476461 } else {
477462 $pageText = false;
478463 }
479 - $status = $this->mUpload->performUpload( $this->mComment, $pageText, $this->mWatchthis, $wgUser );
 464+ $status = $this->mUpload->performUpload( $this->mComment, $pageText, $this->mWatchthis, $this->getUser() );
480465 if ( !$status->isGood() ) {
481 - $this->showUploadError( $wgOut->parse( $status->getWikiText() ) );
 466+ $this->showUploadError( $this->getOutput()->parse( $status->getWikiText() ) );
482467 return;
483468 }
484469
485470 // Success, redirect to description page
486471 $this->mUploadSuccessful = true;
487472 wfRunHooks( 'SpecialUploadComplete', array( &$this ) );
488 - $wgOut->redirect( $this->mLocalFile->getTitle()->getFullURL() );
 473+ $this->getOutput()->redirect( $this->mLocalFile->getTitle()->getFullURL() );
489474 }
490475
491476 /**
@@ -539,8 +524,7 @@
540525 * state can get out of sync.
541526 */
542527 protected function getWatchCheck() {
543 - global $wgUser;
544 - if( $wgUser->getOption( 'watchdefault' ) ) {
 528+ if( $this->getUser()->getOption( 'watchdefault' ) ) {
545529 // Watch all edits!
546530 return true;
547531 }
@@ -552,7 +536,7 @@
553537 return $local->getTitle()->userIsWatching();
554538 } else {
555539 // New page should get watched if that's our option.
556 - return $wgUser->getOption( 'watchcreations' );
 540+ return $this->getUser()->getOption( 'watchcreations' );
557541 }
558542 }
559543
@@ -563,7 +547,7 @@
564548 * @param $details Array: result of UploadBase::verifyUpload
565549 */
566550 protected function processVerificationError( $details ) {
567 - global $wgFileExtensions, $wgLang;
 551+ global $wgFileExtensions;
568552
569553 switch( $details['status'] ) {
570554
@@ -594,11 +578,11 @@
595579 case UploadBase::FILETYPE_BADTYPE:
596580 $msg = wfMessage( 'filetype-banned-type' );
597581 if ( isset( $details['blacklistedExt'] ) ) {
598 - $msg->params( $wgLang->commaList( $details['blacklistedExt'] ) );
 582+ $msg->params( $this->getLang()->commaList( $details['blacklistedExt'] ) );
599583 } else {
600584 $msg->params( $details['finalExt'] );
601585 }
602 - $msg->params( $wgLang->commaList( $wgFileExtensions ),
 586+ $msg->params( $this->getLang()->commaList( $wgFileExtensions ),
603587 count( $wgFileExtensions ) );
604588
605589 // Add PLURAL support for the first parameter. This results
@@ -639,13 +623,12 @@
640624 * @return Boolean: success
641625 */
642626 protected function unsaveUploadedFile() {
643 - global $wgOut;
644627 if ( !( $this->mUpload instanceof UploadFromStash ) ) {
645628 return true;
646629 }
647630 $success = $this->mUpload->unsaveUploadedFile();
648631 if ( !$success ) {
649 - $wgOut->showFileDeleteError( $this->mUpload->getTempPath() );
 632+ $this->getOutput()->showFileDeleteError( $this->mUpload->getTempPath() );
650633 return false;
651634 } else {
652635 return true;
@@ -662,8 +645,6 @@
663646 * @return String: empty string if there is no warning or an HTML fragment
664647 */
665648 public static function getExistsWarning( $exists ) {
666 - global $wgUser;
667 -
668649 if ( !$exists ) {
669650 return '';
670651 }
@@ -672,8 +653,6 @@
673654 $filename = $file->getTitle()->getPrefixedText();
674655 $warning = '';
675656
676 - $sk = $wgUser->getSkin();
677 -
678657 if( $exists['warning'] == 'exists' ) {
679658 // Exact match
680659 $warning = wfMsgExt( 'fileexists', 'parseinline', $filename );
@@ -697,7 +676,7 @@
698677 } elseif ( $exists['warning'] == 'was-deleted' ) {
699678 # If the file existed before and was deleted, warn the user of this
700679 $ltitle = SpecialPage::getTitleFor( 'Log' );
701 - $llink = $sk->linkKnown(
 680+ $llink = Linker::linkKnown(
702681 $ltitle,
703682 wfMsgHtml( 'deletionlog' ),
704683 array(),
@@ -741,7 +720,6 @@
742721 */
743722 public static function getDupeWarning( $dupes ) {
744723 if( $dupes ) {
745 - global $wgOut;
746724 $msg = '<gallery>';
747725 foreach( $dupes as $file ) {
748726 $title = $file->getTitle();
@@ -751,7 +729,7 @@
752730 $msg .= '</gallery>';
753731 return '<li>' .
754732 wfMsgExt( 'file-exists-duplicate', array( 'parse' ), count( $dupes ) ) .
755 - $wgOut->parse( $msg ) .
 733+ $this->getOutput()->parse( $msg ) .
756734 "</li>\n";
757735 } else {
758736 return '';
@@ -779,7 +757,7 @@
780758
781759 protected $mMaxFileSize = array();
782760
783 - public function __construct( $options = array() ) {
 761+ public function __construct( array $options = array(), RequestContext $context = null ) {
784762 $this->mWatch = !empty( $options['watch'] );
785763 $this->mForReUpload = !empty( $options['forreupload'] );
786764 $this->mSessionKey = isset( $options['sessionkey'] )
@@ -803,7 +781,7 @@
804782 + $this->getOptionsSection();
805783
806784 wfRunHooks( 'UploadFormInitDescriptor', array( &$descriptor ) );
807 - parent::__construct( $descriptor, 'upload' );
 785+ parent::__construct( $descriptor, $context, 'upload' );
808786
809787 # Set some form properties
810788 $this->setSubmitText( wfMsg( 'uploadbtn' ) );
@@ -829,8 +807,6 @@
830808 * @return Array: descriptor array
831809 */
832810 protected function getSourceSection() {
833 - global $wgLang, $wgUser, $wgRequest;
834 -
835811 if ( $this->mSessionKey ) {
836812 return array(
837813 'SessionKey' => array(
@@ -844,9 +820,9 @@
845821 );
846822 }
847823
848 - $canUploadByUrl = UploadFromUrl::isEnabled() && $wgUser->isAllowed( 'upload_by_url' );
 824+ $canUploadByUrl = UploadFromUrl::isEnabled() && $this->getUser()->isAllowed( 'upload_by_url' );
849825 $radio = $canUploadByUrl;
850 - $selectedSourceType = strtolower( $wgRequest->getText( 'wpSourceType', 'File' ) );
 826+ $selectedSourceType = strtolower( $this->getRequest()->getText( 'wpSourceType', 'File' ) );
851827
852828 $descriptor = array();
853829 if ( $this->mTextTop ) {
@@ -876,7 +852,7 @@
877853 'radio' => &$radio,
878854 'help' => wfMsgExt( 'upload-maxfilesize',
879855 array( 'parseinline', 'escapenoentities' ),
880 - $wgLang->formatSize( $this->mMaxUploadSize['file'] )
 856+ $this->getContext()->getLang()->formatSize( $this->mMaxUploadSize['file'] )
881857 ) . ' ' . wfMsgHtml( 'upload_source_file' ),
882858 'checked' => $selectedSourceType == 'file',
883859 );
@@ -891,7 +867,7 @@
892868 'radio' => &$radio,
893869 'help' => wfMsgExt( 'upload-maxfilesize',
894870 array( 'parseinline', 'escapenoentities' ),
895 - $wgLang->formatSize( $this->mMaxUploadSize['url'] )
 871+ $this->getContext()->getLang()->formatSize( $this->mMaxUploadSize['url'] )
896872 ) . ' ' . wfMsgHtml( 'upload_source_url' ),
897873 'checked' => $selectedSourceType == 'url',
898874 );
@@ -915,7 +891,7 @@
916892 protected function getExtensionsMessage() {
917893 # Print a list of allowed file extensions, if so configured. We ignore
918894 # MIME type here, it's incomprehensible to most people and too long.
919 - global $wgLang, $wgCheckFileExtensions, $wgStrictFileExtensions,
 895+ global $wgCheckFileExtensions, $wgStrictFileExtensions,
920896 $wgFileExtensions, $wgFileBlacklist;
921897
922898 if( $wgCheckFileExtensions ) {
@@ -923,16 +899,16 @@
924900 # Everything not permitted is banned
925901 $extensionsList =
926902 '<div id="mw-upload-permitted">' .
927 - wfMsgExt( 'upload-permitted', 'parse', $wgLang->commaList( $wgFileExtensions ) ) .
 903+ wfMsgExt( 'upload-permitted', 'parse', $this->getContext()->getLang()->commaList( $wgFileExtensions ) ) .
928904 "</div>\n";
929905 } else {
930906 # We have to list both preferred and prohibited
931907 $extensionsList =
932908 '<div id="mw-upload-preferred">' .
933 - wfMsgExt( 'upload-preferred', 'parse', $wgLang->commaList( $wgFileExtensions ) ) .
 909+ wfMsgExt( 'upload-preferred', 'parse', $this->getContext()->getLang()->commaList( $wgFileExtensions ) ) .
934910 "</div>\n" .
935911 '<div id="mw-upload-prohibited">' .
936 - wfMsgExt( 'upload-prohibited', 'parse', $wgLang->commaList( $wgFileBlacklist ) ) .
 912+ wfMsgExt( 'upload-prohibited', 'parse', $this->getContext()->getLang()->commaList( $wgFileBlacklist ) ) .
937913 "</div>\n";
938914 }
939915 } else {
@@ -949,8 +925,6 @@
950926 * @return Array: descriptor array
951927 */
952928 protected function getDescriptionSection() {
953 - global $wgUser;
954 -
955929 if ( $this->mSessionKey ) {
956930 $stash = RepoGroup::singleton()->getLocalRepo()->getUploadStash();
957931 try {
@@ -990,7 +964,7 @@
991965 ? 'filereuploadsummary'
992966 : 'fileuploadsummary',
993967 'default' => $this->mComment,
994 - 'cols' => intval( $wgUser->getOption( 'cols' ) ),
 968+ 'cols' => intval( $this->getUser()->getOption( 'cols' ) ),
995969 'rows' => 8,
996970 )
997971 );
@@ -1049,16 +1023,15 @@
10501024 * @return Array: descriptor array
10511025 */
10521026 protected function getOptionsSection() {
1053 - global $wgUser;
1054 -
1055 - if ( $wgUser->isLoggedIn() ) {
 1027+ $user = $this->getUser();
 1028+ if ( $user->isLoggedIn() ) {
10561029 $descriptor = array(
10571030 'Watchthis' => array(
10581031 'type' => 'check',
10591032 'id' => 'wpWatchthis',
10601033 'label-message' => 'watchthisupload',
10611034 'section' => 'options',
1062 - 'default' => $wgUser->getOption( 'watchcreations' ),
 1035+ 'default' => $user->getOption( 'watchcreations' ),
10631036 )
10641037 );
10651038 }
@@ -1097,11 +1070,10 @@
10981071 }
10991072
11001073 /**
1101 - * Add upload JS to $wgOut
 1074+ * Add upload JS to the OutputPage
11021075 */
11031076 protected function addUploadJS() {
11041077 global $wgUseAjax, $wgAjaxUploadDestCheck, $wgAjaxLicensePreview, $wgEnableAPI, $wgStrictFileExtensions;
1105 - global $wgOut;
11061078
11071079 $useAjaxDestCheck = $wgUseAjax && $wgAjaxUploadDestCheck;
11081080 $useAjaxLicensePreview = $wgUseAjax && $wgAjaxLicensePreview && $wgEnableAPI;
@@ -1120,10 +1092,11 @@
11211093 'wgMaxUploadSize' => $this->mMaxUploadSize,
11221094 );
11231095
1124 - $wgOut->addScript( Skin::makeVariablesScript( $scriptVars ) );
 1096+ $out = $this->getOutput();
 1097+ $out->addScript( Skin::makeVariablesScript( $scriptVars ) );
11251098
11261099
1127 - $wgOut->addModules( array(
 1100+ $out->addModules( array(
11281101 'mediawiki.action.edit', // For <charinsert> support
11291102 'mediawiki.legacy.upload', // Old form stuff...
11301103 'mediawiki.special.upload', // Newer extras for thumbnail preview.

Follow-up revisions

RevisionCommit summaryAuthorDate
r96639Resolve fixme on r96249: can't use in static contextdemon01:01, 9 September 2011

Comments

#Comment by Platonides (talk | contribs)   21:22, 8 September 2011

You can't use $this->getOutput() in a static function!

Status & tagging log