Added support for stored procedures/functions to MySQL:
[lhc/web/wiklou.git] / includes / SpecialPageFactory.php
index 4465e85..a307575 100644 (file)
@@ -25,6 +25,7 @@
 /**
  * Factory for handling the special page list and generating SpecialPage objects
  * @ingroup SpecialPage
+ * @since 1.17
  */
 class SpecialPageFactory {
 
@@ -73,6 +74,7 @@ class SpecialPageFactory {
                'Unblock'                   => 'SpecialUnblock',
                'BlockList'                 => 'SpecialBlockList',
                'ChangePassword'            => 'SpecialChangePassword',
+               'PasswordReset'             => 'SpecialPasswordReset',
                'DeletedContributions'      => 'DeletedContributionsPage',
                'Preferences'               => 'SpecialPreferences',
                'Contributions'             => 'SpecialContributions',
@@ -152,10 +154,13 @@ class SpecialPageFactory {
        /**
         * Initialise the special page list
         * This must be called before accessing SpecialPage::$mList
+        *
+        * @return array
         */
        static function getList() {
                global $wgSpecialPages;
                global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
+               global $wgEnableEmail;
 
                if ( !is_object( self::$mList ) ) {
                        wfProfileIn( __METHOD__ );
@@ -173,6 +178,10 @@ class SpecialPageFactory {
                                self::$mList['Invalidateemail'] = 'EmailInvalidation';
                        }
 
+                       if ( $wgEnableEmail ) {
+                               self::$mList['ChangeEmail'] = 'SpecialChangeEmail';
+                       }
+
                        // Add extension special pages
                        self::$mList = array_merge( self::$mList, $wgSpecialPages );
 
@@ -188,6 +197,14 @@ class SpecialPageFactory {
                return self::$mList;
        }
 
+       /**
+        * Initialise and return the list of special page aliases.  Returns an object with
+        * properties which can be accessed $obj->pagename - each property is an array of
+        * aliases; the first in the array is the cannonical alias.  All registered special
+        * pages are guaranteed to have a property entry, and for that property array to
+        * contain at least one entry (English fallbacks will be added if necessary).
+        * @return Object
+        */
        static function getAliasList() {
                if ( !is_object( self::$mAliases ) ) {
                        global $wgContLang;
@@ -250,33 +267,35 @@ class SpecialPageFactory {
         */
        public static function setGroup( $page, $group ) {
                global $wgSpecialPageGroups;
-               $name = is_object( $page ) ? $page->mName : $page;
+               $name = is_object( $page ) ? $page->getName() : $page;
                $wgSpecialPageGroups[$name] = $group;
        }
 
        /**
-        * Add a page to a certain display group for Special:SpecialPages
+        * Get the group that the special page belongs in on Special:SpecialPage
         *
         * @param $page SpecialPage
         */
        public static function getGroup( &$page ) {
+               $name = $page->getName();
+
                global $wgSpecialPageGroups;
                static $specialPageGroupsCache = array();
-               if ( isset( $specialPageGroupsCache[$page->mName] ) ) {
-                       return $specialPageGroupsCache[$page->mName];
+               if ( isset( $specialPageGroupsCache[$name] ) ) {
+                       return $specialPageGroupsCache[$name];
                }
-               $msg = wfMessage( 'specialpages-specialpagegroup-' . strtolower( $page->mName ) );
+               $msg = wfMessage( 'specialpages-specialpagegroup-' . strtolower( $name ) );
                if ( !$msg->isBlank() ) {
                        $group = $msg->text();
                } else {
-                       $group = isset( $wgSpecialPageGroups[$page->mName] )
-                               ? $wgSpecialPageGroups[$page->mName]
+                       $group = isset( $wgSpecialPageGroups[$name] )
+                               ? $wgSpecialPageGroups[$name]
                                : '-';
                }
                if ( $group == '-' ) {
                        $group = 'other';
                }
-               $specialPageGroupsCache[$page->mName] = $group;
+               $specialPageGroupsCache[$name] = $group;
                return $group;
        }
 
@@ -303,7 +322,7 @@ class SpecialPageFactory {
                        $rec = self::getList()->$realName;
                        if ( is_string( $rec ) ) {
                                $className = $rec;
-                               self::getList()->$realName = new $className;
+                               return 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." );
@@ -320,18 +339,21 @@ class SpecialPageFactory {
         * Return categorised listable special pages which are available
         * for the current user, and everyone.
         *
+        * @param $user User object to check permissions, $wgUser will be used
+        *              if not provided
         * @return Array( String => Specialpage )
         */
-       public static function getUsablePages() {
-               global $wgUser;
+       public static function getUsablePages( User $user = null ) {
                $pages = array();
+               if ( $user === null ) {
+                       global $wgUser;
+                       $user = $wgUser;
+               }
                foreach ( self::getList() as $name => $rec ) {
                        $page = self::getPage( $name );
-                       if ( $page->isListed()
-                               && (
-                                       !$page->isRestricted()
-                                       || $page->userCanExecute( $wgUser )
-                               )
+                       if ( $page // not null
+                               && $page->isListed()
+                               && ( !$page->isRestricted() || $page->userCanExecute( $user ) )
                        ) {
                                $pages[$name] = $page;
                        }
@@ -386,13 +408,15 @@ class SpecialPageFactory {
         * page, and true if it was successful.
         *
         * @param $title          Title object
-        * @param $context        RequestContext
+        * @param $context        IContextSource
         * @param $including      Bool output is being captured for use in {{special:whatever}}
+        *
+        * @return bool
         */
-       public static function executePath( Title &$title, RequestContext &$context, $including = false ) {
+       public static function executePath( Title &$title, IContextSource &$context, $including = false ) {
                wfProfileIn( __METHOD__ );
 
-               // FIXME: redirects broken due to this call
+               // @todo FIXME: Redirects broken due to this call
                $bits = explode( '/', $title->getDBkey(), 2 );
                $name = $bits[0];
                if ( !isset( $bits[1] ) ) { // bug 2087
@@ -403,10 +427,15 @@ class SpecialPageFactory {
                $page = self::getPage( $name );
                // Nonexistent?
                if ( !$page ) {
-                       $context->output->setArticleRelated( false );
-                       $context->output->setRobotPolicy( 'noindex,nofollow' );
-                       $context->output->setStatusCode( 404 );
-                       $context->output->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
+                       $context->getOutput()->setArticleRelated( false );
+                       $context->getOutput()->setRobotPolicy( 'noindex,nofollow' );
+
+                       global $wgSend404Code;
+                       if ( $wgSend404Code ) {
+                               $context->getOutput()->setStatusCode( 404 );
+                       }
+
+                       $context->getOutput()->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
                        wfProfileOut( __METHOD__ );
                        return false;
                }
@@ -414,42 +443,26 @@ class SpecialPageFactory {
                // Page exists, set the context
                $page->setContext( $context );
 
-               // Check for redirect
                if ( !$including ) {
-                       $redirect = $page->getRedirect( $par );
-                       $query = $page->getRedirectQuery();
-                       if ( $redirect instanceof Title ) {
-                               $url = $redirect->getFullUrl( $query );
-                               $context->output->redirect( $url );
-                               wfProfileOut( __METHOD__ );
-                               return $redirect;
-                       } elseif ( $redirect === true ) {
-                               global $wgScript;
-                               $url = $wgScript . '?' . wfArrayToCGI( $query );
-                               $context->output->redirect( $url );
-                               wfProfileOut( __METHOD__ );
-                               return $redirect;
-                       }
-
                        // Redirect to canonical alias for GET commands
                        // Not for POST, we'd lose the post data, so it's best to just distribute
                        // the request. Such POST requests are possible for old extensions that
                        // generate self-links without being aware that their default name has
                        // changed.
-                       if ( $name != $page->getLocalName() && !$context->request->wasPosted() ) {
-                               $query = $_GET;
+                       if ( $name != $page->getLocalName() && !$context->getRequest()->wasPosted() ) {
+                               $query = $context->getRequest()->getQueryValues();
                                unset( $query['title'] );
                                $query = wfArrayToCGI( $query );
                                $title = $page->getTitle( $par );
                                $url = $title->getFullUrl( $query );
-                               $context->output->redirect( $url );
+                               $context->getOutput()->redirect( $url );
                                wfProfileOut( __METHOD__ );
-                               return $redirect;
+                               return $title;
                        } else {
-                               $context->title = $page->getTitle();
+                               $context->setTitle( $page->getTitle( $par ) );
                        }
 
-               } elseif ( !$page->includable() ) {
+               } elseif ( !$page->isIncludable() ) {
                        wfProfileOut( __METHOD__ );
                        return false;
                }
@@ -466,28 +479,47 @@ class SpecialPageFactory {
        }
 
        /**
-        * Just like executePath() except it returns the HTML instead of outputting it
-        * Returns false if there was no such special page, or a title object if it was
-        * a redirect.
+        * Just like executePath() but will override global variables and execute
+        * the page in "inclusion" mode. Returns true if the execution was
+        * successful or false if there was no such special page, or a title object
+        * if it was a redirect.
+        *
+        * Also saves the current $wgTitle, $wgOut, $wgRequest, $wgUser and $wgLang
+        * variables so that the special page will get the context it'd expect on a
+        * normal request, and then restores them to their previous values after.
+        *
+        * @param $title Title
+        * @param $context IContextSource
         *
         * @return String: HTML fragment
         */
-       static function capturePath( &$title ) {
-               global $wgOut, $wgTitle;
+       static function capturePath( Title $title, IContextSource $context ) {
+               global $wgOut, $wgTitle, $wgRequest, $wgUser, $wgLang;
 
+               // Save current globals
                $oldTitle = $wgTitle;
                $oldOut = $wgOut;
+               $oldRequest = $wgRequest;
+               $oldUser = $wgUser;
+               $oldLang = $wgLang;
 
-               $context = new RequestContext;
-               $context->setTitle( $title );
+               // Set the globals to the current context
+               $wgTitle = $title;
                $wgOut = $context->getOutput();
+               $wgRequest = $context->getRequest();
+               $wgUser = $context->getUser();
+               $wgLang = $context->getLanguage();
 
+               // The useful part
                $ret = self::executePath( $title, $context, true );
-               if ( $ret === true ) {
-                       $ret = $wgOut->getHTML();
-               }
+
+               // And restore the old globals
                $wgTitle = $oldTitle;
                $wgOut = $oldOut;
+               $wgRequest = $oldRequest;
+               $wgUser = $oldUser;
+               $wgLang = $oldLang;
+
                return $ret;
        }
 
@@ -502,6 +534,7 @@ class SpecialPageFactory {
        static function getLocalNameFor( $name, $subpage = false ) {
                global $wgContLang;
                $aliases = $wgContLang->getSpecialPageAliases();
+
                if ( isset( $aliases[$name][0] ) ) {
                        $name = $aliases[$name][0];
                } else {
@@ -530,14 +563,16 @@ class SpecialPageFactory {
        /**
         * Get a title for a given alias
         *
+        * @param $alias String
+        *
         * @return Title or null if there is no such alias
         */
        static function getTitleForAlias( $alias ) {
                $name = self::resolveAlias( $alias );
                if ( $name ) {
-                       return self::getTitleFor( $name );
+                       return SpecialPage::getTitleFor( $name );
                } else {
                        return null;
                }
        }
-}
\ No newline at end of file
+}