Mwjames has uploaded a new change for review.

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


Change subject: [DIC]: SMWHooks::onInternalParseBeforeLinks to 
\SMW\InternalParseBeforeLinks
......................................................................

[DIC]: SMWHooks::onInternalParseBeforeLinks to \SMW\InternalParseBeforeLinks

Change-Id: Iccb3cfc77b26744496094036ac4d0c6cd11ba4c8
---
M SemanticMediaWiki.classes.php
M SemanticMediaWiki.hooks.php
M includes/BasePropertyAnnotator.php
M includes/dic/SharedDependencyContainer.php
A includes/hooks/InternalParseBeforeLinks.php
M includes/hooks/ParserAfterTidy.php
A includes/hooks/README.md
M tests/phpunit/includes/dic/SharedDependencyContainerTest.php
A tests/phpunit/includes/hooks/InternalParseBeforeLinksTest.php
9 files changed, 381 insertions(+), 47 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticMediaWiki 
refs/changes/66/82066/1

diff --git a/SemanticMediaWiki.classes.php b/SemanticMediaWiki.classes.php
index 747b6f2..1cda6b9 100644
--- a/SemanticMediaWiki.classes.php
+++ b/SemanticMediaWiki.classes.php
@@ -105,6 +105,7 @@
        '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',
diff --git a/SemanticMediaWiki.hooks.php b/SemanticMediaWiki.hooks.php
index 72c32df..76e169d 100644
--- a/SemanticMediaWiki.hooks.php
+++ b/SemanticMediaWiki.hooks.php
@@ -646,18 +646,7 @@
         * @return true
         */
        public static function onInternalParseBeforeLinks( Parser &$parser, 
&$text ) {
-
-               if ( !$parser->getTitle()->isSpecialPage() ) {
-
-                       $processor = new SMW\ParserTextProcessor(
-                               new \SMW\ParserData( $parser->getTitle(), 
$parser->getOutput() ),
-                               \SMW\Settings::newFromGlobals()
-                       );
-
-                       $processor->parse( $text );
-               }
-
-               return true;
+               return \SMW\FunctionHookRegistry::register( new 
\SMW\InternalParseBeforeLinks( $parser, $text ) )->process();
        }
 
        /**
diff --git a/includes/BasePropertyAnnotator.php 
b/includes/BasePropertyAnnotator.php
index fc0d5de..dc9289f 100644
--- a/includes/BasePropertyAnnotator.php
+++ b/includes/BasePropertyAnnotator.php
@@ -67,7 +67,7 @@
         *
         * @param array $categoryLinks
         *
-        * @return boolean|null
+        * @return BasePropertyAnnotator
         */
        public function addCategories( array $categoryLinks ) {
 
@@ -89,6 +89,8 @@
                }
 
                $this->setState( 'updateOutput' );
+
+               return $this;
        }
 
        /**
@@ -100,7 +102,7 @@
         *
         * @param string $defaultSort
         *
-        * @return boolean|null
+        * @return BasePropertyAnnotator
         */
        public function addDefaultSort( $defaultSort ) {
                $sortkey = $defaultSort ? $defaultSort : str_replace( '_', ' ', 
$this->semanticData->getSubject()->getTitle()->getDBkey() );
@@ -110,6 +112,8 @@
                );
 
                $this->setState( 'updateOutput' );
+
+               return $this;
        }
 
        /**
@@ -118,11 +122,11 @@
         *
         * @since 1.9
         *
-        * @param \WikiPage $wikiPage
-        * @param \Revision $revision
-        * @param \User $user
+        * @param WikiPage $wikiPage
+        * @param Revision $revision
+        * @param User $user
         *
-        * @return boolean|null
+        * @return BasePropertyAnnotator
         */
        public function addSpecialProperties( WikiPage $wikiPage, Revision 
$revision, User $user ) {
 
@@ -174,6 +178,8 @@
                }
 
                $this->setState( 'updateOutput' );
+
+               return $this;
        }
 
 }
