From 0eca0c1e90a5311e54fa2793a884d8d242e4977f Mon Sep 17 00:00:00 2001 From: Alexandre Emsenhuber Date: Wed, 20 Jul 2011 19:49:15 +0000 Subject: [PATCH] * Use local context instead of global variables * Call Linker methods statically * Throw exceptions directly instead of calling OutputPage's methods that will throw them * Use Title::getUserPermissionsErrors() instead of Title::userCan() so that the User object can be passed to them --- includes/specials/SpecialMovepage.php | 191 +++++++++++++------------- 1 file changed, 93 insertions(+), 98 deletions(-) diff --git a/includes/specials/SpecialMovepage.php b/includes/specials/SpecialMovepage.php index e768255f5f..7a5a4210fb 100644 --- a/includes/specials/SpecialMovepage.php +++ b/includes/specials/SpecialMovepage.php @@ -42,55 +42,53 @@ class MovePageForm extends UnlistedSpecialPage { } public function execute( $par ) { - global $wgUser, $wgOut, $wgRequest; - # Check for database lock if ( wfReadOnly() ) { - $wgOut->readOnlyPage(); - return; + throw new ReadOnlyError; } $this->setHeaders(); $this->outputHeader(); - $target = !is_null( $par ) ? $par : $wgRequest->getVal( 'target' ); + $request = $this->getRequest(); + $target = !is_null( $par ) ? $par : $request->getVal( 'target' ); // Yes, the use of getVal() and getText() is wanted, see bug 20365 - $oldTitleText = $wgRequest->getVal( 'wpOldTitle', $target ); - $newTitleText = $wgRequest->getText( 'wpNewTitle' ); + $oldTitleText = $request->getVal( 'wpOldTitle', $target ); + $newTitleText = $request->getText( 'wpNewTitle' ); $this->oldTitle = Title::newFromText( $oldTitleText ); $this->newTitle = Title::newFromText( $newTitleText ); if( is_null( $this->oldTitle ) ) { - $wgOut->showErrorPage( 'notargettitle', 'notargettext' ); - return; + throw new ErrorPageError( 'notargettitle', 'notargettext' ); } if( !$this->oldTitle->exists() ) { - $wgOut->showErrorPage( 'nopagetitle', 'nopagetext' ); - return; + throw new ErrorPageError( 'nopagetitle', 'nopagetext' ); } + $user = $this->getUser(); + # Check rights - $permErrors = $this->oldTitle->getUserPermissionsErrors( 'move', $wgUser ); + $permErrors = $this->oldTitle->getUserPermissionsErrors( 'move', $user ); if( !empty( $permErrors ) ) { - $wgOut->showPermissionsErrorPage( $permErrors ); + $this->getOutput()->showPermissionsErrorPage( $permErrors ); return; } - $def = !$wgRequest->wasPosted(); + $def = !$request->wasPosted(); - $this->reason = $wgRequest->getText( 'wpReason' ); - $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', $def ); - $this->fixRedirects = $wgRequest->getBool( 'wpFixRedirects', $def ); - $this->leaveRedirect = $wgRequest->getBool( 'wpLeaveRedirect', $def ); - $this->moveSubpages = $wgRequest->getBool( 'wpMovesubpages', false ); - $this->deleteAndMove = $wgRequest->getBool( 'wpDeleteAndMove' ) && $wgRequest->getBool( 'wpConfirm' ); - $this->moveOverShared = $wgRequest->getBool( 'wpMoveOverSharedFile', false ); - $this->watch = $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn(); + $this->reason = $request->getText( 'wpReason' ); + $this->moveTalk = $request->getBool( 'wpMovetalk', $def ); + $this->fixRedirects = $request->getBool( 'wpFixRedirects', $def ); + $this->leaveRedirect = $request->getBool( 'wpLeaveRedirect', $def ); + $this->moveSubpages = $request->getBool( 'wpMovesubpages', false ); + $this->deleteAndMove = $request->getBool( 'wpDeleteAndMove' ) && $request->getBool( 'wpConfirm' ); + $this->moveOverShared = $request->getBool( 'wpMoveOverSharedFile', false ); + $this->watch = $request->getCheck( 'wpWatch' ) && $user->isLoggedIn(); - if ( 'submit' == $wgRequest->getVal( 'action' ) && $wgRequest->wasPosted() - && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) { + if ( 'submit' == $request->getVal( 'action' ) && $request->wasPosted() + && $user->matchEditToken( $request->getVal( 'wpEditToken' ) ) ) { $this->doSubmit(); } else { $this->showForm( '' ); @@ -105,16 +103,15 @@ class MovePageForm extends UnlistedSpecialPage { * OutputPage::wrapWikiMsg(). */ function showForm( $err ) { - global $wgOut, $wgUser, $wgContLang, $wgFixDoubleRedirects; - - $skin = $this->getSkin(); + global $wgContLang, $wgFixDoubleRedirects, $wgMaximumMovedPages; - $oldTitleLink = $skin->link( $this->oldTitle ); + $this->getSkin()->setRelevantTitle( $this->oldTitle ); - $wgOut->setPagetitle( wfMsg( 'move-page', $this->oldTitle->getPrefixedText() ) ); - $skin->setRelevantTitle( $this->oldTitle ); + $oldTitleLink = Linker::link( $this->oldTitle ); - $wgOut->addModules( 'mediawiki.special.movePage' ); + $out = $this->getOutput(); + $out->setPagetitle( wfMsg( 'move-page', $this->oldTitle->getPrefixedText() ) ); + $out->addModules( 'mediawiki.special.movePage' ); $newTitle = $this->newTitle; @@ -135,8 +132,10 @@ class MovePageForm extends UnlistedSpecialPage { } } - if ( !empty($err) && $err[0] == 'articleexists' && $wgUser->isAllowed( 'delete' ) ) { - $wgOut->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() ); + $user = $this->getUser(); + + if ( !empty($err) && $err[0] == 'articleexists' && $user->isAllowed( 'delete' ) ) { + $out->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() ); $movepagebtn = wfMsg( 'delete_and_move' ); $submitVar = 'wpDeleteAndMove'; $confirm = " @@ -149,17 +148,17 @@ class MovePageForm extends UnlistedSpecialPage { $err = ''; } else { if ($this->oldTitle->getNamespace() == NS_USER && !$this->oldTitle->isSubpage() ) { - $wgOut->wrapWikiMsg( "
\n$1\n
", 'moveuserpage-warning' ); + $out->wrapWikiMsg( "
\n$1\n
", 'moveuserpage-warning' ); } - $wgOut->addWikiMsg( $wgFixDoubleRedirects ? 'movepagetext' : + $out->addWikiMsg( $wgFixDoubleRedirects ? 'movepagetext' : 'movepagetext-noredirectfixer' ); $movepagebtn = wfMsg( 'movepagebtn' ); $submitVar = 'wpMove'; $confirm = false; } - if ( !empty($err) && $err[0] == 'file-exists-sharedrepo' && $wgUser->isAllowed( 'reupload-shared' ) ) { - $wgOut->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() ); + if ( !empty($err) && $err[0] == 'file-exists-sharedrepo' && $user->isAllowed( 'reupload-shared' ) ) { + $out->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() ); $submitVar = 'wpMoveOverSharedFile'; $err = ''; } @@ -179,19 +178,19 @@ class MovePageForm extends UnlistedSpecialPage { } if ( $considerTalk ) { - $wgOut->addWikiMsg( 'movepagetalktext' ); + $out->addWikiMsg( 'movepagetalktext' ); } - $token = htmlspecialchars( $wgUser->editToken() ); + $token = htmlspecialchars( $user->editToken() ); if ( !empty($err) ) { - $wgOut->setSubtitle( wfMsg( 'formerror' ) ); + $out->setSubtitle( wfMsg( 'formerror' ) ); if( $err[0] == 'hookaborted' ) { $hookErr = $err[1]; $errMsg = "

$hookErr

\n"; - $wgOut->addHTML( $errMsg ); + $out->addHTML( $errMsg ); } else { - $wgOut->wrapWikiMsg( "

\n$1\n

", $err ); + $out->wrapWikiMsg( "

\n$1\n

", $err ); } } @@ -205,13 +204,13 @@ class MovePageForm extends UnlistedSpecialPage { $noticeMsg = 'protectedpagemovewarning'; $classes[] = 'mw-textarea-protected'; } - $wgOut->addHTML( "
\n" ); - $wgOut->addWikiMsg( $noticeMsg ); - LogEventsList::showLogExtract( $wgOut, 'protect', $this->oldTitle->getPrefixedText(), '', array( 'lim' => 1 ) ); - $wgOut->addHTML( "
\n" ); + $out->addHTML( "
\n" ); + $out->addWikiMsg( $noticeMsg ); + LogEventsList::showLogExtract( $out, 'protect', $this->oldTitle->getPrefixedText(), '', array( 'lim' => 1 ) ); + $out->addHTML( "
\n" ); } - $wgOut->addHTML( + $out->addHTML( Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) . Xml::openElement( 'fieldset' ) . Xml::element( 'legend', null, wfMsg( 'move-page-legend' ) ) . @@ -245,7 +244,7 @@ class MovePageForm extends UnlistedSpecialPage { ); if( $considerTalk ) { - $wgOut->addHTML( " + $out->addHTML( " " . @@ -255,8 +254,8 @@ class MovePageForm extends UnlistedSpecialPage { ); } - if ( $wgUser->isAllowed( 'suppressredirect' ) ) { - $wgOut->addHTML( " + if ( $user->isAllowed( 'suppressredirect' ) ) { + $out->addHTML( " " . @@ -268,7 +267,7 @@ class MovePageForm extends UnlistedSpecialPage { } if ( $hasRedirects ) { - $wgOut->addHTML( " + $out->addHTML( " " . @@ -280,11 +279,9 @@ class MovePageForm extends UnlistedSpecialPage { } if( ($this->oldTitle->hasSubpages() || $this->oldTitle->getTalkPage()->hasSubpages()) - && $this->oldTitle->userCan( 'move-subpages' ) ) + && !count( $this->oldTitle->getUserPermissionsErrors( 'move-subpages', $user ) ) ) { - global $wgMaximumMovedPages, $wgLang; - - $wgOut->addHTML( " + $out->addHTML( " " . @@ -301,7 +298,7 @@ class MovePageForm extends UnlistedSpecialPage { ? 'move-subpages' : 'move-talk-subpages' ), array( 'parseinline' ), - $wgLang->formatNum( $wgMaximumMovedPages ), + $this->getLang()->formatNum( $wgMaximumMovedPages ), # $2 to allow use of PLURAL in message. $wgMaximumMovedPages ) @@ -311,11 +308,11 @@ class MovePageForm extends UnlistedSpecialPage { ); } - $watchChecked = $wgUser->isLoggedIn() && ($this->watch || $wgUser->getBoolOption( 'watchmoves' ) + $watchChecked = $user->isLoggedIn() && ($this->watch || $user->getBoolOption( 'watchmoves' ) || $this->oldTitle->userIsWatching()); # Don't allow watching if user is not logged in - if( $wgUser->isLoggedIn() ) { - $wgOut->addHTML( " + if( $user->isLoggedIn() ) { + $out->addHTML( " " . @@ -324,7 +321,7 @@ class MovePageForm extends UnlistedSpecialPage { "); } - $wgOut->addHTML( " + $out->addHTML( " {$confirm}   @@ -339,32 +336,31 @@ class MovePageForm extends UnlistedSpecialPage { "\n" ); - $this->showLogFragment( $this->oldTitle, $wgOut ); - $this->showSubpages( $this->oldTitle, $wgOut ); + $this->showLogFragment( $this->oldTitle ); + $this->showSubpages( $this->oldTitle ); } function doSubmit() { - global $wgOut, $wgUser, $wgMaximumMovedPages, $wgLang; - global $wgFixDoubleRedirects; + global $wgMaximumMovedPages, $wgFixDoubleRedirects, $wgDeleteRevisionsLimit; - if ( $wgUser->pingLimiter( 'move' ) ) { - $wgOut->rateLimited(); - return; + $user = $this->getUser(); + + if ( $user->pingLimiter( 'move' ) ) { + throw new ThrottledError; } $ot = $this->oldTitle; $nt = $this->newTitle; # Delete to make way if requested - if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove ) { + if ( $user->isAllowed( 'delete' ) && $this->deleteAndMove ) { $article = new Article( $nt ); # Disallow deletions of big articles $bigHistory = $article->isBigDeletion(); - if( $bigHistory && !$nt->userCan( 'bigdelete' ) ) { - global $wgDeleteRevisionsLimit; - $this->showForm( array('delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) ); + if( $bigHistory && count( $nt->getUserPermissionsErrors( 'bigdelete', $user ) ) ) { + $this->showForm( array('delete-toobig', $this->getLang()->formatNum( $wgDeleteRevisionsLimit ) ) ); return; } @@ -386,7 +382,7 @@ class MovePageForm extends UnlistedSpecialPage { # Show a warning if the target file exists on a shared repo if ( $nt->getNamespace() == NS_FILE - && !( $this->moveOverShared && $wgUser->isAllowed( 'reupload-shared' ) ) + && !( $this->moveOverShared && $user->isAllowed( 'reupload-shared' ) ) && !RepoGroup::singleton()->getLocalRepo()->findFile( $nt ) && wfFindFile( $nt ) ) { @@ -395,7 +391,7 @@ class MovePageForm extends UnlistedSpecialPage { } - if ( $wgUser->isAllowed( 'suppressredirect' ) ) { + if ( $user->isAllowed( 'suppressredirect' ) ) { $createRedirect = $this->leaveRedirect; } else { $createRedirect = true; @@ -415,7 +411,8 @@ class MovePageForm extends UnlistedSpecialPage { wfRunHooks( 'SpecialMovepageAfterMove', array( &$this, &$ot, &$nt ) ); - $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) ); + $out = $this->getOutput(); + $out->setPagetitle( wfMsg( 'pagemovedsub' ) ); $oldUrl = $ot->getFullUrl( 'redirect=no' ); $newUrl = $nt->getFullUrl(); @@ -425,8 +422,8 @@ class MovePageForm extends UnlistedSpecialPage { $newLink = "[$newUrl $newText]"; $msgName = $createRedirect ? 'movepage-moved-redirect' : 'movepage-moved-noredirect'; - $wgOut->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText ); - $wgOut->addWikiMsg( $msgName ); + $out->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText ); + $out->addWikiMsg( $msgName ); # Now we move extra pages we've been asked to move: subpages and talk # pages. First, if the old page or the new page is a talk page, we @@ -435,7 +432,7 @@ class MovePageForm extends UnlistedSpecialPage { $this->moveTalk = false; } - if( !$ot->userCan( 'move-subpages' ) ) { + if ( count( $ot->getUserPermissionsErrors( 'move-subpages', $user ) ) ) { $this->moveSubpages = false; } @@ -492,7 +489,6 @@ class MovePageForm extends UnlistedSpecialPage { } $extraOutput = array(); - $skin = $this->getSkin(); $count = 1; foreach( $extraPages as $oldSubpage ) { if( $ot->equals( $oldSubpage ) ) { @@ -514,7 +510,7 @@ class MovePageForm extends UnlistedSpecialPage { # be longer than 255 characters. $newSubpage = Title::makeTitleSafe( $newNs, $newPageName ); if( !$newSubpage ) { - $oldLink = $skin->linkKnown( $oldSubpage ); + $oldLink = Linker::linkKnown( $oldSubpage ); $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, htmlspecialchars(Title::makeName( $newNs, $newPageName ))); continue; @@ -522,7 +518,7 @@ class MovePageForm extends UnlistedSpecialPage { # This was copy-pasted from Renameuser, bleh. if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) { - $link = $skin->linkKnown( $newSubpage ); + $link = Linker::linkKnown( $newSubpage ); $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link ); } else { $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason, $createRedirect ); @@ -530,22 +526,22 @@ class MovePageForm extends UnlistedSpecialPage { if ( $this->fixRedirects ) { DoubleRedirectJob::fixRedirects( 'move', $oldSubpage, $newSubpage ); } - $oldLink = $skin->linkKnown( + $oldLink = Linker::linkKnown( $oldSubpage, null, array(), array( 'redirect' => 'no' ) ); - $newLink = $skin->linkKnown( $newSubpage ); + $newLink = Linker::linkKnown( $newSubpage ); $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink ); ++$count; if( $count >= $wgMaximumMovedPages ) { - $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $wgLang->formatNum( $wgMaximumMovedPages ) ); + $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $this->getLang()->formatNum( $wgMaximumMovedPages ) ); break; } } else { - $oldLink = $skin->linkKnown( $oldSubpage ); - $newLink = $skin->link( $newSubpage ); + $oldLink = Linker::linkKnown( $oldSubpage ); + $newLink = Linker::link( $newSubpage ); $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink ); } } @@ -553,16 +549,16 @@ class MovePageForm extends UnlistedSpecialPage { } if( $extraOutput !== array() ) { - $wgOut->addHTML( "" ); + $out->addHTML( "" ); } # Deal with watches (we don't watch subpages) - if( $this->watch && $wgUser->isLoggedIn() ) { - $wgUser->addWatch( $ot ); - $wgUser->addWatch( $nt ); + if( $this->watch && $user->isLoggedIn() ) { + $user->addWatch( $ot ); + $user->addWatch( $nt ); } else { - $wgUser->removeWatch( $ot ); - $wgUser->removeWatch( $nt ); + $user->removeWatch( $ot ); + $user->removeWatch( $nt ); } # Re-clear the file redirect cache, which may have been polluted by @@ -573,20 +569,20 @@ class MovePageForm extends UnlistedSpecialPage { } } - function showLogFragment( $title, &$out ) { + function showLogFragment( $title ) { + $out = $this->getOutput(); $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'move' ) ) ); LogEventsList::showLogExtract( $out, 'move', $title->getPrefixedText() ); } - function showSubpages( $title, $out ) { - global $wgLang; - + function showSubpages( $title ) { if( !MWNamespace::hasSubpages( $title->getNamespace() ) ) return; $subpages = $title->getSubpages(); $count = $subpages instanceof TitleArray ? $subpages->count() : 0; + $out = $this->getOutput(); $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) ); # No subpages. @@ -595,12 +591,11 @@ class MovePageForm extends UnlistedSpecialPage { return; } - $out->addWikiMsg( 'movesubpagetext', $wgLang->formatNum( $count ) ); - $skin = $this->getSkin(); + $out->addWikiMsg( 'movesubpagetext', $this->getLang()->formatNum( $count ) ); $out->addHTML( "\n" ); -- 2.20.1