Merge "Remove Preferences class"
[lhc/web/wiklou.git] / includes / parser / Parser.php
index 546152f..9ff731d 100644 (file)
@@ -21,6 +21,7 @@
  * @ingroup Parser
  */
 use MediaWiki\Linker\LinkRenderer;
+use MediaWiki\Linker\LinkRendererFactory;
 use MediaWiki\MediaWikiServices;
 use MediaWiki\Special\SpecialPageFactory;
 use Wikimedia\ScopedCallback;
@@ -276,6 +277,12 @@ class Parser {
        /** @var Config */
        private $siteConfig;
 
+       /** @var LinkRendererFactory */
+       private $linkRendererFactory;
+
+       /** @var NamespaceInfo */
+       private $nsInfo;
+
        /**
         * @param array $parserConf See $wgParserConf documentation
         * @param MagicWordFactory|null $magicWordFactory
@@ -284,11 +291,15 @@ class Parser {
         * @param string|null $urlProtocols As returned from wfUrlProtocols()
         * @param SpecialPageFactory|null $spFactory
         * @param Config|null $siteConfig
+        * @param LinkRendererFactory|null $linkRendererFactory
+        * @param NamespaceInfo|null $nsInfo
         */
        public function __construct(
                array $parserConf = [], MagicWordFactory $magicWordFactory = null,
                Language $contLang = null, ParserFactory $factory = null, $urlProtocols = null,
-               SpecialPageFactory $spFactory = null, Config $siteConfig = null
+               SpecialPageFactory $spFactory = null, Config $siteConfig = null,
+               LinkRendererFactory $linkRendererFactory = null,
+               NamespaceInfo $nsInfo = null
        ) {
                $this->mConf = $parserConf;
                $this->mUrlProtocols = $urlProtocols ?? wfUrlProtocols();
@@ -319,7 +330,10 @@ class Parser {
 
                $this->factory = $factory ?? $services->getParserFactory();
                $this->specialPageFactory = $spFactory ?? $services->getSpecialPageFactory();
-               $this->siteConfig = $siteConfig ?? MediaWikiServices::getInstance()->getMainConfig();
+               $this->siteConfig = $siteConfig ?? $services->getMainConfig();
+               $this->linkRendererFactory =
+                       $linkRendererFactory ?? $services->getLinkRendererFactory();
+               $this->nsInfo = $nsInfo ?? $services->getNamespaceInfo();
        }
 
        /**
@@ -973,9 +987,9 @@ class Parser {
         * @return LinkRenderer
         */
        public function getLinkRenderer() {
+               // XXX We make the LinkRenderer with current options and then cache it forever
                if ( !$this->mLinkRenderer ) {
-                       $this->mLinkRenderer = MediaWikiServices::getInstance()
-                               ->getLinkRendererFactory()->create();
+                       $this->mLinkRenderer = $this->linkRendererFactory->create();
                        $this->mLinkRenderer->setStubThreshold(
                                $this->getOptions()->getStubThreshold()
                        );
@@ -1045,10 +1059,7 @@ class Parser {
                                $inside = $p[5];
                        } else {
                                # tag
-                               $element = $p[1];
-                               $attributes = $p[2];
-                               $close = $p[3];
-                               $inside = $p[4];
+                               list( , $element, $attributes, $close, $inside ) = $p;
                        }
 
                        $marker = self::MARKER_PREFIX . "-$element-" . sprintf( '%08X', $n++ ) . self::MARKER_SUFFIX;
@@ -1072,8 +1083,7 @@ class Parser {
                                        $tail = '';
                                        $text = '';
                                } else {
-                                       $tail = $q[1];
-                                       $text = $q[2];
+                                       list( , $tail, $text ) = $q;
                                }
                        }
 
@@ -1425,13 +1435,12 @@ class Parser {
                 */
                if ( !( $this->mOptions->getDisableContentConversion()
                        || isset( $this->mDoubleUnderscores['nocontentconvert'] ) )
+                       && !$this->mOptions->getInterfaceMessage()
                ) {
-                       if ( !$this->mOptions->getInterfaceMessage() ) {
-                               # The position of the convert() call should not be changed. it
-                               # assumes that the links are all replaced and the only thing left
-                               # is the <nowiki> mark.
-                               $text = $this->getTargetLanguage()->convert( $text );
-                       }
+                       # The position of the convert() call should not be changed. it
+                       # assumes that the links are all replaced and the only thing left
+                       # is the <nowiki> mark.
+                       $text = $this->getTargetLanguage()->convert( $text );
                }
 
                $text = $this->mStripState->unstripNoWiki( $text );
@@ -1771,10 +1780,8 @@ class Parser {
                                                // if $firstsingleletterword is set, we don't
                                                // look at the other options, so we can bail early.
                                                break;
-                                       } else {
-                                               if ( $firstmultiletterword == -1 ) {
-                                                       $firstmultiletterword = $i;
-                                               }
+                                       } elseif ( $firstmultiletterword == -1 ) {
+                                               $firstmultiletterword = $i;
                                        }
                                }
                        }
@@ -2240,8 +2247,7 @@ class Parser {
 
                        if ( $useLinkPrefixExtension ) {
                                if ( preg_match( $e2, $s, $m ) ) {
-                                       $prefix = $m[2];
-                                       $s = $m[1];
+                                       list( , $s, $prefix ) = $m;
                                } else {
                                        $prefix = '';
                                }
@@ -2528,7 +2534,7 @@ class Parser {
         */
        public function areSubpagesAllowed() {
                # Some namespaces don't allow subpages
-               return MWNamespace::hasSubpages( $this->mTitle->getNamespace() );
+               return $this->nsInfo->hasSubpages( $this->mTitle->getNamespace() );
        }
 
        /**
@@ -2583,15 +2589,27 @@ class Parser {
                 * Some of these require message or data lookups and can be
                 * expensive to check many times.
                 */
-               if ( Hooks::run( 'ParserGetVariableValueVarCache', [ &$parser, &$this->mVarCache ] ) ) {
-                       if ( isset( $this->mVarCache[$index] ) ) {
-                               return $this->mVarCache[$index];
-                       }
+               if ( Hooks::run( 'ParserGetVariableValueVarCache', [ &$parser, &$this->mVarCache ] )
+                       && isset( $this->mVarCache[$index] )
+               ) {
+                       return $this->mVarCache[$index];
                }
 
                $ts = wfTimestamp( TS_UNIX, $this->mOptions->getTimestamp() );
                Hooks::run( 'ParserGetVariableValueTs', [ &$parser, &$ts ] );
 
+               // In miser mode, disable words that always cause double-parses on page save (T137900)
+               static $slowRevWords = [ 'revisionid' => true ]; // @TODO: 'revisiontimestamp'
+               if (
+                       isset( $slowRevWords[$index] ) &&
+                       $this->siteConfig->get( 'MiserMode' ) &&
+                       !$this->mOptions->getInterfaceMessage() &&
+                       // @TODO: disallow this word on all namespaces
+                       $this->nsInfo->isContent( $this->mTitle->getNamespace() )
+               ) {
+                       return $this->mRevisionId ? '-' : '';
+               };
+
                $pageLang = $this->getFunctionLang();
 
                switch ( $index ) {
@@ -3326,7 +3344,7 @@ class Parser {
                                                        );
                                                }
                                        }
-                               } elseif ( MWNamespace::isNonincludable( $title->getNamespace() ) ) {
+                               } elseif ( $this->nsInfo->isNonincludable( $title->getNamespace() ) ) {
                                        $found = false; # access denied
                                        wfDebug( __METHOD__ . ": template inclusion denied for " .
                                                $title->getPrefixedDBkey() . "\n" );
@@ -5360,10 +5378,8 @@ class Parser {
                                                                        if ( $paramName === 'no-link' ) {
                                                                                $value = true;
                                                                        }
-                                                                       if ( $paramName === 'link-url' ) {
-                                                                               if ( $this->mOptions->getExternalLinkTarget() ) {
-                                                                                       $params[$type]['link-target'] = $this->mOptions->getExternalLinkTarget();
-                                                                               }
+                                                                       if ( ( $paramName === 'link-url' ) && $this->mOptions->getExternalLinkTarget() ) {
+                                                                               $params[$type]['link-target'] = $this->mOptions->getExternalLinkTarget();
                                                                        }
                                                                }
                                                                break;
@@ -5704,15 +5720,15 @@ class Parser {
                        if ( $sectionIndex == 0 ) {
                                if ( $mode === 'get' ) {
                                        return '';
-                               } else {
-                                       return $newText;
                                }
+
+                               return $newText;
                        } else {
                                if ( $mode === 'get' ) {
                                        return $newText;
-                               } else {
-                                       return $text;
                                }
+
+                               return $text;
                        }
                }
 
@@ -6327,10 +6343,8 @@ class Parser {
         */
        public static function stripOuterParagraph( $html ) {
                $m = [];
-               if ( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $html, $m ) ) {
-                       if ( strpos( $m[1], '</p>' ) === false ) {
-                               $html = $m[1];
-                       }
+               if ( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $html, $m ) && strpos( $m[1], '</p>' ) === false ) {
+                       $html = $m[1];
                }
 
                return $html;