Daniel Kinzler has uploaded a new change for review.
https://gerrit.wikimedia.org/r/184002
Change subject: Use ArticleEditUpdates hook for usage tracking
......................................................................
Use ArticleEditUpdates hook for usage tracking
Using ParserAfterParse is inconvenient and error prone.
Bug: T86308
Change-Id: I93f09dc5bb0e9f79acd059111e56c425f30324a7
---
M client/WikibaseClient.php
M client/includes/hooks/DataUpdateHookHandlers.php
M client/tests/phpunit/includes/hooks/DataUpdateHookHandlersTest.php
3 files changed, 93 insertions(+), 71 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/02/184002/1
diff --git a/client/WikibaseClient.php b/client/WikibaseClient.php
index 7baf108..0f807f5 100644
--- a/client/WikibaseClient.php
+++ b/client/WikibaseClient.php
@@ -94,7 +94,7 @@
$wgHooks['SkinTemplateGetLanguageLink'][] =
'\Wikibase\Client\Hooks\SidebarHookHandlers::onSkinTemplateGetLanguageLink';
$wgHooks['ParserAfterParse'][] =
'\Wikibase\Client\Hooks\ParserAfterParseHookHandler::onParserAfterParse';
$wgHooks['SidebarBeforeOutput'][] =
'\Wikibase\Client\Hooks\SidebarHookHandlers::onSidebarBeforeOutput';
- $wgHooks['ParserAfterParse'][] =
'\Wikibase\Client\Hooks\DataUpdateHookHandlers::onParserAfterParse';
+ $wgHooks['ArticleEditUpdates'][] =
'\Wikibase\Client\Hooks\DataUpdateHookHandlers::onArticleEditUpdates';
$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 02781c0..76e0fdb 100644
--- a/client/includes/hooks/DataUpdateHookHandlers.php
+++ b/client/includes/hooks/DataUpdateHookHandlers.php
@@ -11,6 +11,7 @@
use Wikibase\Client\WikibaseClient;
use Wikibase\NamespaceChecker;
use Wikibase\Updates\DataUpdateAdapter;
+use WikiPage;
/**
* Hook handlers for triggering data updates.
@@ -55,25 +56,17 @@
}
/**
- * Static handler for the ParserAfterParse hook.
+ * Static handler for the ArticleEditUpdates hook.
+ * @see https://www.mediawiki.org/wiki/Manual:Hooks/ArticleEditUpdates
*
- * @param Parser|null &$parser
- * @param string|null &$text
- * @param StripState|null $stripState
- *
- * @return bool
+ * @param WikiPage $page The WikiPage object managing the edit
+ * @param object $editInfo The current edit info object.
+ * $editInfo->output is an ParserOutput object.
+ * @param bool $changed False if this is a null edit
*/
- public static function onParserAfterParse(
- Parser &$parser = null,
- &$text = null,
- StripState $stripState = null
- ) {
- if ( $parser === null ||
$parser->getOptions()->getInterfaceMessage() ) {
- return true;
- }
-
+ public static function onArticleEditUpdates( WikiPage $page,
&$editInfo, $changed ) {
$handler = self::newFromGlobalState();
- return $handler->doParserAfterParse( $parser, $text,
$stripState );
+ $handler->doArticleEditUpdates( $page, $editInfo, $changed );
}
public function __construct(
@@ -87,37 +80,26 @@
/**
* Hook runs after internal parsing
- * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterParse
*
- * @param Parser &$parser
- * @param string &$text
- * @param StripState $stripState
- *
- * @return bool
+ * @param WikiPage $page The WikiPage object managing the edit
+ * @param object $editInfo The current edit info object.
+ * $editInfo->output is an ParserOutput object.
+ * @param bool $changed False if this is a null edit
*/
- public function doParserAfterParse( Parser &$parser ) {
- $title = $parser->getTitle();
+ public function doArticleEditUpdates( WikiPage $page, &$editInfo,
$changed ) {
+ $title = $page->getTitle();
if ( !$this->namespaceChecker->isWikibaseEnabled(
$title->getNamespace() ) ) {
// shorten out
- return true;
+ return;
}
- $this->registerDataUpdates( $title, $parser->getOutput() );
+ $usageAcc = new ParserOutputUsageAccumulator( $editInfo->output
);
- return true;
- }
-
- private function registerDataUpdates( Title $title, ParserOutput
$parserOutput ) {
- $usageAcc = new ParserOutputUsageAccumulator( $parserOutput );
-
- $update = new DataUpdateAdapter(
- array( $this->usageUpdater, 'updateUsageForPage' ),
+ $this->usageUpdater->updateUsageForPage(
$title->getArticleId(),
$usageAcc->getUsages()
);
-
- $parserOutput->addSecondaryDataUpdate( $update );
}
}
diff --git a/client/tests/phpunit/includes/hooks/DataUpdateHookHandlersTest.php
b/client/tests/phpunit/includes/hooks/DataUpdateHookHandlersTest.php
index de7393a..0c36c52 100644
--- a/client/tests/phpunit/includes/hooks/DataUpdateHookHandlersTest.php
+++ b/client/tests/phpunit/includes/hooks/DataUpdateHookHandlersTest.php
@@ -3,12 +3,12 @@
namespace Wikibase\Client\Test\Hooks;
use Parser;
-use ParserOptions;
use ParserOutput;
-use StripState;
use Title;
use Wikibase\Client\Hooks\DataUpdateHookHandlers;
use Wikibase\Client\Store\UsageUpdater;
+use Wikibase\Client\Usage\EntityUsage;
+use Wikibase\Client\Usage\ParserOutputUsageAccumulator;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\NamespaceChecker;
use Wikibase\Settings;
@@ -41,23 +41,56 @@
}
/**
+ * @param Title $title
+ * @param array $expectedUsages
+ *
* @return UsageUpdater
*/
- private function getUsageUpdater() {
+ private function newUsageUpdater( Title $title, array $expectedUsages
= null ) {
$usageUpdater = $this->getMockBuilder(
'Wikibase\Client\Store\UsageUpdater' )
->disableOriginalConstructor()
->getMock();
+ if ( $expectedUsages === null ) {
+ $usageUpdater->expects( $this->never() )
+ ->method( 'updateUsageForPage' );
+ } else {
+ $expectedEntityUsageList = $this->makeEntityUsageList(
$expectedUsages );
+ $usageUpdater->expects( $this->once() )
+ ->method( 'updateUsageForPage' )
+ ->with( $title->getArticleID(),
$expectedEntityUsageList );
+ }
+
return $usageUpdater;
}
- private function newDataUpdateHookHandlers( array $settings = array() )
{
+ private function makeEntityUsageList( array $expectedUsages ) {
+ $entityUsageList = array();
+
+ foreach ( $expectedUsages as $aspect => $entityIds ) {
+ foreach ( $entityIds as $id ) {
+ $key = $id->getSerialization() . '#' . $aspect;
+ $entityUsageList[$key] = new EntityUsage( $id,
$aspect );
+ }
+ }
+
+ return $entityUsageList;
+ }
+
+ /**
+ * @param Title $title
+ * @param array $expectedUsages
+ * @param array $settings
+ *
+ * @return DataUpdateHookHandlers
+ */
+ private function newDataUpdateHookHandlers( Title $title, array
$expectedUsages = null, array $settings = array() ) {
$settings = $this->newSettings( $settings );
$namespaces = $settings->getSetting( 'namespaces' );
$namespaceChecker = new NamespaceChecker( array(), $namespaces
);
- $usageUpdater = $this->getUsageUpdater();
+ $usageUpdater = $this->newUsageUpdater( $title, $expectedUsages
);
return new DataUpdateHookHandlers(
$namespaceChecker,
@@ -66,32 +99,33 @@
}
/**
- * @param bool $expectedDataUpdateCount
+ * @param array $usages
*
* @return ParserOutput
*/
- private function newParserOutput( $expectedDataUpdateCount ) {
- $output = $this->getMockBuilder( 'ParserOutput' )
- ->disableOriginalConstructor()
- ->getMock();
+ private function newParserOutput( array $usages = null ) {
+ $output = new ParserOutput();
- $output->expects( $this->exactly( $expectedDataUpdateCount ) )
- ->method( 'addSecondaryDataUpdate' );
+ if ( $usages ) {
+ $acc = new ParserOutputUsageAccumulator( $output );
+
+ foreach ( $usages as $aspect => $entityIds ) {
+ foreach ( $entityIds as $id ) {
+ $acc->addUsage( $id, $aspect );
+ }
+ }
+ }
return $output;
}
/**
* @param Title $title
- * @param bool $expectedDataUpdateCount
*
* @return Parser
*/
- private function newParser( Title $title, $expectedDataUpdateCount ) {
- $options = new ParserOptions();
- $output = $this->newParserOutput( $expectedDataUpdateCount );
-
- $parser = $this->getMockBuilder( 'Parser' )
+ private function newWikiPage( Title $title ) {
+ $parser = $this->getMockBuilder( 'WikiPage' )
->disableOriginalConstructor()
->getMock();
@@ -99,15 +133,20 @@
->method( 'getTitle' )
->will( $this->returnValue( $title ) );
- $parser->expects( $this->any() )
- ->method( 'getOptions' )
- ->will( $this->returnValue( $options ) );
-
- $parser->expects( $this->any() )
- ->method( 'getOutput' )
- ->will( $this->returnValue( $output ) );
-
return $parser;
+ }
+ /**
+ * @param array $usages
+ *
+ * @return Parser
+ */
+ private function newEditInfo( array $usages = null ) {
+ $output = $this->newParserOutput( $usages );
+
+ $editInfo = new \stdClass();
+ $editInfo->output = $output;
+
+ return $editInfo;
}
public function testNewFromGlobalState() {
@@ -115,11 +154,11 @@
$this->assertInstanceOf(
'Wikibase\Client\Hooks\DataUpdateHookHandlers', $handler );
}
- public function parserAfterParseProvider() {
+ public function provideDoParserAfterParse() {
return array(
'usage' => array(
Title::makeTitle( NS_MAIN, 'Oxygen' ),
- array( 'sitelinks' => array( new ItemId( 'Q1' )
) ),
+ array( EntityUsage::SITELINK_USAGE => array(
new ItemId( 'Q1' ), new ItemId( 'Q2' ) ) ),
),
'no usage' => array(
@@ -135,16 +174,17 @@
}
/**
- * @dataProvider parserAfterParseProvider
+ * @dataProvider provideDoParserAfterParse
*/
public function testDoParserAfterParse( Title $title, $usage ) {
- $parser = $this->newParser( $title, $usage === null ? 0 : 1 );
- $handler = $this->newDataUpdateHookHandlers();
+ $title->resetArticleID( 23 );
- $handler->doParserAfterParse( $parser );
+ $page = $this->newWikiPage( $title );
+ $editInfo = $this->newEditInfo( $usage );
- // Assertions are done by the ParserOutput mock
- $dataUpdates = $parser->getOutput()->getSecondaryDataUpdates(
$title );
+ // Assertions are done by the UsageUpdater mock
+ $handler = $this->newDataUpdateHookHandlers( $title, $usage );
+ $handler->doArticleEditUpdates( $page, $editInfo, true );
}
}
--
To view, visit https://gerrit.wikimedia.org/r/184002
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I93f09dc5bb0e9f79acd059111e56c425f30324a7
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