Merge "Improve type hints in parser related classes"
[lhc/web/wiklou.git] / includes / specialpage / SpecialPageFactory.php
index c6ffbe4..40172ab 100644 (file)
 
 namespace MediaWiki\Special;
 
-use Config;
 use Hooks;
 use IContextSource;
 use Language;
+use MediaWiki\Config\ServiceOptions;
 use MediaWiki\Linker\LinkRenderer;
 use Profiler;
 use RequestContext;
 use SpecialPage;
 use Title;
 use User;
-use Wikimedia\ObjectFactory;
 
 /**
  * Factory for handling the special page list and generating SpecialPage objects.
@@ -215,18 +214,36 @@ class SpecialPageFactory {
        /** @var array */
        private $aliases;
 
-       /** @var Config */
-       private $config;
+       /** @var ServiceOptions */
+       private $options;
 
        /** @var Language */
        private $contLang;
 
        /**
-        * @param Config $config
+        * TODO Make this a const when HHVM support is dropped (T192166)
+        *
+        * @var array
+        * @since 1.33
+        * */
+       public static $constructorOptions = [
+               'ContentHandlerUseDB',
+               'DisableInternalSearch',
+               'EmailAuthentication',
+               'EnableEmail',
+               'EnableJavaScriptTest',
+               'EnableSpecialMute',
+               'PageLanguageUseDB',
+               'SpecialPages',
+       ];
+
+       /**
+        * @param ServiceOptions $options
         * @param Language $contLang
         */
