Merge "installer: Remove <doclink/> parser function and last use of it"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 18 Jul 2019 02:18:35 +0000 (02:18 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 18 Jul 2019 02:18:35 +0000 (02:18 +0000)
13 files changed:
includes/Permissions/PermissionManager.php
includes/Revision/RenderedRevision.php
includes/Storage/PageEditStash.php
includes/libs/objectcache/RedisBagOStuff.php
includes/libs/redis/RedisConnectionPool.php
includes/parser/CoreParserFunctions.php
includes/parser/Parser.php
includes/parser/ParserOutput.php
maintenance/doMaintenance.php
tests/parser/ParserTestRunner.php
tests/parser/parserTests.txt
tests/phpunit/includes/Permissions/PermissionManagerTest.php
tests/phpunit/includes/libs/objectcache/BagOStuffTest.php

index 98a5b17..5a3dae3 100644 (file)
@@ -1409,24 +1409,20 @@ class PermissionManager {
         * to make bot-flagged actions through certain special pages.
         * Returns a "scope guard" variable; whenever that variable goes out of scope or is consumed
         * via ScopedCallback::consume(), the temporary rights are revoked.
+        *
+        * @since 1.34
+        *
         * @param UserIdentity $user
         * @param string|string[] $rights
         * @return ScopedCallback
         */
        public function addTemporaryUserRights( UserIdentity $user, $rights ) {
-               $nextKey = count( $this->temporaryUserRights[$user->getId()] ?? [] );
-               $this->temporaryUserRights[$user->getId()][$nextKey] = (array)$rights;
-               return new ScopedCallback( [ $this, 'revokeTemporaryUserRights' ], [ $user->getId(), $nextKey ] );
-       }
-
-       /**
-        * Revoke rights added by addTemporaryUserRights().
-        * @param int $userId
-        * @param int $rightsGroupKey Key in self::$temporaryUserRights
-        * @internal For use by addTemporaryUserRights() only.
-        */
-       public function revokeTemporaryUserRights( $userId, $rightsGroupKey ) {
-               unset( $this->temporaryUserRights[$userId][$rightsGroupKey] );
+               $userId = $user->getId();
+               $nextKey = count( $this->temporaryUserRights[$userId] ?? [] );
+               $this->temporaryUserRights[$userId][$nextKey] = (array)$rights;
+               return new ScopedCallback( function () use ( $userId, $nextKey ) {
+                       unset( $this->temporaryUserRights[$userId][$nextKey] );
+               } );
        }
 
        /**
index 4acb9c0..cf1cc94 100644 (file)
@@ -430,6 +430,16 @@ class RenderedRevision implements SlotRenderingProvider {
                                "$method: Prepared output has vary-revision-exists..."
                        );
                        return true;
+               } elseif (
+                       $out->getFlag( 'vary-revision-sha1' ) &&
+                       $out->getRevisionUsedSha1Base36() !== $this->revision->getSha1()
+               ) {
+                       // If a self-transclusion used the proposed page text, it must match the final
+                       // page content after PST transformations and automatically merged edit conflicts
+                       $this->saveParseLogger->info(
+                               "$method: Prepared output has vary-revision-sha1 with wrong SHA-1..."
+                       );
+                       return true;
                } else {
                        // NOTE: In the original fix for T135261, the output was discarded if 'vary-user' was
                        // set for a null-edit. The reason was that the original rendering in that case was
index 6caca29..4671d99 100644 (file)
@@ -269,23 +269,28 @@ class PageEditStash {
 
                if ( $editInfo->output->getFlag( 'vary-revision' ) ) {
                        // This can be used for the initial parse, e.g. for filters or doEditContent(),
-                       // but a second parse will be triggered in doEditUpdates(). This is not optimal.
+                       // but a second parse will be triggered in doEditUpdates() no matter what
                        $logger->info(
-                               "Cache for key '{key}' has vary_revision; post-insertion parse inevitable.",
-                               $context
-                       );
-               } elseif ( $editInfo->output->getFlag( 'vary-revision-id' ) ) {
-                       // Similar to the above if we didn't guess the ID correctly.
-                       $logger->debug(
-                               "Cache for key '{key}' has vary_revision_id; post-insertion parse possible.",
-                               $context
-                       );
-               } elseif ( $editInfo->output->getFlag( 'vary-revision-timestamp' ) ) {
-                       // Similar to the above if we didn't guess the timestamp correctly.
-                       $logger->debug(
-                               "Cache for key '{key}' has vary_revision_timestamp; post-insertion parse possible.",
+                               "Cache for key '{key}' has 'vary-revision'; post-insertion parse inevitable.",
                                $context
                        );
+               } else {
+                       static $flagsMaybeReparse = [
+                               // Similar to the above if we didn't guess the ID correctly
+                               'vary-revision-id',
+                               // Similar to the above if we didn't guess the timestamp correctly
+                               'vary-revision-timestamp',
+                               // Similar to the above if we didn't guess the content correctly
+                               'vary-revision-sha1'
+                       ];
+                       foreach ( $flagsMaybeReparse as $flag ) {
+                               if ( $editInfo->output->getFlag( $flag ) ) {
+                                       $logger->debug(
+                                               "Cache for key '{key}' has $flag; post-insertion parse possible.",
+                                               $context
+                                       );
+                               }
+                       }
                }
 
                return $editInfo;
index a72b3ff..2d1ed05 100644 (file)
@@ -89,10 +89,12 @@ class RedisBagOStuff extends BagOStuff {
        protected function doGet( $key, $flags = 0, &$casToken = null ) {
                $casToken = null;
 
-               list( $server, $conn ) = $this->getConnection( $key );
+               $conn = $this->getConnection( $key );
                if ( !$conn ) {
                        return false;
                }
+
+               $e = null;
                try {
                        $value = $conn->get( $key );
                        $casToken = $value;
@@ -102,16 +104,20 @@ class RedisBagOStuff extends BagOStuff {
                        $this->handleException( $conn, $e );
                }
 
-               $this->logRequest( 'get', $key, $server, $result );
+               $this->logRequest( 'get', $key, $conn->getServer(), $e );
+
                return $result;
        }
 
        protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
-               list( $server, $conn ) = $this->getConnection( $key );
+               $conn = $this->getConnection( $key );
                if ( !$conn ) {
                        return false;
                }
+
                $ttl = $this->convertToRelative( $exptime );
+
+               $e = null;
                try {
                        if ( $ttl ) {
                                $result = $conn->setex( $key, $ttl, $this->serialize( $value ) );
@@ -123,52 +129,61 @@ class RedisBagOStuff extends BagOStuff {
                        $this->handleException( $conn, $e );
                }
 
-               $this->logRequest( 'set', $key, $server, $result );
+               $this->logRequest( 'set', $key, $conn->getServer(), $e );
+
                return $result;
        }
 
        protected function doDelete( $key, $flags = 0 ) {
-               list( $server, $conn ) = $this->getConnection( $key );
+               $conn = $this->getConnection( $key );
                if ( !$conn ) {
                        return false;
                }
+
+               $e = null;
                try {
-                       $conn->delete( $key );
-                       // Return true even if the key didn't exist
-                       $result = true;
+                       // Note that redis does not return false if the key was not there
+                       $result = ( $conn->delete( $key ) !== false );
                } catch ( RedisException $e ) {
                        $result = false;
                        $this->handleException( $conn, $e );
                }
 
-               $this->logRequest( 'delete', $key, $server, $result );
+               $this->logRequest( 'delete', $key, $conn->getServer(), $e );
+
                return $result;
        }
 
        protected function doGetMulti( array $keys, $flags = 0 ) {
-               $batches = [];
+               /** @var RedisConnRef[]|Redis[] $conns */
                $conns = [];
+               $batches = [];
                foreach ( $keys as $key ) {
-                       list( $server, $conn ) = $this->getConnection( $key );
-                       if ( !$conn ) {
-                               continue;
+                       $conn = $this->getConnection( $key );
+                       if ( $conn ) {
+                               $server = $conn->getServer();
+                               $conns[$server] = $conn;
+                               $batches[$server][] = $key;
                        }
-                       $conns[$server] = $conn;
-                       $batches[$server][] = $key;
                }
+
                $result = [];
                foreach ( $batches as $server => $batchKeys ) {
                        $conn = $conns[$server];
+
+                       $e = null;
                        try {
+                               // Avoid mget() to reduce CPU hogging from a single request
                                $conn->multi( Redis::PIPELINE );
                                foreach ( $batchKeys as $key ) {
                                        $conn->get( $key );
                                }
                                $batchResult = $conn->exec();
                                if ( $batchResult === false ) {
-                                       $this->debug( "multi request to $server failed" );
+                                       $this->logRequest( 'get', implode( ',', $batchKeys ), $server, true );
                                        continue;
                                }
+
                                foreach ( $batchResult as $i => $value ) {
                                        if ( $value !== false ) {
                                                $result[$batchKeys[$i]] = $this->unserialize( $value );
@@ -177,41 +192,47 @@ class RedisBagOStuff extends BagOStuff {
                        } catch ( RedisException $e ) {
                                $this->handleException( $conn, $e );
                        }
+
+                       $this->logRequest( 'get', implode( ',', $batchKeys ), $server, $e );
                }
 
-               $this->debug( "getMulti for " . count( $keys ) . " keys " .
-                       "returned " . count( $result ) . " results" );
                return $result;
        }
 
-       protected function doSetMulti( array $data, $expiry = 0, $flags = 0 ) {
-               $batches = [];
+       protected function doSetMulti( array $data, $exptime = 0, $flags = 0 ) {
+               /** @var RedisConnRef[]|Redis[] $conns */
                $conns = [];
+               $batches = [];
                foreach ( $data as $key => $value ) {
-                       list( $server, $conn ) = $this->getConnection( $key );
-                       if ( !$conn ) {
-                               continue;
+                       $conn = $this->getConnection( $key );
+                       if ( $conn ) {
+                               $server = $conn->getServer();
+                               $conns[$server] = $conn;
+                               $batches[$server][] = $key;
                        }
-                       $conns[$server] = $conn;
-                       $batches[$server][] = $key;
                }
 
-               $expiry = $this->convertToRelative( $expiry );
+               $ttl = $this->convertToRelative( $exptime );
+               $op = $ttl ? 'setex' : 'set';
+
                $result = true;
                foreach ( $batches as $server => $batchKeys ) {
                        $conn = $conns[$server];
+
+                       $e = null;
                        try {
+                               // Avoid mset() to reduce CPU hogging from a single request
                                $conn->multi( Redis::PIPELINE );
                                foreach ( $batchKeys as $key ) {
-                                       if ( $expiry ) {
-                                               $conn->setex( $key, $expiry, $this->serialize( $data[$key] ) );
+                                       if ( $ttl ) {
+                                               $conn->setex( $key, $ttl, $this->serialize( $data[$key] ) );
                                        } else {
                                                $conn->set( $key, $this->serialize( $data[$key] ) );
                                        }
                                }
                                $batchResult = $conn->exec();
                                if ( $batchResult === false ) {
-                                       $this->debug( "setMulti request to $server failed" );
+                                       $this->logRequest( $op, implode( ',', $batchKeys ), $server, true );
                                        continue;
                                }
                                $result = $result && !in_array( false, $batchResult, true );
@@ -219,48 +240,106 @@ class RedisBagOStuff extends BagOStuff {
                                $this->handleException( $conn, $e );
                                $result = false;
                        }
+
+                       $this->logRequest( $op, implode( ',', $batchKeys ), $server, $e );
                }
 
                return $result;
        }
 
        protected function doDeleteMulti( array $keys, $flags = 0 ) {
-               $batches = [];
+               /** @var RedisConnRef[]|Redis[] $conns */
                $conns = [];
+               $batches = [];
                foreach ( $keys as $key ) {
-                       list( $server, $conn ) = $this->getConnection( $key );
-                       if ( !$conn ) {
-                               continue;
+                       $conn = $this->getConnection( $key );
+                       if ( $conn ) {
+                               $server = $conn->getServer();
+                               $conns[$server] = $conn;
+                               $batches[$server][] = $key;
                        }
-                       $conns[$server] = $conn;
-                       $batches[$server][] = $key;
                }
 
                $result = true;
                foreach ( $batches as $server => $batchKeys ) {
                        $conn = $conns[$server];
+
+                       $e = null;
                        try {
+                               // Avoid delete() with array to reduce CPU hogging from a single request
                                $conn->multi( Redis::PIPELINE );
                                foreach ( $batchKeys as $key ) {
                                        $conn->delete( $key );
                                }
                                $batchResult = $conn->exec();
                                if ( $batchResult === false ) {
-                                       $this->debug( "deleteMulti request to $server failed" );
+                                       $this->logRequest( 'delete', implode( ',', $batchKeys ), $server, true );
                                        continue;
                                }
+                               // Note that redis does not return false if the key was not there
                                $result = $result && !in_array( false, $batchResult, true );
                        } catch ( RedisException $e ) {
                                $this->handleException( $conn, $e );
                                $result = false;
                        }
+
+                       $this->logRequest( 'delete', implode( ',', $batchKeys ), $server, $e );
+               }
+
+               return $result;
+       }
+
+       public function changeTTLMulti( array $keys, $exptime, $flags = 0 ) {
+               /** @var RedisConnRef[]|Redis[] $conns */
+               $conns = [];
+               $batches = [];
+               foreach ( $keys as $key ) {
+                       $conn = $this->getConnection( $key );
+                       if ( $conn ) {
+                               $server = $conn->getServer();
+                               $conns[$server] = $conn;
+                               $batches[$server][] = $key;
+                       }
+               }
+
+               $relative = $this->expiryIsRelative( $exptime );
+               $op = ( $exptime == 0 ) ? 'persist' : ( $relative ? 'expire' : 'expireAt' );
+
+               $result = true;
+               foreach ( $batches as $server => $batchKeys ) {
+                       $conn = $conns[$server];
+
+                       $e = null;
+                       try {
+                               $conn->multi( Redis::PIPELINE );
+                               foreach ( $batchKeys as $key ) {
+                                       if ( $exptime == 0 ) {
+                                               $conn->persist( $key );
+                                       } elseif ( $relative ) {
+                                               $conn->expire( $key, $this->convertToRelative( $exptime ) );
+                                       } else {
+                                               $conn->expireAt( $key, $this->convertToExpiry( $exptime ) );
+                                       }
+                               }
+                               $batchResult = $conn->exec();
+                               if ( $batchResult === false ) {
+                                       $this->logRequest( $op, implode( ',', $batchKeys ), $server, true );
+                                       continue;
+                               }
+                               $result = in_array( false, $batchResult, true ) ? false : $result;
+                       } catch ( RedisException $e ) {
+                               $this->handleException( $conn, $e );
+                               $result = false;
+                       }
+
+                       $this->logRequest( $op, implode( ',', $batchKeys ), $server, $e );
                }
 
                return $result;
        }
 
        public function add( $key, $value, $expiry = 0, $flags = 0 ) {
-               list( $server, $conn ) = $this->getConnection( $key );
+               $conn = $this->getConnection( $key );
                if ( !$conn ) {
                        return false;
                }
@@ -277,13 +356,13 @@ class RedisBagOStuff extends BagOStuff {
                        $this->handleException( $conn, $e );
                }
 
-               $this->logRequest( 'add', $key, $server, $result );
+               $this->logRequest( 'add', $key, $conn->getServer(), $result );
 
                return $result;
        }
 
        public function incr( $key, $value = 1 ) {
-               list( $server, $conn ) = $this->getConnection( $key );
+               $conn = $this->getConnection( $key );
                if ( !$conn ) {
                        return false;
                }
@@ -313,13 +392,13 @@ class RedisBagOStuff extends BagOStuff {
                        $this->handleException( $conn, $e );
                }
 
-               $this->logRequest( 'incr', $key, $server, $result );
+               $this->logRequest( 'incr', $key, $conn->getServer(), $result );
 
                return $result;
        }
 
        public function incrWithInit( $key, $exptime, $value = 1, $init = 1 ) {
-               list( $server, $conn ) = $this->getConnection( $key );
+               $conn = $this->getConnection( $key );
                if ( !$conn ) {
                        return false;
                }
@@ -333,7 +412,7 @@ class RedisBagOStuff extends BagOStuff {
                        $batchResult = $conn->exec();
                        if ( $batchResult === false ) {
                                $result = false;
-                               $this->debug( "incrWithInit request to $server failed" );
+                               $this->debug( "incrWithInit request to {$conn->getServer()} failed" );
                        } else {
                                $result = end( $batchResult );
                        }
@@ -342,13 +421,13 @@ class RedisBagOStuff extends BagOStuff {
                        $this->handleException( $conn, $e );
                }
 
-               $this->logRequest( 'incr', $key, $server, $result );
+               $this->logRequest( 'incr', $key, $conn->getServer(), $result );
 
                return $result;
        }
 
        protected function doChangeTTL( $key, $exptime, $flags ) {
-               list( $server, $conn ) = $this->getConnection( $key );
+               $conn = $this->getConnection( $key );
                if ( !$conn ) {
                        return false;
                }
@@ -357,13 +436,13 @@ class RedisBagOStuff extends BagOStuff {
                try {
                        if ( $exptime == 0 ) {
                                $result = $conn->persist( $key );
-                               $this->logRequest( 'persist', $key, $server, $result );
+                               $this->logRequest( 'persist', $key, $conn->getServer(), $result );
                        } elseif ( $relative ) {
                                $result = $conn->expire( $key, $this->convertToRelative( $exptime ) );
-                               $this->logRequest( 'expire', $key, $server, $result );
+                               $this->logRequest( 'expire', $key, $conn->getServer(), $result );
                        } else {
                                $result = $conn->expireAt( $key, $this->convertToExpiry( $exptime ) );
-                               $this->logRequest( 'expireAt', $key, $server, $result );
+                               $this->logRequest( 'expireAt', $key, $conn->getServer(), $result );
                        }
                } catch ( RedisException $e ) {
                        $result = false;
@@ -374,9 +453,8 @@ class RedisBagOStuff extends BagOStuff {
        }
 
        /**
-        * Get a Redis object with a connection suitable for fetching the specified key
         * @param string $key
-        * @return array (server, RedisConnRef) or (false, false)
+        * @return RedisConnRef|Redis|null Redis handle wrapper for the key or null on failure
         */
        protected function getConnection( $key ) {
                $candidates = array_keys( $this->serverTagMap );
@@ -402,7 +480,9 @@ class RedisBagOStuff extends BagOStuff {
                        // by now in such cases.
                        if ( $this->automaticFailover && $candidates ) {
                                try {
-                                       if ( $this->getMasterLinkStatus( $conn ) === 'down' ) {
+                                       /** @var string[] $info */
+                                       $info = $conn->info();
+                                       if ( ( $info['master_link_status'] ?? null ) === 'down' ) {
                                                // If the master cannot be reached, fail-over to the next server.
                                                // If masters are in data-center A, and replica DBs in data-center B,
                                                // this helps avoid the case were fail-over happens in A but not
@@ -411,28 +491,17 @@ class RedisBagOStuff extends BagOStuff {
                                        }
                                } catch ( RedisException $e ) {
                                        // Server is not accepting commands
-                                       $this->handleException( $conn, $e );
+                                       $this->redisPool->handleError( $conn, $e );
                                        continue;
                                }
                        }
 
-                       return [ $server, $conn ];
+                       return $conn;
                }
 
                $this->setLastError( BagOStuff::ERR_UNREACHABLE );
 
-               return [ false, false ];
-       }
-
-       /**
-        * Check the master link status of a Redis server that is configured as a replica DB.
-        * @param RedisConnRef $conn
-        * @return string|null Master link status (either 'up' or 'down'), or null
-        *  if the server is not a replica DB.
-        */
-       protected function getMasterLinkStatus( RedisConnRef $conn ) {
-               $info = $conn->info();
-               return $info['master_link_status'] ?? null;
+               return null;
        }
 
        /**
@@ -451,20 +520,19 @@ class RedisBagOStuff extends BagOStuff {
         * @param RedisConnRef $conn
         * @param RedisException $e
         */
-       protected function handleException( RedisConnRef $conn, $e ) {
+       protected function handleException( RedisConnRef $conn, RedisException $e ) {
                $this->setLastError( BagOStuff::ERR_UNEXPECTED );
                $this->redisPool->handleError( $conn, $e );
        }
 
        /**
         * Send information about a single request to the debug log
-        * @param string $method
-        * @param string $key
+        * @param string $op
+        * @param string $keys
         * @param string $server
-        * @param bool $result
+        * @param Exception|bool|null $e
         */
-       public function logRequest( $method, $key, $server, $result ) {
-               $this->debug( "$method $key on $server: " .
-                       ( $result === false ? "failure" : "success" ) );
+       public function logRequest( $op, $keys, $server, $e = null ) {
+               $this->debug( "$op($keys) on $server: " . ( $e ? "failure" : "success" ) );
        }
 }
index b477855..eb645cc 100644 (file)
@@ -99,10 +99,6 @@ class RedisConnectionPool implements LoggerAwareInterface {
                $this->id = $id;
        }
 
-       /**
-        * @param LoggerInterface $logger
-        * @return null
-        */
        public function setLogger( LoggerInterface $logger ) {
                $this->logger = $logger;
        }
@@ -170,10 +166,13 @@ class RedisConnectionPool implements LoggerAwareInterface {
         * @param string $server A hostname/port combination or the absolute path of a UNIX socket.
         *                       If a hostname is specified but no port, port 6379 will be used.
         * @param LoggerInterface|null $logger PSR-3 logger intance. [optional]
-        * @return RedisConnRef|bool Returns false on failure
+        * @return RedisConnRef|Redis|bool Returns false on failure
         * @throws MWException
         */
        public function getConnection( $server, LoggerInterface $logger = null ) {
+               // The above @return also documents 'Redis' for convenience with IDEs.
+               // RedisConnRef uses PHP magic methods, which wouldn't be recognised.
+
                $logger = $logger ?: $this->logger;
                // Check the listing "dead" servers which have had a connection errors.
                // Servers are marked dead for a limited period of time, to
index 7fece00..5aa1a69 100644 (file)
@@ -823,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
@@ -1126,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;
        }
 
        /**
@@ -1221,7 +1236,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() : '';
        }
 
@@ -1238,7 +1253,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' ) : '';
        }
 
@@ -1255,7 +1270,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' ) : '';
        }
 
@@ -1272,7 +1287,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' ) : '';
        }
 
@@ -1289,7 +1304,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' ) : '';
        }
 
@@ -1306,7 +1321,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' ) : '';
        }
 
@@ -1323,7 +1338,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' ) : '';
        }
 
@@ -1340,7 +1355,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() : '';
        }
 
index a2c5eec..0721446 100644 (file)
@@ -3693,6 +3693,18 @@ class Parser {
                return $this->currentRevisionCache->get( $cacheKey );
        }
 
+       /**
+        * @param Title $title
+        * @return bool
+        * @since 1.34
+        */
+       public function isCurrentRevisionOfTitleCached( $title ) {
+               return (
+                       $this->currentRevisionCache &&
+                       $this->currentRevisionCache->has( $title->getPrefixedText() )
+               );
+       }
+
        /**
         * Wrapper around Revision::newFromTitle to allow passing additional parameters
         * without passing them on to it.
@@ -3727,8 +3739,7 @@ class Parser {
                        foreach ( $stuff['deps'] as $dep ) {
                                $this->mOutput->addTemplate( $dep['title'], $dep['page_id'], $dep['rev_id'] );
                                if ( $dep['title']->equals( $this->getTitle() ) ) {
-                                       // If we transclude ourselves, the final result
-                                       // will change based on the new version of the page
+                                       // Self-transclusion; final result may change based on the new page version
                                        $this->mOutput->setFlag( 'vary-revision' );
                                        wfDebug( __METHOD__ . ": self transclusion, setting vary-revision" );
                                }
index c8113f3..23e5911 100644 (file)
@@ -216,6 +216,9 @@ class ParserOutput extends CacheTime {
        /** @var int|null Assumed rev timestamp for {{REVISIONTIMESTAMP}} if no revision is set */
        private $revisionTimestampUsed;
 
+       /** @var string|null SHA-1 base 36 hash of any self-transclusion */
+       private $revisionUsedSha1Base36;
+
        /** string CSS classes to use for the wrapping div, stored in the array keys.
         * If no class is given, no wrapper is added.
         */
@@ -464,6 +467,33 @@ class ParserOutput extends CacheTime {
                return $this->revisionTimestampUsed;
        }
 
+       /**
+        * @param string $hash Lowercase SHA-1 base 36 hash
+        * @since 1.34
+        */
+       public function setRevisionUsedSha1Base36( $hash ) {
+               if ( $hash === null ) {
+                       return; // e.g. RevisionRecord::getSha1() returned null
+               }
+
+               if (
+                       $this->revisionUsedSha1Base36 !== null &&
+                       $this->revisionUsedSha1Base36 !== $hash
+               ) {
+                       $this->revisionUsedSha1Base36 = ''; // mismatched
+               } else {
+                       $this->revisionUsedSha1Base36 = $hash;
+               }
+       }
+
+       /**
+        * @return string|null Lowercase SHA-1 base 36 hash, null if unused, or "" on inconsistency
+        * @since 1.34
+        */
+       public function getRevisionUsedSha1Base36() {
+               return $this->revisionUsedSha1Base36;
+       }
+
        public function &getLanguageLinks() {
                return $this->mLanguageLinks;
        }
index 0ee1e6a..fe4905b 100644 (file)
@@ -99,12 +99,20 @@ try {
        $success = $maintenance->execute();
 } catch ( Exception $ex ) {
        $success = false;
+       $exReportMessage = '';
        while ( $ex ) {
                $cls = get_class( $ex );
-               print "$cls from line {$ex->getLine()} of {$ex->getFile()}: {$ex->getMessage()}\n";
-               print $ex->getTraceAsString() . "\n";
+               $exReportMessage .= "$cls from line {$ex->getLine()} of {$ex->getFile()}: {$ex->getMessage()}\n";
+               $exReportMessage .= $ex->getTraceAsString() . "\n";
                $ex = $ex->getPrevious();
        }
+       // Print the exception to stderr if possible, don't mix it in
+       // with stdout output.
+       if ( defined( 'STDERR' ) ) {
+               fwrite( STDERR, $exReportMessage );
+       } else {
+               echo $exReportMessage;
+       }
 }
 
 // Potentially debug globals
index f29b0d7..ba85027 100644 (file)
@@ -25,6 +25,7 @@
  * @file
  * @ingroup Testing
  */
+
 use Wikimedia\Rdbms\IDatabase;
 use MediaWiki\MediaWikiServices;
 use MediaWiki\Tidy\TidyDriverBase;
@@ -129,6 +130,9 @@ class ParserTestRunner {
         */
        private $keepUploads;
 
+       /** @var Title */
+       private $defaultTitle;
+
        /**
         * @param TestRecorder $recorder
         * @param array $options
@@ -165,6 +169,8 @@ class ParserTestRunner {
                if ( isset( $options['upload-dir'] ) ) {
                        $this->uploadDir = $options['upload-dir'];
                }
+
+               $this->defaultTitle = Title::newFromText( 'Parser test' );
        }
 
        /**
@@ -839,10 +845,43 @@ class ParserTestRunner {
                        $options->setTidy( true );
                }
 
-               if ( isset( $opts['title'] ) ) {
-                       $titleText = $opts['title'];
-               } else {
-                       $titleText = 'Parser test';
+               $revId = 1337; // see Parser::getRevisionId()
+               $title = isset( $opts['title'] )
+                       ? Title::newFromText( $opts['title'] )
+                       : $this->defaultTitle;
+
+               if ( isset( $opts['lastsavedrevision'] ) ) {
+                       $content = new WikitextContent( $test['input'] );
+                       $title = Title::newFromRow( (object)[
+                               'page_id' => 187,
+                               'page_len' => $content->getSize(),
+                               'page_latest' => 1337,
+                               'page_namespace' => $title->getNamespace(),
+                               'page_title' => $title->getDBkey(),
+                               'page_is_redirect' => 0
+                       ] );
+                       $rev = new Revision(
+                               [
+                                       'id' => $title->getLatestRevID(),
+                                       'page' => $title->getArticleID(),
+                                       'user' => $user,
+                                       'content' => $content,
+                                       'timestamp' => $this->getFakeTimestamp(),
+                                       'title' => $title
+                               ],
+                               Revision::READ_LATEST,
+                               $title
+                       );
+                       $oldCallback = $options->getCurrentRevisionCallback();
+                       $options->setCurrentRevisionCallback(
+                               function ( Title $t, $parser ) use ( $title, $rev, $oldCallback ) {
+                                       if ( $t->equals( $title ) ) {
+                                               return $rev;
+                                       } else {
+                                               return call_user_func( $oldCallback, $t, $parser );
+                                       }
+                               }
+                       );
                }
 
                if ( isset( $opts['maxincludesize'] ) ) {
@@ -855,7 +894,6 @@ class ParserTestRunner {
                $local = isset( $opts['local'] );
                $preprocessor = $opts['preprocessor'] ?? null;
                $parser = $this->getParser( $preprocessor );
-               $title = Title::newFromText( $titleText );
 
                if ( isset( $opts['styletag'] ) ) {
                        // For testing the behavior of <style> (including those deduplicated
@@ -887,7 +925,7 @@ class ParserTestRunner {
                } elseif ( isset( $opts['preload'] ) ) {
                        $out = $parser->getPreloadText( $test['input'], $title, $options );
                } else {
-                       $output = $parser->parse( $test['input'], $title, $options, true, true, 1337 );
+                       $output = $parser->parse( $test['input'], $title, $options, true, true, $revId );
                        $out = $output->getText( [
                                'allowTOC' => !isset( $opts['notoc'] ),
                                'unwrap' => !isset( $opts['wrap'] ),
index 0facec2..7046a7f 100644 (file)
@@ -10813,8 +10813,9 @@ parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true }
 !! end
 
 !! test
-Magic Word: {{REVISIONID}}
+Magic Word: {{REVISIONID}} on latest revision
 !! options
+lastsavedrevision
 parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true }
 showflags
 !! wikitext
@@ -10825,6 +10826,156 @@ showflags
 flags=vary-revision-id
 !! end
 
+!! test
+Magic Word: {{REVISIONID}} on non-latest revision
+!! options
+parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true }
+showflags
+!! wikitext
+{{REVISIONID}}
+!! html/*
+<p>1337
+</p>
+flags=vary-revision-id
+!! end
+
+!! test
+Magic Word: {{REVISIONTIMESTAMP}} on latest revision
+!! options
+lastsavedrevision
+parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true }
+showflags
+!! wikitext
+{{REVISIONTIMESTAMP}}
+!! html/*
+<p>19700101000203
+</p>
+flags=
+!! end
+
+!! test
+Magic Word: {{REVISIONTIMESTAMP}} on non-existing page
+!! options
+parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true }
+showflags
+!! wikitext
+{{REVISIONTIMESTAMP}}
+!! html/*
+<p>123
+</p>
+flags=vary-revision-timestamp
+!! end
+
+!! test
+Magic Word: {{REVISIONUSER}} on latest revision
+!! options
+lastsavedrevision
+parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true }
+showflags
+!! wikitext
+{{REVISIONUSER}}
+!! html/*
+<p>127.0.0.1
+</p>
+flags=vary-user
+!! end
+
+!! test
+Parser Function: {{REVISIONID:{{PAGENAME}}}} on latest revision
+!! options
+lastsavedrevision
+parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true }
+showflags
+!! wikitext
+{{REVISIONID:{{PAGENAME}}}}
+!! html/*
+<p>1337
+</p>
+flags=vary-revision-id
+!! end
+
+!! test
+Parser Function: {{REVISIONID:{{PAGENAME}}}} on non-saved revision
+!! options
+parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true }
+showflags
+!! wikitext
+{{REVISIONID:{{PAGENAME}}}}
+!! html/*
+
+flags=vary-revision-id
+!! end
+
+!! test
+Parser Function: {{REVISIONTIMESTAMP:{{PAGENAME}}}} on latest revision
+!! options
+lastsavedrevision
+parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true }
+showflags
+!! wikitext
+{{REVISIONTIMESTAMP:{{PAGENAME}}}}
+!! html/*
+<p>19700101000203
+</p>
+flags=vary-revision-timestamp
+!! end
+
+!! test
+Parser Function: {{REVISIONDAY:{{PAGENAME}}}} on latest revision
+!! options
+lastsavedrevision
+parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true }
+showflags
+!! wikitext
+{{REVISIONDAY:{{PAGENAME}}}}
+!! html/*
+<p>1
+</p>
+flags=vary-revision-timestamp
+!! end
+
+!! test
+Parser Function: {{REVISIONMONTH:{{PAGENAME}}}} on latest revision
+!! options
+lastsavedrevision
+parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true }
+showflags
+!! wikitext
+{{REVISIONMONTH:{{PAGENAME}}}}
+!! html/*
+<p>01
+</p>
+flags=vary-revision-timestamp
+!! end
+
+!! test
+Parser Function: {{REVISIONYEAR:{{PAGENAME}}}} on latest revision
+!! options
+lastsavedrevision
+parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true }
+showflags
+!! wikitext
+{{REVISIONYEAR:{{PAGENAME}}}}
+!! html/*
+<p>1970
+</p>
+flags=vary-revision-timestamp
+!! end
+
+!! test
+Parser Function: {{PAGESIZE:{{PAGENAME}}}} on latest revision
+!! options
+lastsavedrevision
+parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true }
+showflags
+!! wikitext
+{{PAGESIZE:{{PAGENAME}}}}
+!! html/*
+<p>25
+</p>
+flags=vary-revision-sha1
+!! end
+
 !! test
 Magic Word: {{SCRIPTPATH}}
 !! options
index 03b35b5..5b015b3 100644 (file)
@@ -1651,9 +1651,8 @@ class PermissionManagerTest extends MediaWikiLangTestCase {
 
        /**
         * @covers \MediaWiki\Permissions\PermissionManager::addTemporaryUserRights
-        * @covers \MediaWiki\Permissions\PermissionManager::revokeTemporaryUserRights
         */
-       public function testTemporaryUserRights() {
+       public function testAddTemporaryUserRights() {
                $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
                $this->overrideUserPermissions( $this->user, [ 'read', 'edit' ] );
                // sanity checks
index 9884987..da532b0 100644 (file)
@@ -150,11 +150,14 @@ class BagOStuffTest extends MediaWikiTestCase {
                $this->assertFalse( $this->cache->get( $key2 ) );
                $this->assertFalse( $this->cache->get( $key3 ) );
 
-               $this->cache->setMulti( [
-                       $key1 => 1,
-                       $key2 => 2,
-                       $key3 => 3
-               ] );
+               $ok = $this->cache->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
+
+               $this->assertTrue( $ok, "setMulti() succeeded" );
+               $this->assertEquals(
+                       3,
+                       count( $this->cache->getMulti( [ $key1, $key2, $key3 ] ) ),
+                       "setMulti() succeeded via getMulti() check"
+               );
 
                $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], 300 );
                $this->assertTrue( $ok, "TTL bumped for all keys" );