From b6afa0bd1b0fc305f6f34698ab66b1575187ed30 Mon Sep 17 00:00:00 2001 From: Adam Wight Date: Thu, 22 Aug 2019 15:11:11 +0200 Subject: [PATCH] Include change tags in revision import structure This makes it possible to import change tags, which will be leveraged by Extension:FileImporter. Bug: T227849 Change-Id: I70a8df2b2be0ec11806eb8d798115c52683cc787 --- includes/import/ImportableOldRevision.php | 6 ++ .../import/ImportableOldRevisionImporter.php | 5 ++ includes/import/WikiRevision.php | 22 ++++++++ .../ImportableOldRevisionImporterTest.php | 56 +++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 tests/phpunit/includes/import/ImportableOldRevisionImporterTest.php diff --git a/includes/import/ImportableOldRevision.php b/includes/import/ImportableOldRevision.php index 6d1e24264c..8cfb6052d1 100644 --- a/includes/import/ImportableOldRevision.php +++ b/includes/import/ImportableOldRevision.php @@ -65,4 +65,10 @@ interface ImportableOldRevision { */ public function getSha1Base36(); + /** + * @since 1.34 + * @return string[] + */ + public function getTags(); + } diff --git a/includes/import/ImportableOldRevisionImporter.php b/includes/import/ImportableOldRevisionImporter.php index ad62e163f0..821d6f6d37 100644 --- a/includes/import/ImportableOldRevisionImporter.php +++ b/includes/import/ImportableOldRevisionImporter.php @@ -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 diff --git a/includes/import/WikiRevision.php b/includes/import/WikiRevision.php index e36d673499..c29ba2144f 100644 --- a/includes/import/WikiRevision.php +++ b/includes/import/WikiRevision.php @@ -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 index 0000000000..a68ac83d2f --- /dev/null +++ b/tests/phpunit/includes/import/ImportableOldRevisionImporterTest.php @@ -0,0 +1,56 @@ +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 ); + } +} -- 2.20.1