jenkins-bot has submitted this change and it was merged.
Change subject: [DIC]: SMWHooks::onNewRevisionFromEditComplete to
\SMW\NewRevisionFromEditComplete
......................................................................
[DIC]: SMWHooks::onNewRevisionFromEditComplete to
\SMW\NewRevisionFromEditComplete
Code coverage: 100%
CRAP: 4
Change-Id: I1e41a82303f65c20bc0b02d88c2c7f3eab1d3cb0
---
M SemanticMediaWiki.classes.php
M SemanticMediaWiki.hooks.php
A includes/hooks/NewRevisionFromEditComplete.php
M includes/hooks/README.md
A tests/phpunit/includes/hooks/NewRevisionFromEditCompleteTest.php
5 files changed, 313 insertions(+), 24 deletions(-)
Approvals:
Mwjames: Looks good to me, approved
jenkins-bot: Verified
diff --git a/SemanticMediaWiki.classes.php b/SemanticMediaWiki.classes.php
index 6e66ab9..92d5b8a 100644
--- a/SemanticMediaWiki.classes.php
+++ b/SemanticMediaWiki.classes.php
@@ -104,13 +104,14 @@
'SMW\CacheIdGenerator' =>
'includes/cache/CacheIdGenerator.php',
// Hooks
- 'SMW\InternalParseBeforeLinks' =>
'includes/hooks/InternalParseBeforeLinks.php',
- 'SMW\ParserAfterTidy' => 'includes/hooks/ParserAfterTidy.php',
- 'SMW\LinksUpdateConstructed' =>
'includes/hooks/LinksUpdateConstructed.php',
- 'SMW\BeforePageDisplay' =>
'includes/hooks/BeforePageDisplay.php',
- 'SMW\ArticlePurge' => 'includes/hooks/ArticlePurge.php',
- 'SMW\FunctionHook' => 'includes/hooks/FunctionHook.php',
- 'SMW\FunctionHookRegistry' =>
'includes/hooks/FunctionHookRegistry.php',
+ 'SMW\NewRevisionFromEditComplete' =>
'includes/hooks/NewRevisionFromEditComplete.php',
+ 'SMW\InternalParseBeforeLinks' =>
'includes/hooks/InternalParseBeforeLinks.php',
+ 'SMW\ParserAfterTidy' =>
'includes/hooks/ParserAfterTidy.php',
+ 'SMW\LinksUpdateConstructed' =>
'includes/hooks/LinksUpdateConstructed.php',
+ 'SMW\BeforePageDisplay' =>
'includes/hooks/BeforePageDisplay.php',
+ 'SMW\ArticlePurge' => 'includes/hooks/ArticlePurge.php',
+ 'SMW\FunctionHook' => 'includes/hooks/FunctionHook.php',
+ 'SMW\FunctionHookRegistry' =>
'includes/hooks/FunctionHookRegistry.php',
// Formatters
'SMW\ArrayFormatter' =>
'includes/formatters/ArrayFormatter.php',
diff --git a/SemanticMediaWiki.hooks.php b/SemanticMediaWiki.hooks.php
index 76e169d..2e8d9bb 100644
--- a/SemanticMediaWiki.hooks.php
+++ b/SemanticMediaWiki.hooks.php
@@ -606,23 +606,7 @@
* @return true
*/
public static function onNewRevisionFromEditComplete( $wikiPage,
$revision, $baseId, $user ) {
- $parserOutput = $wikiPage->getParserOutput(
- $wikiPage->makeParserOptions( $user ),
- $revision->getId()
- );
-
- if ( !( $parserOutput instanceof ParserOutput ) ) {
- return true;
- }
-
- $settings = \SMW\Settings::newFromGlobals();
- $parserData = new SMW\ParserData( $wikiPage->getTitle(),
$parserOutput );
-
- $complementor = new \SMW\BasePropertyAnnotator(
$parserData->getData(), $settings );
- $complementor->attach( $parserData );
- $complementor->addSpecialProperties( $wikiPage, $revision,
$user );
-
- return true;
+ return \SMW\FunctionHookRegistry::register( new
\SMW\NewRevisionFromEditComplete( $wikiPage, $revision, $baseId, $user )
)->process();
}
/**
diff --git a/includes/hooks/NewRevisionFromEditComplete.php
b/includes/hooks/NewRevisionFromEditComplete.php
new file mode 100644
index 0000000..1d3c0c2
--- /dev/null
+++ b/includes/hooks/NewRevisionFromEditComplete.php
@@ -0,0 +1,109 @@
+<?php
+
+namespace SMW;
+
+use ParserOutput;
+use Title;
+
+/**
+ * NewRevisionFromEditComplete hook
+ *
+ * @file
+ *
+ * @license GNU GPL v2+
+ * @since 1.9
+ *
+ * @author mwjames
+ */
+
+/**
+ * Hook: NewRevisionFromEditComplete called when a revision was inserted
+ * due to an edit
+ *
+ * Fetch additional information that is related to the saving that has just
happened,
+ * e.g. regarding the last edit date. In runs where this hook is not
triggered, the
+ * last DB entry (of MW) will be used to fill such properties.
+ *
+ * Called from LocalFile.php, SpecialImport.php, Article.php, Title.php
+ *
+ * @see http://www.mediawiki.org/wiki/Manual:Hooks/NewRevisionFromEditComplete
+ *
+ * @ingroup Hook
+ */
+class NewRevisionFromEditComplete extends FunctionHook {
+
+ /** @var Parser */
+ protected $wikiPage = null;
+
+ /** @var Parser */
+ protected $revision = null;
+
+ /** @var Parser */
+ protected $baseId = null;
+
+ /** @var Parser */
+ protected $user = null;
+
+ /**
+ * @since 1.9
+ *
+ * @param WikiPage $article the article edited
+ * @param Revision $rev the new revision. Revision object
+ * @param $baseId the revision ID this was based off, if any
+ * @param User $user the revision author. User object
+ */
+ public function __construct( $wikiPage, $revision, $baseId, $user ) {
+ $this->wikiPage = $wikiPage;
+ $this->revision = $revision;
+ $this->baseId = $baseId;
+ $this->user = $user;
+ }
+
+ /**
+ * @since 1.9
+ *
+ * @param ParserOutput $parserOutput
+ *
+ * @return true
+ */
+ protected function performUpdate( ParserOutput $parserOutput ) {
+
+ /**
+ * @var ParserData $parserData
+ */
+ $parserData = $this->getDependencyBuilder()->newObject(
'ParserData', array(
+ 'Title' => $this->wikiPage->getTitle(),
+ 'ParserOutput' => $parserOutput
+ ) );
+
+ /**
+ * @var BasePropertyAnnotator $propertyAnnotator
+ */
+ $propertyAnnotator = $this->getDependencyBuilder()->newObject(
'BasePropertyAnnotator', array(
+ 'SemanticData' => $parserData->getData(),
+ ) );
+
+ $propertyAnnotator->attach( $parserData )
+ ->addSpecialProperties( $this->wikiPage,
$this->revision, $this->user );
+
+ return true;
+ }
+
+ /**
+ * @see FunctionHook::process
+ *
+ * @since 1.9
+ *
+ * @return true
+ */
+ public function process() {
+
+ $parserOutput = $this->wikiPage->getParserOutput(
+ $this->wikiPage->makeParserOptions( $this->user ),
+ $this->revision->getId()
+ );
+
+ return $parserOutput instanceof ParserOutput ?
$this->performUpdate( $parserOutput ) : true;
+ }
+
+}
diff --git a/includes/hooks/README.md b/includes/hooks/README.md
index 570f97a..eeb6d5f 100644
--- a/includes/hooks/README.md
+++ b/includes/hooks/README.md
@@ -12,6 +12,9 @@
#### LinksUpdateConstructed
LinksUpdateConstructed is called at the end of LinksUpdate and is being used
to initiate a store update for data that were held by the ParserOutput object.
+#### NewRevisionFromEditComplete
+NewRevisionFromEditComplete called when a new revision was inserted due to an
edit and used to update the ParserOuput with the latests special property
annotation.
+
#### ParserAfterTidy
ParserAfterTidy is used to re-introduce content, update base annotations (e.g.
special properties, categories etc.) and in case of a manual article purge
initiates a store update (LinksUpdateConstructed wouldn't work because it acts
only on link changes and therefore would not trigger a LinksUpdateConstructed
event).
diff --git a/tests/phpunit/includes/hooks/NewRevisionFromEditCompleteTest.php
b/tests/phpunit/includes/hooks/NewRevisionFromEditCompleteTest.php
new file mode 100644
index 0000000..8a63c0a
--- /dev/null
+++ b/tests/phpunit/includes/hooks/NewRevisionFromEditCompleteTest.php
@@ -0,0 +1,192 @@
+<?php
+
+namespace SMW\Test;
+
+use SMW\NewRevisionFromEditComplete;
+use SMW\SharedDependencyContainer;
+use SMW\DIProperty;
+
+use WikiPage;
+use Revision;
+
+/**
+ * Tests for the NewRevisionFromEditComplete class
+ *
+ * @file
+ *
+ * @license GNU GPL v2+
+ * @since 1.9
+ *
+ * @author mwjames
+ */
+
+/**
+ * @covers \SMW\NewRevisionFromEditComplete
+ *
+ * @ingroup Test
+ *
+ * @group SMW
+ * @group SMWExtension
+ */
+class NewRevisionFromEditCompleteTest extends ParserTestCase {
+
+ /**
+ * Returns the name of the class to be tested
+ *
+ * @return string|false
+ */
+ public function getClass() {
+ return '\SMW\NewRevisionFromEditComplete';
+ }
+
+ /**
+ * Helper method that returns a NewRevisionFromEditComplete object
+ *
+ * @since 1.9
+ *
+ * @return NewRevisionFromEditComplete
+ */
+ private function newInstance( WikiPage $wikiPage = null, Revision
$revision = null ) {
+
+ $baseId = 9001;
+ $user = $this->newMockUser();
+
+ if ( $wikiPage === null ) {
+ $wikiPage = $this->newMockBuilder()->newObject(
'WikiPage' );
+ }
+
+ if ( $revision === null ) {
+ $revision = $this->newMockBuilder()->newObject(
'Revision' );
+ }
+
+ $instance = new NewRevisionFromEditComplete( $wikiPage,
$revision, $baseId, $user );
+ $instance->setDependencyBuilder( $this->newDependencyBuilder(
new SharedDependencyContainer() ) );
+
+ return $instance;
+ }
+
+ /**
+ * @test NewRevisionFromEditComplete::__construct
+ *
+ * @since 1.9
+ */
+ public function testConstructor() {
+ $this->assertInstanceOf( $this->getClass(),
$this->newInstance() );
+ }
+
+ /**
+ * @test NewRevisionFromEditComplete::process
+ * @dataProvider titleDataProvider
+ *
+ * @since 1.9
+ *
+ * @param $setup
+ * @param $expected
+ */
+ public function testProcess( $setup, $expected ) {
+
+ $instance = $this->newInstance( $setup['wikiPage'],
$setup['revision'] );
+
+ $this->assertTrue(
+ $instance->process(),
+ 'asserts that process() always returns true'
+ );
+
+ }
+
+ /**
+ * @test NewRevisionFromEditComplete::process
+ * @dataProvider titleDataProvider
+ *
+ * @since 1.9
+ *
+ * @param $setup
+ * @param $expected
+ */
+ public function testProcessAnnotationIntegration( $setup, $expected ) {
+
+ $settings = $this->newSettings( $setup['settings'] );
+ $instance = $this->newInstance( $setup['wikiPage'],
$setup['revision'] );
+
+
$instance->getDependencyBuilder()->getContainer()->registerObject( 'Settings',
$settings );
+
+ $this->assertTrue(
+ $instance->process(),
+ 'asserts that process() always returns true'
+ );
+
+ $parserOutput = $setup['wikiPage']->getParserOutput(
+ $setup['wikiPage']->makeParserOptions(
$this->newMockUser() ),
+ $setup['revision']->getId()
+ );
+
+ if ( $parserOutput !== null ) {
+
+ $parserData = $this->newParserData(
+ $setup['wikiPage']->getTitle(),
+ $parserOutput
+ );
+
+ $this->assertSemanticData(
+ $parserData->getData(),
+ $expected,
+ "asserts whether addSpecialProperties() adds
the {$expected['propertyKey']} annotation"
+ );
+
+ }
+
+ }
+
+ /**
+ * @return array
+ */
+ public function titleDataProvider() {
+
+ $provider = array();
+
+ $revision = $this->newMockBuilder()->newObject( 'Revision',
array(
+ 'getId' => 1001
+ ) );
+
+ // #0 No parserOutput object
+ $wikiPage = $this->newMockBuilder()->newObject( 'WikiPage',
array(
+ 'getParserOutput' => null,
+ 'makeParserOptions' =>
$this->newMockBuilder()->newObject( 'ParserOptions' )
+ ) );
+
+ $provider[] = array(
+ array(
+ 'wikiPage' => $wikiPage,
+ 'revision' => $revision,
+ 'settings' => array()
+ ),
+ array()
+ );
+
+ // #1
+ $wikiPage = $this->newMockBuilder()->newObject( 'WikiPage',
array(
+ 'getTitle' => $this->newTitle(),
+ 'getParserOutput' => $this->newParserOutput(),
+ 'makeParserOptions' =>
$this->newMockBuilder()->newObject( 'ParserOptions' ),
+ 'getTimestamp' => 1272508903
+ ) );
+
+ $provider[] = array(
+ array(
+ 'wikiPage' => $wikiPage,
+ 'revision' => $revision,
+ 'settings' => array(
+ 'smwgPageSpecialProperties' => array(
DIProperty::TYPE_MODIFICATION_DATE )
+ )
+ ),
+ array(
+ 'propertyCount' => 1,
+ 'propertyKey' => '_MDAT',
+ 'propertyValue' => array( '2010-04-29T02:41:43'
),
+ )
+ );
+
+ return $provider;
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/83377
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I1e41a82303f65c20bc0b02d88c2c7f3eab1d3cb0
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>
Gerrit-Reviewer: Mwjames <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits