New infrastructure for actions, as discussed on wikitech-l. Fairly huge commit.
[lhc/web/wiklou.git] / includes / SpecialPage.php
index 4a2af8a..5eea301 100644 (file)
@@ -73,6 +73,12 @@ class SpecialPage {
         * Query parameteres added by redirects
         */
        var $mAddedRedirectParams = array();
+       /**
+        * Current request context
+        * @var RequestContext
+        */
+       protected $mContext;
+               
        /**
         * List of special pages, followed by parameters.
         * If the only parameter is a string, that is the page name.
@@ -120,9 +126,9 @@ class SpecialPage {
                'CreateAccount'             => array( 'SpecialRedirectToSpecial', 'CreateAccount', 'Userlogin', 'signup', array( 'uselang' ) ),
 
                # Users and rights
-               'Blockip'                   => 'IPBlockForm',
-               'Ipblocklist'               => 'IPUnblockForm',
-               'Unblock'                   => array( 'SpecialRedirectToSpecial', 'Unblock', 'Ipblocklist', false, array( 'uselang', 'ip', 'id' ), array( 'action' => 'unblock' ) ),
+               'Block'                     => 'SpecialBlock',
+               'Unblock'                   => 'SpecialUnblock',
+               'BlockList'                 => 'SpecialBlockList',
                'Resetpass'                 => 'SpecialResetpass',
                'DeletedContributions'      => 'DeletedContributionsPage',
                'Preferences'               => 'SpecialPreferences',
@@ -134,18 +140,19 @@ class SpecialPage {
                'Activeusers'               => 'SpecialActiveUsers',
                'Userrights'                => 'UserrightsPage',
                'DisableAccount'            => 'SpecialDisableAccount',
+               'EditWatchlist'             => 'SpecialEditWatchlist',
 
                # Recent changes and logs
-               'Newimages'                 => array( 'IncludableSpecialPage', 'Newimages' ),
+               'Newimages'                 => 'SpecialNewFiles',
                'Log'                       => 'SpecialLog',
-               'Watchlist'                 => array( 'SpecialPage', 'Watchlist' ),
+               'Watchlist'                 => 'SpecialWatchlist',
                'Newpages'                  => 'SpecialNewpages',
                'Recentchanges'             => 'SpecialRecentchanges',
                'Recentchangeslinked'       => 'SpecialRecentchangeslinked',
                'Tags'                      => 'SpecialTags',
 
                # Media reports and uploads
-               'Listfiles'                 => array( 'SpecialPage', 'Listfiles' ),
+               'Listfiles'                 => 'SpecialListFiles',
                'Filepath'                  => 'SpecialFilepath',
                'MIMEsearch'                => 'MIMEsearchPage',
                'FileDuplicateSearch'       => 'FileDuplicateSearchPage',
@@ -341,9 +348,10 @@ class SpecialPage {
                if( isset($specialPageGroupsCache[$page->mName]) ) {
                        return $specialPageGroupsCache[$page->mName];
                }
-               $group = wfMsg('specialpages-specialpagegroup-'.strtolower($page->mName));
-               if( $group == ''
-                || wfEmptyMsg('specialpages-specialpagegroup-'.strtolower($page->mName), $group ) ) {
+               $msg = wfMessage('specialpages-specialpagegroup-'.strtolower($page->mName));
+               if ( !$msg->isBlank() ) {
+                       $group = $msg->text();
+               } else {
                        $group = isset($wgSpecialPageGroups[$page->mName])
                                ? $wgSpecialPageGroups[$page->mName]
                                : '-';
@@ -505,17 +513,17 @@ class SpecialPage {
 
        /**
         * Execute a special page path.
-        * The path     may contain parameters, e.g. Special:Name/Params
+        * The path may contain parameters, e.g. Special:Name/Params
         * Extracts the special page name and call the execute method, passing the parameters
         *
         * Returns a title object if the page is redirected, false if there was no such special
         * page, and true if it was successful.
         *
-        * @param $title          a title object
-        * @param $including      output is being captured for use in {{special:whatever}}
+        * @param $title          Title object
+        * @param $context        RequestContext
+        * @param $including      Bool output is being captured for use in {{special:whatever}}
         */
-       static function executePath( &$title, $including = false ) {
-               global $wgOut, $wgTitle, $wgRequest;
+       public static function executePath( &$title, RequestContext &$context, $including = false ) {
                wfProfileIn( __METHOD__ );
 
                # FIXME: redirects broken due to this call
@@ -529,15 +537,16 @@ class SpecialPage {
                $page = SpecialPage::getPageByAlias( $name );
                # Nonexistent?
                if ( !$page ) {
-                       if ( !$including ) {
-                               $wgOut->setArticleRelated( false );
-                               $wgOut->setRobotPolicy( 'noindex,nofollow' );
-                               $wgOut->setStatusCode( 404 );
-                               $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
-                       }
+                       $context->output->setArticleRelated( false );
+                       $context->output->setRobotPolicy( 'noindex,nofollow' );
+                       $context->output->setStatusCode( 404 );
+                       $context->output->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
                        wfProfileOut( __METHOD__ );
                        return false;
                }
+               
+               # Page exists, set the context
+               $page->setContext( $context );
 
                # Check for redirect
                if ( !$including ) {
@@ -545,13 +554,13 @@ class SpecialPage {
                        $query = $page->getRedirectQuery();
                        if ( $redirect instanceof Title ) {
                                $url = $redirect->getFullUrl( $query );
-                               $wgOut->redirect( $url );
+                               $context->output->redirect( $url );
                                wfProfileOut( __METHOD__ );
                                return $redirect;
                        } elseif( $redirect === true ) {
                                global $wgScript;
                                $url = $wgScript . '?' . wfArrayToCGI( $query );
-                               $wgOut->redirect( $url );
+                               $context->output->redirect( $url );
                                wfProfileOut( __METHOD__ );
                                return $redirect;
                        }
@@ -562,13 +571,13 @@ class SpecialPage {
                # the request. Such POST requests are possible for old extensions that
                # generate self-links without being aware that their default name has
                # changed.
-               if ( !$including && $name != $page->getLocalName() && !$wgRequest->wasPosted() ) {
+               if ( !$including && $name != $page->getLocalName() && !$context->request->wasPosted() ) {
                        $query = $_GET;
                        unset( $query['title'] );
                        $query = wfArrayToCGI( $query );
                        $title = $page->getTitle( $par );
                        $url = $title->getFullUrl( $query );
-                       $wgOut->redirect( $url );
+                       $context->output->redirect( $url );
                        wfProfileOut( __METHOD__ );
                        return $redirect;
                }
@@ -577,7 +586,7 @@ class SpecialPage {
                        wfProfileOut( __METHOD__ );
                        return false;
                } elseif ( !$including ) {
-                       $wgTitle = $page->getTitle();
+                       $context->title = $page->getTitle();
                }
                $page->including( $including );
 
@@ -602,10 +611,12 @@ class SpecialPage {
 
                $oldTitle = $wgTitle;
                $oldOut = $wgOut;
-               $wgOut = new OutputPage;
-               $wgOut->setTitle( $title );
+               
+               $context = new RequestContext;
+               $context->setTitle( $title );
+               $wgOut = $context->getOutput();
 
-               $ret = SpecialPage::executePath( $title, true );
+               $ret = SpecialPage::executePath( $title, $context, true );
                if ( $ret === true ) {
                        $ret = $wgOut->getHTML();
                }
@@ -632,7 +643,7 @@ class SpecialPage {
                        $found = false;
                        foreach ( $aliases as $n => $values ) {
                                if ( strcasecmp( $name, $n ) === 0 ) {
-                                       wfWarn( "Found alias defined for $n when searching for" .
+                                       wfWarn( "Found alias defined for $n when searching for " .
                                                "special page aliases for $name. Case mismatch?" );
                                        $name = $values[0];
                                        $found = true;
@@ -640,7 +651,7 @@ class SpecialPage {
                                }
                        }
                        if ( !$found ) {
-                               wfWarn( "Did not find alias for special page '$name'. " . 
+                               wfWarn( "Did not find alias for special page '$name'. " .
                                        "Perhaps no aliases are defined for it?" );
                        }
                }
@@ -799,6 +810,18 @@ class SpecialPage {
                return $this->mLocalName;
        }
 
+       /**
+        * Is this page expensive (for some definition of expensive)?
+        * Expensive pages are disabled or cached in miser mode.  Originally used
+        * (and still overridden) by QueryPage and subclasses, moved here so that
+        * Special:SpecialPages can safely call it for all special pages.
+        *
+        * @return Boolean
+        */
+       public function isExpensive() {
+               return false;
+       }
+
        /**
         * Can be overridden by subclasses with more complicated permissions
         * schemes.
@@ -828,18 +851,17 @@ class SpecialPage {
         * Output an error message telling the user what access level they have to have
         */
        function displayRestrictionError() {
-               global $wgOut;
-               $wgOut->permissionRequired( $this->mRestriction );
+               $this->getOutput()->permissionRequired( $this->mRestriction );
        }
 
        /**
         * Sets headers - this should be called from the execute() method of all derived classes!
         */
        function setHeaders() {
-               global $wgOut;
-               $wgOut->setArticleRelated( false );
-               $wgOut->setRobotPolicy( "noindex,nofollow" );
-               $wgOut->setPageTitle( $this->getDescription() );
+               $out = $this->getOutput();
+               $out->setArticleRelated( false );
+               $out->setRobotPolicy( "noindex,nofollow" );
+               $out->setPageTitle( $this->getDescription() );
        }
 
        /**
@@ -849,11 +871,9 @@ class SpecialPage {
         * This may be overridden by subclasses.
         */
        function execute( $par ) {
-               global $wgUser;
-
                $this->setHeaders();
 
-               if ( $this->userCanExecute( $wgUser ) ) {
+               if ( $this->userCanExecute( $this->getUser() ) ) {
                        $func = $this->mFunction;
                        // only load file if the function does not exist
                        if(!is_callable($func) and $this->mFile) {
@@ -875,16 +895,15 @@ class SpecialPage {
         * @param $summaryMessageKey String: message key of the summary
         */
        function outputHeader( $summaryMessageKey = '' ) {
-               global $wgOut, $wgContLang;
+               global $wgContLang;
 
                if( $summaryMessageKey == '' ) {
                        $msg = $wgContLang->lc( $this->name() ) . '-summary';
                } else {
                        $msg = $summaryMessageKey;
                }
-               $out = wfMsgNoTrans( $msg );
-               if ( ! wfEmptyMsg( $msg, $out ) and  $out !== '' and ! $this->including() ) {
-                       $wgOut->wrapWikiMsg( "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
+               if ( !wfMessage( $msg )->isBlank() and ! $this->including() ) {
+                       $this->getOutput()->wrapWikiMsg( "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
                }
 
        }
@@ -934,12 +953,11 @@ class SpecialPage {
         * @return String
         */
        function getRedirectQuery() {
-               global $wgRequest;
                $params = array();
 
                foreach( $this->mAllowedRedirectParams as $arg ) {
-                       if( $wgRequest->getVal( $arg, null ) !== null ){
-                               $params[$arg] = $wgRequest->getVal( $arg );
+                       if( $this->getContext()->request->getVal( $arg, null ) !== null ){
+                               $params[$arg] = $this->getContext()->request->getVal( $arg );
                        }
                }
 
@@ -951,6 +969,91 @@ class SpecialPage {
                        ? $params
                        : false;
        }
+       
+       /**
+        * Sets the context this SpecialPage is executed in
+        * 
+        * @param $context RequestContext
+        * @since 1.18
+        */
+       protected function setContext( $context ) {
+               $this->mContext = $context;
+       }
+
+       /**
+        * Gets the context this SpecialPage is executed in
+        * 
+        * @return RequestContext
+        * @since 1.18
+        */
+       public function getContext() {
+               if ( $this->mContext instanceof RequestContext ) {
+                       return $this->mContext;
+               } else {
+                       wfDebug( __METHOD__ . " called and \$mContext is null. Return RequestContext::getMain(); for sanity\n" );
+                       return RequestContext::getMain();
+               }
+       }
+
+       /**
+        * Get the WebRequest being used for this instance
+        *
+        * @return WebRequest
+        * @since 1.18
+        */
+       public function getRequest() {
+               return $this->getContext()->getRequest();
+       }
+
+       /**
+        * Get the OutputPage being used for this instance
+        *
+        * @return OutputPage
+        * @since 1.18
+        */
+       public function getOutput() {
+               return $this->getContext()->getOutput();
+       }
+
+       /**
+        * Shortcut to get the skin being used for this instance
+        *
+        * @return User
+        * @since 1.18
+        */
+       public function getUser() {
+               return $this->getContext()->getUser();
+       }
+
+       /**
+        * Shortcut to get the skin being used for this instance
+        *
+        * @return Skin
+        * @since 1.18
+        */
+       public function getSkin() {
+               return $this->getContext()->getSkin();
+       }
+
+       /**
+        * Return the full title, including $par
+        *
+        * @return Title
+        * @since 1.18
+        */
+       public function getFullTitle() {
+               return $this->getContext()->getTitle();
+       }
+
+       /**
+        * Wrapper around wfMessage that sets the current context. Currently this
+        * is only the title.
+        * 
+        * @see wfMessage
+        */
+       public function msg( /* $args */ ) {
+               return call_user_func_array( 'wfMessage', func_get_args() )->title( $this->getFullTitle() );
+       }
 }
 
 /**