Daniel Kinzler has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/215660

Change subject: Track usages on ParserCacheSaveComplete
......................................................................

Track usages on ParserCacheSaveComplete

Bug: T99512
Change-Id: I4290c22201a792d269308b7005e259d7bb2b2d44
---
M client/WikibaseClient.php
M client/includes/Hooks/DataUpdateHookHandlers.php
M client/tests/phpunit/includes/Hooks/DataUpdateHookHandlersTest.php
3 files changed, 88 insertions(+), 17 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/60/215660/1

diff --git a/client/WikibaseClient.php b/client/WikibaseClient.php
index 467a7f0..4c48108 100644
--- a/client/WikibaseClient.php
+++ b/client/WikibaseClient.php
@@ -96,6 +96,7 @@
        $wgHooks['SidebarBeforeOutput'][] = 
'\Wikibase\Client\Hooks\SidebarHookHandlers::onSidebarBeforeOutput';
        $wgHooks['LinksUpdateComplete'][]                               = 
'\Wikibase\Client\Hooks\DataUpdateHookHandlers::onLinksUpdateComplete';
        $wgHooks['ArticleDeleteComplete'][]                             = 
'\Wikibase\Client\Hooks\DataUpdateHookHandlers::onArticleDeleteComplete';
+       $wgHooks['ParserCacheSaveComplete'][]                           = 
'\Wikibase\Client\Hooks\DataUpdateHookHandlers::onParserCacheSaveComplete';
        $wgHooks['ParserFirstCallInit'][]                       = 
'\Wikibase\ClientHooks::onParserFirstCallInit';
        $wgHooks['MagicWordwgVariableIDs'][]                    = 
'\Wikibase\ClientHooks::onMagicWordwgVariableIDs';
        $wgHooks['ParserGetVariableValueSwitch'][]              = 
'\Wikibase\ClientHooks::onParserGetVariableValueSwitch';
diff --git a/client/includes/Hooks/DataUpdateHookHandlers.php 
b/client/includes/Hooks/DataUpdateHookHandlers.php
index 06998e7..fffabc0 100644
--- a/client/includes/Hooks/DataUpdateHookHandlers.php
+++ b/client/includes/Hooks/DataUpdateHookHandlers.php
@@ -5,6 +5,10 @@
 use Content;
 use LinksUpdate;
 use ManualLogEntry;
+use ParserCache;
+use ParserOptions;
+use ParserOutput;
+use Title;
 use User;
 use Wikibase\Client\Store\UsageUpdater;
 use Wikibase\Client\Usage\ParserOutputUsageAccumulator;
@@ -69,8 +73,6 @@
         * @param int $id id of the article that was deleted
         * @param Content $content
         * @param ManualLogEntry $logEntry
-        *
-        * @return bool
         */
        public static function onArticleDeleteComplete(
                WikiPage &$article,
@@ -86,6 +88,30 @@
                $handler->doArticleDeleteComplete( $title->getNamespace(), $id, 
$logEntry->getTimestamp() );
        }
 
+       /**
+        * Static handler for ParserCacheSaveComplete
+        *
+        * @see 
https://www.mediawiki.org/wiki/Manual:Hooks/ParserCacheSaveComplete
+        * @see doParserCacheSaveComplete
+        *
+        *
+        * @param ParserCache $parserCache
+        * @param ParserOutput $pout
+        * @param Title $title
+        * @param ParserOptions $pops
+        * @param int $revId
+        */
+       public static function onParserCacheSaveComplete(
+               ParserCache $parserCache,
+               ParserOutput $pout,
+               Title $title,
+               ParserOptions $pops,
+               $revId
+       ) {
+               $handler = self::newFromGlobalState();
+               $handler->doParserCacheSaveComplete( $pout, $title );
+       }
+
        public function __construct(
                UsageUpdater $usageUpdater
        ) {
@@ -93,7 +119,7 @@
        }
 
        /**
-        * Hook run after a new revision was stored.
+        * Triggered when a page gets re-rendered to update secondary link 
tables.
         * Implemented to update usage tracking information via UsageUpdater.
         *
         * @param LinksUpdate $linksUpdate
@@ -108,14 +134,14 @@
                // These timestamps should usually be the same, but asking 
$title may cause a database query.
                $touched = $parserOutput->getTimestamp() ?: 
$title->getTouched();
 
-               // Add or touch any usages present in the new revision
+               // Add or touch any usages present in the rendering
                $this->usageUpdater->addUsagesForPage(
                        $title->getArticleId(),
                        $usageAcc->getUsages(),
                        $touched
                );
 
-               // Prune any usages older than the new revision's timestamp.
+               // Prune any usages older than the new rendering's timestamp.
                // NOTE: only prune after adding the new updates, to avoid 
unsubscribing and then
                // immediately re-subscribing to the used entities.
                $this->usageUpdater->pruneUsagesForPage(
@@ -125,7 +151,30 @@
        }
 
        /**
-        * Hook run after a page was deleted.
+        * Triggered when a new rendering of a page is committed to the 
ParserCache.
+        * Implemented to update usage tracking information via UsageUpdater.
+        *
+        * @param ParserOutput $parserOutput
+        * @param $title $title
+        */
+       public function doParserCacheSaveComplete( ParserOutput $parserOutput, 
Title $title ) {
+               $usageAcc = new ParserOutputUsageAccumulator( $parserOutput );
+
+               // The parser output should tell us when it was parsed. If not, 
ask the Title object.
+               // These timestamps should usually be the same, but asking 
$title may cause a database query.
+               $touched = $parserOutput->getTimestamp() ?: 
$title->getTouched();
+
+               // Add or touch any usages present in the new rendering.
+               // This allows us to track usages in each user language 
separately, for multilingual sites.
+               $this->usageUpdater->addUsagesForPage(
+                       $title->getArticleId(),
+                       $usageAcc->getUsages(),
+                       $touched
+               );
+       }
+
+       /**
+        * Triggered after a page was deleted.
         * Implemented to prune usage tracking information via UsageUpdater.
         *
         * @param int $namespace
diff --git a/client/tests/phpunit/includes/Hooks/DataUpdateHookHandlersTest.php 
b/client/tests/phpunit/includes/Hooks/DataUpdateHookHandlersTest.php
index 615fde9..7a25f06 100644
--- a/client/tests/phpunit/includes/Hooks/DataUpdateHookHandlersTest.php
+++ b/client/tests/phpunit/includes/Hooks/DataUpdateHookHandlersTest.php
@@ -28,10 +28,11 @@
         * @param Title $title
         * @param EntityUsage[]|null $expectedUsages
         * @param string|null $touched
+        * @param bool $prune
         *
         * @return UsageUpdater
         */
