jenkins-bot has submitted this change and it was merged.
Change subject: Some renaming + some tests
......................................................................
Some renaming + some tests
ContentParser add capability to fetch output from the ContentHandler as well
Change-Id: I63b7e4b14556ec650493ecb5ac59304dad2242bf
---
M includes/Setup.php
M includes/jobs/UpdateJob.php
M includes/utilities/ContentParser.php
M tests/phpunit/MockObjectBuilder.php
M tests/phpunit/SemanticMediaWikiTestCase.php
M tests/phpunit/includes/jobs/UpdateJobTest.php
M tests/phpunit/includes/utilities/ContentParserTest.php
7 files changed, 254 insertions(+), 99 deletions(-)
Approvals:
Mwjames: Looks good to me, approved
jenkins-bot: Verified
diff --git a/includes/Setup.php b/includes/Setup.php
index 6a3c6c4..e21e3a3 100644
--- a/includes/Setup.php
+++ b/includes/Setup.php
@@ -165,7 +165,7 @@
$wgAutoloadClasses['SMW\ArrayAccessor'] = $incDir .
'/utilities/ArrayAccessor.php';
$wgAutoloadClasses['SMW\MessageReporter'] = $incDir .
'/utilities/MessageReporter.php';
$wgAutoloadClasses['SMW\ObservableMessageReporter'] = $incDir .
'/utilities/MessageReporter.php';
- $wgAutoloadClasses['SMW\ParserOutputGenerator'] = $incDir .
'/utilities/ContentParser.php';
+ $wgAutoloadClasses['SMW\ContentParser'] = $incDir .
'/utilities/ContentParser.php';
$wgAutoloadClasses['SMW\ChangeObserver'] = $incDir .
'/utilities/ChangeObserver.php';
$wgAutoloadClasses['SMW\TitleProvider'] = $incDir .
'/utilities/MediaWikiInterfaceProvider.php';
diff --git a/includes/jobs/UpdateJob.php b/includes/jobs/UpdateJob.php
index 2a75003..b246efc 100644
--- a/includes/jobs/UpdateJob.php
+++ b/includes/jobs/UpdateJob.php
@@ -55,8 +55,8 @@
*/
class UpdateJob extends JobBase {
- /** @var ParserOutputGenerator */
- protected $outputGenerator = null;
+ /** @var ContentParser */
+ protected $contentParser = null;
/**
* @since 1.9
@@ -86,14 +86,14 @@
return true;
}
- if ( !$this->getOutputGenerator()->getOutput() instanceof
ParserOutput ) {
- $this->setLastError(
$this->getOutputGenerator()->getErrors() );
+ if ( !$this->getContentParser()->getOutput() instanceof
ParserOutput ) {
+ $this->setLastError(
$this->getContentParser()->getErrors() );
return false;
}
Profiler::In( __METHOD__ . '-update' );
- $parserData = new ParserData( $this->getTitle(),
$this->getOutputGenerator()->getOutput() );
+ $parserData = new ParserData( $this->getTitle(),
$this->getContentParser()->getOutput() );
$parserData->disableUpdateJobs();
$parserData->updateStore();
@@ -104,20 +104,20 @@
}
/**
- * Returns a ParserOutputGenerator object
+ * Returns a ContentParser object
*
* @since 1.9
*
- * @return ParserOutputGenerator
+ * @return ContentParser
*/
- protected function getOutputGenerator() {
+ protected function getContentParser() {
- if ( $this->outputGenerator === null ) {
- $this->outputGenerator = new ParserOutputGenerator(
$this->title );
- $this->outputGenerator->generate();
+ if ( $this->contentParser === null ) {
+ $this->contentParser = new ContentParser( $this->title
);
+ $this->contentParser->parse();
}
- return $this->outputGenerator;
+ return $this->contentParser;
}
/**
diff --git a/includes/utilities/ContentParser.php
b/includes/utilities/ContentParser.php
index 3dd579f..cd2f53e 100644
--- a/includes/utilities/ContentParser.php
+++ b/includes/utilities/ContentParser.php
@@ -8,7 +8,7 @@
use Title;
/**
- * Produces a ParserOutput object
+ * Fetch page content
*
* 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
@@ -34,11 +34,13 @@
*/
/**
- * Produces a ParserOutput object
+ * Fetch page content either by parsing an invoked text compenent, reparsing
+ * a text revision, or accessing the ContentHandler to produce a ParserOutput
+ * object
*
- * @ingroup Generator
+ * @ingroup SMW
*/
-class ParserOutputGenerator {
+class ContentParser {
/** @var Title */
protected $title;
@@ -54,6 +56,9 @@
/** @var Parser */
protected $parser = null;
+
+ /** @var string */
+ protected $text = null;
/** @var array */
protected $errors = array();
@@ -79,6 +84,17 @@
}
/**
+ * Returns Title object
+ *
+ * @since 1.9
+ *
+ * @return Title
+ */
+ public function getTitel() {
+ return $this->title;
+ }
+
+ /**
* Returns ParserOutput object
*
* @since 1.9
@@ -90,58 +106,152 @@
}
/**
- * Generates an object from the page content
+ * Invokes text to be parsed
*
* @since 1.9
*
- * @return ParserOutputGenerator
+ * @return ContentParser|null
*/
- public function generate() {
+ public function setText( $text ) {
+ $this->text = $text;
+ return $this;
+ }
- $this->buildFromText();
+ /**
+ * Generates or fetches output content from the appropriate source
+ *
+ * @since 1.9
+ *
+ * @return ContentParser
+ */
+ public function parse() {
+
+ if ( $this->text !== null ) {
+ $this->parseText();
+ } else {
// if ( class_exists( 'ContentHandler') ) {
- // $this->buildFromContent();
- // }
+ // $this->fetchFromContentHandler(); // Add unit
test
+ // } else {}
+ $this->fetchFromParser();
+ }
return $this;
}
/**
- * Generates an object from reparsing the page content
+ * Parsing text
*
* @since 1.9
*/
- protected function buildFromText() {
+ protected function parseText() {
Profiler::In( __METHOD__ );
- // For now nothing can be done to get rid of this global
- if ( $this->parser === null ) {
- $this->parser = $GLOBALS['wgParser'];
+ $this->parserOutput = $this->getParser()->parse( $this->text,
$this->getTitel(), $this->getParserOptions() );
+
+ Profiler::Out( __METHOD__ );
+ }
+
+ /**
+ * Using the content handler to return a ParserOutput object
+ *
+ * @since 1.9
+ */
+ protected function fetchFromContentHandler() {
+ Profiler::In( __METHOD__ );
+
+ $content = $this->getRevision()->getContent( Revision::RAW );
+
+ if ( !$content ) {
+ // if there is no content, pretend the content is empty
+ $content =
$this->getRevision()->getContentHandler()->makeEmptyContent();
}
- if ( $this->revision === null ) {
- $this->revision = Revision::newFromTitle( $this->title
);
- }
+ // Revision ID must be passed to the parser output to get
revision variables correct
+ $this->parserOutput = $content->getParserOutput(
$this->getTitle(), $getRevision()->getId(), null, false );
- if ( $this->revision !== null && $this->parserOptions === null
) {
- $this->parserOptions = new ParserOptions(
User::newFromId( $this->revision->getUser() ) );
- }
+ Profiler::Out( __METHOD__ );
+ }
- if ( $this->revision !== null && $this->parserOptions !== null
) {
- $this->parserOutput = $this->parser->parse(
- $this->revision->getText(),
- $this->title,
- $this->parserOptions,
+ /**
+ * Reparsing page content from a revision
+ *
+ * @since 1.9
+ */
+ protected function fetchFromParser() {
+ Profiler::In( __METHOD__ );
+
+ if ( $this->getRevision() !== null ) {
+ $this->parserOutput = $this->getParser()->parse(
+ $this->getRevision()->getText(),
+ $this->getTitel(),
+ $this->getParserOptions(),
true,
true,
- $this->revision->getID()
+ $this->getRevision()->getID()
);
} else {
- $this->errors = array( __METHOD__ . " No revision
available for {$this->title->getPrefixedDBkey()}" );
+ $this->errors = array( __METHOD__ . " No revision
available for {$this->getTitel()->getPrefixedDBkey()}" );
}
Profiler::Out( __METHOD__ );
}
+ /**
+ * Returns ParserOptions object
+ *
+ * @since 1.9
+ *
+ * @return ParserOptions
+ */
+ protected function getParserOptions() {
+
+ if ( $this->parserOptions === null ) {
+ if ( $this->revision === null ) {
+ $this->parserOptions = new ParserOptions();
+ } else {
+ $this->parserOptions = new ParserOptions(
User::newFromId( $this->revision->getUser() ) );
+ }
+ }
+
+ return $this->parserOptions;
+ }
+
+ /**
+ * Returns Revision object
+ *
+ * @note Revision::READ_NORMAL does not exists in MW 1.19
+ *
+ * @since 1.9
+ *
+ * @return Revision
+ */
+ protected function getRevision() {
+
+ if ( $this->revision === null ) {
+ if ( class_exists( 'ContentHandler') ) {
+ $this->revision = Revision::newFromTitle(
$this->getTitel(), false, Revision::READ_NORMAL );
+ } else{
+ $this->revision = Revision::newFromTitle(
$this->getTitel() );
+ }
+ }
+
+ return $this->revision;
+ }
+
+ /**
+ * Returns Parser object
+ *
+ * @since 1.9
+ *
+ * @return Parser
+ */
+ protected function getParser() {
+
+ if ( $this->parser === null ) {
+ $this->parser = $GLOBALS['wgParser'];
+ }
+
+ return $this->parser;
+ }
}
diff --git a/tests/phpunit/MockObjectBuilder.php
b/tests/phpunit/MockObjectBuilder.php
index aee31eb..8e18525 100644
--- a/tests/phpunit/MockObjectBuilder.php
+++ b/tests/phpunit/MockObjectBuilder.php
@@ -155,27 +155,27 @@
}
/**
- * Returns a ParserOutputGenerator object
+ * Returns a ContentParser object
*
* @since 1.9
*
- * @return ParserOutputGenerator
+ * @return ContentParser
*/
- public function getMockParserOutputGenerator() {
+ public function getMockContentParser() {
- $outputGenerator = $this->getMockBuilder(
'\SMW\ParserOutputGenerator' )
+ $contentParser = $this->getMockBuilder( '\SMW\ContentParser' )
->disableOriginalConstructor()
->getMock();
- $outputGenerator->expects( $this->any() )
+ $contentParser->expects( $this->any() )
->method( 'getOutput' )
->will( $this->returnValue( $this->set( 'getOutput',
$this->getMockParserOutput() ) ) );
- $outputGenerator->expects( $this->any() )
+ $contentParser->expects( $this->any() )
->method( 'getErrors' )
->will( $this->returnValue( $this->set( 'getErrors',
array() ) ) );
- return $outputGenerator;
+ return $contentParser;
}
/**
diff --git a/tests/phpunit/SemanticMediaWikiTestCase.php
b/tests/phpunit/SemanticMediaWikiTestCase.php
index 792bad4..4e984c5 100644
--- a/tests/phpunit/SemanticMediaWikiTestCase.php
+++ b/tests/phpunit/SemanticMediaWikiTestCase.php
@@ -198,6 +198,19 @@
* @return Settings
*/
protected function getSettings( array $settings = array() ) {
+ return $this->newSettings( $settings );
+ }
+
+ /**
+ * Helper method that returns a Settings object
+ *
+ * @since 1.9
+ *
+ * @param array $settings
+ *
+ * @return Settings
+ */
+ protected function newSettings( array $settings = array() ) {
return Settings::newFromArray( $settings );
}
@@ -210,8 +223,22 @@
*
* @return string
*/
- protected function getRandomString( $length = 10 ) {
- return substr( str_shuffle(
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ), 0, $length
);
+ protected function getRandomString( $length = 10, $prefix = null ) {
+ return $this->newRandomString( $length, $prefix );
+ }
+
+ /**
+ * Helper method that returns a random string
+ *
+ * @since 1.9
+ *
+ * @param $length
+ * @param $prefix identify a specific random string during testing
+ *
+ * @return string
+ */
+ protected function newRandomString( $length = 10, $prefix = null ) {
+ return $prefix . ( $prefix ? '-' : '' ) . substr( str_shuffle(
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ), 0, $length
);
}
/**
diff --git a/tests/phpunit/includes/jobs/UpdateJobTest.php
b/tests/phpunit/includes/jobs/UpdateJobTest.php
index f967114..fcdb8d0 100644
--- a/tests/phpunit/includes/jobs/UpdateJobTest.php
+++ b/tests/phpunit/includes/jobs/UpdateJobTest.php
@@ -5,6 +5,7 @@
use SMW\UpdateJob;
use Title;
+
/**
* Tests for the UpdateJob class
*
@@ -91,9 +92,9 @@
$instance = $this->getInstance( $test['title'] );
$instance->setStore( $this->newMockObject()->getMockStore() );
- $outputGenerator = $reflector->getProperty( 'outputGenerator' );
- $outputGenerator->setAccessible( true );
- $outputGenerator->setValue( $instance, $test['outputGenerator']
);
+ $contentParser = $reflector->getProperty( 'contentParser' );
+ $contentParser->setAccessible( true );
+ $contentParser->setValue( $instance, $test['contentParser'] );
$this->assertEquals( $expected['result'], $instance->run() );
}
@@ -115,11 +116,11 @@
$provider[] = array(
array(
- 'title' => $title,
- 'outputGenerator' => null
+ 'title' => $title,
+ 'contentParser' => null
),
array(
- 'result' => true
+ 'result' => true
)
);
@@ -129,17 +130,17 @@
'exists' => true
) )->getMockTitle();
- $outputGenerator = $this->newMockobject( array(
+ $contentParser = $this->newMockobject( array(
'getOutput' => null
- ) )->getMockParserOutputGenerator();
+ ) )->getMockContentParser();
$provider[] = array(
array(
- 'title' => $title,
- 'outputGenerator' => $outputGenerator
+ 'title' => $title,
+ 'contentParser' => $contentParser
),
array(
- 'result' => false
+ 'result' => false
)
);
@@ -149,17 +150,17 @@
'exists' => true
) )->getMockTitle();
- $outputGenerator = $this->newMockobject( array(
+ $contentParser = $this->newMockobject( array(
'getOutput' =>
$this->newMockobject()->getMockParserOutput()
- ) )->getMockParserOutputGenerator();
+ ) )->getMockContentParser();
$provider[] = array(
array(
- 'title' => $title,
- 'outputGenerator' => $outputGenerator
+ 'title' => $title,
+ 'contentParser' => $contentParser
),
array(
- 'result' => true
+ 'result' => true
)
);
diff --git a/tests/phpunit/includes/utilities/ContentParserTest.php
b/tests/phpunit/includes/utilities/ContentParserTest.php
index 42845de..ab60ae7 100644
--- a/tests/phpunit/includes/utilities/ContentParserTest.php
+++ b/tests/phpunit/includes/utilities/ContentParserTest.php
@@ -2,12 +2,12 @@
namespace SMW\Test;
-use SMW\ParserOutputGenerator;
+use SMW\ContentParser;
use Title;
/**
- * Tests for the ParserOutputGenerator class
+ * Tests for the ContentParser 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
@@ -33,14 +33,14 @@
*/
/**
- * @covers \SMW\ParserOutputGenerator
+ * @covers \SMW\ContentParser
*
* @ingroup Test
*
* @group SMW
* @group SMWExtension
*/
-class ParserOutputGeneratorTest extends ParserTestCase {
+class ContentParserTest extends ParserTestCase {
/**
* Returns the name of the class to be tested
@@ -48,22 +48,22 @@
* @return string|false
*/
public function getClass() {
- return '\SMW\ParserOutputGenerator';
+ return '\SMW\ContentParser';
}
/**
- * Helper method that returns a ParserOutputGenerator object
+ * Helper method that returns a ContentParser object
*
* @since 1.9
*
- * @return ParserOutputGenerator
+ * @return ContentParser
*/
private function getInstance( Title $title = null ) {
- return new ParserOutputGenerator( $title === null ?
$this->newTitle() : $title );
+ return new ContentParser( $title === null ? $this->newTitle() :
$title );
}
/**
- * @test ParserOutputGenerator::__construct
+ * @test ContentParser::__construct
*
* @since 1.9
*/
@@ -72,31 +72,47 @@
}
/**
- * @test ParserOutputGenerator::generate
+ * @test ContentParser::parse
+ *
+ * @since 1.9
+ */
+ public function testParseFromText() {
+
+ $text = $this->newRandomString( 20, __METHOD__ );
+ $expected ='<p>' . $text . "\n" . '</p>';
+
+ $instance = $this->getInstance();
+ $instance->setText( $text )->parse();
+
+ $this->assertInstanceOf( 'ParserOutput', $instance->getOutput()
);
+ $this->assertEquals( $expected,
$instance->getOutput()->getText() );
+
+ }
+
+ /**
+ * @test ContentParser::parse
* @dataProvider titleRevisionDataProvider
*
* @since 1.9
*/
- public function testRun( $test, $expected ) {
+ public function testParseFromRevision( $setup, $expected ) {
$reflector = $this->newReflector();
- $instance = $this->getInstance( $test['title'] );
+ $instance = $this->getInstance( $setup['title'] );
$revision = $reflector->getProperty( 'revision' );
$revision->setAccessible( true );
- $revision->setValue( $instance, $test['revision'] );
+ $revision->setValue( $instance, $setup['revision'] );
- $options = $reflector->getProperty( 'parserOptions' );
- $options->setAccessible( true );
- $options->setValue( $instance, $test['parserOptions'] );
-
- $instance->generate();
+ $instance->parse();
if ( $expected['error'] ) {
$this->assertInternalType( 'array',
$instance->getErrors() );
} else {
$this->assertInstanceOf( 'ParserOutput',
$instance->getOutput() );
+ $this->assertEquals( $expected['text'],
$instance->getOutput()->getText() );
}
+
}
/**
@@ -106,50 +122,51 @@
*/
public function titleRevisionDataProvider() {
- // Mocking this object was not really an option as
- // the Parser is quite complex
- $parserOptions = new \ParserOptions();
- $parserOptions->setTargetLanguage( $this->getLanguage() );
-
$provider = array();
+
+ $text = $this->newRandomString( 20, __METHOD__ );
+ $expected ='<p>' . $text . "\n" . '</p>';
// #0 Title does not exists
$title = $this->newMockObject( array(
- 'getDBkey' => 'Lila',
- 'exists' => false
+ 'getDBkey' => 'Lila',
+ 'exists' => false,
+ 'getText' => null,
+ 'getPageLanguage' => $this->getLanguage()
) )->getMockTitle();
$provider[] = array(
array(
'title' => $title,
'revision' => null,
- 'parserOptions' => null
),
array(
- 'error' => true
+ 'error' => true,
+ 'text' => ''
)
);
- // #1 Valid revision generates a valid ParserOuput object
+ // #1 Valid revision
$title = $this->newMockObject( array(
- 'getDBkey' => 'Lula',
- 'exists' => true
+ 'getDBkey' => 'Lula',
+ 'exists' => true,
+ 'getPageLanguage' => $this->getLanguage()
) )->getMockTitle();
$revision = $this->newMockObject( array(
'getId' => 9001,
'getUser' => 'Lala',
- 'getText' => 'Lala',
+ 'getText' => $text,
) )->getMockRevision();
$provider[] = array(
array(
'title' => $title,
'revision' => $revision,
- 'parserOptions' => $parserOptions
),
array(
- 'error' => false
+ 'error' => false,
+ 'text' => $expected
)
);
--
To view, visit https://gerrit.wikimedia.org/r/76322
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I63b7e4b14556ec650493ecb5ac59304dad2242bf
Gerrit-PatchSet: 3
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