Mwjames has uploaded a new change for review.
https://gerrit.wikimedia.org/r/74095
Change subject: (Bug 51498) SMW\RedirectBuilder move responsibility out of
ParserTextProcessor
......................................................................
(Bug 51498) SMW\RedirectBuilder move responsibility out of ParserTextProcessor
Change-Id: Ie4b95c0db250f01c25c71d312a4f8af8b812bc71
---
M includes/ParserTextProcessor.php
M includes/Setup.php
A includes/utilities/RedirectBuilder.php
M tests/phpunit/MockObjectBuilder.php
M tests/phpunit/includes/ParserTextProcessorTest.php
A tests/phpunit/includes/utilities/RedirectBuilderTest.php
6 files changed, 348 insertions(+), 62 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticMediaWiki
refs/changes/95/74095/1
diff --git a/includes/ParserTextProcessor.php b/includes/ParserTextProcessor.php
index 7cf3446..df4fd5e 100644
--- a/includes/ParserTextProcessor.php
+++ b/includes/ParserTextProcessor.php
@@ -2,14 +2,12 @@
namespace SMW;
+use SMWOutputs;
+
+use SpecialPage;
use MagicWord;
use Title;
use Html;
-use SpecialPage;
-
-use SMWOutputs;
-use SMWDIWikiPage;
-use SMWDIProperty;
/**
* Class collects all functions for wiki text parsing / processing that are
@@ -119,8 +117,9 @@
// Attest if semantic data should be processed
$this->isEnabled = NamespaceExaminer::newFromArray(
$this->settings->get( 'smwgNamespacesWithSemanticLinks' ) )->isSemanticEnabled(
$title->getNamespace() );
- // Process redirects
- $this->setRedirect( $title );
+ // Build redirect
+ $redirect = new RedirectBuilder( $this->parserData->getData() );
+ $redirect->canBuild( $this->isEnabled )->build( $text );
// Parse links to extract semantic properties
$linksInValues = $this->settings->get( 'smwgLinksInValues' );
@@ -148,22 +147,6 @@
'ext.smw.style',
'ext.smw.tooltips'
);
- }
-
- /**
- * Process and add '_REDI' property in case the current Title is a
redirect
- *
- * @since 1.9
- *
- * @param Title $title
- */
- protected function setRedirect( Title $title ) {
- if ( $this->isEnabled && $title->isRedirect() ) {
- $this->parserData->getData()->addPropertyObjectValue(
- new SMWDIProperty( '_REDI' ),
- SMWDIWikiPage::newFromTitle( $title, '__red' )
- );
- }
}
/**
diff --git a/includes/Setup.php b/includes/Setup.php
index e1cf91a..2259b15 100644
--- a/includes/Setup.php
+++ b/includes/Setup.php
@@ -148,10 +148,6 @@
$wgAutoloadClasses['SMW\RecurringEvents'] = $incDir .
'RecurringEvents.php';
$wgAutoloadClasses['SMW\Settings'] = $incDir .
'Settings.php';
- $wgAutoloadClasses['SMW\NamespaceExaminer'] = $incDir .
'NamespaceExaminer.php';
- $wgAutoloadClasses['SMW\Profiler'] = $incDir .
'Profiler.php';
- $wgAutoloadClasses['SMW\IdGenerator'] = $incDir .
'HashIdGenerator.php';
- $wgAutoloadClasses['SMW\HashIdGenerator'] = $incDir .
'HashIdGenerator.php';
$wgAutoloadClasses['SMW\Accessor'] = $incDir .
'ArrayAccessor.php';
$wgAutoloadClasses['SMW\Arrayable'] = $incDir .
'ArrayAccessor.php';
@@ -161,6 +157,14 @@
$wgAutoloadClasses['SMW\ResultCacheMapper'] = $incDir .
'/cache/ResultCacheMapper.php';
$wgAutoloadClasses['SMW\CacheIdGenerator'] = $incDir .
'/cache/CacheIdGenerator.php';
+ // Utilities
+ $wgAutoloadClasses['SMW\NamespaceExaminer'] = $incDir .
'NamespaceExaminer.php';
+ $wgAutoloadClasses['SMW\Profiler'] = $incDir .
'Profiler.php';
+ $wgAutoloadClasses['SMW\IdGenerator'] = $incDir .
'HashIdGenerator.php';
+ $wgAutoloadClasses['SMW\HashIdGenerator'] = $incDir .
'HashIdGenerator.php';
+
+ $wgAutoloadClasses['SMW\RedirectBuilder'] = $incDir .
'/utilities/RedirectBuilder.php';
+
// Formatters
$wgAutoloadClasses['SMW\ArrayFormatter'] = $incDir .
'formatters/ArrayFormatter.php';
$wgAutoloadClasses['SMW\ParserParameterFormatter'] = $incDir .
'formatters/ParserParameterFormatter.php';
diff --git a/includes/utilities/RedirectBuilder.php
b/includes/utilities/RedirectBuilder.php
new file mode 100644
index 0000000..9637b39
--- /dev/null
+++ b/includes/utilities/RedirectBuilder.php
@@ -0,0 +1,130 @@
+<?php
+
+namespace SMW;
+
+use SMWSemanticData;
+
+use ContentHandler;
+use Title;
+
+/**
+ * Process and builds a '_REDI' property where a redirect
+ * object is availavle
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ *
+ * @license GNU GPL v2+
+ * @since 1.9
+ *
+ * @author mwjames
+ */
+
+/**
+ * Process and builds a '_REDI' property where a redirect
+ * object is availavle
+ *
+ * @ingroup Utility
+ * @ingroup Builder
+ */
+class RedirectBuilder {
+
+ /** @var SMWSemanticData */
+ protected $semanticData = null;
+
+ /** @var boolean */
+ protected $canBuild = true;
+
+ /**
+ * @since 1.9
+ *
+ * @param SMWSemanticData $semanticData
+ */
+ public function __construct( SMWSemanticData $semanticData ) {
+ $this->semanticData = $semanticData;
+ }
+
+ /**
+ * Indicates if the current object instance can
+ * build a redirect
+ *
+ * @since 1.9
+ *
+ * @return string|null
+ */
+ public function canBuild( $canBuild = true ) {
+ $this->canBuild = $canBuild;
+ return $this;
+ }
+
+ /**
+ * Builds a '_REDI' property and attaches it to the
+ * available semantic data container
+ *
+ * @par Example:
+ * @code
+ * $redirect = new RedirectBuilder( $semanticData );
+ * $redirect->canBuild( true )->build( $text );
+ * @endcode
+ *
+ * @since 1.9
+ *
+ * @param vargs
+ *
+ * @return string
+ */
+ public function build( /* ... */ ) {
+
+ $argument = func_get_arg( 0 );
+
+ if ( is_string( $argument ) ) {
+ $title = $this->buildFromText( $argument );
+ } else if ( $argument instanceof Title ) {
+ $title = $argument;
+ } else {
+ $title = null;
+ }
+
+ if ( $this->canBuild && $title !== null ) {
+ $this->semanticData->addPropertyObjectValue( new
DIProperty( '_REDI' ), DIWikiPage::newFromTitle( $title, '__red' ) );
+ }
+
+ }
+
+ /**
+ * Extract a redirect destination from a string and return the Title,
+ * or null if the text doesn't contain a valid redirect
+ *
+ * @since 1.9
+ *
+ * @param string $text
+ *
+ * @return Title|null
+ */
+ protected function buildFromText( $text ) {
+
+ if ( class_exists( 'ContentHandler' ) ) {
+ $title = ContentHandler::makeContent( $text, null,
CONTENT_MODEL_WIKITEXT )->getRedirectTarget();
+ } else {
+ // @codeCoverageIgnoreStart
+ $title = Title::newFromRedirect( $text );
+ // @codeCoverageIgnoreEnd
+ }
+
+ return $title;
+ }
+}
diff --git a/tests/phpunit/MockObjectBuilder.php
b/tests/phpunit/MockObjectBuilder.php
index 7479de6..64772fc 100644
--- a/tests/phpunit/MockObjectBuilder.php
+++ b/tests/phpunit/MockObjectBuilder.php
@@ -70,6 +70,35 @@
}
/**
+ * Returns a SemanticData object
+ *
+ * @since 1.9
+ *
+ * @return SemanticData
+ */
+ public function getMockSemanticData() {
+
+ $semanticData = $this->getMockBuilder( 'SMWSemanticData' )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $semanticData->expects( $this->any() )
+ ->method( 'getSubject' )
+ ->will( $this->returnValue( $this->set( 'getSubject' )
) );
+
+ // array of SMWDataItem
+ $semanticData->expects( $this->any() )
+ ->method( 'getPropertyValues' )
+ ->will( $this->returnValue( $this->set(
'getPropertyValues' ) ) );
+
+ $semanticData->expects( $this->any() )
+ ->method( 'addPropertyObjectValue' )
+ ->will( is_callable( $this->set(
'addPropertyObjectValue' ) ) ? $this->returnCallback( $this->set(
'addPropertyObjectValue' ) ) : $this->returnValue( $this->set(
'addPropertyObjectValue' ) ) );
+
+ return $semanticData;
+ }
+
+ /**
* Returns a SMWQuery object
*
* @since 1.9
diff --git a/tests/phpunit/includes/ParserTextProcessorTest.php
b/tests/phpunit/includes/ParserTextProcessorTest.php
index dc401ec..eddd449 100644
--- a/tests/phpunit/includes/ParserTextProcessorTest.php
+++ b/tests/phpunit/includes/ParserTextProcessorTest.php
@@ -326,56 +326,33 @@
}
/**
- * @test ParserTextProcessor::setRedirect
+ * @test ParserTextProcessor::parse
*
* @since 1.9
*/
- public function testSetRedirect() {
+ public function testRedirect() {
+
$namespace = NS_MAIN;
-
- // Mock Title object to avoid DB access
- $mockTitle = $this->getMock( 'Title' );
-
- // Attach isRedirect method
- $mockTitle->expects( $this->any() )
- ->method( 'isRedirect' )
- ->will( $this->returnValue( true )
- );
-
- // Attach getNamespace method
- $mockTitle->expects( $this->any() )
- ->method( 'getNamespace' )
- ->will( $this->returnValue( $namespace )
- );
+ $text = '#REDIRECT [[:Lala]]';
// Create text processor instance
$parserOutput = $this->getParserOutput();
$title = $this->getTitle( $namespace );
- $settings = array(
- 'smwgNamespacesWithSemanticLinks' => array( $namespace
=> true )
- );
+ $settings = $this->getSettings( array(
+ 'smwgNamespacesWithSemanticLinks' => array( $namespace
=> true ),
+ 'smwgLinksInValues' => false,
+ 'smwgInlineErrors' => true,
+ ) );
$parserData = $this->getParserData( $title, $parserOutput );
- $instance = new ParserTextProcessor(
- $parserData,
- $this->getSettings( $settings )
- );
- // Make protected methods accessible
- $reflection = new ReflectionClass( $this->getClass() );
-
- $property = $reflection->getProperty( 'isEnabled' );
- $property->setAccessible( true );
- $property->setValue( $instance, true );
-
- $method = $reflection->getMethod( 'setRedirect' );
- $method->setAccessible( true );
- $result = $method->invoke( $instance, $mockTitle );
+ $instance = new ParserTextProcessor( $parserData, $settings );
+ $instance->parse( $text );
// Build expected results from a successful setRedirect
execution
$expected['propertyCount'] = 1;
$expected['propertyKey'] = '_REDI';
- $expected['propertyValue'] = ':' . $title->getText();
+ $expected['propertyValue'] = ':Lala';
// Check the returned instance
$this->assertInstanceOf( 'SMWSemanticData',
$parserData->getData() );
diff --git a/tests/phpunit/includes/utilities/RedirectBuilderTest.php
b/tests/phpunit/includes/utilities/RedirectBuilderTest.php
new file mode 100644
index 0000000..2869716
--- /dev/null
+++ b/tests/phpunit/includes/utilities/RedirectBuilderTest.php
@@ -0,0 +1,163 @@
+<?php
+
+namespace SMW\Test;
+
+use SMW\RedirectBuilder;
+use SMW\DIProperty;
+
+use SMWSemanticData;
+
+/**
+ * Tests for the RedirectBuilder class
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ *
+ * @license GNU GPL v2+
+ * @since 1.9
+ *
+ * @author mwjames
+ */
+
+/**
+ * @covers \SMW\RedirectBuilder
+ *
+ * @ingroup Test
+ *
+ * @group SMW
+ * @group SMWExtension
+ */
+class RedirectBuilderTest extends SemanticMediaWikiTestCase {
+
+ /**
+ * Returns the name of the class to be tested
+ *
+ * @return string|false
+ */
+ public function getClass() {
+ return '\SMW\RedirectBuilder';
+ }
+
+ /**
+ * Helper method that returns a RedirectBuilder object
+ *
+ * @since 1.9
+ *
+ * @param $data
+ *
+ * @return RedirectBuilder
+ */
+ private function getInstance( SMWSemanticData $data = null ) {
+ return new RedirectBuilder( $data === null ?
$this->newMockObject()->getMockSemanticData() : $data );
+ }
+
+ /**
+ * @test RedirectBuilder::__construct
+ *
+ * @since 1.9
+ */
+ public function testConstructor() {
+ $this->assertInstanceOf( $this->getClass(),
$this->getInstance() );
+ }
+
+ /**
+ * @test RedirectBuilder::build
+ * @dataProvider redirectsDataProvider
+ *
+ * @since 1.9
+ *
+ * @param $test
+ * @param $expected
+ */
+ public function testBuild( $test, $expected ) {
+
+ $semanticData = new SMWSemanticData( $this->getSubject() );
+
+ $instance = $this->getInstance( $semanticData );
+ $instance->canBuild( $test['canBuild'] )->build( $test['build']
);
+
+ $this->assertSemanticData( $semanticData, $expected );
+ }
+
+
+ /**
+ * Provides redirects injection sample
+ *
+ * @return array
+ */
+ public function redirectsDataProvider() {
+
+ $title = $this->getTitle();
+
+ // #0 Title
+ $provider[] = array(
+ array( 'build' => $title, 'canBuild' => true ),
+ array(
+ 'propertyCount' => 1,
+ 'propertyKey' => '_REDI',
+ 'propertyValue' => ':' . $title->getText()
+ )
+ );
+
+ // #1 Disabled
+ $provider[] = array(
+ array( 'build' => $title, 'canBuild' => false ),
+ array(
+ 'propertyCount' => 0,
+ )
+ );
+
+ // #2 Free text
+ $provider[] = array(
+ array( 'build' => '#REDIRECT [[:Lala]]', 'canBuild' =>
true ),
+ array(
+ 'propertyCount' => 1,
+ 'propertyKey' => '_REDI',
+ 'propertyValue' => ':Lala'
+ )
+ );
+
+ // #3 Free text
+ $provider[] = array(
+ array( 'build' => '#REDIRECT [[Lala]]', 'canBuild' =>
true ),
+ array(
+ 'propertyCount' => 1,
+ 'propertyKey' => '_REDI',
+ 'propertyValue' => ':Lala'
+ )
+ );
+
+ // #4 Disabled
+ $provider[] = array(
+ array( 'build' => '#REDIRECT [[:Lala]]', 'canBuild' =>
false ),
+ array(
+ 'propertyCount' => 0,
+ )
+ );
+
+ // #5 Invalid free text
+ $provider[] = array(
+ array( 'build' => '#REDIR [[:Lala]]', 'canBuild' =>
true ),
+ array(
+ 'propertyCount' => 0,
+ )
+ );
+
+ return $provider;
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/74095
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie4b95c0db250f01c25c71d312a4f8af8b812bc71
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