Merge "Bump PHP version requirement to 7.2.0+"
[lhc/web/wiklou.git] / includes / parser / CoreParserFunctions.php
index 5aa1a69..b803241 100644 (file)
@@ -434,7 +434,7 @@ class CoreParserFunctions {
                if ( !$wgRestrictDisplayTitle ||
                        ( $title instanceof Title
                        && !$title->hasFragment()
-                       && $title->equals( $parser->mTitle ) )
+                       && $title->equals( $parser->getTitle() ) )
                ) {
                        $old = $parser->mOutput->getProperty( 'displaytitle' );
                        if ( $old === false || $arg !== 'displaytitle_noreplace' ) {
@@ -845,10 +845,7 @@ class CoreParserFunctions {
         * @return string
         */
        public static function protectionlevel( $parser, $type = '', $title = '' ) {
-               $titleObject = Title::newFromText( $title );
-               if ( !( $titleObject instanceof Title ) ) {
-                       $titleObject = $parser->mTitle;
-               }
+               $titleObject = Title::newFromText( $title ) ?? $parser->getTitle();
                if ( $titleObject->areRestrictionsLoaded() || $parser->incrementExpensiveFunctionCount() ) {
                        $restrictions = $titleObject->getRestrictions( strtolower( $type ) );
                        # Title::getRestrictions returns an array, its possible it may have
@@ -871,10 +868,7 @@ class CoreParserFunctions {
         * @return string
         */
        public static function protectionexpiry( $parser, $type = '', $title = '' ) {
-               $titleObject = Title::newFromText( $title );
-               if ( !( $titleObject instanceof Title ) ) {
-                       $titleObject = $parser->mTitle;
-               }
+               $titleObject = Title::newFromText( $title ) ?? $parser->getTitle();
                if ( $titleObject->areRestrictionsLoaded() || $parser->incrementExpensiveFunctionCount() ) {
                        $expiry = $titleObject->getRestrictionExpiry( strtolower( $type ) );
                        // getRestrictionExpiry() returns false on invalid type; trying to
@@ -1127,7 +1121,7 @@ class CoreParserFunctions {
         * @param Parser $parser
         * @param Title $title
         * @param string $vary ParserOuput vary-* flag
-        * @return Revision
+        * @return Revision|null
         * @since 1.23
         */
        private static function getCachedRevisionObject( $parser, $title, $vary ) {
@@ -1187,39 +1181,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;
        }
 
@@ -1372,10 +1371,7 @@ class CoreParserFunctions {
         * @since 1.23
         */
        public static function cascadingsources( $parser, $title = '' ) {
-               $titleObject = Title::newFromText( $title );
-               if ( !( $titleObject instanceof Title ) ) {
-                       $titleObject = $parser->mTitle;
-               }
+               $titleObject = Title::newFromText( $title ) ?? $parser->getTitle();
                if ( $titleObject->areCascadeProtectionSourcesLoaded()
                        || $parser->incrementExpensiveFunctionCount()
                ) {