Adding context to the API
authorJohn Du Hart <johnduhart@users.mediawiki.org>
Wed, 26 Oct 2011 23:27:01 +0000 (23:27 +0000)
committerJohn Du Hart <johnduhart@users.mediawiki.org>
Wed, 26 Oct 2011 23:27:01 +0000 (23:27 +0000)
This will probably break something somewhere so when it does please yell at me on IRC and I'll fix it.

30 files changed:
includes/api/ApiBase.php
includes/api/ApiBlock.php
includes/api/ApiEditPage.php
includes/api/ApiEmailUser.php
includes/api/ApiFileRevert.php
includes/api/ApiImport.php
includes/api/ApiLogin.php
includes/api/ApiLogout.php
includes/api/ApiMain.php
includes/api/ApiMove.php
includes/api/ApiParse.php
includes/api/ApiPatrol.php
includes/api/ApiProtect.php
includes/api/ApiPurge.php
includes/api/ApiQueryBase.php
includes/api/ApiQueryBlocks.php
includes/api/ApiQueryDeletedrevs.php
includes/api/ApiQueryFilearchive.php
includes/api/ApiQueryInfo.php
includes/api/ApiQueryQueryPage.php
includes/api/ApiQueryRecentChanges.php
includes/api/ApiQueryUserContributions.php
includes/api/ApiQueryUserInfo.php
includes/api/ApiQueryWatchlist.php
includes/api/ApiRollback.php
includes/api/ApiUnblock.php
includes/api/ApiUndelete.php
includes/api/ApiUpload.php
includes/api/ApiUserrights.php
includes/api/ApiWatch.php

index 614c0cc..15f0078 100644 (file)
@@ -39,7 +39,7 @@
  *
  * @ingroup API
  */
