Mwjames has uploaded a new change for review.
https://gerrit.wikimedia.org/r/74840
Change subject: \SMW\JobBase improve testability and eliminate GLOBALS
......................................................................
\SMW\JobBase improve testability and eliminate GLOBALS
* Use JobBase to access general purpose Setter/Getter
* Move SMWUpdateJob to SMW\UpdateJob (SMWUpdateJob object is still accessible)
* Eliminate global wgParser from UpdateJob
Change-Id: I3b41d4e506a2c425903ec3690f3500dcec939ac4
---
M includes/Setup.php
A includes/jobs/JobBase.php
M includes/jobs/PropertySubjectsUpdateDispatcherJob.php
D includes/jobs/SMW_UpdateJob.php
A includes/jobs/UpdateJob.php
M tests/phpunit/MockObjectBuilder.php
M tests/phpunit/includes/jobs/UpdateJobTest.php
7 files changed, 261 insertions(+), 127 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticMediaWiki
refs/changes/40/74840/1
diff --git a/includes/Setup.php b/includes/Setup.php
index c49793e..17b4054 100644
--- a/includes/Setup.php
+++ b/includes/Setup.php
@@ -417,9 +417,10 @@
$wgAutoloadClasses['SMW\Test\CompatibilityTestCase'] =
$testsDir . 'CompatibilityTestCase.php';
// Jobs
- $wgJobClasses['SMWUpdateJob'] = 'SMWUpdateJob';
- $wgAutoloadClasses['SMWUpdateJob'] = $smwgIP .
'includes/jobs/SMW_UpdateJob.php';
- $wgAutoloadClasses['SMW\UpdateJob'] = $smwgIP .
'includes/jobs/SMW_UpdateJob.php'; // 1.9
+ $wgAutoloadClasses['SMW\JobBase'] = $smwgIP .
'includes/jobs/JobBase.php';
+ $wgJobClasses['SMW\UpdateJob'] = 'SMW\UpdateJob';
+ $wgAutoloadClasses['SMWUpdateJob'] = $smwgIP .
'includes/jobs/UpdateJob.php';
+ $wgAutoloadClasses['SMW\UpdateJob'] = $smwgIP .
'includes/jobs/UpdateJob.php'; // 1.9
$wgJobClasses['SMWRefreshJob'] = 'SMWRefreshJob';
$wgAutoloadClasses['SMWRefreshJob'] = $smwgIP .
'includes/jobs/SMW_RefreshJob.php';
diff --git a/includes/jobs/JobBase.php b/includes/jobs/JobBase.php
new file mode 100644
index 0000000..d4cabd8
--- /dev/null
+++ b/includes/jobs/JobBase.php
@@ -0,0 +1,83 @@
+<?php
+
+namespace SMW;
+
+use Job;
+
+/**
+ * Job base 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
+ */
+
+/**
+ * Job base class
+ *
+ * @ingroup Job
+ */
+abstract class JobBase extends Job {
+
+ /** $var Store */
+ protected $store = null;
+
+ /** $var Settings */
+ protected $settings = null;
+
+ /**
+ * Sets Store object
+ *
+ * @since 1.9
+ *
+ * @param Store $store
+ */
+ public function setStore( Store $store ) {
+ $this->store = $store;
+ }
+
+ /**
+ * Sets Settings object
+ *
+ * @since 1.9
+ *
+ * @param Store $store
+ */
+ public function setSettings( Settings $settings ) {
+ $this->settings = $settings;
+ }
+
+ /**
+ * Returns settings object
+ *
+ * @since 1.9
+ *
+ * @return Settings
+ */
+ public function getSettings() {
+
+ if ( $this->settings === null ) {
+ $this->settings = Settings::newFromGlobals();
+ }
+
+ return $this->settings;
+ }
+}
diff --git a/includes/jobs/PropertySubjectsUpdateDispatcherJob.php
b/includes/jobs/PropertySubjectsUpdateDispatcherJob.php
index e0d4142..284495e 100644
--- a/includes/jobs/PropertySubjectsUpdateDispatcherJob.php
+++ b/includes/jobs/PropertySubjectsUpdateDispatcherJob.php
@@ -39,10 +39,7 @@
* @ingroup Job
* @ingroup Dispatcher
*/
-class PropertySubjectsUpdateDispatcherJob extends Job {
-
- /** $var Store */
- protected $store = null;
+class PropertySubjectsUpdateDispatcherJob extends JobBase {
/** $var Job */
protected $jobs = array();
@@ -60,17 +57,6 @@
public function __construct( Title $title, $params = array(), $id = 0 )
{
parent::__construct( 'SMW\PropertySubjectsUpdateDispatcherJob',
$title, $params, $id );
$this->store = StoreFactory::getStore( isset( $params['store']
) ? $params['store'] : null );
- }
-
- /**
- * Sets Store object
- *
- * @since 1.9
- *
- * @param Store $store
- */
- public function setStore( Store $store ) {
- $this->store = $store;
}
/**
diff --git a/includes/jobs/SMW_UpdateJob.php b/includes/jobs/SMW_UpdateJob.php
deleted file mode 100644
index 7d0e471..0000000
--- a/includes/jobs/SMW_UpdateJob.php
+++ /dev/null
@@ -1,107 +0,0 @@
-<?php
-/**
- * File containing SMWUpdateJob.
- *
- * @author Daniel M. Herzig
- * @author Markus Krötzsch
- * @file
- * @ingroup SMW
- */
-
-/**
- * SMWUpdateJob updates the semantic data in the database for a given title
- * using the MediaWiki JobQueue. Update jobs are created if, when saving an
article,
- * it is detected that the content of other pages must be re-parsed as well
(e.g.
- * due to some type change).
- *
- * @note This job does not update the page display or parser cache, so in
general
- * it might happen that part of the wiki page still displays based on old data
(e.g.
- * formatting in-page values based on a datatype thathas since been changed),
whereas
- * the Factbox and query/browsing interfaces might already show the updated
records.
- *
- * @ingroup SMW
- */
-class SMWUpdateJob extends Job {
-
- function __construct( Title $title ) {
- parent::__construct( 'SMWUpdateJob', $title );
- }
-
- /**
- * Returns Title object
- *
- * @since 1.9
- *
- * @return \Title
- */
- public function getTitle() {
- return $this->title;
- }
-
- /**
- * Run job
- * @return boolean success
- */
- function run() {
- wfProfileIn( 'SMWUpdateJob::run (SMW)' );
- global $wgParser;
-
- LinkCache::singleton()->clear();
-
- if ( is_null( $this->title ) ) {
- $this->error = "SMWUpdateJob: Invalid title";
- wfProfileOut( 'SMWUpdateJob::run (SMW)' );
- return false;
- } elseif ( !$this->title->exists() ) {
- smwfGetStore()->deleteSubject( $this->title ); // be
sure to clear the data
- wfProfileOut( 'SMWUpdateJob::run (SMW)' );
- return true;
- }
-
- $revision = Revision::newFromTitle( $this->title );
- if ( !$revision ) {
- $this->error = 'SMWUpdateJob: Page exists but no
revision was found for "' . $this->title->getPrefixedDBkey() . '"';
- wfProfileOut( 'SMWUpdateJob::run (SMW)' );
- return false;
- }
-
- wfProfileIn( __METHOD__ . '-parse' );
- $options = new ParserOptions();
- $output = $wgParser->parse( $revision->getText(), $this->title,
$options, true, true, $revision->getID() );
-
- wfProfileOut( __METHOD__ . '-parse' );
- wfProfileIn( __METHOD__ . '-update' );
-
- // @since 1.9
- // SMWParseData::storeData( $output, $this->title, false );
- $parserData = new SMW\ParserData( $this->title, $output );
- $parserData->disableUpdateJobs();
- $parserData->updateStore();
-
- wfProfileOut( __METHOD__ . '-update' );
- wfProfileOut( 'SMWUpdateJob::run (SMW)' );
-
- return true;
- }
-
- /**
- * This actually files the job. This is prevented if the configuration
of SMW
- * disables jobs.
- * @note Any method that inserts jobs with Job::batchInsert or
otherwise must
- * implement this check individually. The below is not called in these
cases.
- */
- function insert() {
- global $smwgEnableUpdateJobs;
- if ( $smwgEnableUpdateJobs ) {
- parent::insert();
- }
- }
-
-}
-
-/**
- * SMWUpdateJob class alias
- *
- * @since 1.9
- */
-class_alias( 'SMWUpdateJob', 'SMW\UpdateJob' );
diff --git a/includes/jobs/UpdateJob.php b/includes/jobs/UpdateJob.php
new file mode 100644
index 0000000..dc10121
--- /dev/null
+++ b/includes/jobs/UpdateJob.php
@@ -0,0 +1,136 @@
+<?php
+
+namespace SMW;
+
+use LinkCache;
+use WikiPage;
+use Revision;
+use Title;
+use User;
+use Job;
+
+/**
+ * UpdateJob class is responsible for the asynchronous update of semantic data
+ * in the database
+ *
+ * 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 Daniel M. Herzig
+ * @author Markus Krötzsch
+ * @author mwjames
+ */
+
+/**
+ * UpdateJob class is responsible for the asynchronous update of semantic data
+ * in the database for a given title using the MediaWiki JobQueue.
+ *
+ * Update jobs are created if, when saving an article,
+ * it is detected that the content of other pages must be re-parsed as well
(e.g.
+ * due to some type change).
+ *
+ * @note This job does not update the page display or parser cache, so in
general
+ * it might happen that part of the wiki page still displays based on old data
(e.g.
+ * formatting in-page values based on a datatype thathas since been changed),
whereas
+ * the Factbox and query/browsing interfaces might already show the updated
records.
+ *
+ * @ingroup Job
+ */
+class UpdateJob extends JobBase {
+
+ /**
+ * @since 1.9
+ *
+ * @param Title $title
+ */
+ function __construct( Title $title ) {
+ parent::__construct( 'SMW\UpdateJob', $title );
+ }
+
+ /**
+ * Run job
+ * @return boolean success
+ */
+ function run() {
+ Profiler::In( __METHOD__ . '-run' );
+
+ LinkCache::singleton()->clear();
+
+ if ( $this->title === null ) {
+ $this->error = __METHOD__ . ": Invalid title";
+ Profiler::Out( __METHOD__ . '-run' );
+ return false;
+ } elseif ( !$this->title->exists() ) {
+ $this->store->deleteSubject( $this->title ); // be sure
to clear the data
+ Profiler::Out( __METHOD__ . '-run' );
+ return true;
+ }
+
+ $wikiPage = WikiPage::factory( $this->title );
+ $revision = $wikiPage->getRevision();
+
+ if ( !$revision || $revision === null ) {
+ $this->error = __METHOD__ . ': Page exists but no
revision was found for "' . $this->title->getPrefixedDBkey() . '"';
+ Profiler::Out( __METHOD__ . '-run' );
+ return false;
+ }
+
+ Profiler::In( __METHOD__ . '-parserOutput' );
+
+ $parserOutput = $wikiPage->getParserOutput(
+ $wikiPage->makeParserOptions( User::newFromId(
$revision->getUser() ) ),
+ $revision->getId()
+ );
+
+ Profiler::Out( __METHOD__ . '-parserOutput' );
+ Profiler::In( __METHOD__ . '-update' );
+
+ // @since 1.9
+ // SMWParseData::storeData( $output, $this->title, false );
+ $parserData = new ParserData( $this->title, $parserOutput );
+ $parserData->disableUpdateJobs();
+ $parserData->updateStore();
+
+ Profiler::Out( __METHOD__ . '-update' );
+ Profiler::Out( __METHOD__ . '-run' );
+
+ return true;
+ }
+
+ /**
+ * This actually files the job. This is prevented if the configuration
of SMW
+ * disables jobs.
+ * @note Any method that inserts jobs with Job::batchInsert or
otherwise must
+ * implement this check individually. The below is not called in these
cases.
+ */
+ function insert() {
+ if ( $this->getSettings()->get( 'smwgEnableUpdateJobs' ) ) {
+ parent::insert();
+ }
+ }
+
+}
+
+/**
+ * SMWUpdateJob
+ *
+ * @deprecated since 1.9
+ */
+class_alias( 'SMW\UpdateJob', 'SMWUpdateJob' );
diff --git a/tests/phpunit/MockObjectBuilder.php
b/tests/phpunit/MockObjectBuilder.php
index a767270..5ffd11f 100644
--- a/tests/phpunit/MockObjectBuilder.php
+++ b/tests/phpunit/MockObjectBuilder.php
@@ -347,6 +347,10 @@
->will( $this->returnValue( $this->set(
'getPropertiesSpecial' ) ) );
$store->expects( $this->any() )
+ ->method( 'deleteSubject' )
+ ->will( $this->returnValue( $this->set( 'deleteSubject'
) ) );
+
+ $store->expects( $this->any() )
->method( 'getUnusedPropertiesSpecial' )
->will( $this->returnValue( $this->set(
'getUnusedPropertiesSpecial' ) ) );
diff --git a/tests/phpunit/includes/jobs/UpdateJobTest.php
b/tests/phpunit/includes/jobs/UpdateJobTest.php
index b3f312b..4f29b13 100644
--- a/tests/phpunit/includes/jobs/UpdateJobTest.php
+++ b/tests/phpunit/includes/jobs/UpdateJobTest.php
@@ -4,6 +4,7 @@
use SMW\UpdateJob;
+use Title;
/**
* Tests for the UpdateJob class
*
@@ -56,8 +57,10 @@
*
* @return UpdateJob
*/
- private function getInstance() {
- return new UpdateJob( $this->getTitle() );
+ private function getInstance( Title $title = null ) {
+ $instance = new UpdateJob( $title === null ? $this->getTitle()
: $title );
+ $instance->setStore( $this->newMockObject()->getMockStore() );
+ return $instance;
}
/**
@@ -70,4 +73,32 @@
$this->assertInstanceOf( 'SMWUpdateJob', $this->getInstance() );
}
+ /**
+ * @test UpdateJob::run
+ * @dataProvider titleDataProvider
+ *
+ * @since 1.9
+ */
+ public function testRun( $test, $expected ) {
+ $instance = $this->getInstance( $test['title'] );
+ $this->assertEquals( $expected['result'], $instance->run() );
+ }
+
+ /**
+ * Provides title samples
+ *
+ * @return array
+ */
+ public function titleDataProvider() {
+
+ $provider = array();
+
+ $provider[] = array(
+ array( 'title' => $this->getTitle() ),
+ array( 'result' => true )
+ );
+
+ return $provider;
+ }
+
}
--
To view, visit https://gerrit.wikimedia.org/r/74840
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I3b41d4e506a2c425903ec3690f3500dcec939ac4
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