diff --git a/includes/dic/SharedDependencyContainer.php 
b/includes/dic/SharedDependencyContainer.php
index 51cbb40..c17f956 100644
--- a/includes/dic/SharedDependencyContainer.php
+++ b/includes/dic/SharedDependencyContainer.php
@@ -85,12 +85,22 @@
         */
        public function loadObjects() {
                return array(
-                       'ParserData'        => '\SMW\DiParserData',
-                       'NamespaceExaminer' => $this->getNamespaceExaminer(),
-                       'UpdateObserver'    => $this->getUpdateObserver(),
-                       'ContentParser'     => function ( DependencyBuilder 
$builder ) {
+                       'ParserData'            => '\SMW\DiParserData',
+                       'NamespaceExaminer'     => 
$this->getNamespaceExaminer(),
+                       'UpdateObserver'        => $this->getUpdateObserver(),
+                       'BasePropertyAnnotator' => 
$this->getBasePropertyAnnotator(),
+
+                       'ContentAnnotator' => function ( DependencyBuilder 
$builder ) {
+                                       return new ParserTextProcessor(
+                                               $builder->getArgument( 
'ParserData' ),
+                                               $builder->newObject( 'Settings' 
)
+                                       );
+                               },
+
+                       'ContentParser' => function ( DependencyBuilder 
$builder ) {
                                        return new ContentParser( 
$builder->getArgument( 'Title' ) );
                                },
+
                        'ObservableUpdateDispatcher' => function ( 
DependencyBuilder $builder ){
                                        return new ObservableSubjectDispatcher( 
$builder->newObject( 'UpdateObserver' ) );
                                }
@@ -124,4 +134,21 @@
                        return $updateObserver;
                };
        }
+
+       /**
+        * BasePropertyAnnotator object definition
+        *
+        * @since  1.9
+        *
+        * @return BasePropertyAnnotator
+        */
+       protected function getBasePropertyAnnotator() {
+               return function ( DependencyBuilder $builder ) {
+                       return new BasePropertyAnnotator(
+                               $builder->getArgument( 'SemanticData' ),
+                               $builder->newObject( 'Settings' )
+                       );
+               };
+       }
+
 }
diff --git a/includes/hooks/InternalParseBeforeLinks.php 
b/includes/hooks/InternalParseBeforeLinks.php
new file mode 100644
index 0000000..a0ed231
--- /dev/null
+++ b/includes/hooks/InternalParseBeforeLinks.php
@@ -0,0 +1,93 @@
+<?php
+
+namespace SMW;
+
+use Parser;
+use Title;
+
+/**
+ * InternalParseBeforeLinks hook
+ *
+ * @file
+ *
+ * @license GNU GPL v2+
+ * @since   1.9
+ *
+ * @author mwjames
+ */
+
+/**
+ * Hook: InternalParseBeforeLinks is used to process the expanded wiki
+ * code after <nowiki>, HTML-comments, and templates have been treated.
+ *
+ * This method will be called before an article is displayed or previewed.
+ * For display and preview we strip out the semantic properties and append them
+ * at the end of the article.
+ *
+ * @note MW 1.20+ see InternalParseBeforeSanitize
+ *
+ * @see http://www.mediawiki.org/wiki/Manual:Hooks/InternalParseBeforeLinks
+ *
+ * @ingroup Hook
+ */
+class InternalParseBeforeLinks extends FunctionHook {
+
+       /** @var Parser */
+       protected $parser = null;
+
+       /** @var string */
+       protected $text;
+
+       /**
+        * @since 1.9
+        *
+        * @param Parser $parser
+        * @param string $text
+        */
+       public function __construct( Parser &$parser, &$text ) {
+               $this->parser = $parser;
+               $this->text =& $text;
+       }
+
+       /**
+        * Performs [[link::syntax]] parsing and adding of property annotations
+        * to the ParserOutput
+        *
+        * @since 1.9
+        *
+        * @return true
+        */
+       protected function performUpdate( Title $title ) {
+
+               /**
+                * @var ParserData $parserData
+                */
+               $parserData = $this->getDependencyBuilder()->newObject( 
'ParserData', array(
+                       'Title'        => $title,
+                       'ParserOutput' => $this->parser->getOutput()
+               ) );
+
+               /**
+                * @var ParserTextProcessor $contentAnnotator
+                */
+               $contentAnnotator = $this->getDependencyBuilder()->newObject( 
'ContentAnnotator', array(
+                       'ParserData' => $parserData
+               ) );
+
+               $contentAnnotator->parse( $this->text );
+
+               return true;
+       }
+
+       /**
+        * @see FunctionHook::process
+        *
+        * @since 1.9
+        *
+        * @return true
+        */
+       public function process() {
+               return !$this->parser->getTitle()->isSpecialPage() ? 
$this->performUpdate( $this->parser->getTitle() ) : true;
+       }
+
+}
diff --git a/includes/hooks/ParserAfterTidy.php 
b/includes/hooks/ParserAfterTidy.php
index 37680c6..c8598b1 100644
--- a/includes/hooks/ParserAfterTidy.php
+++ b/includes/hooks/ParserAfterTidy.php
@@ -3,6 +3,7 @@
 namespace SMW;
 
 use Parser;
