Mwjames has uploaded a new change for review.

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


Change subject: Some renaming + some tests
......................................................................

Some renaming + some tests

Rename ParserOutputGenerator to ContentParser, and adding capability
to fetch output from the ContentHandler as well

Change-Id: I63b7e4b14556ec650493ecb5ac59304dad2242bf
---
M includes/Setup.php
M includes/jobs/UpdateJob.php
A includes/utilities/ContentParser.php
D includes/utilities/ParserOutputGenerator.php
M tests/phpunit/MockObjectBuilder.php
M tests/phpunit/SemanticMediaWikiTestCase.php
M tests/phpunit/includes/jobs/UpdateJobTest.php
R tests/phpunit/includes/utilities/ContentParserTest.php
8 files changed, 356 insertions(+), 200 deletions(-)


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

diff --git a/includes/Setup.php b/includes/Setup.php
index 76296ee..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/ParserOutputGenerator.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
new file mode 100644
index 0000000..2e99456
--- /dev/null
+++ b/includes/utilities/ContentParser.php
@@ -0,0 +1,257 @@
+<?php
+
+namespace SMW;
+
+use ParserOptions;
+use Revision;
+use User;
+use Title;
+
+/**
+ * Fetching page content by either reparsing text or accessing the
+ * ContentHandlerto produce a ParserOutput object
+ *
+ * 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
+ */
+
+/**
+ * Fetching page content by either reparsing text or accessing the
+ * ContentHandlerto produce a ParserOutput object
+ *
+ * @ingroup SMW
+ */
+class ContentParser {
+
+       /** @var Title */
+       protected $title;
+
+       /** @var ParserOutput */
+       protected $parserOutput = null;
+
+       /** @var Revision */
+       protected $revision = null;
+
+       /** @var ParserOptions */
+       protected $parserOptions = null;
+
+       /** @var Parser */
+       protected $parser = null;
+
+       /** @var string */
+       protected $text = null;
+
+       /** @var array */
+       protected $errors = array();
+
+       /**
+        * @since 1.9
+        *
+        * @param Title $title
+        */
+       public function __construct( Title $title ) {
+               $this->title = $title;
+       }
+
+       /**
+        * Returns collected errors occurred during processing
+        *
+        * @since 1.9
+        *
+        * @return array
+        */
+       public function getErrors() {
+               return $this->errors;
+       }
+
+       /**
+        * Returns Title object
+        *
+        * @since 1.9
+        *
+        * @return Title
+        */
+       public function getTitel() {
+               return $this->title;
+       }
+
+       /**
+        * Returns ParserOutput object
+        *
+        * @since 1.9
+        *
+        * @return ParserOutput|null
+        */
+       public function getOutput() {
+               return $this->parserOutput;
+       }
+
+       /**
+        * Invokes text to be parsed
+        *
+        * @since 1.9
+        *
+        * @return ContentParser|null
+        */
+       public function setText( $text ) {
+               $this->text = $text;
+               return $this;
+       }
+
+       /**
+        * 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->fetchFromContentHandler(); // Add unit 
test
+               //      } else {}
+                       $this->fetchFromParser();
+               }
+
+               return $this;
+       }
+
+       /**
+        * Parsing text
+        *
+        * @since 1.9
+        */
+       protected function parseText() {
+               Profiler::In( __METHOD__ );
+
+               $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();
+               }
+
+               // Revision ID must be passed to the parser output to get 
revision variables correct
+               $this->parserOutput = $content->getParserOutput( 
$this->getTitle(), $getRevision()->getId(), null, false );
+
+               Profiler::Out( __METHOD__ );
+       }
+
+       /**
+        * 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->getRevision()->getID()
+                       );
+               } else {
+                       $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->getRevision() === null ) {
+                               $this->parserOptions = new ParserOptions();
+                       } else {
+                               $this->parserOptions = new ParserOptions( 
User::newFromId( $this->getRevision()->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/includes/utilities/ParserOutputGenerator.php 
b/includes/utilities/ParserOutputGenerator.php
deleted file mode 100644
index 3dd579f..0000000
--- a/includes/utilities/ParserOutputGenerator.php
+++ /dev/null
@@ -1,147 +0,0 @@
-<?php
-
-namespace SMW;
-
-use ParserOptions;
-use Revision;
-use User;
-use Title;
-
-/**
- * Produces a ParserOutput object
- *
- * 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
- */
-
-/**
- * Produces a ParserOutput object
- *
- * @ingroup Generator
- */
-class ParserOutputGenerator {
-
-       /** @var Title */
-       protected $title;
-
-       /** @var ParserOutput */
-       protected $parserOutput = null;
-
-       /** @var Revision */
-       protected $revision = null;
-
-       /** @var ParserOptions */
-       protected $parserOptions = null;
-
-       /** @var Parser */
-       protected $parser = null;
-
-       /** @var array */
-       protected $errors = array();
-
-       /**
-        * @since 1.9
-        *
-        * @param Title $title
-        */
-       public function __construct( Title $title ) {
-               $this->title = $title;
-       }
-
-       /**
-        * Returns collected errors occurred during processing
-        *
-        * @since 1.9
-        *
-        * @return array
-        */
-       public function getErrors() {
-               return $this->errors;
-       }
-
-       /**
-        * Returns ParserOutput object
-        *
-        * @since 1.9
-        *
-        * @return ParserOutput|null
-        */
-       public function getOutput() {
-               return $this->parserOutput;
-       }
-
-       /**
-        * Generates an object from the page content
-        *
-        * @since 1.9
-        *
-        * @return ParserOutputGenerator
-        */
-       public function generate() {
-
-               $this->buildFromText();
-
-               //      if ( class_exists( 'ContentHandler') ) {
-               //              $this->buildFromContent();
-               //      }
-
-               return $this;
-       }
-
-       /**
-        * Generates an object from reparsing the page content
-        *
-        * @since 1.9
-        */
-       protected function buildFromText() {
-               Profiler::In( __METHOD__ );
-
-               // For now nothing can be done to get rid of this global
-               if ( $this->parser === null ) {
-                       $this->parser = $GLOBALS['wgParser'];
-               }
-
-               if ( $this->revision === null ) {
-                       $this->revision = Revision::newFromTitle( $this->title 
);
-               }
-
-               if ( $this->revision !== null && $this->parserOptions === null 
) {
-                       $this->parserOptions = new ParserOptions( 
User::newFromId( $this->revision->getUser() ) );
-               }
-
-               if ( $this->revision !== null && $this->parserOptions !== null 
) {
-                       $this->parserOutput = $this->parser->parse(
-                               $this->revision->getText(),
-                               $this->title,
-                               $this->parserOptions,
-                               true,
-                               true,
-                               $this->revision->getID()
-                       );
-               } else {
-                       $this->errors = array( __METHOD__ . " No revision 
available for {$this->title->getPrefixedDBkey()}" );
-               }
-
-               Profiler::Out( __METHOD__ );
-       }
-
-}
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/ParserOutputGeneratorTest.php 
b/tests/phpunit/includes/utilities/ContentParserTest.php
similarity index 73%
rename from tests/phpunit/includes/utilities/ParserOutputGeneratorTest.php
rename to tests/phpunit/includes/utilities/ContentParserTest.php
index 42845de..d71377c 100644
--- a/tests/phpunit/includes/utilities/ParserOutputGeneratorTest.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,25 +72,43 @@
        }
 
        /**
-        * @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'] );
+               $options->setValue( $instance, $setup['parserOptions'] );
 
-               $instance->generate();
+               $instance->parse();
 
                if ( $expected['error'] ) {
                        $this->assertInternalType( 'array', 
$instance->getErrors() );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I63b7e4b14556ec650493ecb5ac59304dad2242bf
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