Include change tags in revision import structure
authorAdam Wight <adam.wight@wikimedia.de>
Thu, 22 Aug 2019 13:11:11 +0000 (15:11 +0200)
committerAdam Wight <adam.wight@wikimedia.de>
Tue, 3 Sep 2019 19:14:16 +0000 (21:14 +0200)
This makes it possible to import change tags, which will be leveraged by
Extension:FileImporter.

Bug: T227849
Change-Id: I70a8df2b2be0ec11806eb8d798115c52683cc787

includes/import/ImportableOldRevision.php
includes/import/ImportableOldRevisionImporter.php
includes/import/WikiRevision.php
tests/phpunit/includes/import/ImportableOldRevisionImporterTest.php [new file with mode: 0644]

index 6d1e242..8cfb605 100644 (file)
@@ -65,4 +65,10 @@ interface ImportableOldRevision {
         */
        public function getSha1Base36();
 
+       /**
+        * @since 1.34
+        * @return string[]
+        */
+       public function getTags();
+
 }
index ad62e16..821d6f6 100644 (file)
@@ -129,6 +129,11 @@ class ImportableOldRevisionImporter implements OldRevisionImporter {
                $revision->insertOn( $dbw );
                $changed = $page->updateIfNewerOn( $dbw, $revision );
 
+               $tags = $importableRevision->getTags();
+               if ( $tags !== [] ) {
+                       ChangeTags::addTags( $tags, null, $revision->getId() );
+               }
+
                if ( $changed !== false && $this->doUpdates ) {
                        $this->logger->debug( __METHOD__ . ": running updates\n" );
                        // countable/oldcountable stuff is handled in WikiImporter::finishImportPage
index e36d673..c29ba21 100644 (file)
@@ -144,6 +144,12 @@ class WikiRevision implements ImportableUploadRevision, ImportableOldRevision {
         */
        public $sha1base36 = false;
 
+       /**
+        * @since 1.34
+        * @var string[]
+        */
+       protected $tags = [];
+
        /**
         * @since 1.17
         * @var string
@@ -310,6 +316,14 @@ class WikiRevision implements ImportableUploadRevision, ImportableOldRevision {
                $this->sha1base36 = $sha1base36;
        }
 
+       /**
+        * @since 1.34
+        * @param string[] $tags
+        */
+       public function setTags( array $tags ) {
+               $this->tags = $tags;
+       }
+
        /**
         * @since 1.12.2
         * @param string $filename
@@ -509,6 +523,14 @@ class WikiRevision implements ImportableUploadRevision, ImportableOldRevision {
                return false;
        }
 
+       /**
+        * @since 1.34
+        * @return string[]
+        */
+       public function getTags() {
+               return $this->tags;
+       }
+
        /**
         * @since 1.17
         * @return string
diff --git a/tests/phpunit/includes/import/ImportableOldRevisionImporterTest.php b/tests/phpunit/includes/import/ImportableOldRevisionImporterTest.php
new file mode 100644 (file)
index 0000000..a68ac83
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+
+use MediaWiki\MediaWikiServices;
+use Psr\Log\NullLogger;
+
+/**
+ * @group Database
+ * @coversDefaultClass ImportableOldRevisionImporter
+ */
+class ImportableOldRevisionImporterTest extends MediaWikiIntegrationTestCase {
+
+       public function setUp() {
+               parent::setUp();
+
+               $this->tablesUsed[] = 'change_tag';
+               $this->tablesUsed[] = 'change_tag_def';
+
+               ChangeTags::defineTag( 'tag1' );
+       }
+
+       public function provideTestCases() {
+               yield [ [] ];
+               yield [ [ "tag1" ] ];
+       }
+
+       /**
+        * @covers ::import
+        * @param $expectedTags
+        * @dataProvider provideTestCases
+        */
+       public function testImport( $expectedTags ) {
+               $services = MediaWikiServices::getInstance();
+
+               $title = Title::newFromText( __CLASS__ . rand() );
+               $revision = new WikiRevision( $services->getMainConfig() );
+               $revision->setTitle( $title );
+               $revision->setTags( $expectedTags );
+               $revision->setText( "dummy edit" );
+
+               $importer = new ImportableOldRevisionImporter(
+                       true,
+                       new NullLogger(),
+                       $services->getDBLoadBalancer()
+               );
+               $result = $importer->import( $revision );
+               $this->assertTrue( $result );
+
+               $page = WikiPage::factory( $title );
+               $tags = ChangeTags::getTags(
+                       $services->getDBLoadBalancer()->getConnection( DB_MASTER ),
+                       null,
+                       $page->getLatest()
+               );
+               $this->assertSame( $expectedTags, $tags );
+       }
+}