-abstract class ApiBase {
+abstract class ApiBase extends ContextSource {
 
        // These constants allow modules to specify exactly how to treat incoming parameters.
 
@@ -72,6 +72,10 @@ abstract class ApiBase {
                $this->mMainModule = $mainModule;
                $this->mModuleName = $moduleName;
                $this->mModulePrefix = $modulePrefix;
+
+               if ( !$this->isMain() ) {
+                       $this->setContext( $this->mMainModule );
+               }
        }
 
        /*****************************************************************************
@@ -179,16 +183,11 @@ abstract class ApiBase {
         * The object will have the WebRequest and the User object set to the ones
         * used in this instance.
         *
+        * @deprecated 1.19 use getContext to get the current context
         * @return RequestContext
         */
        public function createContext() {
-               global $wgUser;
-
-               $context = new RequestContext;
-               $context->setRequest( $this->getMain()->getRequest() );
-               $context->setUser( $wgUser ); /// @todo FIXME: we should store the User object
-
-               return $context;
+               return $this->getContext();
        }
 
        /**
@@ -674,7 +673,6 @@ abstract class ApiBase {
 
                $userWatching = $titleObj->userIsWatching();
 
-               global $wgUser;
                switch ( $watchlist ) {
                        case 'watch':
                                return true;
@@ -693,7 +691,7 @@ abstract class ApiBase {
                                                ? 'watchdefault' : 'watchcreations';
                                }
                                # Watch the article based on the user preference
-                               return (bool)$wgUser->getOption( $userOption );
+                               return (bool)$this->getUser()->getOption( $userOption );
 
                        case 'nochange':
                                return $userWatching;
@@ -715,11 +713,11 @@ abstract class ApiBase {
                        return;
                }
 
-               global $wgUser;
+               $user = $this->getUser();
                if ( $value ) {
-                       WatchAction::doWatch( $titleObj, $wgUser );
+                       WatchAction::doWatch( $titleObj, $user );
                } else {
-                       WatchAction::doUnwatch( $titleObj, $wgUser );
+                       WatchAction::doUnwatch( $titleObj, $user );
                }
        }
 
@@ -767,9 +765,9 @@ abstract class ApiBase {
                                ApiBase::dieDebug( __METHOD__, "Boolean param $encParamName's default is set to '$default'" );
                        }
 
-                       $value = $this->getMain()->getRequest()->getCheck( $encParamName );
+                       $value = $this->getRequest()->getCheck( $encParamName );
                } else {
-                       $value = $this->getMain()->getRequest()->getVal( $encParamName, $default );
+                       $value = $this->getRequest()->getVal( $encParamName, $default );
 
                        if ( isset( $value ) && $type == 'namespace' ) {
                                $type = MWNamespace::getValidNamespaces();
@@ -1297,7 +1295,6 @@ abstract class ApiBase {
         * @return User
         */
        public function getWatchlistUser( $params ) {
-               global $wgUser;
                if ( !is_null( $params['owner'] ) && !is_null( $params['token'] ) ) {
                        $user = User::newFromName( $params['owner'], false );
                        if ( !$user->getId() ) {
@@ -1308,10 +1305,10 @@ abstract class ApiBase {
                                $this->dieUsage( 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences', 'bad_wltoken' );
                        }
                } else {
-                       if ( !$wgUser->isLoggedIn() ) {
+                       if ( !$this->getUser()->isLoggedIn() ) {
                                $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
                        }
-                       $user = $wgUser;
+                       $user = $this->getUser();
                }
                return $user;
        }
index f77fdf4..65001d6 100644 (file)
@@ -48,29 +48,29 @@ class ApiBlock extends ApiBase {
         * of success. If it fails, the result will specify the nature of the error.
         */
        public function execute() {
-               global $wgUser;
+               $user = $this->getUser();
                $params = $this->extractRequestParams();
 
                if ( $params['gettoken'] ) {
-                       $res['blocktoken'] = $wgUser->editToken( '', $this->getMain()->getRequest() );
+                       $res['blocktoken'] = $user->editToken( '', $this->getMain()->getRequest() );
                        $this->getResult()->addValue( null, $this->getModuleName(), $res );
                        return;
                }
 
-               if ( !$wgUser->isAllowed( 'block' ) ) {
+               if ( !$user->isAllowed( 'block' ) ) {
                        $this->dieUsageMsg( 'cantblock' );
                }
                # bug 15810: blocked admins should have limited access here
-               if ( $wgUser->isBlocked() ) {
+               if ( $user->isBlocked() ) {
                        $status = SpecialBlock::checkUnblockSelf( $params['user'] );
                        if ( $status !== true ) {
                                $this->dieUsageMsg( array( $status ) );
                        }
                }
-               if ( $params['hidename'] && !$wgUser->isAllowed( 'hideuser' ) ) {
+               if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
                        $this->dieUsageMsg( 'canthide' );
                }
-               if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $wgUser ) ) {
+               if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) {
                        $this->dieUsageMsg( 'cantblock-email' );
                }
 
index 4cfa343..61dea37 100644 (file)
@@ -43,7 +43,7 @@ class ApiEditPage extends ApiBase {
        }
 
        public function execute() {
-               global $wgUser;
+               $user = $this->getUser();
                $params = $this->extractRequestParams();
 
                if ( is_null( $params['text'] ) && is_null( $params['appendtext'] ) &&
@@ -88,6 +88,7 @@ class ApiEditPage extends ApiBase {
                }
 
                // Some functions depend on $wgTitle == $ep->mTitle
+               // TODO: Make them not or check if they still do
                global $wgTitle;
                $wgTitle = $titleObj;
 
@@ -99,9 +100,9 @@ class ApiEditPage extends ApiBase {
                }
 
                // Now let's check whether we're even allowed to do this
-               $errors = $titleObj->getUserPermissionsErrors( 'edit', $wgUser );
+               $errors = $titleObj->getUserPermissionsErrors( 'edit', $user );
                if ( !$titleObj->exists() ) {
-                       $errors = array_merge( $errors, $titleObj->getUserPermissionsErrors( 'create', $wgUser ) );
+                       $errors = array_merge( $errors, $titleObj->getUserPermissionsErrors( 'create', $user ) );
                }
                if ( count( $errors ) ) {
                        $this->dieUsageMsg( $errors[0] );
@@ -207,7 +208,7 @@ class ApiEditPage extends ApiBase {
                        $reqArr['wpStarttime'] = wfTimestampNow();      // Fake wpStartime
                }
 
-               if ( $params['minor'] || ( !$params['notminor'] && $wgUser->getOption( 'minordefault' ) ) )     {
+               if ( $params['minor'] || ( !$params['notminor'] && $user->getOption( 'minordefault' ) ) )       {
                        $reqArr['wpMinoredit'] = '';
                }
 
@@ -270,7 +271,7 @@ class ApiEditPage extends ApiBase {
                $oldRequest = $wgRequest;
                $wgRequest = $req;
 
-               $status = $ep->internalAttemptSave( $result, $wgUser->isAllowed( 'bot' ) && $params['bot'] );
+               $status = $ep->internalAttemptSave( $result, $user->isAllowed( 'bot' ) && $params['bot'] );
                $wgRequest = $oldRequest;
                global $wgMaxArticleSize;
 
index b79850c..82d8b6f 100644 (file)
@@ -40,8 +40,6 @@ class ApiEmailUser extends ApiBase {
        }
 
        public function execute() {
-               global $wgUser;
-
                $params = $this->extractRequestParams();
 
                // Validate target
@@ -51,7 +49,7 @@ class ApiEmailUser extends ApiBase {
                }
 
                // Check permissions and errors
-               $error = SpecialEmailUser::getPermissionsError( $wgUser, $params['token'] );
+               $error = SpecialEmailUser::getPermissionsError( $this->getUser(), $params['token'] );
                if ( $error ) {
                        $this->dieUsageMsg( array( $error ) );
                }
index 5a9ccc1..50d6856 100644 (file)
@@ -47,14 +47,12 @@ class ApiFileRevert extends ApiBase {
        }
 
        public function execute() {
-               global $wgUser;
-
                $this->params = $this->extractRequestParams();
                // Extract the file and archiveName from the request parameters
                $this->validateParameters();
 
                // Check whether we're allowed to revert this file
-               $this->checkPermissions( $wgUser );
+               $this->checkPermissions( $this->getUser() );
 
                $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
                $status = $this->file->upload( $sourceUrl, $this->params['comment'], $this->params['comment'] );
index 3bb3b9a..11c953c 100644 (file)
@@ -41,13 +41,12 @@ class ApiImport extends ApiBase {
        }
 
        public function execute() {
-               global $wgUser;
-
+               $user = $this->getUser();
                $params = $this->extractRequestParams();
 
                $isUpload = false;
                if ( isset( $params['interwikisource'] ) ) {
-                       if ( !$wgUser->isAllowed( 'import' ) ) {
+                       if ( !$user->isAllowed( 'import' ) ) {
                                $this->dieUsageMsg( 'cantimport' );
                        }
                        if ( !isset( $params['interwikipage'] ) ) {
@@ -61,7 +60,7 @@ class ApiImport extends ApiBase {
                        );
                } else {
                        $isUpload = true;
-                       if ( !$wgUser->isAllowed( 'importupload' ) ) {
+                       if ( !$user->isAllowed( 'importupload' ) ) {
                                $this->dieUsageMsg( 'cantimport-upload' );
                        }
                        $source = ImportStreamSource::newFromUpload( 'xml' );
index b18954b..38fd155 100644 (file)
@@ -68,29 +68,32 @@ class ApiLogin extends ApiBase {
                        wfSetupSession();
                }
 
-               $context = $this->createContext();
+               $context = new DerivativeContext( $this->getContext() );
                $context->setRequest( $req );
+               /*$context = $this->createContext();
+               $context->setRequest( $req );*/
                $loginForm = new LoginForm();
                $loginForm->setContext( $context );
+               $user = $this->getUser();
 
-               global $wgCookiePrefix, $wgUser, $wgPasswordAttemptThrottle;
+               global $wgCookiePrefix, $wgPasswordAttemptThrottle;
 
                $authRes = $loginForm->authenticateUserData();
                switch ( $authRes ) {
                        case LoginForm::SUCCESS:
-                               $wgUser->setOption( 'rememberpassword', 1 );
-                               $wgUser->setCookies( $this->getMain()->getRequest() );
+                               $user->setOption( 'rememberpassword', 1 );
+                               $user->setCookies( $this->getMain()->getRequest() );
 
                                // Run hooks.
                                // @todo FIXME: Split back and frontend from this hook.
                                // @todo FIXME: This hook should be placed in the backend
                                $injected_html = '';
-                               wfRunHooks( 'UserLoginComplete', array( &$wgUser, &$injected_html ) );
+                               wfRunHooks( 'UserLoginComplete', array( &$user, &$injected_html ) );
 
                                $result['result'] = 'Success';
-                               $result['lguserid'] = intval( $wgUser->getId() );
-                               $result['lgusername'] = $wgUser->getName();
-                               $result['lgtoken'] = $wgUser->getToken();
+                               $result['lguserid'] = intval( $user->getId() );
+                               $result['lgusername'] = $user->getName();
+                               $result['lgtoken'] = $user->getToken();
                                $result['cookieprefix'] = $wgCookiePrefix;
                                $result['sessionid'] = session_id();
                                break;
index dac48cd..96ce5c9 100644 (file)
@@ -42,13 +42,13 @@ class ApiLogout extends ApiBase {
        }
 
        public function execute() {
-               global $wgUser;
-               $oldName = $wgUser->getName();
-               $wgUser->logout();
+               $user = $this->getUser();
+               $oldName = $user->getName();
+               $user->logout();
 
                // Give extensions to do something after user logout
                $injected_html = '';
-               wfRunHooks( 'UserLogoutComplete', array( &$wgUser, &$injected_html, $oldName ) );
+               wfRunHooks( 'UserLogoutComplete', array( &$user, &$injected_html, $oldName ) );
        }
 
        public function isReadMode() {
index 549aad6..070460c 100644 (file)
@@ -132,7 +132,7 @@ class ApiMain extends ApiBase {
        private $mPrinter;
 
        private $mModules, $mModuleNames, $mFormats, $mFormatNames;
-       private $mResult, $mAction, $mShowVersions, $mEnableWrite, $mRequest;
+       private $mResult, $mAction, $mShowVersions, $mEnableWrite/*, $mRequest*/;
        private $mInternalMode, $mSquidMaxage, $mModule;
 
        private $mCacheMode = 'private';
@@ -141,11 +141,22 @@ class ApiMain extends ApiBase {
        /**
         * Constructs an instance of ApiMain that utilizes the module and format specified by $request.
         *
-        * @param $request WebRequest - if this is an instance of FauxRequest, errors are thrown and no printing occurs
+        * @param $context IContextSource|WebRequest - if this is an instance of FauxRequest, errors are thrown and no printing occurs
         * @param $enableWrite bool should be set to true if the api may modify data
         */
-       public function __construct( $request, $enableWrite = false ) {
-               $this->mInternalMode = ( $request instanceof FauxRequest );
+       public function __construct( $context = null, $enableWrite = false ) {
+               if ( $context === null ) {
+                       $context = RequestContext::getMain();
+               } elseif ( $context instanceof WebRequest ) {
+                       // BC for pre-1.19
+                       $request = $context;
+                       $context = RequestContext::getMain();
+                       $context->setRequest( $request );
+               }
+               // We set a derivative context so we can change stuff later
+               $this->setContext( new DerivativeContext( $context ) );
+
+               $this->mInternalMode = ( $this->getRequest() instanceof FauxRequest );
 
                // Special handling for the main module: $parent === $this
                parent::__construct( $this, $this->mInternalMode ? 'main_int' : 'main' );
@@ -156,11 +167,12 @@ class ApiMain extends ApiBase {
                        // Remove all modules other than login
                        global $wgUser;
 
-                       if ( $request->getVal( 'callback' ) !== null ) {
+                       if ( $this->getRequest()->getVal( 'callback' ) !== null ) {
                                // JSON callback allows cross-site reads.
                                // For safety, strip user credentials.
                                wfDebug( "API: stripping user credentials for JSON callback\n" );
                                $wgUser = new User();
+                               $this->getContext()->setUser( $wgUser );
                        }
                }
 
@@ -175,7 +187,7 @@ class ApiMain extends ApiBase {
                $this->mShowVersions = false;
                $this->mEnableWrite = $enableWrite;
 
-               $this->mRequest = &$request;
+               //$this->mRequest = &$request;
 
                $this->mSquidMaxage = - 1; // flag for executeActionWithErrorHandling()
                $this->mCommit = false;
@@ -193,9 +205,9 @@ class ApiMain extends ApiBase {
         * Return the request object that contains client's request
         * @return WebRequest
         */
-       public function getRequest() {
+       /*public function getRequest() {
                return $this->mRequest;
-       }
+       }*/
 
        /**
         * Get the ApiResult object associated with current request
@@ -596,8 +608,7 @@ class ApiMain extends ApiBase {
                        if ( !isset( $moduleParams['token'] ) ) {
                                $this->dieUsageMsg( array( 'missingparam', 'token' ) );
                        } else {
-                               global $wgUser;
-                               if ( !$wgUser->matchEditToken( $moduleParams['token'], $salt, $this->getRequest() ) ) {
+                               if ( !$this->getUser()->matchEditToken( $moduleParams['token'], $salt, $this->getRequest() ) ) {
                                        $this->dieUsageMsg( 'sessionfailure' );
                                }
                        }
@@ -639,9 +650,9 @@ class ApiMain extends ApiBase {
         * @param $module ApiBase An Api module
         */
        protected function checkExecutePermissions( $module ) {
-               global $wgUser;
+               $user = $this->getUser();
                if ( $module->isReadMode() && !in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) &&
-                       !$wgUser->isAllowed( 'read' ) )
+                       !$user->isAllowed( 'read' ) )
                {
                        $this->dieUsageMsg( 'readrequired' );
                }
@@ -649,7 +660,7 @@ class ApiMain extends ApiBase {
                        if ( !$this->mEnableWrite ) {
                                $this->dieUsageMsg( 'writedisabled' );
                        }
-                       if ( !$wgUser->isAllowed( 'writeapi' ) ) {
+                       if ( !$user->isAllowed( 'writeapi' ) ) {
                                $this->dieUsageMsg( 'writerequired' );
                        }
                        if ( wfReadOnly() ) {
@@ -665,7 +676,7 @@ class ApiMain extends ApiBase {
         */
        protected function setupExternalResponse( $module, $params ) {
                // Ignore mustBePosted() for internal calls
-               if ( $module->mustBePosted() && !$this->mRequest->wasPosted() ) {
+               if ( $module->mustBePosted() && !$this->getRequest()->wasPosted() ) {
                        $this->dieUsageMsg( array( 'mustbeposted', $this->mAction ) );
                }
 
@@ -975,8 +986,7 @@ class ApiMain extends ApiBase {
         */
        public function canApiHighLimits() {
                if ( !isset( $this->mCanApiHighLimits ) ) {
-                       global $wgUser;
-                       $this->mCanApiHighLimits = $wgUser->isAllowed( 'apihighlimits' );
+                       $this->mCanApiHighLimits = $this->getUser()->isAllowed( 'apihighlimits' );
                }
 
                return $this->mCanApiHighLimits;
index 7ee0c42..f3294ee 100644 (file)
@@ -40,7 +40,7 @@ class ApiMove extends ApiBase {
        }
 
        public function execute() {
-               global $wgUser;
+               $user = $this->getUser();
                $params = $this->extractRequestParams();
                if ( is_null( $params['reason'] ) ) {
                        $params['reason'] = '';
@@ -75,9 +75,9 @@ class ApiMove extends ApiBase {
                        && !RepoGroup::singleton()->getLocalRepo()->findFile( $toTitle )
                        && wfFindFile( $toTitle ) )
                {
-                       if ( !$params['ignorewarnings'] && $wgUser->isAllowed( 'reupload-shared' ) ) {
+                       if ( !$params['ignorewarnings'] && $user->isAllowed( 'reupload-shared' ) ) {
                                $this->dieUsageMsg( 'sharedfile-exists' );
-                       } elseif ( !$wgUser->isAllowed( 'reupload-shared' ) ) {
+                       } elseif ( !$user->isAllowed( 'reupload-shared' ) ) {
                                $this->dieUsageMsg( 'cantoverwrite-sharedfile' );
                        }
                }
@@ -89,7 +89,7 @@ class ApiMove extends ApiBase {
                }
 
                $r = array( 'from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason'] );
-               if ( !$params['noredirect'] || !$wgUser->isAllowed( 'suppressredirect' ) ) {
+               if ( !$params['noredirect'] || !$user->isAllowed( 'suppressredirect' ) ) {
                        $r['redirectcreated'] = '';
                }
 
index 395dd34..1c80b27 100644 (file)
@@ -62,7 +62,8 @@ class ApiParse extends ApiBase {
 
                // The parser needs $wgTitle to be set, apparently the
                // $title parameter in Parser::parse isn't enough *sigh*
-               global $wgParser, $wgUser, $wgTitle, $wgLang;
+               // TODO: Does this still need $wgTitle?
+               global $wgParser, $wgTitle, $wgLang;
 
                // Currently unnecessary, code to act as a safeguard against any change in current behaviour of uselang breaks
                $oldLang = null;
@@ -179,7 +180,7 @@ class ApiParse extends ApiBase {
                        }
 
                        if ( $params['pst'] || $params['onlypst'] ) {
-                               $this->pstText = $wgParser->preSaveTransform( $this->text, $titleObj, $wgUser, $popts );
+                               $this->pstText = $wgParser->preSaveTransform( $this->text, $titleObj, $this->getUser(), $popts );
                        }
                        if ( $params['onlypst'] ) {
                                // Build a result and bail out
index 2adc9a6..f9428cc 100644 (file)
@@ -42,15 +42,13 @@ class ApiPatrol extends ApiBase {
         * Patrols the article or provides the reason the patrol failed.
         */
        public function execute() {
-               global $wgUser;
-
                $params = $this->extractRequestParams();
 
                $rc = RecentChange::newFromID( $params['rcid'] );
                if ( !$rc instanceof RecentChange ) {
                        $this->dieUsageMsg( array( 'nosuchrcid', $params['rcid'] ) );
                }
-               $retval = $rc->doMarkPatrolled( $wgUser );
+               $retval = $rc->doMarkPatrolled( $this->getUser() );
 
                if ( $retval ) {
                        $this->dieUsageMsg( reset( $retval ) );
index a1a235d..177522a 100644 (file)
@@ -39,7 +39,7 @@ class ApiProtect extends ApiBase {
        }
 
        public function execute() {
-               global $wgUser, $wgRestrictionLevels;
+               global $wgRestrictionLevels;
                $params = $this->extractRequestParams();
 
                $titleObj = Title::newFromText( $params['title'] );
@@ -47,7 +47,7 @@ class ApiProtect extends ApiBase {
                        $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
                }
 
-               $errors = $titleObj->getUserPermissionsErrors( 'protect', $wgUser );
+               $errors = $titleObj->getUserPermissionsErrors( 'protect', $this->getUser() );
                if ( $errors ) {
                        // We don't care about multiple errors, just report one of them
                        $this->dieUsageMsg( reset( $errors ) );
index 4e89835..2d30c9f 100644 (file)
@@ -43,10 +43,10 @@ class ApiPurge extends ApiBase {
         * Purges the cache of a page
         */
        public function execute() {
-               global $wgUser;
+               $user = $this->getUser();
                $params = $this->extractRequestParams();
-               if ( !$wgUser->isAllowed( 'purge' ) && !$this->getMain()->isInternalMode() &&
-                               !$this->getMain()->getRequest()->wasPosted() ) {
+               if ( !$user->isAllowed( 'purge' ) && !$this->getMain()->isInternalMode() &&
+                               !$this->getRequest()->wasPosted() ) {
                        $this->dieUsageMsg( array( 'mustbeposted', $this->getModuleName() ) );
                }
 
@@ -75,7 +75,7 @@ class ApiPurge extends ApiBase {
                        $r['purged'] = '';
 
                        if( $forceLinkUpdate ) {
-                               if ( !$wgUser->pingLimiter() ) {
+                               if ( !$user->pingLimiter() ) {
                                        global $wgParser, $wgEnableParserCache;
                                        $popts = new ParserOptions();
                                        $p_result = $wgParser->parse( $article->getContent(), $title, $popts );
index 130d040..aa0cb36 100644 (file)
@@ -502,8 +502,7 @@ abstract class ApiQueryBase extends ApiBase {
         * @return void
         */
        public function showHiddenUsersAddBlockInfo( $showBlockInfo ) {
-               global $wgUser;
-               $userCanViewHiddenUsers = $wgUser->isAllowed( 'hideuser' );
+               $userCanViewHiddenUsers = $this->getUser()->isAllowed( 'hideuser' );
 
                if ( $showBlockInfo || !$userCanViewHiddenUsers ) {
                        $this->addTables( 'ipblocks' );
index 36f0896..9a416df 100644 (file)
@@ -46,7 +46,7 @@ class ApiQueryBlocks extends ApiQueryBase {
        }
 
        public function execute() {
-               global $wgUser, $wgContLang;
+               global $wgContLang;
 
                $params = $this->extractRequestParams();
                $this->requireMaxOneParameter( $params, 'users', 'ip' );
@@ -136,7 +136,7 @@ class ApiQueryBlocks extends ApiQueryBase {
                        $this->addWhereIf( "ipb_range_end > ipb_range_start", isset( $show['range'] ) );
                }
 
-               if ( !$wgUser->isAllowed( 'hideuser' ) ) {
+               if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
                        $this->addWhereFld( 'ipb_deleted', 0 );
                }
 
index 90a9a98..62ae9e3 100644 (file)
@@ -41,9 +41,9 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
        }
 
        public function execute() {
-               global $wgUser;
+               $user = $this->getUser();
                // Before doing anything at all, let's check permissions
-               if ( !$wgUser->isAllowed( 'deletedhistory' ) ) {
+               if ( !$user->isAllowed( 'deletedhistory' ) ) {
                        $this->dieUsage( 'You don\'t have permission to view deleted revision information', 'permissiondenied' );
                }
 
@@ -113,7 +113,7 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
                        $this->addWhere( 'ar_text_id = old_id' );
 
                        // This also means stricter restrictions
-                       if ( !$wgUser->isAllowed( 'undelete' ) ) {
+                       if ( !$user->isAllowed( 'undelete' ) ) {
                                $this->dieUsage( 'You don\'t have permission to view deleted revision content', 'permissiondenied' );
                        }
                }
@@ -132,7 +132,7 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
 
                if ( $fld_token ) {
                        // Undelete tokens are identical for all pages, so we cache one here
-                       $token = $wgUser->editToken( '', $this->getMain()->getRequest() );
+                       $token = $user->editToken( '', $this->getMain()->getRequest() );
                }
 
                $dir = $params['dir'];
index 4f8c095..166f009 100644 (file)
@@ -43,9 +43,9 @@ class ApiQueryFilearchive extends ApiQueryBase {
        }
 
        public function execute() {
-               global $wgUser;
+               $user = $this->getUser();
                // Before doing anything at all, let's check permissions
-               if ( !$wgUser->isAllowed( 'deletedhistory' ) ) {
+               if ( !$user->isAllowed( 'deletedhistory' ) ) {
                        $this->dieUsage( 'You don\'t have permission to view deleted file information', 'permissiondenied' );
                }
 
@@ -110,7 +110,7 @@ class ApiQueryFilearchive extends ApiQueryBase {
                        }
                }
 
-               if ( !$wgUser->isAllowed( 'suppressrevision' ) ) {
+               if ( !$user->isAllowed( 'suppressrevision' ) ) {
                        // Filter out revisions that the user is not allowed to see. There
                        // is no way to indicate that we have skipped stuff because the
                        // continuation parameter is fa_name
index 9170b73..b56775c 100644 (file)
@@ -619,9 +619,9 @@ class ApiQueryInfo extends ApiQueryBase {
         * Get information about watched status and put it in $this->watched
         */
        private function getWatchedInfo() {
-               global $wgUser;
+               $user = $this->getUser();
 
-               if ( $wgUser->isAnon() || count( $this->everything ) == 0 ) {
+               if ( $user->isAnon() || count( $this->everything ) == 0 ) {
                        return;
                }
 
@@ -635,7 +635,7 @@ class ApiQueryInfo extends ApiQueryBase {
                $this->addFields( array( 'wl_title', 'wl_namespace' ) );
                $this->addWhere( array(
                        $lb->constructSet( 'wl', $db ),
-                       'wl_user' => $wgUser->getID()
+                       'wl_user' => $user->getID()
                ) );
 
                $res = $this->select( __METHOD__ );
index 41c9c48..1414ad5 100644 (file)
@@ -75,12 +75,11 @@ class ApiQueryQueryPage extends ApiQueryGeneratorBase {
         * @param $resultPageSet ApiPageSet
         */
        public function run( $resultPageSet = null ) {
-               global $wgUser;
                $params = $this->extractRequestParams();
                $result = $this->getResult();
 
                $qp = new $this->qpMap[$params['page']]();
-               if ( !$qp->userCanExecute( $wgUser ) ) {
+               if ( !$qp->userCanExecute( $this->getUser() ) ) {
                        $this->dieUsageMsg( 'specialpage-cantexecute' );
                }
 
index d955aaa..9bcbefb 100644 (file)
@@ -129,7 +129,7 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase {
         * @param $resultPageSet ApiPageSet
         */
        public function run( $resultPageSet = null ) {
-               global $wgUser;
+               $user = $this->getUser();
                /* Get the parameters of the request. */
                $params = $this->extractRequestParams();
 
@@ -163,7 +163,7 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase {
 
                        // Check permissions
                        if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
-                               if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
+                               if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
                                        $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
                                }
                        }
@@ -219,7 +219,7 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase {
                        /* Set up internal members based upon params. */
                        $this->initProperties( $prop );
 
-                       if ( $this->fld_patrolled && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
+                       if ( $this->fld_patrolled && !$user->useRCPatrol() && !$user->useNPPatrol() ) {
                                $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
                        }
 
index 62194a4..24b3277 100644 (file)
@@ -146,7 +146,7 @@ class ApiQueryContributions extends ApiQueryBase {
                // We're after the revision table, and the corresponding page
                // row for anything we retrieve. We may also need the
                // recentchanges row and/or tag summary row.
-               global $wgUser;
+               $user = $this->getUser();
                $tables = array( 'page', 'revision' ); // Order may change
                $this->addWhere( 'page_id=rev_page' );
 
@@ -167,7 +167,7 @@ class ApiQueryContributions extends ApiQueryBase {
                        );
                }
 
-               if ( !$wgUser->isAllowed( 'hideuser' ) ) {
+               if ( !$user->isAllowed( 'hideuser' ) ) {
                        $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' );
                }
                // We only want pages by the specified users.
@@ -216,7 +216,7 @@ class ApiQueryContributions extends ApiQueryBase {
 
                if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
                                 $this->fld_patrolled ) {
-                       if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
+                       if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
                                $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
                        }
 
index eaa7cff..5fd9bb1 100644 (file)
@@ -55,47 +55,48 @@ class ApiQueryUserInfo extends ApiQueryBase {
        }
 
        protected function getCurrentUserInfo() {
-               global $wgUser, $wgRequest, $wgHiddenPrefs;
+               global $wgRequest, $wgHiddenPrefs;
+               $user = $this->getUser();
                $result = $this->getResult();
                $vals = array();
-               $vals['id'] = intval( $wgUser->getId() );
-               $vals['name'] = $wgUser->getName();
+               $vals['id'] = intval( $user->getId() );
+               $vals['name'] = $user->getName();
 
-               if ( $wgUser->isAnon() ) {
+               if ( $user->isAnon() ) {
                        $vals['anon'] = '';
                }
 
                if ( isset( $this->prop['blockinfo'] ) ) {
-                       if ( $wgUser->isBlocked() ) {
-                               $vals['blockedby'] = User::whoIs( $wgUser->blockedBy() );
-                               $vals['blockreason'] = $wgUser->blockedFor();
+                       if ( $user->isBlocked() ) {
+                               $vals['blockedby'] = User::whoIs( $user->blockedBy() );
+                               $vals['blockreason'] = $user->blockedFor();
                        }
                }
 
-               if ( isset( $this->prop['hasmsg'] ) && $wgUser->getNewtalk() ) {
+               if ( isset( $this->prop['hasmsg'] ) && $user->getNewtalk() ) {
                        $vals['messages'] = '';
                }
 
                if ( isset( $this->prop['groups'] ) ) {
-                       $autolist = ApiQueryUsers::getAutoGroups( $wgUser );
+                       $autolist = ApiQueryUsers::getAutoGroups( $user );
 
-                       $vals['groups'] = array_merge( $autolist, $wgUser->getGroups() );
+                       $vals['groups'] = array_merge( $autolist, $user->getGroups() );
                        $result->setIndexedTagName( $vals['groups'], 'g' );     // even if empty
                }
 
                if ( isset( $this->prop['implicitgroups'] ) ) {
-                       $vals['implicitgroups'] = ApiQueryUsers::getAutoGroups( $wgUser );
+                       $vals['implicitgroups'] = ApiQueryUsers::getAutoGroups( $user );
                        $result->setIndexedTagName( $vals['implicitgroups'], 'g' );     // even if empty
                }
 
                if ( isset( $this->prop['rights'] ) ) {
                        // User::getRights() may return duplicate values, strip them
-                       $vals['rights'] = array_values( array_unique( $wgUser->getRights() ) );
+                       $vals['rights'] = array_values( array_unique( $user->getRights() ) );
                        $result->setIndexedTagName( $vals['rights'], 'r' );     // even if empty
                }
 
                if ( isset( $this->prop['changeablegroups'] ) ) {
-                       $vals['changeablegroups'] = $wgUser->changeableGroups();
+                       $vals['changeablegroups'] = $user->changeableGroups();
                        $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
                        $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
                        $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
@@ -103,17 +104,17 @@ class ApiQueryUserInfo extends ApiQueryBase {
                }
 
                if ( isset( $this->prop['options'] ) ) {
-                       $vals['options'] = $wgUser->getOptions();
+                       $vals['options'] = $user->getOptions();
                }
 
                if ( isset( $this->prop['preferencestoken'] ) &&
                        is_null( $this->getMain()->getRequest()->getVal( 'callback' ) )
                ) {
-                       $vals['preferencestoken'] = $wgUser->editToken( '', $this->getMain()->getRequest() );
+                       $vals['preferencestoken'] = $user->editToken( '', $this->getMain()->getRequest() );
                }
 
                if ( isset( $this->prop['editcount'] ) ) {
-                       $vals['editcount'] = intval( $wgUser->getEditCount() );
+                       $vals['editcount'] = intval( $user->getEditCount() );
                }
 
                if ( isset( $this->prop['ratelimits'] ) ) {
@@ -121,19 +122,19 @@ class ApiQueryUserInfo extends ApiQueryBase {
                }
 
                if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
-                       $vals['realname'] = $wgUser->getRealName();
+                       $vals['realname'] = $user->getRealName();
                }
 
                if ( isset( $this->prop['email'] ) ) {
-                       $vals['email'] = $wgUser->getEmail();
-                       $auth = $wgUser->getEmailAuthenticationTimestamp();
+                       $vals['email'] = $user->getEmail();
+                       $auth = $user->getEmailAuthenticationTimestamp();
                        if ( !is_null( $auth ) ) {
                                $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
                        }
                }
 
                if ( isset( $this->prop['registrationdate'] ) ) {
-                       $regDate = $wgUser->getRegistration();
+                       $regDate = $user->getRegistration();
                        if ( $regDate !== false ) {
                                $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
                        }
@@ -154,25 +155,26 @@ class ApiQueryUserInfo extends ApiQueryBase {
        }
 
        protected function getRateLimits() {
-               global $wgUser, $wgRateLimits;
-               if ( !$wgUser->isPingLimitable() ) {
+               global $wgRateLimits;
+               $user = $this->getUser();
+               if ( !$user->isPingLimitable() ) {
                        return array(); // No limits
                }
 
                // Find out which categories we belong to
                $categories = array();
-               if ( $wgUser->isAnon() ) {
+               if ( $user->isAnon() ) {
                        $categories[] = 'anon';
                } else {
                        $categories[] = 'user';
                }
-               if ( $wgUser->isNewbie() ) {
+               if ( $user->isNewbie() ) {
                        $categories[] = 'ip';
                        $categories[] = 'subnet';
-                       if ( !$wgUser->isAnon() )
+                       if ( !$user->isAnon() )
                                $categories[] = 'newbie';
                }
-               $categories = array_merge( $categories, $wgUser->getGroups() );
+               $categories = array_merge( $categories, $user->getGroups() );
 
                // Now get the actual limits
                $retval = array();
index 76965ce..c95373f 100644 (file)
@@ -159,8 +159,8 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
 
                        // Check permissions.
                        if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
-                               global $wgUser;
-                               if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
+                               $user = $this->getUser();
+                               if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
                                        $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
                                }
                        }
index 9189692..5c84a14 100644 (file)
@@ -52,11 +52,11 @@ class ApiRollback extends ApiBase {
                $params = $this->extractRequestParams();
 
                // User and title already validated in call to getTokenSalt from Main
-               $titleObj = $this->getTitle();
+               $titleObj = $this->getRbTitle();
                $articleObj = new Article( $titleObj );
                $summary = ( isset( $params['summary'] ) ? $params['summary'] : '' );
                $details = array();
-               $retval = $articleObj->doRollback( $this->getUser(), $summary, $params['token'], $params['markbot'], $details );
+               $retval = $articleObj->doRollback( $this->getRbUser(), $summary, $params['token'], $params['markbot'], $details );
 
                if ( $retval ) {
                        // We don't care about multiple errors, just report one of them
@@ -141,10 +141,10 @@ class ApiRollback extends ApiBase {
        }
 
        public function getTokenSalt() {
-               return array( $this->getTitle()->getPrefixedText(), $this->getUser() );
+               return array( $this->getRbTitle()->getPrefixedText(), $this->getRbUser() );
        }
 
-       private function getUser() {
+       private function getRbUser() {
                if ( $this->mUser !== null ) {
                        return $this->mUser;
                }
@@ -165,7 +165,7 @@ class ApiRollback extends ApiBase {
        /**
         * @return Title
         */
-       private function getTitle() {
+       private function getRbTitle() {
                if ( $this->mTitleObj !== null ) {
                        return $this->mTitleObj;
                }
index 6fd9816..a2921bd 100644 (file)
@@ -45,11 +45,11 @@ class ApiUnblock extends ApiBase {
         * Unblocks the specified user or provides the reason the unblock failed.
         */
        public function execute() {
-               global $wgUser;
+               $user = $this->getUser();
                $params = $this->extractRequestParams();
 
                if ( $params['gettoken'] ) {
-                       $res['unblocktoken'] = $wgUser->editToken( '', $this->getMain()->getRequest() );
+                       $res['unblocktoken'] = $user->editToken( '', $this->getMain()->getRequest() );
                        $this->getResult()->addValue( null, $this->getModuleName(), $res );
                        return;
                }
@@ -61,11 +61,11 @@ class ApiUnblock extends ApiBase {
                        $this->dieUsageMsg( 'unblock-idanduser' );
                }
 
-               if ( !$wgUser->isAllowed( 'block' ) ) {
+               if ( !$user->isAllowed( 'block' ) ) {
                        $this->dieUsageMsg( 'cantunblock' );
                }
                # bug 15810: blocked admins should have limited access here
-               if ( $wgUser->isBlocked() ) {
+               if ( $user->isBlocked() ) {
                        $status = SpecialBlock::checkUnblockSelf( $params['user'] );
                        if ( $status !== true ) {
                                $this->dieUsageMsg( $status );
index c3f7433..9315ac3 100644 (file)
@@ -39,14 +39,13 @@ class ApiUndelete extends ApiBase {
        }
 
        public function execute() {
-               global $wgUser;
                $params = $this->extractRequestParams();
 
-               if ( !$wgUser->isAllowed( 'undelete' ) ) {
+               if ( !$this->getUser()->isAllowed( 'undelete' ) ) {
                        $this->dieUsageMsg( 'permdenied-undelete' );
                }
 
-               if ( $wgUser->isBlocked() ) {
+               if ( $this->getUser()->isBlocked() ) {
                        $this->dieUsageMsg( 'blockedtext' );
                }
 
@@ -74,7 +73,7 @@ class ApiUndelete extends ApiBase {
 
                if ( $retval[1] ) {
                        wfRunHooks( 'FileUndeleteComplete',
-                               array( $titleObj, array(), $wgUser, $params['reason'] ) );
+                               array( $titleObj, array(), $this->getUser(), $params['reason'] ) );
                }
 
                $this->setWatch( $params['watchlist'], $titleObj );
index 947e015..17262ec 100644 (file)
@@ -46,13 +46,13 @@ class ApiUpload extends ApiBase {
        }
 
        public function execute() {
-               global $wgUser;
-
                // Check whether upload is enabled
                if ( !UploadBase::isEnabled() ) {
                        $this->dieUsageMsg( 'uploaddisabled' );
                }
 
+               $user = $this->getUser();
+
                // Parameter handling
                $this->mParams = $this->extractRequestParams();
                $request = $this->getMain()->getRequest();
@@ -75,7 +75,7 @@ class ApiUpload extends ApiBase {
                }
 
                // First check permission to upload
-               $this->checkPermissions( $wgUser );
+               $this->checkPermissions( $user );
 
                // Fetch the file
                $status = $this->mUpload->fetchFile();
@@ -100,7 +100,7 @@ class ApiUpload extends ApiBase {
                // (This check is irrelevant if stashing is already requested, since the errors
                //  can always be fixed by changing the title)
                if ( ! $this->mParams['stash'] ) {
-                       $permErrors = $this->mUpload->verifyTitlePermissions( $wgUser );
+                       $permErrors = $this->mUpload->verifyTitlePermissions( $user );
                        if ( $permErrors !== true ) {
                                $this->dieRecoverableError( $permErrors[0], 'filename' );
                        }
@@ -254,17 +254,7 @@ class ApiUpload extends ApiBase {
                                $this->dieUsageMsg( 'invalid-file-key' );
                        }
 
-                       if( class_exists( 'RequestContext' ) ) {
-                               // context allows access to the current user without creating new $wgUser references
-                               $context = $this->createContext();
-                               $this->mUpload = new UploadFromStash( $context->getUser() );
-                       } else {
-                               // this is here to maintain 1.17 compatibility, so these changes can
-                               // be merged into production
-                               // remove this after we've moved to 1.18
-                               global $wgUser;
-                               $this->mUpload = new UploadFromStash( $wgUser );
-                       }
+                       $this->mUpload = new UploadFromStash( $this->getUser() );
 
                        $this->mUpload->initialize( $this->mParams['filekey'], $this->mParams['filename'] );
 
@@ -440,8 +430,6 @@ class ApiUpload extends ApiBase {
         * @return array
         */
        protected function performUpload() {
-               global $wgUser;
-
                // Use comment as initial page text by default
                if ( is_null( $this->mParams['text'] ) ) {
                        $this->mParams['text'] = $this->mParams['comment'];
@@ -457,7 +445,7 @@ class ApiUpload extends ApiBase {
 
                // No errors, no warnings: do the upload
                $status = $this->mUpload->performUpload( $this->mParams['comment'],
-                       $this->mParams['text'], $watch, $wgUser );
+                       $this->mParams['text'], $watch, $this->getUser() );
 
                if ( !$status->isGood() ) {
                        $error = $status->getErrorsArray();
index 97e3ca5..796f306 100644 (file)
@@ -44,7 +44,7 @@ class ApiUserrights extends ApiBase {
        public function execute() {
                $params = $this->extractRequestParams();
 
-               $user = $this->getUser();
+               $user = $this->getUrUser();
 
                $form = new UserrightsPage;
                $r['user'] = $user->getName();
@@ -62,7 +62,7 @@ class ApiUserrights extends ApiBase {
        /**
         * @return User
         */
-       private function getUser() {
+       private function getUrUser() {
                if ( $this->mUser !== null ) {
                        return $this->mUser;
                }
@@ -130,7 +130,7 @@ class ApiUserrights extends ApiBase {
        }
 
        public function getTokenSalt() {
-               return $this->getUser()->getName();
+               return $this->getUrUser()->getName();
        }
 
        public function getExamples() {
index d9b26b5..3518ea8 100644 (file)
@@ -41,8 +41,8 @@ class ApiWatch extends ApiBase {
        }
 
        public function execute() {
-               global $wgUser;
-               if ( !$wgUser->isLoggedIn() ) {
+               $user = $this->getUser();
+               if ( !$user->isLoggedIn() ) {
                        $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
                }
 
@@ -59,11 +59,11 @@ class ApiWatch extends ApiBase {
                if ( $params['unwatch'] ) {
                        $res['unwatched'] = '';
                        $res['message'] = wfMsgExt( 'removedwatchtext', array( 'parse' ), $title->getPrefixedText() );
-                       $success = UnwatchAction::doUnwatch( $title, $wgUser );
+                       $success = UnwatchAction::doUnwatch( $title, $user );
                } else {
                        $res['watched'] = '';
                        $res['message'] = wfMsgExt( 'addedwatchtext', array( 'parse' ), $title->getPrefixedText() );
-                       $success = WatchAction::doWatch( $title, $wgUser );
+                       $success = WatchAction::doWatch( $title, $user );
                }
                if ( !$success ) {
                        $this->dieUsageMsg( 'hookaborted' );