parser: add speculative page IDs to use with {{PAGEID}}
[lhc/web/wiklou.git] / includes / parser / CoreParserFunctions.php
index 7ce96be..a7916c5 100644 (file)
@@ -514,6 +514,7 @@ class CoreParserFunctions {
        public static function numberofusers( $parser, $raw = null ) {
                return self::formatRaw( SiteStats::users(), $raw, $parser->getFunctionLang() );
        }
+
        public static function numberofactiveusers( $parser, $raw = null ) {
                return self::formatRaw( SiteStats::activeUsers(), $raw, $parser->getFunctionLang() );
        }
@@ -545,6 +546,7 @@ class CoreParserFunctions {
                        $parser->getFunctionLang()
                );
        }
+
        public static function numberingroup( $parser, $name = '', $raw = null ) {
                return self::formatRaw(
                        SiteStats::numberingroup( strtolower( $name ) ),
@@ -569,6 +571,7 @@ class CoreParserFunctions {
                }
                return str_replace( '_', ' ', $t->getNsText() );
        }
+
        public static function namespacee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
@@ -576,6 +579,7 @@ class CoreParserFunctions {
                }
                return wfUrlencode( $t->getNsText() );
        }
+
        public static function namespacenumber( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
@@ -583,6 +587,7 @@ class CoreParserFunctions {
                }
                return $t->getNamespace();
        }
+
        public static function talkspace( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) || !$t->canHaveTalkPage() ) {
@@ -590,6 +595,7 @@ class CoreParserFunctions {
                }
                return str_replace( '_', ' ', $t->getTalkNsText() );
        }
+
        public static function talkspacee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) || !$t->canHaveTalkPage() ) {
@@ -597,6 +603,7 @@ class CoreParserFunctions {
                }
                return wfUrlencode( $t->getTalkNsText() );
        }
+
        public static function subjectspace( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
@@ -604,6 +611,7 @@ class CoreParserFunctions {
                }
                return str_replace( '_', ' ', $t->getSubjectNsText() );
        }
+
        public static function subjectspacee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
@@ -626,6 +634,7 @@ class CoreParserFunctions {
                }
                return wfEscapeWikiText( $t->getText() );
        }
+
        public static function pagenamee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
@@ -633,6 +642,7 @@ class CoreParserFunctions {
                }
                return wfEscapeWikiText( $t->getPartialURL() );
        }
+
        public static function fullpagename( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) || !$t->canHaveTalkPage() ) {
@@ -640,6 +650,7 @@ class CoreParserFunctions {
                }
                return wfEscapeWikiText( $t->getPrefixedText() );
        }
+
        public static function fullpagenamee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) || !$t->canHaveTalkPage() ) {
@@ -647,6 +658,7 @@ class CoreParserFunctions {
                }
                return wfEscapeWikiText( $t->getPrefixedURL() );
        }
+
        public static function subpagename( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
@@ -654,6 +666,7 @@ class CoreParserFunctions {
                }
                return wfEscapeWikiText( $t->getSubpageText() );
        }
+
        public static function subpagenamee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
@@ -661,6 +674,7 @@ class CoreParserFunctions {
                }
                return wfEscapeWikiText( $t->getSubpageUrlForm() );
        }
+
        public static function rootpagename( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
@@ -668,6 +682,7 @@ class CoreParserFunctions {
                }
                return wfEscapeWikiText( $t->getRootText() );
        }
+
        public static function rootpagenamee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
@@ -675,6 +690,7 @@ class CoreParserFunctions {
                }
                return wfEscapeWikiText( wfUrlencode( str_replace( ' ', '_', $t->getRootText() ) ) );
        }
+
        public static function basepagename( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
@@ -682,6 +698,7 @@ class CoreParserFunctions {
                }
                return wfEscapeWikiText( $t->getBaseText() );
        }
+
        public static function basepagenamee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
@@ -689,6 +706,7 @@ class CoreParserFunctions {
                }
                return wfEscapeWikiText( wfUrlencode( str_replace( ' ', '_', $t->getBaseText() ) ) );
        }
+
        public static function talkpagename( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) || !$t->canHaveTalkPage() ) {
@@ -696,6 +714,7 @@ class CoreParserFunctions {
                }
                return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
        }
+
        public static function talkpagenamee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) || !$t->canHaveTalkPage() ) {
@@ -703,6 +722,7 @@ class CoreParserFunctions {
                }
                return wfEscapeWikiText( $t->getTalkPage()->getPrefixedURL() );
        }
+
        public static function subjectpagename( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
@@ -710,6 +730,7 @@ class CoreParserFunctions {
                }
                return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
        }
