Aude has uploaded a new change for review.

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

Change subject: Set root job for AddUsagesForPageJob
......................................................................

Set root job for AddUsagesForPageJob

This allows the jobs to be de-duplicated based on title and
parser language. (for multilingual wikis, parser cache may
be split by user language and otherwise uses content language)

Bug: T109448
Change-Id: Ic83365706888c01e94095abeff3c80c1ea015ab4
---
M client/includes/Hooks/DataUpdateHookHandlers.php
M client/includes/store/AddUsagesForPageJob.php
M client/tests/phpunit/includes/Hooks/DataUpdateHookHandlersTest.php
M client/tests/phpunit/includes/store/AddUsagesForPageJobTest.php
4 files changed, 98 insertions(+), 30 deletions(-)


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

diff --git a/client/includes/Hooks/DataUpdateHookHandlers.php 
b/client/includes/Hooks/DataUpdateHookHandlers.php
index 6776f65..61c255e 100644
--- a/client/includes/Hooks/DataUpdateHookHandlers.php
+++ b/client/includes/Hooks/DataUpdateHookHandlers.php
@@ -107,8 +107,17 @@
                ParserOptions $pops,
                $revId
        ) {
+               $settings = WikibaseClient::getDefaultInstance()->getSettings();
+
+               if ( $settings->getSetting( 'allowDataAccessInUserLanguage' ) 
=== true ) {
+                       $langCode = $pops->getUserLangObj()->getCode();
+               } else {
+                       global $wgContLang;
+                       $langCode = $wgContLang->getCode();
+               }
+
                $handler = self::newFromGlobalState();
-               $handler->doParserCacheSaveComplete( $pout, $title );
+               $handler->doParserCacheSaveComplete( $pout, $title, $langCode );
        }
 
        public function __construct(
@@ -162,8 +171,9 @@
         *
         * @param ParserOutput $parserOutput
         * @param Title $title
+        * @param string $langCode
         */
-       public function doParserCacheSaveComplete( ParserOutput $parserOutput, 
Title $title ) {
+       public function doParserCacheSaveComplete( ParserOutput $parserOutput, 
Title $title, $langCode ) {
                $usageAcc = new ParserOutputUsageAccumulator( $parserOutput );
 
                // The parser output should tell us when it was parsed. If not, 
ask the Title object.
@@ -184,7 +194,13 @@
 
                //TODO: Before posting a job, check slave database. If no 
changes are needed, skip update.
 
-               $addUsagesForPageJob = AddUsagesForPageJob::newSpec( $title, 
$usageAcc->getUsages(), $touched );
+               $addUsagesForPageJob = AddUsagesForPageJob::newSpec(
+                       $title,
+                       $usageAcc->getUsages(),
+                       $touched,
+                       $langCode
+               );
+
                $enqueueJob = EnqueueJob::newFromLocalJobs( 
$addUsagesForPageJob );
 
                $this->jobScheduler->lazyPush( $enqueueJob );
diff --git a/client/includes/store/AddUsagesForPageJob.php 
b/client/includes/store/AddUsagesForPageJob.php
index aecc466..a5571c4 100644
--- a/client/includes/store/AddUsagesForPageJob.php
+++ b/client/includes/store/AddUsagesForPageJob.php
@@ -34,6 +34,11 @@
        private $touched;
 
        /**
+        * @var string
+        */
+       private $langCode;
+
+       /**
         * @var UsageUpdater
         */
        private $usageUpdater;
@@ -49,10 +54,11 @@
         * @param Title $title
         * @param EntityUsage[] $usages
         * @param string $touched
+        * @param string $langCode
         *
         * @return JobSpecification
         */
-       public static function newSpec( Title $title, array $usages, $touched ) 
{
+       public static function newSpec( Title $title, array $usages, $touched, 
$langCode ) {
                // NOTE: Map EntityUsage objects to scalar arrays, for JSON 
serialization in the job queue.
                $usages = array_map( function ( EntityUsage $usage ) {
                        return $usage->asArray();
@@ -61,12 +67,15 @@
                $jobParams = array(
                        'pageId' => $title->getArticleId(),
                        'usages' => $usages,
-                       'touched' => $touched
+                       'touched' => $touched,
+                       'langCode' => $langCode
                );
 
                return new JobSpecification(
                        'wikibase-addUsagesForPage',
-                       $jobParams,
+                       $jobParams + Job::newRootJobParams(
+                               'wikibase-UsagesRoot:' . 
$title->getPrefixedText() . ':' . $langCode
+                       ),
                        array( 'removeDuplicates' => true ),
                        $title
                );
@@ -94,6 +103,9 @@
                        '$params["touched"]',
                        'must be a timestamp string' );
 
+               // @todo assert langCode parameter is a string and not empty, 
but there might
+               // still be old jobs in the queue that don't have the param.
+
                Assert::parameterElementType(
                        'array',
                        $params['usages'],
@@ -103,6 +115,11 @@
                $this->usages = $params['usages'];
                $this->touched = $params['touched'];
 
+               // unused for now, see @todo above
+               if ( isset( $params['langCode'] ) ) {
+                       $this->langCode = $params['langCode'];
+               }
+
                $usageUpdater = 
WikibaseClient::getDefaultInstance()->getStore()->getUsageUpdater();
                $idParser = 
WikibaseClient::getDefaultInstance()->getEntityIdParser();
                $this->overrideServices( $usageUpdater, $idParser );
diff --git a/client/tests/phpunit/includes/Hooks/DataUpdateHookHandlersTest.php 
b/client/tests/phpunit/includes/Hooks/DataUpdateHookHandlersTest.php
index f69fdc6..8ebd3d3 100644
--- a/client/tests/phpunit/includes/Hooks/DataUpdateHookHandlersTest.php
+++ b/client/tests/phpunit/includes/Hooks/DataUpdateHookHandlersTest.php
@@ -103,38 +103,56 @@
                        }, $expectedUsages );
 
                        $params = array(
-                               'jobsByWiki' => array(
-                                       wfWikiID() => array(
-                                               array(
-                                                       'type' => 
'wikibase-addUsagesForPage',
-                                                       'params' => array(
-                                                               'pageId' => 
$title->getArticleID(),
-                                                               'usages' => 
$expectedUsageArray,
-                                                               'touched' => 
$touched
-                                                       ),
-                                                       'opts' => array(
-                                                               
'removeDuplicates' => true
-                                                       ),
-                                                       'title' => array(
-                                                               'ns' => NS_MAIN,
-                                                               'key' => 
'Oxygen'
-                                                       )
-                                               )
-                                       )
-                               )
+                               'pageId' => $title->getArticleID(),
+                               'usages' => $expectedUsageArray,
+                               'touched' => $touched,
+                               'langCode' => 'en',
+                               'rootJobIsSelf' => true
                        );
 
                        $jobScheduler->expects( $this->once() )
                                ->method( 'lazyPush' )
                                ->with( $this->callback( function ( $job ) use 
( $params ) {
+
                                        
DataUpdateHookHandlersTest::assertEquals( 'enqueue', $job->getType() );
-                                       
DataUpdateHookHandlersTest::assertEquals( $params, $job->getParams() );
+
+                                       $jobParams = $job->getParams();
+                                       $wikiId = wfWikiID();
+
+                                       unset( 
$jobParams['jobsByWiki'][$wikiId][0]['params']['rootJobSignature'] );
+                                       unset( 
$jobParams['jobsByWiki'][$wikiId][0]['params']['rootJobTimestamp'] );
+
+                                       
DataUpdateHookHandlersTest::assertEquals(
+                                               $this->getExpectedJobParams( 
$params, $wikiId ),
+                                               $jobParams
+                                       );
+
                                        return true;
                                } ) );
 
                }
 
                return $jobScheduler;
+       }
+
+       private function getExpectedJobParams( array $params, $wikiId ) {
+               return array(
+                       'jobsByWiki' => array(
+                               $wikiId => array(
+                                       array(
+                                               'type' => 
'wikibase-addUsagesForPage',
+                                               'params' => $params,
+                                               'opts' => array(
+                                                       'removeDuplicates' => 
true
+                                               ),
+                                               'title' => array(
+                                                       'ns' => NS_MAIN,
+                                                       'key' => 'Oxygen'
+                                               )
+                                       )
+                               )
+                       )
+               );
        }
 
        /**
@@ -257,7 +275,7 @@
 
                // Assertions are done by the UsageUpdater mock
                $handler = $this->newDataUpdateHookHandlers( $title, $usage, 
$timestamp, false, true );
-               $handler->doParserCacheSaveComplete( $parserOutput, $title );
+               $handler->doParserCacheSaveComplete( $parserOutput, $title, 
'en' );
        }
 
        public function testDoArticleDeleteComplete() {
diff --git a/client/tests/phpunit/includes/store/AddUsagesForPageJobTest.php 
b/client/tests/phpunit/includes/store/AddUsagesForPageJobTest.php
index fbfb744..dc6f128 100644
--- a/client/tests/phpunit/includes/store/AddUsagesForPageJobTest.php
+++ b/client/tests/phpunit/includes/store/AddUsagesForPageJobTest.php
@@ -92,6 +92,16 @@
                new AddUsagesForPageJob( $title, $params );
        }
 
+       public function testIsRootJob() {
+               $title = Title::makeTitle( NS_MAIN, 'Foo' );
+               $title->resetArticleID( 17 );
+
+               $usage = new EntityUsage( new ItemId( 'Q100' ), 'X' );
+
+               $job = AddUsagesForPageJob::newSpec( $title, array( $usage ), 
'20150102000000', 'en' );
+               $this->assertTrue( $job->isRootJob() );
+       }
+
        public function testRun() {
                $usageQ5X = new EntityUsage( new ItemId( 'Q5' ), 'X' );
                $params = array(
@@ -128,17 +138,24 @@
                $touched = '20150101000000';
                $usages = array( $usageQ5X );
 
-               $spec = AddUsagesForPageJob::newSpec( $title, $usages, $touched 
);
+               $spec = AddUsagesForPageJob::newSpec( $title, $usages, 
$touched, 'es' );
 
-               $params = array(
+               $expected = array(
                        'pageId' => $title->getArticleID(),
                        'usages' => array( $usageQ5X->asArray() ),
                        'touched' => '20150101000000',
+                       'langCode' => 'es',
+                       'rootJobIsSelf' => true
                );
+
+               $params = $spec->getParams();
+
+               unset( $params['rootJobSignature'] );
+               unset( $params['rootJobTimestamp'] );
 
                $this->assertEquals( 'wikibase-addUsagesForPage', 
$spec->getType() );
                $this->assertEquals( $title->getFullText(), 
$spec->getTitle()->getFullText() );
-               $this->assertEquals( $params, $spec->getParams() );
+               $this->assertEquals( $expected, $params );
        }
 
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic83365706888c01e94095abeff3c80c1ea015ab4
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aude <aude.w...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to