+use Title;
 
 /**
  * ParserAfterTidy hook
@@ -43,8 +44,6 @@
        }
 
        /**
-        * @see FunctionHook::process
-        *
         * @note Article purge: In case an article was manually purged/moved
         * the store is updated as well and for all other cases 
LinksUpdateConstructed
         * will handle the store update
@@ -58,23 +57,7 @@
         *
         * @return true
         */
-       public function process() {
-
-               $title = $this->parser->getTitle();
-
-               if ( $title->isSpecialPage() ) {
-                       return true;
-               }
-
-               /**
-                * @var Settings $settings
-                */
-               $settings = $this->getDependencyBuilder()->newObject( 
'Settings' );
-
-               /**
-                * @var CacheHandler $cache
-                */
-               $cache = $this->getDependencyBuilder()->newObject( 
'CacheHandler' );
+       protected function performUpdate( Title $title ) {
 
                /**
                 * @var ParserData $parserData
@@ -84,11 +67,21 @@
                        'ParserOutput' => $this->parser->getOutput()
                ) );
 
-               $annotator = new BasePropertyAnnotator( $parserData->getData(), 
$settings );
-               $annotator->attach( $parserData );
+               /**
+                * @var BasePropertyAnnotator $propertyAnnotator
+                */
+               $propertyAnnotator = $this->getDependencyBuilder()->newObject( 
'BasePropertyAnnotator', array(
+                       'SemanticData' => $parserData->getData(),
+               ) );
 
-               $annotator->addCategories( 
$this->parser->getOutput()->getCategoryLinks() );
-               $annotator->addDefaultSort( $this->parser->getDefaultSort() );
+               $propertyAnnotator->attach( $parserData )
+                       ->addCategories( 
$this->parser->getOutput()->getCategoryLinks() )
+                       ->addDefaultSort( $this->parser->getDefaultSort() );
+
+               /**
+                * @var CacheHandler $cache
+                */
+               $cache = $this->getDependencyBuilder()->newObject( 
'CacheHandler' );
 
                $cache->setKey( ArticlePurge::newIdGenerator( 
$title->getArticleID() ) );
 
@@ -100,4 +93,15 @@
                return true;
        }
 
+       /**
+        * @see FunctionHook::process
+        *
+        * @since 1.9
+        *
+        * @return true
+        */
+       public function process() {
+               return !$this->parser->getTitle()->isSpecialPage() ? 
$this->performUpdate( $this->parser->getTitle() ) : true;
+       }
+
 }
