Expose external link additions and deletions
authorBaha <bmansurov@wikimedia.org>
Thu, 7 Feb 2019 16:10:09 +0000 (11:10 -0500)
committerBaha <bmansurov@wikimedia.org>
Thu, 7 Feb 2019 22:09:36 +0000 (17:09 -0500)
Needed for links event stream.

Bug: T214706
Change-Id: Ie93c8d90c2f69d7a08b8a6e9a0a5bb8266d4dd51

RELEASE-NOTES-1.33
includes/deferred/LinksUpdate.php
tests/phpunit/includes/deferred/LinksUpdateTest.php

index 8105446..b21a65a 100644 (file)
@@ -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 ===
 
index 937196d..7c7cabd 100644 (file)
@@ -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.
index 90a5ed1..cd3ddfa 100644 (file)
@@ -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() );
        }
 
        /**