(follow-up r86121) Use strict comparision.
[lhc/web/wiklou.git] / includes / SpecialPage.php
index cfd8803..03671b5 100644 (file)
@@ -74,29 +74,13 @@ class SpecialPage {
         */
        var $mAddedRedirectParams = array();
        /**
-        * Current request
-        * @var WebRequest 
+        * Current request context
+        * @var RequestContext
         */
-       protected $mRequest;
-       /**
-        * Current output page
-        * @var OutputPage
-        */
-       protected $mOutput;
-       /**
-        * Full title including $par
-        * @var Title
-        */
-       protected $mFullTitle;
+       protected $mContext;
                
        /**
-        * List of special pages, followed by parameters.
-        * If the only parameter is a string, that is the page name.
-        * Otherwise, it is an array. The format is one of:
-        ** array( 'SpecialPage', name, right )
-        ** array( 'IncludableSpecialPage', name, right, listed? )
-        ** array( 'UnlistedSpecialPage', name, right )
-        ** array( 'SpecialRedirectToSpecial', name, page to redirect to, special page param, ... )
+        * List of special page names to the subclass of SpecialPage which handles them.
         */
        static public $mList = array(
                # Maintenance Reports
@@ -133,35 +117,36 @@ class SpecialPage {
 
                # Login/create account
                'Userlogin'                 => 'LoginForm',
-               'CreateAccount'             => array( 'SpecialRedirectToSpecial', 'CreateAccount', 'Userlogin', 'signup', array( 'uselang' ) ),
+               'CreateAccount'             => 'SpecialCreateAccount',
 
                # 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',
                'Contributions'             => 'SpecialContributions',
                'Listgrouprights'           => 'SpecialListGroupRights',
-               'Listusers'                 => array( 'SpecialPage', 'Listusers' ),
-               'Listadmins'                => array( 'SpecialRedirectToSpecial', 'Listadmins', 'Listusers', 'sysop' ),
-               'Listbots'                  => array( 'SpecialRedirectToSpecial', 'Listbots', 'Listusers', 'bot' ),
+               'Listusers'                 => 'SpecialListUsers' ,
+               'Listadmins'                => 'SpecialListAdmins',
+               'Listbots'                  => 'SpecialListBots',
                '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',
@@ -423,6 +408,8 @@ class SpecialPage {
                                $className = $rec;
                                self::$mList[$name] = new $className;
                        } elseif ( is_array( $rec ) ) {
+                               // @deprecated officially since 1.18, unofficially since forever
+                               wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
                                $className = array_shift( $rec );
                                self::$mList[$name] = MWFunction::newObj( $className, $rec );
                        }
@@ -528,11 +515,11 @@ class SpecialPage {
         * 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
@@ -546,18 +533,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( $wgRequest, $wgOut );
+               $page->setContext( $context );
 
                # Check for redirect
                if ( !$including ) {
@@ -565,13 +550,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;
                        }
@@ -582,13 +567,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;
                }
@@ -597,7 +582,7 @@ class SpecialPage {
                        wfProfileOut( __METHOD__ );
                        return false;
                } elseif ( !$including ) {
-                       $wgTitle = $page->getTitle();
+                       $context->title = $page->getTitle();
                }
                $page->including( $including );
 
@@ -622,10 +607,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();
                }
@@ -761,13 +748,14 @@ class SpecialPage {
         *
         * @param $fName String Name of called method
         * @param $a Array Arguments to the method
-        * @deprecated Call isn't deprecated, but SpecialPage::SpecialPage() is
+        * @deprecated since 1.17, call parent::__construct()
         */
        public function __call( $fName, $a ) {
                // Sometimes $fName is SpecialPage, sometimes it's specialpage. <3 PHP
                if( strtolower( $fName ) == 'specialpage' ) {
-                       // Debug messages now, warnings in 1.19 or 1.20?
-                       wfDebug( "Deprecated SpecialPage::SpecialPage() called, use __construct();\n" );
+                       // Deprecated messages now, remove in 1.19 or 1.20?
+                       wfDeprecated( __METHOD__ );
+
                        $name = isset( $a[0] ) ? $a[0] : '';
                        $restriction = isset( $a[1] ) ? $a[1] : '';
                        $listed = isset( $a[2] ) ? $a[2] : true;
@@ -819,6 +807,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.
@@ -848,18 +848,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() );
        }
 
        /**
@@ -869,11 +868,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) {
@@ -895,7 +892,7 @@ 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';
@@ -903,7 +900,7 @@ class SpecialPage {
                        $msg = $summaryMessageKey;
                }
                if ( !wfMessage( $msg )->isBlank() and ! $this->including() ) {
-                       $wgOut->wrapWikiMsg( "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
+                       $this->getOutput()->wrapWikiMsg( "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
                }
 
        }
@@ -953,12 +950,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 );
                        }
                }
 
@@ -974,14 +970,78 @@ class SpecialPage {
        /**
         * Sets the context this SpecialPage is executed in
         * 
-        * @param $request WebRequest
-        * @param $output OutputPage
+        * @param $context RequestContext
+        * @since 1.18
         */
-       protected function setContext( $request, $output ) {
-               $this->mRequest = $request;
-               $this->mOutput = $output;
-               $this->mFullTitle = $output->getTitle();
+       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.
@@ -989,7 +1049,7 @@ class SpecialPage {
         * @see wfMessage
         */
        public function msg( /* $args */ ) {
-               return call_user_func_array( 'wfMessage', func_get_args() )->title( $this->mFullTitle );
+               return call_user_func_array( 'wfMessage', func_get_args() )->title( $this->getFullTitle() );
        }
 }
 
@@ -1019,7 +1079,7 @@ class IncludableSpecialPage extends SpecialPage
  * Shortcut to construct a special page alias.
  * @ingroup SpecialPage
  */
-class SpecialRedirectToSpecial extends UnlistedSpecialPage {
+abstract class SpecialRedirectToSpecial extends UnlistedSpecialPage {
        var $redirName, $redirSubpage;
 
        function __construct( $name, $redirName, $redirSubpage = false, $allowedRedirectParams = array(), $addedRedirectParams = array() ) {
@@ -1039,6 +1099,33 @@ class SpecialRedirectToSpecial extends UnlistedSpecialPage {
        }
 }
 
+/**
+ * ListAdmins --> ListUsers/admin
+ */
+class SpecialListAdmins extends SpecialRedirectToSpecial {
+       function __construct(){
+               parent::__construct( 'ListAdmins', 'ListUsers', 'sysop' );
+       }
+}
+
+/**
+ * ListBots --> ListUsers/admin
+ */
+class SpecialListBots extends SpecialRedirectToSpecial {
+       function __construct(){
+               parent::__construct( 'ListAdmins', 'ListUsers', 'bot' );
+       }
+}
+
+/**
+ * CreateAccount --> UserLogin/signup
+ * FIXME: this (and the rest of the login frontend) needs to die a horrible painful death
+ */
+class SpecialCreateAccount extends SpecialRedirectToSpecial {
+       function __construct(){
+               parent::__construct( 'CreateAccount', 'Userlogin', 'signup', array( 'uselang' ) );
+       }
+}
 /**
  * SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
  * are used to get user independant links pointing to the user page, talk