Merge "Add SPARQL client to core"
[lhc/web/wiklou.git] / includes / Storage / RevisionStore.php
index 79ecec6..d832104 100644 (file)
@@ -42,6 +42,9 @@ use MediaWiki\User\UserIdentityValue;
 use Message;
 use MWException;
 use MWUnknownContentModelException;
+use Psr\Log\LoggerAwareInterface;
+use Psr\Log\LoggerInterface;
+use Psr\Log\NullLogger;
 use RecentChange;
 use stdClass;
 use Title;
@@ -61,7 +64,8 @@ use Wikimedia\Rdbms\LoadBalancer;
  * @note This was written to act as a drop-in replacement for the corresponding
  *       static methods in Revision.
  */
-class RevisionStore implements IDBAccessObject, RevisionFactory, RevisionLookup {
+class RevisionStore
+       implements IDBAccessObject, RevisionFactory, RevisionLookup, LoggerAwareInterface {
 
        /**
         * @var SqlBlobStore
@@ -88,18 +92,30 @@ class RevisionStore implements IDBAccessObject, RevisionFactory, RevisionLookup
         */
        private $cache;
 
+       /**
+        * @var CommentStore
+        */
+       private $commentStore;
+
+       /**
+        * @var LoggerInterface
+        */
+       private $logger;
+
        /**
         * @todo $blobStore should be allowed to be any BlobStore!
         *
         * @param LoadBalancer $loadBalancer
         * @param SqlBlobStore $blobStore
         * @param WANObjectCache $cache
+        * @param CommentStore $commentStore
         * @param bool|string $wikiId
         */
        public function __construct(
                LoadBalancer $loadBalancer,
                SqlBlobStore $blobStore,
                WANObjectCache $cache,
+               CommentStore $commentStore,
                $wikiId = false
        ) {
                Assert::parameterType( 'string|boolean', $wikiId, '$wikiId' );
@@ -107,7 +123,13 @@ class RevisionStore implements IDBAccessObject, RevisionFactory, RevisionLookup
                $this->loadBalancer = $loadBalancer;
                $this->blobStore = $blobStore;
                $this->cache = $cache;
+               $this->commentStore = $commentStore;
                $this->wikiId = $wikiId;
+               $this->logger = new NullLogger();
+       }
+
+       public function setLogger( LoggerInterface $logger ) {
+               $this->logger = $logger;
        }
 
        /**
@@ -173,23 +195,34 @@ class RevisionStore implements IDBAccessObject, RevisionFactory, RevisionLookup
         * @return Title
         * @throws RevisionAccessException
         */
-       public function getTitle( $pageId, $revId, $queryFlags = 0 ) {
+       public function getTitle( $pageId, $revId, $queryFlags = self::READ_NORMAL ) {
                if ( !$pageId && !$revId ) {
                        throw new InvalidArgumentException( '$pageId and $revId cannot both be 0 or null' );
                }
 
-               list( $dbMode, $dbOptions, , ) = DBAccessObjectUtils::getDBOptions( $queryFlags );
-               $titleFlags = $dbMode == DB_MASTER ? Title::GAID_FOR_UPDATE : 0;
-               $title = null;
+               // This method recalls itself with READ_LATEST if READ_NORMAL doesn't get us a Title
+               // So ignore READ_LATEST_IMMUTABLE flags and handle the fallback logic in this method
+               if ( DBAccessObjectUtils::hasFlags( $queryFlags, self::READ_LATEST_IMMUTABLE ) ) {
+                       $queryFlags = self::READ_NORMAL;
+               }
+
+               $canUseTitleNewFromId = ( $pageId !== null && $pageId > 0 && $this->wikiId === false );
+               list( $dbMode, $dbOptions ) = DBAccessObjectUtils::getDBOptions( $queryFlags );
+               $titleFlags = ( $dbMode == DB_MASTER ? Title::GAID_FOR_UPDATE : 0 );
 
                // Loading by ID is best, but Title::newFromID does not support that for foreign IDs.
-               if ( $pageId !== null && $pageId > 0 && $this->wikiId === false ) {
+               if ( $canUseTitleNewFromId ) {
                        // TODO: better foreign title handling (introduce TitleFactory)
                        $title = Title::newFromID( $pageId, $titleFlags );
+                       if ( $title ) {
+                               return $title;
+                       }
                }
 
                // rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
-               if ( !$title && $revId !== null && $revId > 0 ) {
+               $canUseRevId = ( $revId !== null && $revId > 0 );
+
+               if ( $canUseRevId ) {
                        $dbr = $this->getDBConnectionRef( $dbMode );
                        // @todo: Title::getSelectFields(), or Title::getQueryInfo(), or something like that
                        $row = $dbr->selectRow(
@@ -209,17 +242,25 @@ class RevisionStore implements IDBAccessObject, RevisionFactory, RevisionLookup
                        );
                        if ( $row ) {
                                // TODO: better foreign title handling (introduce TitleFactory)
-                               $title = Title::newFromRow( $row );
+                               return Title::newFromRow( $row );
                        }
                }
 
-               if ( !$title ) {
-                       throw new RevisionAccessException(
-                               "Could not determine title for page ID $pageId and revision ID $revId"
-                       );
+               // If we still don't have a title, fallback to master if that wasn't already happening.
+               if ( $dbMode !== DB_MASTER ) {
+                       $title = $this->getTitle( $pageId, $revId, self::READ_LATEST );
+                       if ( $title ) {
+                               $this->logger->info(
+                                       __METHOD__ . ' fell back to READ_LATEST and got a Title.',
+                                       [ 'trace' => wfDebugBacktrace() ]
+                               );
+                               return $title;
+                       }
                }
 
-               return $title;
+               throw new RevisionAccessException(
+                       "Could not determine title for page ID $pageId and revision ID $revId"
+               );
        }
 
        /**
@@ -257,8 +298,8 @@ class RevisionStore implements IDBAccessObject, RevisionFactory, RevisionLookup
        }
 
        /**
-        * Insert a new revision into the database, returning the new revision ID
-        * number on success and dies horribly on failure.
+        * Insert a new revision into the database, returning the new revision record
+        * on success and dies horribly on failure.
         *
         * MCR migration note: this replaces Revision::insertOn
         *
@@ -360,7 +401,7 @@ class RevisionStore implements IDBAccessObject, RevisionFactory, RevisionLookup
                }
 
                list( $commentFields, $commentCallback ) =
-                       CommentStore::newKey( 'rev_comment' )->insertWithTempTable( $dbw, $comment );
+                       $this->commentStore->insertWithTempTable( $dbw, 'rev_comment', $comment );
                $row += $commentFields;
 
                if ( $this->contentHandlerUseDB ) {
@@ -1036,9 +1077,9 @@ class RevisionStore implements IDBAccessObject, RevisionFactory, RevisionLookup
 
                $user = $this->getUserIdentityFromRowObject( $row, 'ar_' );
 
-               $comment = CommentStore::newKey( 'ar_comment' )
+               $comment = $this->commentStore
                        // Legacy because $row may have come from self::selectFields()
-                       ->getCommentLegacy( $this->getDBConnection( DB_REPLICA ), $row, true );
+                       ->getCommentLegacy( $this->getDBConnection( DB_REPLICA ), 'ar_comment', $row, true );
 
                $mainSlot = $this->emulateMainSlot_1_29( $row, $queryFlags, $title );
                $slots = new RevisionSlots( [ 'main' => $mainSlot ] );
@@ -1106,9 +1147,9 @@ class RevisionStore implements IDBAccessObject, RevisionFactory, RevisionLookup
 
                $user = $this->getUserIdentityFromRowObject( $row );
 
-               $comment = CommentStore::newKey( 'rev_comment' )
+               $comment = $this->commentStore
                        // Legacy because $row may have come from self::selectFields()
-                       ->getCommentLegacy( $this->getDBConnection( DB_REPLICA ), $row, true );
+                       ->getCommentLegacy( $this->getDBConnection( DB_REPLICA ), 'rev_comment', $row, true );
 
                $mainSlot = $this->emulateMainSlot_1_29( $row, $queryFlags, $title );
                $slots = new RevisionSlots( [ 'main' => $mainSlot ] );
@@ -1581,7 +1622,7 @@ class RevisionStore implements IDBAccessObject, RevisionFactory, RevisionLookup
                        'rev_sha1',
                ] );
 
-               $commentQuery = CommentStore::newKey( 'rev_comment' )->getJoin();
+               $commentQuery = $this->commentStore->getJoin( 'rev_comment' );
                $ret['tables'] = array_merge( $ret['tables'], $commentQuery['tables'] );
                $ret['fields'] = array_merge( $ret['fields'], $commentQuery['fields'] );
                $ret['joins'] = array_merge( $ret['joins'], $commentQuery['joins'] );
@@ -1638,7 +1679,7 @@ class RevisionStore implements IDBAccessObject, RevisionFactory, RevisionLookup
         *   - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
         */
        public function getArchiveQueryInfo() {
-               $commentQuery = CommentStore::newKey( 'ar_comment' )->getJoin();
+               $commentQuery = $this->commentStore->getJoin( 'ar_comment' );
                $ret = [
                        'tables' => [ 'archive' ] + $commentQuery['tables'],
                        'fields' => [