Merge "Show current content model on Special:ChangeContentModel"
[lhc/web/wiklou.git] / includes / Revision / RevisionRenderer.php
index a63e4f1..ca4bb73 100644 (file)
@@ -54,22 +54,21 @@ class RevisionRenderer {
        private $roleRegistery;
 
        /** @var string|bool */
-       private $wikiId;
+       private $dbDomain;
 
        /**
         * @param ILoadBalancer $loadBalancer
         * @param SlotRoleRegistry $roleRegistry
-        * @param bool|string $wikiId
+        * @param bool|string $dbDomain DB domain of the relevant wiki or false for the current one
         */
        public function __construct(
                ILoadBalancer $loadBalancer,
                SlotRoleRegistry $roleRegistry,
-               $wikiId = false
+               $dbDomain = false
        ) {
                $this->loadBalancer = $loadBalancer;
                $this->roleRegistery = $roleRegistry;
-               $this->wikiId = $wikiId;
-
+               $this->dbDomain = $dbDomain;
                $this->saveParseLogger = new NullLogger();
        }
 
@@ -105,7 +104,7 @@ class RevisionRenderer {
                User $forUser = null,
                array $hints = []
        ) {
-               if ( $rev->getWikiId() !== $this->wikiId ) {
+               if ( $rev->getWikiId() !== $this->dbDomain ) {
                        throw new InvalidArgumentException( 'Mismatching wiki ID ' . $rev->getWikiId() );
                }
 
@@ -131,6 +130,9 @@ class RevisionRenderer {
                $options->setSpeculativeRevIdCallback( function () use ( $dbIndex ) {
                        return $this->getSpeculativeRevId( $dbIndex );
                } );
+               $options->setSpeculativePageIdCallback( function () use ( $dbIndex ) {
+                       return $this->getSpeculativePageId( $dbIndex );
+               } );
 
                if ( !$rev->getId() && $rev->getTimestamp() ) {
                        // This is an unsaved revision with an already determined timestamp.
@@ -167,9 +169,10 @@ class RevisionRenderer {
                // HACK: But don't use a fresh connection in unit tests, since it would not have
                // the fake tables. This should be handled by the LoadBalancer!
                $flags = defined( 'MW_PHPUNIT_TEST' ) || $dbIndex === DB_REPLICA
-                       ? 0 : ILoadBalancer::CONN_TRX_AUTOCOMMIT;
+                       ? 0
+                       : ILoadBalancer::CONN_TRX_AUTOCOMMIT;
 
-               $db = $this->loadBalancer->getConnectionRef( $dbIndex, [], $this->wikiId, $flags );
+               $db = $this->loadBalancer->getConnectionRef( $dbIndex, [], $this->dbDomain, $flags );
 
                return 1 + (int)$db->selectField(
                        'revision',
@@ -179,6 +182,25 @@ class RevisionRenderer {
                );
        }
 
+       private function getSpeculativePageId( $dbIndex ) {
+               // Use a fresh master connection in order to see the latest data, by avoiding
+               // stale data from REPEATABLE-READ snapshots.
+               // HACK: But don't use a fresh connection in unit tests, since it would not have
+               // the fake tables. This should be handled by the LoadBalancer!
+               $flags = defined( 'MW_PHPUNIT_TEST' ) || $dbIndex === DB_REPLICA
+                       ? 0
+                       : ILoadBalancer::CONN_TRX_AUTOCOMMIT;
+
+               $db = $this->loadBalancer->getConnectionRef( $dbIndex, [], $this->dbDomain, $flags );
+
+               return 1 + (int)$db->selectField(
+                       'page',
+                       'MAX(page_id)',
+                       [],
+                       __METHOD__
+               );
+       }
+
        /**
         * This implements the layout for combining the output of multiple slots.
         *
@@ -216,7 +238,7 @@ class RevisionRenderer {
                        $slotOutput[$role] = $out;
 
                        // XXX: should the SlotRoleHandler be able to intervene here?
-                       $combinedOutput->mergeInternalMetaDataFrom( $out, $role );
+                       $combinedOutput->mergeInternalMetaDataFrom( $out );
                        $combinedOutput->mergeTrackingMetaDataFrom( $out );
                }