Merge "Pass message arguments as array to ErrorPageError"
[lhc/web/wiklou.git] / includes / Revision / RevisionStore.php
index ec1c08c..9e8dfe7 100644 (file)
@@ -276,17 +276,6 @@ class RevisionStore
                return $this->loadBalancer;
        }
 
-       /**
-        * @param int $mode DB_MASTER or DB_REPLICA
-        * @param array $groups
-        *
-        * @return IDatabase
-        */
-       private function getDBConnection( $mode, $groups = [] ) {
-               $lb = $this->getDBLoadBalancer();
-               return $lb->getConnection( $mode, $groups, $this->dbDomain );
-       }
-
        /**
         * @param int $queryFlags a bit field composed of READ_XXX flags
         *
@@ -297,22 +286,15 @@ class RevisionStore
                return $this->getDBConnectionRef( $mode );
        }
 
-       /**
-        * @param IDatabase $connection
-        */
-       private function releaseDBConnection( IDatabase $connection ) {
-               $lb = $this->getDBLoadBalancer();
-               $lb->reuseConnection( $connection );
-       }
-
        /**
         * @param int $mode DB_MASTER or DB_REPLICA
         *
+        * @param array $groups
         * @return DBConnRef
         */
-       private function getDBConnectionRef( $mode ) {
+       private function getDBConnectionRef( $mode, $groups = [] ) {
                $lb = $this->getDBLoadBalancer();
-               return $lb->getConnectionRef( $mode, [], $this->dbDomain );
+               return $lb->getConnectionRef( $mode, $groups, $this->dbDomain );
        }
 
        /**
@@ -445,7 +427,7 @@ class RevisionStore
         */
        public function insertRevisionOn( RevisionRecord $rev, IDatabase $dbw ) {
                // TODO: pass in a DBTransactionContext instead of a database connection.
-               $this->checkDatabaseWikiId( $dbw );
+               $this->checkDatabaseDomain( $dbw );
 
                $slotRoles = $rev->getSlotRoles();
 
@@ -1073,7 +1055,7 @@ class RevisionStore
                $minor,
                User $user
        ) {
-               $this->checkDatabaseWikiId( $dbw );
+               $this->checkDatabaseDomain( $dbw );
 
                $pageId = $title->getArticleID();
 
@@ -1154,7 +1136,7 @@ class RevisionStore
         */
        public function getRecentChange( RevisionRecord $rev, $flags = 0 ) {
                list( $dbType, ) = DBAccessObjectUtils::getDBOptions( $flags );
-               $db = $this->getDBConnection( $dbType );
+               $db = $this->getDBConnectionRef( $dbType );
 
                $userIdentity = $rev->getUser( RevisionRecord::RAW );
 
@@ -1176,8 +1158,6 @@ class RevisionStore
                        $dbType
                );
 
-               $this->releaseDBConnection( $db );
-
                // XXX: cache this locally? Glue it to the RevisionRecord?
                return $rc;
        }
@@ -1593,7 +1573,7 @@ class RevisionStore
         * @return RevisionRecord|null
         */
        public function getRevisionByTimestamp( $title, $timestamp ) {
-               $db = $this->getDBConnection( DB_REPLICA );
+               $db = $this->getDBConnectionRef( DB_REPLICA );
                return $this->newRevisionFromConds(
                        [
                                'rev_timestamp' => $db->timestamp( $timestamp ),
@@ -2203,9 +2183,8 @@ class RevisionStore
                        && $lb->hasOrMadeRecentMasterChanges()
                ) {
                        $flags = self::READ_LATEST;
-                       $dbw = $this->getDBConnection( DB_MASTER );
+                       $dbw = $this->getDBConnectionRef( DB_MASTER );
                        $rev = $this->loadRevisionFromConds( $dbw, $conditions, $flags, $title );
-                       $this->releaseDBConnection( $dbw );
                }
 
                return $rev;
@@ -2247,32 +2226,14 @@ class RevisionStore
         * @param IDatabase $db
         * @throws MWException
         */
-       private function checkDatabaseWikiId( IDatabase $db ) {
-               $storeWiki = $this->dbDomain;
-               $dbWiki = $db->getDomainID();
-
-               if ( $dbWiki === $storeWiki ) {
-                       return;
-               }
-
-               $storeWiki = $storeWiki ?: $this->loadBalancer->getLocalDomainID();
-               // @FIXME: when would getDomainID() be false here?
-               $dbWiki = $dbWiki ?: wfWikiID();
-
-               if ( $dbWiki === $storeWiki ) {
-                       return;
-               }
-
-               // HACK: counteract encoding imposed by DatabaseDomain
-               $storeWiki = str_replace( '?h', '-', $storeWiki );
-               $dbWiki = str_replace( '?h', '-', $dbWiki );
-
-               if ( $dbWiki === $storeWiki ) {
+       private function checkDatabaseDomain( IDatabase $db ) {
+               $dbDomain = $db->getDomainID();
+               $storeDomain = $this->loadBalancer->resolveDomainID( $this->dbDomain );
+               if ( $dbDomain === $storeDomain ) {
                        return;
                }
 
-               throw new MWException( "RevisionStore for $storeWiki "
-                       . "cannot be used with a DB connection for $dbWiki" );
+               throw new MWException( "DB connection domain '$dbDomain' does not match '$storeDomain'" );
        }
 
        /**
@@ -2288,7 +2249,7 @@ class RevisionStore
         * @return object|false data row as a raw object
         */
        private function fetchRevisionRowFromConds( IDatabase $db, $conditions, $flags = 0 ) {
-               $this->checkDatabaseWikiId( $db );
+               $this->checkDatabaseDomain( $db );
 
                $revQuery = $this->getQueryInfo( [ 'page', 'user' ] );
                $options = [];
@@ -2592,7 +2553,7 @@ class RevisionStore
         *         of the corresponding revision.
         */
        public function getRevisionSizes( array $revIds ) {
-               return $this->listRevisionSizes( $this->getDBConnection( DB_REPLICA ), $revIds );
+               return $this->listRevisionSizes( $this->getDBConnectionRef( DB_REPLICA ), $revIds );
        }
 
        /**
@@ -2608,7 +2569,7 @@ class RevisionStore
         *         of the corresponding revision.
         */
        public function listRevisionSizes( IDatabase $db, array $revIds ) {
-               $this->checkDatabaseWikiId( $db );
+               $this->checkDatabaseDomain( $db );
 
                $revLens = [];
                if ( !$revIds ) {
@@ -2652,7 +2613,7 @@ class RevisionStore
                }
 
                list( $dbType, ) = DBAccessObjectUtils::getDBOptions( $flags );
-               $db = $this->getDBConnection( $dbType, [ 'contributions' ] );
+               $db = $this->getDBConnectionRef( $dbType, [ 'contributions' ] );
 
                $ts = $this->getTimestampFromId( $rev->getId(), $flags );
                if ( $ts === false ) {
@@ -2745,7 +2706,7 @@ class RevisionStore
         * @return int
         */
        private function getPreviousRevisionId( IDatabase $db, RevisionRecord $rev ) {
-               $this->checkDatabaseWikiId( $db );
+               $this->checkDatabaseDomain( $db );
 
                if ( $rev->getPageId() === null ) {
                        return 0;
@@ -2804,7 +2765,7 @@ class RevisionStore
         * @return int
         */
        public function countRevisionsByPageId( IDatabase $db, $id ) {
-               $this->checkDatabaseWikiId( $db );
+               $this->checkDatabaseDomain( $db );
 
                $row = $db->selectRow( 'revision',
                        [ 'revCount' => 'COUNT(*)' ],
@@ -2853,7 +2814,7 @@ class RevisionStore
         * @return bool True if the given user was the only one to edit since the given timestamp
         */
        public function userWasLastToEdit( IDatabase $db, $pageId, $userId, $since ) {
-               $this->checkDatabaseWikiId( $db );
+               $this->checkDatabaseDomain( $db );
 
                if ( !$userId ) {
                        return false;