diff --git a/includes/hooks/README.md b/includes/hooks/README.md
new file mode 100644
index 0000000..4c8e7f0
--- /dev/null
+++ b/includes/hooks/README.md
@@ -0,0 +1,19 @@
+[Hooks][hooks] are so-called event handlers that allow costum code to be 
executed. Semantic MediaWiki (SMW) uses several of those hooks to enable 
specific process logic to be integrated with MediaWiki. SMW currently 
integrates the following hooks:
+
+#### ArticlePurge
+ArticlePurge executes during a pruge action of an article and depending on the 
settings is being used to track the manual refresh of a page.
+
+#### BeforePageDisplay
+BeforePageDisplay allows last minute changes to the output page and is being 
used to render a ExportRDF link into each individual article.
+
+#### InternalParseBeforeLinks
+InternalParseBeforeLinks is used to process and expand text content, and for
+identifying and resolving [[link::syntax]] property annotation syntax.
+
+#### LinksUpdateConstructed
+LinksUpdateConstructed is called at the end of LinksUpdate and is used to 
iniate a store update.
+
+#### ParserAfterTidy
+ParserAfterTidy is used to re-introduce content, update base annotation 
(special properties, categories etc.) and in case of a manual article purge 
initiates a store update.
+
+[hooks]: https://www.mediawiki.org/wiki/Hooks "Manual:Hooks"
\ No newline at end of file
diff --git a/tests/phpunit/includes/dic/SharedDependencyContainerTest.php 
b/tests/phpunit/includes/dic/SharedDependencyContainerTest.php
index 7798db7..3993721 100644
--- a/tests/phpunit/includes/dic/SharedDependencyContainerTest.php
+++ b/tests/phpunit/includes/dic/SharedDependencyContainerTest.php
@@ -125,13 +125,25 @@
                $provider[] = array( 'UpdateObserver',             array( 
'\SMW\UpdateObserver'              => array() ) );
                $provider[] = array( 'ObservableUpdateDispatcher', array( 
'\SMW\ObservableSubjectDispatcher' => array() ) );
 
