Display SVGs in target language
[lhc/web/wiklou.git] / includes / Revision.php
index a55b1c4..6d1812a 100644 (file)
  * @file
  */
 
-use MediaWiki\Storage\MutableRevisionRecord;
-use MediaWiki\Storage\RevisionAccessException;
-use MediaWiki\Storage\RevisionFactory;
-use MediaWiki\Storage\RevisionLookup;
-use MediaWiki\Storage\RevisionRecord;
-use MediaWiki\Storage\RevisionStore;
-use MediaWiki\Storage\RevisionStoreRecord;
-use MediaWiki\Storage\SlotRecord;
+use MediaWiki\Revision\MutableRevisionRecord;
+use MediaWiki\Revision\RevisionAccessException;
+use MediaWiki\Revision\RevisionFactory;
+use MediaWiki\Revision\RevisionLookup;
+use MediaWiki\Revision\RevisionRecord;
+use MediaWiki\Revision\RevisionStore;
+use MediaWiki\Revision\RevisionStoreRecord;
+use MediaWiki\Revision\SlotRecord;
 use MediaWiki\Storage\SqlBlobStore;
 use Wikimedia\Rdbms\IDatabase;
 use MediaWiki\Linker\LinkTarget;
@@ -61,8 +61,13 @@ class Revision implements IDBAccessObject {
        /**
         * @return RevisionStore
         */
-       protected static function getRevisionStore() {
-               return MediaWikiServices::getInstance()->getRevisionStore();
+       protected static function getRevisionStore( $wiki = false ) {
+               if ( $wiki ) {
+                       return MediaWikiServices::getInstance()->getRevisionStoreFactory()
+                               ->getRevisionStore( $wiki );
+               } else {
+                       return MediaWikiServices::getInstance()->getRevisionStore();
+               }
        }
 
        /**
@@ -318,12 +323,13 @@ class Revision implements IDBAccessObject {
                global $wgActorTableSchemaMigrationStage;
 
                wfDeprecated( __METHOD__, '1.31' );
-               if ( $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH ) {
+               if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW ) {
                        // If code is using this instead of self::getQueryInfo(), there's
                        // no way the join it's trying to do can work once the old fields
-                       // aren't being written anymore.
+                       // aren't being used anymore.
                        throw new BadMethodCallException(
-                               'Cannot use ' . __METHOD__ . ' when $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH'
+                               'Cannot use ' . __METHOD__
+                                       . ' when $wgActorTableSchemaMigrationStage has SCHEMA_COMPAT_READ_NEW'
                        );
                }
 
@@ -352,13 +358,14 @@ class Revision implements IDBAccessObject {
                global $wgContentHandlerUseDB, $wgActorTableSchemaMigrationStage;
                global $wgMultiContentRevisionSchemaMigrationStage;
 
-               if ( $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH ) {
+               if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW ) {
                        // If code is using this instead of self::getQueryInfo(), there's a
                        // decent chance it's going to try to directly access
                        // $row->rev_user or $row->rev_user_text and we can't give it
-                       // useful values here once those aren't being written anymore.
+                       // useful values here once those aren't being used anymore.
                        throw new BadMethodCallException(
-                               'Cannot use ' . __METHOD__ . ' when $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH'
+                               'Cannot use ' . __METHOD__
+                                       . ' when $wgActorTableSchemaMigrationStage has SCHEMA_COMPAT_READ_NEW'
                        );
                }
 
@@ -411,13 +418,14 @@ class Revision implements IDBAccessObject {
                global $wgContentHandlerUseDB, $wgActorTableSchemaMigrationStage;
                global $wgMultiContentRevisionSchemaMigrationStage;
 
-               if ( $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH ) {
+               if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW ) {
                        // If code is using this instead of self::getQueryInfo(), there's a
                        // decent chance it's going to try to directly access
                        // $row->ar_user or $row->ar_user_text and we can't give it
-                       // useful values here once those aren't being written anymore.
+                       // useful values here once those aren't being used anymore.
                        throw new BadMethodCallException(
-                               'Cannot use ' . __METHOD__ . ' when $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH'
+                               'Cannot use ' . __METHOD__
+                                       . ' when $wgActorTableSchemaMigrationStage has SCHEMA_COMPAT_READ_NEW'
                        );
                }
 
@@ -1033,10 +1041,17 @@ class Revision implements IDBAccessObject {
        /**
         * Get revision text associated with an old or archive row
         *
-        * Both the flags and the text field must be included. Including the old_id
+        * If the text field is not included, this uses RevisionStore to load the appropriate slot
+        * and return its serialized content. This is the default backwards-compatibility behavior
+        * when reading from the MCR aware database schema is enabled. For this to work, either
+        * the revision ID or the page ID must be included in the row.
+        *
+        * When using the old text field, the flags field must also be set. Including the old_id
         * field will activate cache usage as long as the $wiki parameter is not set.
         *
-        * @param stdClass $row The text data
+        * @deprecated since 1.32, use RevisionStore::newRevisionFromRow instead.
+        *
+        * @param stdClass $row The text data. If a falsy value is passed instead, false is returned.
         * @param string $prefix Table prefix (default 'old_')
         * @param string|bool $wiki The name of the wiki to load the revision text from
         *   (same as the wiki $row was loaded from) or false to indicate the local
@@ -1045,19 +1060,51 @@ class Revision implements IDBAccessObject {
         * @return string|false Text the text requested or false on failure
         */
        public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
+               global $wgMultiContentRevisionSchemaMigrationStage;
+
+               if ( !$row ) {
+                       return false;
+               }
+
                $textField = $prefix . 'text';
                $flagsField = $prefix . 'flags';
 
-               if ( isset( $row->$flagsField ) ) {
-                       $flags = explode( ',', $row->$flagsField );
+               if ( isset( $row->$textField ) ) {
+                       if ( !( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) ) {
+                               // The text field was read, but it's no longer being populated!
+                               // We could gloss over this by using the text when it's there and loading
+                               // if when it's not, but it seems preferable to complain loudly about a
+                               // query that is no longer guaranteed to work reliably.
+                               throw new LogicException(
+                                       'Cannot use ' . __METHOD__ . ' with the ' . $textField . ' field when'
+                                       . ' $wgMultiContentRevisionSchemaMigrationStage does not include'
+                                       . ' SCHEMA_COMPAT_WRITE_OLD. The field may not be populated for all revisions!'
+                               );
+                       }
+
+                       $text = $row->$textField;
                } else {
-                       $flags = [];
+                       // Missing text field, we are probably looking at the MCR-enabled DB schema.
+
+                       if ( !( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) ) {
+                               // This method should no longer be used with the new schema. Ideally, we
+                               // would already trigger a deprecation warning when SCHEMA_COMPAT_READ_NEW is set.
+                               wfDeprecated( __METHOD__ . ' (MCR without SCHEMA_COMPAT_WRITE_OLD)', '1.32' );
+                       }
+
+                       $store = self::getRevisionStore( $wiki );
+                       $rev = $prefix === 'ar_'
+                               ? $store->newRevisionFromArchiveRow( $row )
+                               : $store->newRevisionFromRow( $row );
+
+                       $content = $rev->getContent( SlotRecord::MAIN );
+                       return $content ? $content->serialize() : false;
                }
 
-               if ( isset( $row->$textField ) ) {
-                       $text = $row->$textField;
+               if ( isset( $row->$flagsField ) ) {
+                       $flags = explode( ',', $row->$flagsField );
                } else {
-                       return false;
+                       $flags = [];
                }
 
                $cacheKey = isset( $row->old_id )
@@ -1179,7 +1226,7 @@ class Revision implements IDBAccessObject {
 
                $rec = self::getRevisionStore()->newNullRevision( $dbw, $title, $comment, $minor, $user );
 
-               return new Revision( $rec );
+               return $rec ? new Revision( $rec ) : null;
        }
 
        /**