Merge "Simplify and unify the {{REVISIONID}} handling code in Parser"
[lhc/web/wiklou.git] / includes / parser / Parser.php
index 0ced8a1..2764983 100644 (file)
@@ -21,6 +21,8 @@
  * @ingroup Parser
  */
 use MediaWiki\Linker\LinkRenderer;
+use MediaWiki\Linker\LinkRendererFactory;
+use MediaWiki\Linker\LinkTarget;
 use MediaWiki\MediaWikiServices;
 use MediaWiki\Special\SpecialPageFactory;
 use Wikimedia\ScopedCallback;
@@ -276,6 +278,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 +292,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 +331,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 +988,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()
                        );
@@ -1966,7 +1981,7 @@ class Parser {
         * @since 1.21
         * @param string|bool $url Optional URL, to extract the domain from for rel =>
         *   nofollow if appropriate
-        * @param Title|null $title Optional Title, for wgNoFollowNsExceptions lookups
+        * @param LinkTarget|null $title Optional LinkTarget, for wgNoFollowNsExceptions lookups
         * @return string|null Rel attribute for $url
         */
        public static function getExternalLinkRel( $url = false, $title = null ) {
@@ -2520,7 +2535,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() );
        }
 
        /**
@@ -2575,8 +2590,9 @@ 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 ] )
-                       && isset( $this->mVarCache[$index] )
+               if (
+                       Hooks::run( 'ParserGetVariableValueVarCache', [ &$parser, &$this->mVarCache ] ) &&
+                       isset( $this->mVarCache[$index] )
                ) {
                        return $this->mVarCache[$index];
                }
@@ -2584,18 +2600,6 @@ class Parser {
                $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
-                       MWNamespace::isContent( $this->mTitle->getNamespace() )
-               ) {
-                       return $this->mRevisionId ? '-' : '';
-               };
-
                $pageLang = $this->getFunctionLang();
 
                switch ( $index ) {
@@ -2719,23 +2723,35 @@ class Parser {
                                $value = $pageid ?: null;
                                break;
                        case 'revisionid':
-                               # Let the edit saving system know we should parse the page
-                               # *after* a revision ID has been assigned.
-                               $this->mOutput->setFlag( 'vary-revision-id' );
-                               wfDebug( __METHOD__ . ": {{REVISIONID}} used, setting vary-revision-id...\n" );
-                               $value = $this->mRevisionId;
-
-                               if ( !$value ) {
-                                       $rev = $this->getRevisionObject();
-                                       if ( $rev ) {
-                                               $value = $rev->getId();
+                               if (
+                                       $this->siteConfig->get( 'MiserMode' ) &&
+                                       !$this->mOptions->getInterfaceMessage() &&
+                                       // @TODO: disallow this word on all namespaces
+                                       $this->nsInfo->isContent( $this->mTitle->getNamespace() )
+                               ) {
+                                       // Use a stub result instead of the actual revision ID in order to avoid
+                                       // double parses on page save but still allow preview detection (T137900)
+                                       if ( $this->getRevisionId() || $this->mOptions->getSpeculativeRevId() ) {
+                                               $value = '-';
+                                       } else {
+                                               $this->mOutput->setFlag( 'vary-revision-exists' );
+                                               $value = '';
                                        }
-                               }
-
-                               if ( !$value ) {
-                                       $value = $this->mOptions->getSpeculativeRevId();
-                                       if ( $value ) {
-                                               $this->mOutput->setSpeculativeRevIdUsed( $value );
+                               } else {
+                                       # Inform the edit saving system that getting the canonical output after
+                                       # revision insertion requires another parse using the actual revision ID
+                                       $this->mOutput->setFlag( 'vary-revision-id' );
+                                       wfDebug( __METHOD__ . ": {{REVISIONID}} used, setting vary-revision-id...\n" );
+                                       $value = $this->getRevisionId();
+                                       if ( $value === 0 ) {
+                                               $rev = $this->getRevisionObject();
+                                               $value = $rev ? $rev->getId() : $value;
+                                       }
+                                       if ( !$value ) {
+                                               $value = $this->mOptions->getSpeculativeRevId();
+                                               if ( $value ) {
+                                                       $this->mOutput->setSpeculativeRevIdUsed( $value );
+                                               }
                                        }
                                }
                                break;
@@ -3330,7 +3346,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" );
@@ -4652,7 +4668,7 @@ class Parser {
         * If you have pre-fetched the nickname or the fancySig option, you can
         * specify them here to save a database query.
         * Do not reuse this parser instance after calling getUserSig(),
-        * as it may have changed if it's the $wgParser.
+        * as it may have changed.
         *
         * @param User &$user
         * @param string|bool $nickname Nickname to use or false to use user's default nickname
@@ -5830,6 +5846,11 @@ class Parser {
        /**
         * Get the ID of the revision we are parsing
         *
+        * The return value will be either:
+        *   - a) Positive, indicating a specific revision ID (current or old)
+        *   - b) Zero, meaning the revision ID specified by getCurrentRevisionCallback()
+        *   - c) Null, meaning the parse is for preview mode and there is no revision
+        *
         * @return int|null
         */
        public function getRevisionId() {
@@ -6339,9 +6360,9 @@ class Parser {
        /**
         * Return this parser if it is not doing anything, otherwise
         * get a fresh parser. You can use this method by doing
-        * $myParser = $wgParser->getFreshParser(), or more simply
-        * $wgParser->getFreshParser()->parse( ... );
-        * if you're unsure if $wgParser is safe to use.
+        * $newParser = $oldParser->getFreshParser(), or more simply
+        * $oldParser->getFreshParser()->parse( ... );
+        * if you're unsure if $oldParser is safe to use.
         *
         * @since 1.24
         * @return Parser A parser object that is not parsing anything