-               $provider[] = array( 'ContentParser' , array( 
'\SMW\ContentParser' => array(
+               $provider[] = array( 'ContentParser', array( 
'\SMW\ContentParser' => array(
                                'Title'        => 
$this->newMockObject()->getMockTitle()
                                )
                        )
                );
 
-               $provider[] = array( 'ParserData' , array( '\SMW\ParserData' => 
array(
+               $provider[] = array( 'ContentAnnotator', array( 
'\SMW\ParserTextProcessor' => array(
+                               'ParserData'  => 
$this->newMockObject()->getMockParserData()
+                               )
+                       )
+               );
+
+               $provider[] = array( 'BasePropertyAnnotator', array( 
'\SMW\BasePropertyAnnotator' => array(
+                               'SemanticData' => 
$this->newMockObject()->getMockSemanticData()
+                               )
+                       )
+               );
+
+               $provider[] = array( 'ParserData', array( '\SMW\ParserData' => 
array(
                                'Title'        => 
$this->newMockObject()->getMockTitle(),
                                'ParserOutput' => 
$this->newMockObject()->getMockParserOutput()
                                )
diff --git a/tests/phpunit/includes/hooks/InternalParseBeforeLinksTest.php 
b/tests/phpunit/includes/hooks/InternalParseBeforeLinksTest.php
new file mode 100644
index 0000000..2aa564a
--- /dev/null
+++ b/tests/phpunit/includes/hooks/InternalParseBeforeLinksTest.php
@@ -0,0 +1,183 @@
+<?php
+
+namespace SMW\Test;
+
+use SMW\SharedDependencyContainer;
+use SMW\InternalParseBeforeLinks;
+
+use Title;
+
+/**
+ * Tests for the InternalParseBeforeLinks class
+ *
+ * @file
+ *
+ * @license GNU GPL v2+
+ * @since   1.9
+ *
+ * @author mwjames
+ */
+
+/**
+ * @covers \SMW\InternalParseBeforeLinks
+ *
+ * @ingroup Test
+ *
+ * @group SMW
+ * @group SMWExtension
+ */
+class InternalParseBeforeLinksTest extends ParserTestCase {
+
+       /**
+        * Returns the name of the class to be tested
+        *
+        * @return string|false
+        */
+       public function getClass() {
+               return '\SMW\InternalParseBeforeLinks';
+       }
+
+       /**
+        * Returns a InternalParseBeforeLinks object
+        *
+        * @since 1.9
+        *
+        * @param Title|null $title
+        * @param &$text
+        *
+        * @return InternalParseBeforeLinks
+        */
+       public function newInstance( &$parser = null, &$text = '' ) {
+
+               if ( $parser === null ) {
+                       $parser = $this->newParser( $this->newTitle(), 
$this->getUser() );
+               }
+
+               $instance = new InternalParseBeforeLinks( $parser, $text );
+               $instance->setDependencyBuilder( $this->newDependencyBuilder( 
new SharedDependencyContainer() ) );
+
+               return $instance;
+       }
+
+       /**
+        * @test InternalParseBeforeLinks::__construct
+        *
+        * @since 1.9
+        */
+       public function testConstructor() {
+               $this->assertInstanceOf( $this->getClass(), 
$this->newInstance() );
+       }
+
+       /**
+        * @test InternalParseBeforeLinks::process
+        * @dataProvider titleDataProvider
+        *
+        * @since 1.9
+        */
+       public function testProcess( $title ) {
+
+               $parser = $this->newParser( $title, $this->getUser() );
+
+               $this->assertTrue(
+                       $this->newInstance( $parser )->process(),
+                       'asserts that process() always returns true'
+               );
+
+       }
+
+       /**
+        * @test InternalParseBeforeLinks::process
+        * @dataProvider textDataProvider
+        *
+        * @see ParserTextProcessorTest
+        *
+        * @since 1.9
+        *
+        * @param array $setup
+        * @param array $expected
+        */
+       public function testSemanticDataParserOuputUpdateIntegration( $setup, 
$expected ) {
+
+               $text     = $setup['text'];
+               $parser   = $this->newParser( $setup['title'], $this->getUser() 
);
+               $instance = $this->newInstance( $parser, $text );
+
+               $instance->getDependencyBuilder()
+                       ->getContainer()
+                       ->registerObject( 'Settings', $this->newSettings( 
$setup['settings'] ) );
+
+               $this->assertTrue(
+                       $instance->process(),
+                       'asserts that process() always returns true'
+               );
+
+               $this->assertEquals(
+                       $expected['resultText'],
+                       $text,
+                       'asserts that the text was modified within expected 
parameters'
+               );
+
+               // Re-read data from the Parser
+               $parserData = $this->getParserData( $parser->getTitle(), 
$parser->getOutput() );
+               $this->assertSemanticData(
+                       $parserData->getData(),
+                       $expected,
+                       'asserts whether the container contains expected 
semantic triples'
+               );
+
+       }
+
+       /**
+        * @return array
+        */
+       public function titleDataProvider() {
+
+               $provider = array();
+
+               // #0 Normal title
+               $provider[] = array( $this->newTitle() );
+
+               // #1 Title is a special page
+               $title = $this->newMockObject( array(
+                       'isSpecialPage'   => true,
+               ) )->getMockTitle();
+
+               $provider[] = array( $title );
+
+               return $provider;
+       }
+
+       /**
+        * @return array
+        */
+       public function textDataProvider() {
+
+               $provider = array();
+
+               // #0 NS_MAIN; [[FooBar...]] with a different caption
+               $provider[] = array(
+                       array(
+                               'title'    => $this->newTitle( NS_MAIN ),
+                               'settings' => array(
+                                       'smwgNamespacesWithSemanticLinks' => 
array( NS_MAIN => true ),
+                                       'smwgLinksInValues' => false,
+                                       'smwgInlineErrors'  => true,
+                               ),
+                               'text'  => 'Lorem ipsum dolor sit &$% 
[[FooBar::dictumst|寒い]]' .
+                                       ' [[Bar::tincidunt semper]] facilisi 
{{volutpat}} Ut quis' .
+                                       ' [[foo::9001]] et Donec.',
+                               ),
+                               array(
+                                       'resultText' => 'Lorem ipsum dolor sit 
&$% [[:Dictumst|寒い]]' .
+                                               ' [[:Tincidunt semper|tincidunt 
semper]] facilisi {{volutpat}} Ut quis' .
+                                               ' [[:9001|9001]] et Donec.',
+                                       'propertyCount' => 3,
+                                       'propertyLabel' => array( 'Foo', 'Bar', 
'FooBar' ),
+                                       'propertyValue' => array( 'Dictumst', 
'Tincidunt semper', '9001' )
+                               )
+               );
+
+               return $provider;
+       }
+
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iccb3cfc77b26744496094036ac4d0c6cd11ba4c8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>

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

Reply via email to