-       public function __construct( Config $config, Language $contLang ) {
-               $this->config = $config;
+       public function __construct( ServiceOptions $options, Language $contLang ) {
+               $options->assertRequiredOptions( self::$constructorOptions );
+               $this->options = $options;
                $this->contLang = $contLang;
        }
 
@@ -249,32 +266,37 @@ class SpecialPageFactory {
                if ( !is_array( $this->list ) ) {
                        $this->list = self::$coreList;
 
-                       if ( !$this->config->get( 'DisableInternalSearch' ) ) {
+                       if ( !$this->options->get( 'DisableInternalSearch' ) ) {
                                $this->list['Search'] = \SpecialSearch::class;
                        }
 
-                       if ( $this->config->get( 'EmailAuthentication' ) ) {
+                       if ( $this->options->get( 'EmailAuthentication' ) ) {
                                $this->list['Confirmemail'] = \EmailConfirmation::class;
                                $this->list['Invalidateemail'] = \EmailInvalidation::class;
                        }
 
-                       if ( $this->config->get( 'EnableEmail' ) ) {
+                       if ( $this->options->get( 'EnableEmail' ) ) {
                                $this->list['ChangeEmail'] = \SpecialChangeEmail::class;
                        }
 
-                       if ( $this->config->get( 'EnableJavaScriptTest' ) ) {
+                       if ( $this->options->get( 'EnableJavaScriptTest' ) ) {
                                $this->list['JavaScriptTest'] = \SpecialJavaScriptTest::class;
                        }
 
-                       if ( $this->config->get( 'PageLanguageUseDB' ) ) {
+                       if ( $this->options->get( 'EnableSpecialMute' ) ) {
+                               $this->list['Mute'] = \SpecialMute::class;
+                       }
+
+                       if ( $this->options->get( 'PageLanguageUseDB' ) ) {
                                $this->list['PageLanguage'] = \SpecialPageLanguage::class;
                        }
-                       if ( $this->config->get( 'ContentHandlerUseDB' ) ) {
+
+                       if ( $this->options->get( 'ContentHandlerUseDB' ) ) {
                                $this->list['ChangeContentModel'] = \SpecialChangeContentModel::class;
                        }
 
                        // Add extension special pages
-                       $this->list = array_merge( $this->list, $this->config->get( 'SpecialPages' ) );
+                       $this->list = array_merge( $this->list, $this->options->get( 'SpecialPages' ) );
 
                        // This hook can be used to disable unwanted core special pages
                        // or conditionally register special pages.
@@ -345,7 +367,7 @@ class SpecialPageFactory {
         * subpage.
         *
         * @param string $alias
-        * @return array Array( String, String|null ), or array( null, null ) if the page is invalid
+        * @return array [ String, String|null ], or [ null, null ] if the page is invalid
         */
        public function resolveAlias( $alias ) {
                $bits = explode( '/', $alias, 2 );
@@ -353,17 +375,11 @@ class SpecialPageFactory {
                $caseFoldedAlias = $this->contLang->caseFold( $bits[0] );
                $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
                $aliases = $this->getAliasList();
-               if ( isset( $aliases[$caseFoldedAlias] ) ) {
-                       $name = $aliases[$caseFoldedAlias];
-               } else {
+               if ( !isset( $aliases[$caseFoldedAlias] ) ) {
                        return [ null, null ];
                }
-
-               if ( !isset( $bits[1] ) ) { // T4087
-                       $par = null;
-               } else {
-                       $par = $bits[1];
-               }
+               $name = $aliases[$caseFoldedAlias];
+               $par = $bits[1] ?? null; // T4087
 
                return [ $name, $par ];
        }
@@ -401,16 +417,6 @@ class SpecialPageFactory {
                        } elseif ( is_string( $rec ) ) {
                                $className = $rec;
                                $page = new $className;
-                       } elseif ( is_array( $rec ) ) {
-                               $className = array_shift( $rec );
-                               // @deprecated, officially since 1.18, unofficially since forever
-                               wfDeprecated( "Array syntax for \$wgSpecialPages is deprecated ($className), " .
-                                       "define a subclass of SpecialPage instead.", '1.18' );
-                               $page = ObjectFactory::getObjectFromSpec( [
-                                       'class' => $className,
-                                       'args' => $rec,
-                                       'closure_expansion' => false,
-                               ] );
                        } elseif ( $rec instanceof SpecialPage ) {
                                $page = $rec; // XXX: we should deep clone here
                        } else {
@@ -419,16 +425,14 @@ class SpecialPageFactory {
 
                        if ( $page instanceof SpecialPage ) {
                                return $page;
-                       } else {
-                               // It's not a classname, nor a callback, nor a legacy constructor array,
-                               // nor a special page object. Give up.
-                               wfLogWarning( "Cannot instantiate special page $realName: bad spec!" );
-                               return null;
                        }
 
-               } else {
-                       return null;
+                       // It's not a classname, nor a callback, nor a legacy constructor array,
+                       // nor a special page object. Give up.
+                       wfLogWarning( "Cannot instantiate special page $realName: bad spec!" );
                }
+
+               return null;
        }
 
        /**
@@ -517,11 +521,7 @@ class SpecialPageFactory {
                // @todo FIXME: Redirects broken due to this call
                $bits = explode( '/', $title->getDBkey(), 2 );
                $name = $bits[0];
-               if ( !isset( $bits[1] ) ) { // T4087
-                       $par = null;
-               } else {
-                       $par = $bits[1];
-               }
+               $par = $bits[1] ?? null; // T4087
 
                $page = $this->getPage( $name );
                if ( !$page ) {
@@ -565,9 +565,9 @@ class SpecialPageFactory {
                                $context->getOutput()->redirect( $url );
 
                                return $title;
-                       } else {
-                               $context->setTitle( $page->getPageTitle( $par ) );
                        }
+
+                       $context->setTitle( $page->getPageTitle( $par ) );
                } elseif ( !$page->isIncludable() ) {
                        return false;
                }
@@ -619,6 +619,9 @@ class SpecialPageFactory {
                        'user' => $main->getUser(),
                        'language' => $main->getLanguage(),
                ];
+               if ( $main->canUseWikiPage() ) {
+                       $ctx['wikipage'] = $main->getWikiPage();
+               }
 
                // Override
                $wgTitle = $title;
@@ -646,6 +649,9 @@ class SpecialPageFactory {
                $main->setRequest( $ctx['request'] );
                $main->setUser( $ctx['user'] );
                $main->setLanguage( $ctx['language'] );
+               if ( isset( $ctx['wikipage'] ) ) {
+                       $main->setWikiPage( $ctx['wikipage'] );
+               }
 
                return $ret;
        }
@@ -714,8 +720,8 @@ class SpecialPageFactory {
                list( $name, $subpage ) = $this->resolveAlias( $alias );
                if ( $name != null ) {
                        return SpecialPage::getTitleFor( $name, $subpage );
-               } else {
-                       return null;
                }
+
+               return null;
        }
 }