-       private function newUsageUpdater( Title $title, array $expectedUsages = 
null, $touched = null ) {
+       private function newUsageUpdater( Title $title, array $expectedUsages = 
null, $touched = null, $prune = true ) {
                $usageUpdater = $this->getMockBuilder( 
'Wikibase\Client\Store\UsageUpdater' )
                        ->disableOriginalConstructor()
                        ->getMock();
@@ -49,9 +50,14 @@
                                ->with( $title->getArticleID(), 
$expectedUsages, $touched );
                }
 
-               $usageUpdater->expects( $this->once() )
-                       ->method( 'pruneUsagesForPage' )
-                       ->with( $title->getArticleID(), $touched );
+               if ( $prune ) {
+                       $usageUpdater->expects( $this->once() )
+                               ->method( 'pruneUsagesForPage' )
+                               ->with( $title->getArticleID(), $touched );
+               } else {
+                       $usageUpdater->expects( $this->never() )
+                               ->method( 'pruneUsagesForPage' );
+               }
 
                return $usageUpdater;
        }
@@ -60,11 +66,12 @@
         * @param Title $title
         * @param EntityUsage[]|null $expectedUsages
         * @param string|null $touched timestamp
+        * @param bool $prune
         *
         * @return DataUpdateHookHandlers
         */
-       private function newDataUpdateHookHandlers( Title $title, array 
$expectedUsages = null, $touched = null ) {
-               $usageUpdater = $this->newUsageUpdater( $title, 
$expectedUsages, $touched );
+       private function newDataUpdateHookHandlers( Title $title, array 
$expectedUsages = null, $touched = null, $prune = true ) {
+               $usageUpdater = $this->newUsageUpdater( $title, 
$expectedUsages, $touched, $prune );
 
                return new DataUpdateHookHandlers(
                        $usageUpdater
@@ -123,7 +130,7 @@
                $this->assertInstanceOf( 
'Wikibase\Client\Hooks\DataUpdateHookHandlers', $handler );
        }
 
-       public function provideDoArticleEditUpdates() {
+       public function provideLinksUpdateComplete() {
                return array(
                        'usage' => array(
                                Title::makeTitle( NS_MAIN, 'Oxygen' ),
@@ -142,17 +149,31 @@
        }
 
        /**
-        * @dataProvider provideDoArticleEditUpdates
+        * @dataProvider provideLinksUpdateComplete
         */
-       public function testDoArticleEditUpdates( Title $title, $usage ) {
+       public function testLinksUpdateComplete( Title $title, $usage ) {
                $title->resetArticleID( 23 );
                $timestamp = '20150505000000';
 
                $linksUpdate = $this->newLinksUpdate( $title, $usage, 
$timestamp );
 
                // Assertions are done by the UsageUpdater mock
-               $handler = $this->newDataUpdateHookHandlers( $title, $usage, 
$timestamp );
+               $handler = $this->newDataUpdateHookHandlers( $title, $usage, 
$timestamp, true );
                $handler->doLinksUpdateComplete( $linksUpdate );
+       }
+
+       /**
+        * @dataProvider provideLinksUpdateComplete
+        */
+       public function testDoParserCacheSaveComplete( Title $title, $usage ) {
+               $title->resetArticleID( 23 );
+               $timestamp = '20150505000000';
+
+               $parserOutput = $this->newParserOutput( $usage, $timestamp );
+
+               // Assertions are done by the UsageUpdater mock
+               $handler = $this->newDataUpdateHookHandlers( $title, $usage, 
$timestamp, false );
+               $handler->doParserCacheSaveComplete( $parserOutput, $title );
        }
 
        public function testDoArticleDeleteComplete() {
@@ -161,7 +182,7 @@
                $timestamp = '20150505000000';
 
                // Assertions are done by the UsageUpdater mock
-               $handler = $this->newDataUpdateHookHandlers( $title, null, 
$timestamp );
+               $handler = $this->newDataUpdateHookHandlers( $title, null, 
$timestamp, true );
                $handler->doArticleDeleteComplete( $title->getNamespace(), 
$title->getArticleID(), $timestamp );
        }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/215660
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4290c22201a792d269308b7005e259d7bb2b2d44
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to