From 9ead461970970326cc417630b22a0ae7028c906f Mon Sep 17 00:00:00 2001 From: Baha Date: Thu, 7 Feb 2019 11:10:09 -0500 Subject: [PATCH] Expose external link additions and deletions Needed for links event stream. Bug: T214706 Change-Id: Ie93c8d90c2f69d7a08b8a6e9a0a5bb8266d4dd51 --- RELEASE-NOTES-1.33 | 2 + includes/deferred/LinksUpdate.php | 47 ++++++++++++++++++- .../includes/deferred/LinksUpdateTest.php | 29 +++++++++++- 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/RELEASE-NOTES-1.33 b/RELEASE-NOTES-1.33 index 8105446e9f..b21a65aab5 100644 --- a/RELEASE-NOTES-1.33 +++ b/RELEASE-NOTES-1.33 @@ -48,6 +48,8 @@ production. Content::getNativeData() for text-based content models. * (T210814) SVGs are now by default displayed in wiki language on image pages. +* (T214706) LinksUpdate::getAddedExternalLinks() and + LinksUpdate::getRemovedExternalLinks() were introduced. === External library changes in 1.33 === diff --git a/includes/deferred/LinksUpdate.php b/includes/deferred/LinksUpdate.php index 937196d943..7c7cabd2b8 100644 --- a/includes/deferred/LinksUpdate.php +++ b/includes/deferred/LinksUpdate.php @@ -84,6 +84,16 @@ class LinksUpdate extends DataUpdate implements EnqueueableDataUpdate { */ private $linkDeletions = null; + /** + * @var null|array Added external links if calculated. + */ + private $externalLinkInsertions = null; + + /** + * @var null|array Deleted external links if calculated. + */ + private $externalLinkDeletions = null; + /** * @var null|array Added properties if calculated. */ @@ -234,11 +244,14 @@ class LinksUpdate extends DataUpdate implements EnqueueableDataUpdate { # External links $existingEL = $this->getExistingExternals(); + $this->externalLinkDeletions = $this->getExternalDeletions( $existingEL ); + $this->externalLinkInsertions = $this->getExternalInsertions( + $existingEL ); $this->incrTableUpdate( 'externallinks', 'el', - $this->getExternalDeletions( $existingEL ), - $this->getExternalInsertions( $existingEL ) ); + $this->externalLinkDeletions, + $this->externalLinkInsertions ); # Language links $existingLL = $this->getExistingInterlangs(); @@ -1099,6 +1112,36 @@ class LinksUpdate extends DataUpdate implements EnqueueableDataUpdate { return $result; } + /** + * Fetch external links added by this LinksUpdate. Only available after + * the update is complete. + * @since 1.33 + * @return null|array Array of Strings + */ + public function getAddedExternalLinks() { + if ( $this->externalLinkInsertions === null ) { + return null; + } + $result = []; + foreach ( $this->externalLinkInsertions as $key => $value ) { + $result[] = $value['el_to']; + } + return $result; + } + + /** + * Fetch external links removed by this LinksUpdate. Only available after + * the update is complete. + * @since 1.33 + * @return null|array Array of Strings + */ + public function getRemovedExternalLinks() { + if ( $this->externalLinkDeletions === null ) { + return null; + } + return array_keys( $this->externalLinkDeletions ); + } + /** * Fetch page properties added by this LinksUpdate. * Only available after the update is complete. diff --git a/tests/phpunit/includes/deferred/LinksUpdateTest.php b/tests/phpunit/includes/deferred/LinksUpdateTest.php index 90a5ed17ff..cd3ddfa8e8 100644 --- a/tests/phpunit/includes/deferred/LinksUpdateTest.php +++ b/tests/phpunit/includes/deferred/LinksUpdateTest.php @@ -118,6 +118,8 @@ class LinksUpdateTest extends MediaWikiLangTestCase { /** * @covers ParserOutput::addExternalLink + * @covers LinksUpdate::getAddedExternalLinks + * @covers LinksUpdate::getRemovedExternalLinks */ public function testUpdate_externallinks() { /** @var ParserOutput $po */ @@ -125,7 +127,7 @@ class LinksUpdateTest extends MediaWikiLangTestCase { $po->addExternalLink( "http://testing.com/wiki/Foo" ); - $this->assertLinksUpdate( + $update = $this->assertLinksUpdate( $t, $po, 'externallinks', @@ -135,6 +137,31 @@ class LinksUpdateTest extends MediaWikiLangTestCase { [ 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ], ] ); + + $this->assertArrayEquals( [ + "http://testing.com/wiki/Foo" + ], $update->getAddedExternalLinks() ); + + $po = new ParserOutput(); + $po->setTitleText( $t->getPrefixedText() ); + $po->addExternalLink( 'http://testing.com/wiki/Bar' ); + $update = $this->assertLinksUpdate( + $t, + $po, + 'externallinks', + 'el_to, el_index', + 'el_from = ' . self::$testingPageId, + [ + [ 'http://testing.com/wiki/Bar', 'http://com.testing./wiki/Bar' ], + ] + ); + + $this->assertArrayEquals( [ + "http://testing.com/wiki/Bar" + ], $update->getAddedExternalLinks() ); + $this->assertArrayEquals( [ + "http://testing.com/wiki/Foo" + ], $update->getRemovedExternalLinks() ); } /** -- 2.20.1