+
        public static function subjectpagenamee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
@@ -802,7 +823,7 @@ class CoreParserFunctions {
                }
 
                // fetch revision from cache/database and return the value
-               $rev = self::getCachedRevisionObject( $parser, $title );
+               $rev = self::getCachedRevisionObject( $parser, $title, 'vary-revision-sha1' );
                $length = $rev ? $rev->getSize() : 0;
                if ( $length === null ) {
                        // We've had bugs where rev_len was not being recorded for empty pages, see T135414
@@ -1009,7 +1030,7 @@ class CoreParserFunctions {
         * @return array|string
         */
        public static function filepath( $parser, $name = '', $argA = '', $argB = '' ) {
-               $file = wfFindFile( $name );
+               $file = MediaWikiServices::getInstance()->getRepoGroup()->findFile( $name );
 
                if ( $argA == 'nowiki' ) {
                        // {{filepath: | option [| size] }}
@@ -1105,41 +1126,56 @@ class CoreParserFunctions {
         *
         * @param Parser $parser
         * @param Title $title
+        * @param string $vary ParserOuput vary-* flag
         * @return Revision
         * @since 1.23
         */
-       private static function getCachedRevisionObject( $parser, $title = null ) {
-               if ( is_null( $title ) ) {
+       private static function getCachedRevisionObject( $parser, $title, $vary ) {
+               if ( !$title ) {
                        return null;
                }
 
-               // Use the revision from the parser itself, when param is the current page
-               // and the revision is the current one
-               if ( $title->equals( $parser->getTitle() ) ) {
-                       $parserRev = $parser->getRevisionObject();
-                       if ( $parserRev && $parserRev->isCurrent() ) {
-                               // force reparse after edit with vary-revision flag
-                               $parser->getOutput()->setFlag( 'vary-revision' );
-                               wfDebug( __METHOD__ . ": use current revision from parser, setting vary-revision...\n" );
-                               return $parserRev;
+               $revision = null;
+
+               $isSelfReferential = $title->equals( $parser->getTitle() );
+               if ( $isSelfReferential ) {
+                       // Revision is for the same title that is currently being parsed. Only use the last
+                       // saved revision, regardless of Parser::getRevisionId() or fake revision injection
+                       // callbacks against the current title.
+                       $parserRevision = $parser->getRevisionObject();
+                       if ( $parserRevision && $parserRevision->isCurrent() ) {
+                               $revision = $parserRevision;
+                               wfDebug( __METHOD__ . ": used current revision, setting $vary" );
                        }
                }
 
-               // Normalize name for cache
-               $page = $title->getPrefixedDBkey();
-
-               if ( !( $parser->currentRevisionCache && $parser->currentRevisionCache->has( $page ) )
-                       && !$parser->incrementExpensiveFunctionCount() ) {
-                       return null;
+               $parserOutput = $parser->getOutput();
+               if ( !$revision ) {
+                       if (
+                               !$parser->isCurrentRevisionOfTitleCached( $title ) &&
+                               !$parser->incrementExpensiveFunctionCount()
+                       ) {
+                               return null; // not allowed
+                       }
+                       // Get the current revision, ignoring Parser::getRevisionId() being null/old
+                       $revision = $parser->fetchCurrentRevisionOfTitle( $title );
+                       // Register dependency in templatelinks
+                       $parserOutput->addTemplate(
+                               $title,
+                               $revision ? $revision->getPage() : 0,
+                               $revision ? $revision->getId() : 0
+                       );
                }
-               $rev = $parser->fetchCurrentRevisionOfTitle( $title );
-               $pageID = $rev ? $rev->getPage() : 0;
-               $revID = $rev ? $rev->getId() : 0;
 
-               // Register dependency in templatelinks
-               $parser->getOutput()->addTemplate( $title, $pageID, $revID );
+               if ( $isSelfReferential ) {
+                       // Upon page save, the result of the parser function using this might change
+                       $parserOutput->setFlag( $vary );
+                       if ( $vary === 'vary-revision-sha1' && $revision ) {
+                               $parserOutput->setRevisionUsedSha1Base36( $revision->getSha1() );
+                       }
+               }
 
-               return $rev;
+               return $revision;
        }
 
        /**
@@ -1151,39 +1187,44 @@ class CoreParserFunctions {
         */
        public static function pageid( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null( $t ) ) {
+               if ( !$t ) {
                        return '';
+               } elseif ( !$t->canExist() || $t->isExternal() ) {
+                       return 0; // e.g. special page or interwiki link
                }
-               // Use title from parser to have correct pageid after edit
+
+               $parserOutput = $parser->getOutput();
+
                if ( $t->equals( $parser->getTitle() ) ) {
-                       $t = $parser->getTitle();
-                       return $t->getArticleID();
-               }
+                       // Revision is for the same title that is currently being parsed.
+                       // Use the title from Parser in case a new page ID was injected into it.
+                       $parserOutput->setFlag( 'vary-page-id' );
+                       $id = $parser->getTitle()->getArticleID();
+                       if ( $id ) {
+                               $parserOutput->setSpeculativePageIdUsed( $id );
+                       }
 
-               // These can't have ids
-               if ( !$t->canExist() || $t->isExternal() ) {
-                       return 0;
+                       return $id;
                }
 
-               // Check the link cache, maybe something already looked it up.
+               // Check the link cache for the title
                $linkCache = MediaWikiServices::getInstance()->getLinkCache();
                $pdbk = $t->getPrefixedDBkey();
                $id = $linkCache->getGoodLinkID( $pdbk );
-               if ( $id != 0 ) {
-                       $parser->mOutput->addLink( $t, $id );
-                       return $id;
-               }
-               if ( $linkCache->isBadLink( $pdbk ) ) {
-                       $parser->mOutput->addLink( $t, 0 );
+               if ( $id != 0 || $linkCache->isBadLink( $pdbk ) ) {
+                       $parserOutput->addLink( $t, $id );
+
                        return $id;
                }
 
                // We need to load it from the DB, so mark expensive
                if ( $parser->incrementExpensiveFunctionCount() ) {
                        $id = $t->getArticleID();
-                       $parser->mOutput->addLink( $t, $id );
+                       $parserOutput->addLink( $t, $id );
+
                        return $id;
                }
+
                return null;
        }
 
@@ -1200,7 +1241,7 @@ class CoreParserFunctions {
                        return '';
                }
                // fetch revision from cache/database and return the value
-               $rev = self::getCachedRevisionObject( $parser, $t );
+               $rev = self::getCachedRevisionObject( $parser, $t, 'vary-revision-id' );
                return $rev ? $rev->getId() : '';
        }
 
@@ -1217,7 +1258,7 @@ class CoreParserFunctions {
                        return '';
                }
                // fetch revision from cache/database and return the value
-               $rev = self::getCachedRevisionObject( $parser, $t );
+               $rev = self::getCachedRevisionObject( $parser, $t, 'vary-revision-timestamp' );
                return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'j' ) : '';
        }
 
@@ -1234,7 +1275,7 @@ class CoreParserFunctions {
                        return '';
                }
                // fetch revision from cache/database and return the value
-               $rev = self::getCachedRevisionObject( $parser, $t );
+               $rev = self::getCachedRevisionObject( $parser, $t, 'vary-revision-timestamp' );
                return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'd' ) : '';
        }
 
@@ -1251,7 +1292,7 @@ class CoreParserFunctions {
                        return '';
                }
                // fetch revision from cache/database and return the value
-               $rev = self::getCachedRevisionObject( $parser, $t );
+               $rev = self::getCachedRevisionObject( $parser, $t, 'vary-revision-timestamp' );
                return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'm' ) : '';
        }
 
@@ -1268,7 +1309,7 @@ class CoreParserFunctions {
                        return '';
                }
                // fetch revision from cache/database and return the value
-               $rev = self::getCachedRevisionObject( $parser, $t );
+               $rev = self::getCachedRevisionObject( $parser, $t, 'vary-revision-timestamp' );
                return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'n' ) : '';
        }
 
@@ -1285,7 +1326,7 @@ class CoreParserFunctions {
                        return '';
                }
                // fetch revision from cache/database and return the value
-               $rev = self::getCachedRevisionObject( $parser, $t );
+               $rev = self::getCachedRevisionObject( $parser, $t, 'vary-revision-timestamp' );
                return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'Y' ) : '';
        }
 
@@ -1302,7 +1343,7 @@ class CoreParserFunctions {
                        return '';
                }
                // fetch revision from cache/database and return the value
-               $rev = self::getCachedRevisionObject( $parser, $t );
+               $rev = self::getCachedRevisionObject( $parser, $t, 'vary-revision-timestamp' );
                return $rev ? MWTimestamp::getLocalInstance( $rev->getTimestamp() )->format( 'YmdHis' ) : '';
        }
 
@@ -1319,7 +1360,7 @@ class CoreParserFunctions {
                        return '';
                }
                // fetch revision from cache/database and return the value
-               $rev = self::getCachedRevisionObject( $parser, $t );
+               $rev = self::getCachedRevisionObject( $parser, $t, 'vary-user' );
                return $rev ? $rev->getUserText() : '';
        }