Merge "Make UserNotLoggedIn redirect to login page"
[lhc/web/wiklou.git] / includes / specialpage / SpecialPage.php
index 82d6177..a72b297 100644 (file)
@@ -45,12 +45,6 @@ class SpecialPage {
        // Listed in Special:Specialpages?
        private $mListed;
 
-       // Function name called by the default execute()
-       private $mFunction;
-
-       // File which needs to be included before the function above can be called
-       private $mFile;
-
        // Whether or not this special page is being included from an article
        protected $mIncluding;
 
@@ -107,30 +101,18 @@ class SpecialPage {
         * @param string $name Name of the special page, as seen in links and URLs
         * @param string $restriction User right required, e.g. "block" or "delete"
         * @param bool $listed Whether the page is listed in Special:Specialpages
-        * @param callable|bool $function Function called by execute(). By default
-        *   it is constructed from $name
-        * @param string $file File which is included by execute(). It is also
-        *   constructed from $name by default
+        * @param callable|bool $function Unused
+        * @param string $file Unused
         * @param bool $includable Whether the page can be included in normal pages
         */
        public function __construct(
                $name = '', $restriction = '', $listed = true,
-               $function = false, $file = 'default', $includable = false
+               $function = false, $file = '', $includable = false
        ) {
                $this->mName = $name;
                $this->mRestriction = $restriction;
                $this->mListed = $listed;
                $this->mIncludable = $includable;
-               if ( !$function ) {
-                       $this->mFunction = 'wfSpecial' . $name;
-               } else {
-                       $this->mFunction = $function;
-               }
-               if ( $file === 'default' ) {
-                       $this->mFile = __DIR__ . "/specials/Special$name.php";
-               } else {
-                       $this->mFile = $file;
-               }
        }
 
        /**
@@ -149,23 +131,10 @@ class SpecialPage {
                return $this->mRestriction;
        }
 
-       /**
-        * Get the file which will be included by SpecialPage::execute() if your extension is
-        * still stuck in the past and hasn't overridden the execute() method.  No modern code
-        * should want or need to know this.
-        * @return string
-        * @deprecated since 1.18
-        */
-       function getFile() {
-               wfDeprecated( __METHOD__, '1.18' );
-
-               return $this->mFile;
-       }
-
        // @todo FIXME: Decide which syntax to use for this, and stick to it
        /**
         * Whether this special page is listed in Special:SpecialPages
-        * @since r3583 (v1.3)
+        * @since 1.3 (r3583)
         * @return bool
         */
        function isListed() {
@@ -305,48 +274,58 @@ class SpecialPage {
        }
 
        /**
-        * If the user is not logged in, throws UserNotLoggedIn error.
+        * If the user is not logged in, throws UserNotLoggedIn error
         *
-        * Default error message includes a link to Special:Userlogin with properly set 'returnto' query
-        * parameter.
+        * The user will be redirected to Special:Userlogin with the given message as an error on
+        * the form.
         *
         * @since 1.23
-        * @param string|Message $reasonMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
-        *     will be used as message keys. If a string is given, the message will also receive a
-        *     formatted login link (generated using the 'loginreqlink' message) as first parameter. If a
-        *     Message is given, it will be passed on verbatim.
-        * @param string|Message $titleMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
-        *     will be used as message keys.
+        * @param string $reasonMsg [optional] Message key to be displayed on login page
+        * @param string $titleMsg [optional] Passed on to UserNotLoggedIn constructor
         * @throws UserNotLoggedIn
         */
-       public function requireLogin( $reasonMsg = null, $titleMsg = null ) {
+       public function requireLogin(
+               $reasonMsg = 'exception-nologin-text', $titleMsg = 'exception-nologin'
+       ) {
                if ( $this->getUser()->isAnon() ) {
-                       // Use default messages if not given or explicit null passed
-                       if ( !$reasonMsg ) {
-                               $reasonMsg = 'exception-nologin-text-manual';
-                       }
-                       if ( !$titleMsg ) {
-                               $titleMsg = 'exception-nologin';
-                       }
-
-                       // Convert to Messages with current context
-                       if ( is_string( $reasonMsg ) ) {
-                               $loginreqlink = Linker::linkKnown(
-                                       SpecialPage::getTitleFor( 'Userlogin' ),
-                                       $this->msg( 'loginreqlink' )->escaped(),
-                                       array(),
-                                       array( 'returnto' => $this->getPageTitle()->getPrefixedText() )
-                               );
-                               $reasonMsg = $this->msg( $reasonMsg )->rawParams( $loginreqlink );
-                       }
-                       if ( is_string( $titleMsg ) ) {
-                               $titleMsg = $this->msg( $titleMsg );
-                       }
-
                        throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
                }
        }
 
+       /**
+        * Return an array of subpages beginning with $search that this special page will accept.
+        *
+        * For example, if a page supports subpages "foo", "bar" and "baz" (as in Special:PageName/foo,
+        * etc.):
+        *
+        *   - `prefixSearchSubpages( "ba" )` should return `array( "bar", "baz" )`
+        *   - `prefixSearchSubpages( "f" )` should return `array( "foo" )`
+        *   - `prefixSearchSubpages( "z" )` should return `array()`
+        *   - `prefixSearchSubpages( "" )` should return `array( foo", "bar", "baz" )`
+        *
+        * @param string $search Prefix to search for
+        * @param int $limit Maximum number of results to return
+        * @return string[] Matching subpages
+        */
+       public function prefixSearchSubpages( $search, $limit = 10 ) {
+               return array();
+       }
+
+       /**
+        * Helper function for implementations of prefixSearchSubpages() that
+        * filter the values in memory (as oppposed to making a query).
+        *
+        * @since 1.24
+        * @param string $search
+        * @param int $limit
+        * @param array $subpages
+        * @return string[]
+        */
+       protected static function prefixSearchArray( $search, $limit, array $subpages ) {
+               $escaped = preg_quote( $search, '/' );
+               return array_slice( preg_grep( "/^$escaped/i", $subpages ), 0, $limit );
+       }
+
        /**
         * Sets headers - this should be called from the execute() method of all derived classes!
         */
@@ -414,7 +393,7 @@ class SpecialPage {
 
        /**
         * Default execute method
-        * Checks user permissions, calls the function given in mFunction
+        * Checks user permissions
         *
         * This must be overridden by subclasses; it will be made abstract in a future version
         *
@@ -423,14 +402,7 @@ class SpecialPage {
        public function execute( $subPage ) {
                $this->setHeaders();
                $this->checkPermissions();
-
-               $func = $this->mFunction;
-               // only load file if the function does not exist
-               if ( !is_callable( $func ) && $this->mFile ) {
-                       require_once $this->mFile;
-               }
                $this->outputHeader();
-               call_user_func( $func, $subPage, $this );
        }
 
        /**
@@ -560,24 +532,20 @@ class SpecialPage {
        /**
         * Shortcut to get user's language
         *
-        * @deprecated since 1.19 Use getLanguage instead
         * @return Language
-        * @since 1.18
+        * @since 1.19
         */
-       public function getLang() {
-               wfDeprecated( __METHOD__, '1.19' );
-
-               return $this->getLanguage();
+       public function getLanguage() {
+               return $this->getContext()->getLanguage();
        }
 
        /**
-        * Shortcut to get user's language
-        *
-        * @return Language
-        * @since 1.19
+        * Shortcut to get main config object
+        * @return Config
+        * @since 1.24
         */
-       public function getLanguage() {
-               return $this->getContext()->getLanguage();
+       public function getConfig() {
+               return $this->getContext()->getConfig();
        }
 
        /**
@@ -629,11 +597,9 @@ class SpecialPage {
         * @param array $params
         */
        protected function addFeedLinks( $params ) {
-               global $wgFeedClasses;
-
                $feedTemplate = wfScript( 'api' );
 
-               foreach ( $wgFeedClasses as $format => $class ) {
+               foreach ( $this->getConfig()->get( 'FeedClasses' ) as $format => $class ) {
                        $theseParams = $params + array( 'feedformat' => $format );
                        $url = wfAppendQuery( $feedTemplate, $theseParams );
                        $this->getOutput()->addFeedLink( $format, $url );
@@ -649,8 +615,8 @@ class SpecialPage {
         * @since 1.21
         */
        public function getFinalGroupName() {
-               global $wgSpecialPageGroups;
                $name = $this->getName();
+               $specialPageGroups = $this->getConfig()->get( 'SpecialPageGroups' );
 
                // Allow overbidding the group from the wiki side
                $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
@@ -663,8 +629,8 @@ class SpecialPage {
                        // Group '-' is used as default to have the chance to determine,
                        // if the special pages overrides this method,
                        // if not overridden, $wgSpecialPageGroups is checked for b/c
-                       if ( $group === '-' && isset( $wgSpecialPageGroups[$name] ) ) {
-                               $group = $wgSpecialPageGroups[$name];
+                       if ( $group === '-' && isset( $specialPageGroups[$name] ) ) {
+                               $group = $specialPageGroups[$name];
                        }
                }