jenkins-bot has submitted this change and it was merged.
Change subject: \SMW\RefreshJob
......................................................................
\SMW\RefreshJob
Contains the same functionality
* Put the code in some sane order
* Add hasParameter/getParameter to manage access to the parameters array
* Add rudimentary test but needs some improvement to verify what's
expected of getProgress()
NOT FIXING the issue in SMWAdmin (this class is a real mess) of
accessing the job table.
Change-Id: I2405e4e908098fc40a2fa8c86ef2f24c2c0788c2
---
M SemanticMediaWiki.classes.php
M SemanticMediaWiki.php
M includes/Aliases.php
M includes/Setup.php
M includes/jobs/JobBase.php
M includes/jobs/RefreshJob.php
M includes/jobs/UpdateDispatcherJob.php
M includes/jobs/UpdateJob.php
M includes/specials/SMW_SpecialSMWAdmin.php
M tests/phpunit/MockObjectRepository.php
M tests/phpunit/includes/SetupTest.php
A tests/phpunit/includes/jobs/RefreshJobTest.php
12 files changed, 336 insertions(+), 49 deletions(-)
Approvals:
Mwjames: Looks good to me, approved
jenkins-bot: Verified
diff --git a/SemanticMediaWiki.classes.php b/SemanticMediaWiki.classes.php
index 3dc80f4..a37ca9b 100644
--- a/SemanticMediaWiki.classes.php
+++ b/SemanticMediaWiki.classes.php
@@ -356,8 +356,8 @@
// Jobs
'SMW\UpdateDispatcherJob' => 'includes/jobs/UpdateDispatcherJob.php',
'SMW\JobBase' => 'includes/jobs/JobBase.php',
- 'SMW\UpdateJob' => 'includes/jobs/UpdateJob.php', // 1.9
- 'SMWRefreshJob' => 'includes/jobs/RefreshJob.php',
+ 'SMW\UpdateJob' => 'includes/jobs/UpdateJob.php',
+ 'SMW\RefreshJob' => 'includes/jobs/RefreshJob.php',
// API modules
'SMW\Api\Base' => 'includes/api/Base.php',
diff --git a/SemanticMediaWiki.php b/SemanticMediaWiki.php
index dc252b3..e13f069 100644
--- a/SemanticMediaWiki.php
+++ b/SemanticMediaWiki.php
@@ -105,6 +105,7 @@
// Compatibility aliases for classes that got moved into the SMW namespace in
1.9.
class_alias( 'SMW\Store', 'SMWStore' );
class_alias( 'SMW\UpdateJob', 'SMWUpdateJob' );
+class_alias( 'SMW\RefreshJob', 'SMWRefreshJob' );
class_alias( 'SMW\SemanticData', 'SMWSemanticData' );
class_alias( 'SMW\DIWikiPage', 'SMWDIWikiPage' );
class_alias( 'SMW\DIProperty', 'SMWDIProperty' );
diff --git a/includes/Aliases.php b/includes/Aliases.php
index 0713c9d..26db844 100644
--- a/includes/Aliases.php
+++ b/includes/Aliases.php
@@ -30,3 +30,5 @@
class SMWDISerializer extends SMW\Serializers\QueryResultSerializer {}
class SMWUpdateJob extends SMW\UpdateJob {}
+
+class SMWRefreshJob extends SMW\RefreshJob {}
diff --git a/includes/Setup.php b/includes/Setup.php
index fa19242..118924f 100644
--- a/includes/Setup.php
+++ b/includes/Setup.php
@@ -167,7 +167,7 @@
protected function registerJobClasses() {
$this->globals['wgJobClasses']['SMW\UpdateJob'] =
'SMW\UpdateJob';
- $this->globals['wgJobClasses']['SMWRefreshJob'] =
'SMWRefreshJob';
+ $this->globals['wgJobClasses']['SMW\RefreshJob'] =
'SMW\RefreshJob';
$this->globals['wgJobClasses']['SMW\UpdateDispatcherJob'] =
'SMW\UpdateDispatcherJob';
}
diff --git a/includes/jobs/JobBase.php b/includes/jobs/JobBase.php
index 720fd6f..edbee56 100644
--- a/includes/jobs/JobBase.php
+++ b/includes/jobs/JobBase.php
@@ -61,4 +61,30 @@
return $this->title;
}
+ /**
+ * Whether the parameters contain an element for a given key
+ *
+ * @since 1.9
+ *
+ * @param mixed $key
+ *
+ * @return boolean
+ */
+ public function hasParameter( $key ) {
+ return isset( $this->params[ $key ] ) || array_key_exists(
$key, $this->params );
+ }
+
+ /**
+ * Returns a parameter value for a given key
+ *
+ * @since 1.9
+ *
+ * @param mixed $key
+ *
+ * @return boolean
+ */
+ public function getParameter( $key ) {
+ return $this->params[ $key ];
+ }
+
}
diff --git a/includes/jobs/RefreshJob.php b/includes/jobs/RefreshJob.php
index 5199565..2edf5b6 100644
--- a/includes/jobs/RefreshJob.php
+++ b/includes/jobs/RefreshJob.php
@@ -1,24 +1,28 @@
<?php
-/**
- * @file
- * @ingroup SMW
- */
+
+namespace SMW;
/**
- * SMWRefreshJob iterates over all page ids of the wiki, to perform an update
+ * RefreshJob iterates over all page ids of the wiki, to perform an update
* action for all of them in sequence. This corresponds to the in-wiki version
- * of the SMW_refreshData.php script for updating the whole wiki, but it also
- * fixes problems with SMWSQLStore2 (which may have objects in its database
- * that are not proper wiki pages).
+ * of the SMW_refreshData.php script for updating the whole wiki.
*
* @note This class ignores $smwgEnableUpdateJobs and always creates updates.
* In fact, it might be needed specifically on wikis that do not use update
* jobs in normal operation.
*
- * @author Markus Krötzsch
* @ingroup SMW
+ *
+ * @licence GNU GPL v2+
+ * @since 1.9
+ *
+ * @author Markus Krötzsch
+ * @author mwjames
*/
-class SMWRefreshJob extends Job {
+class RefreshJob extends JobBase {
+
+ /** $var boolean */
+ protected $enabled = true;
/**
* Constructor. The parameters optionally specified in the second
@@ -32,39 +36,17 @@
* @param $title Title not relevant but needed for MW jobs
* @param $params array (associative) as explained above
*/
- function __construct( $title, $params = array( 'spos' => 1, 'prog' =>
0, 'rc' => 1 ) ) {
- parent::__construct( 'SMWRefreshJob', $title, $params );
+ public function __construct( $title, $params = array( 'spos' => 1,
'prog' => 0, 'rc' => 1 ) ) {
+ parent::__construct( 'SMW\RefreshJob', $title, $params );
}
/**
- * Run job
+ * @since 1.9
*
* @return boolean success
*/
- function run() {
- wfProfileIn( 'SMWRefreshJob::run (SMW)' );
-
- if ( !array_key_exists( 'spos', $this->params ) ) {
- wfProfileOut( 'SMWRefreshJob::run (SMW)' );
- return true;
- }
-
- $run = array_key_exists( 'run', $this->params ) ?
$this->params['run']:1;
- $spos = $this->params['spos'];
- $namespaces = ( ( $this->params['rc'] > 1 ) && ( $run == 1 ) )
? array( SMW_NS_PROPERTY, SMW_NS_TYPE ):false;
- $progress = smwfGetStore()->refreshData( $spos, 20, $namespaces
);
-
- if ( $spos > 0 ) {
- $nextjob = new SMWRefreshJob( $this->title, array(
'spos' => $spos, 'prog' => $progress, 'rc' => $this->params['rc'], 'run' =>
$run ) );
- $nextjob->insert();
- } elseif ( $this->params['rc'] > $run ) { // do another run
from the beginning
- $nextjob = new SMWRefreshJob( $this->title, array(
'spos' => 1, 'prog' => 0, 'rc' => $this->params['rc'], 'run' => $run + 1 ) );
- $nextjob->insert();
- }
-
- wfProfileOut( 'SMWRefreshJob::run (SMW)' );
-
- return true;
+ public function run() {
+ return $this->hasParameter( 'spos' ) ? $this->refreshData(
$this->getParameter( 'spos' ) ) : true;
}
/**
@@ -75,9 +57,89 @@
* @return double
*/
public function getProgress() {
- $prog = array_key_exists( 'prog', $this->params ) ?
$this->params['prog'] : 0;
- $run = array_key_exists( 'run', $this->params ) ?
$this->params['run'] : 1;
- return ( $run - 1 + $prog ) / $this->params['rc'];
+
+ $prog = $this->hasParameter( 'prog' ) ? $this->getParameter(
'prog' ) : 0;
+ $run = $this->hasParameter( 'run' ) ? $this->getParameter(
'run' ): 1;
+ $rc = $this->hasParameter( 'rc' ) ? $this->getParameter( 'rc'
) : 1;
+
+ return ( $run - 1 + $prog ) / $rc;
}
-
+
+ /**
+ * Disables ability to insert jobs into the JobQueue and is normally
only
+ * executed when running unit tests
+ *
+ * @since 1.9
+ *
+ * @return RefreshJob
+ */
+ public function disable() {
+ $this->enabled = false;
+ return $this;
+ }
+
+ /**
+ * @see Job::insert
+ * @codeCoverageIgnore
+ */
+ public function insert() {
+ if ( $this->enabled ) {
+ parent::insert();
+ }
+ }
+
+ /**
+ * @since 1.9
+ *
+ * @param $spos start index
+ *
+ * @return boolean success
+ */
+ protected function refreshData( $spos ) {
+ Profiler::In();
+
+ $run = $this->hasParameter( 'run' ) ? $this->getParameter(
'run' ) : 1;
+ $prog = $this->withContext()->getStore()->refreshData( $spos,
20, $this->getNamespace( $run ) );
+
+ if ( $spos > 0 ) {
+
+ $this->createNextJob( array(
+ 'spos' => $spos,
+ 'prog' => $prog,
+ 'rc' => $this->getParameter( 'rc' ),
+ 'run' => $run
+ ) );
+
+ } elseif ( $this->getParameter( 'rc' ) > $run ) { // do another
run from the beginning
+
+ $this->createNextJob( array(
+ 'spos' => 1,
+ 'prog' => 0,
+ 'rc' => $this->getParameter( 'rc' ),
+ 'run' => $run + 1
+ ) );
+
+ }
+
+ Profiler::Out();
+ return true;
+ }
+
+ /**
+ * @since 1.9
+ */
+ protected function createNextJob( array $parameters ) {
+ $nextjob = new self( $this->getTitle(), $parameters );
+ $nextjob->insert();
+ }
+
+ /**
+ * @since 1.9
+ *
+ * @return array|false
+ */
+ protected function getNamespace( $run ) {
+ return ( ( $this->getParameter( 'rc' ) > 1 ) && ( $run == 1 ) )
? array( SMW_NS_PROPERTY, SMW_NS_TYPE ) : false;
+ }
+
}
\ No newline at end of file
diff --git a/includes/jobs/UpdateDispatcherJob.php
b/includes/jobs/UpdateDispatcherJob.php
index baf3e61..fb58c15 100644
--- a/includes/jobs/UpdateDispatcherJob.php
+++ b/includes/jobs/UpdateDispatcherJob.php
@@ -11,6 +11,8 @@
* Can be run either in deferred or immediate mode to restore the data parity
* between a property and its attached subjects
*
+ * @ingroup SMW
+ *
* @licence GNU GPL v2+
* @since 1.9
*
diff --git a/includes/jobs/UpdateJob.php b/includes/jobs/UpdateJob.php
index 7bbda9d..ab49c56 100644
--- a/includes/jobs/UpdateJob.php
+++ b/includes/jobs/UpdateJob.php
@@ -19,7 +19,10 @@
* 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.
*
- * @since 1.9
+ * @ingroup SMW
+ *
+ * @licence GNU GPL v2+
+ * @since 1.9
*
* @author Daniel M. Herzig
* @author Markus Krötzsch
diff --git a/includes/specials/SMW_SpecialSMWAdmin.php
b/includes/specials/SMW_SpecialSMWAdmin.php
index 37974ce..54a6aa4 100644
--- a/includes/specials/SMW_SpecialSMWAdmin.php
+++ b/includes/specials/SMW_SpecialSMWAdmin.php
@@ -40,9 +40,16 @@
$this->setHeaders();
+ // FIXME Searching the job table needs to be fixed
+ //
+ // SMW shouldn't expose itself to an internal MW DB table at
+ // this level. If an official Api doesn't provide needed
+ // functionality, the DB call should be encapsulate within its
+ // own class
+
/**** Get status of refresh job, if any ****/
$dbr = wfGetDB( DB_SLAVE );
- $row = $dbr->selectRow( 'job', '*', array( 'job_cmd' =>
'SMWRefreshJob' ), __METHOD__ );
+ $row = $dbr->selectRow( 'job', '*', array( 'job_cmd' =>
'SMW\RefreshJob' ), __METHOD__ );
if ( $row !== false ) { // similar to Job::pop_type, but
without deleting the job
$title = Title::makeTitleSafe( $row->job_namespace,
$row->job_title );
$blob = (string)$row->job_params !== '' ? unserialize(
$row->job_params ) : false;
@@ -77,16 +84,19 @@
$title = SpecialPage::getTitleFor( 'SMWAdmin' );
if ( $sure == 'yes' ) {
if ( is_null( $refreshjob ) ) { // careful,
there might be race conditions here
- $newjob = new SMWRefreshJob( $title,
array( 'spos' => 1, 'prog' => 0, 'rc' => 2 ) );
+ $newjob = new \SMW\RefreshJob( $title,
array( 'spos' => 1, 'prog' => 0, 'rc' => 2 ) );
$newjob->insert();
$wgOut->addHTML( '<p>' . wfMessage(
'smw_smwadmin_updatestarted', '<a href="' . htmlspecialchars(
$title->getFullURL() ) . '">Special:SMWAdmin</a>' )->text() . '</p>' );
} else {
$wgOut->addHTML( '<p>' . wfMessage(
'smw_smwadmin_updatenotstarted', '<a href="' . htmlspecialchars(
$title->getFullURL() ) . '">Special:SMWAdmin</a>' )->text() . '</p>' );
}
} elseif ( $sure == 'stop' ) {
+
+ // FIXME See above comments !!
+
$dbw = wfGetDB( DB_MASTER );
// delete (all) existing iteration jobs
- $dbw->delete( 'job', array( 'job_cmd' =>
'SMWRefreshJob' ), __METHOD__ );
+ $dbw->delete( 'job', array( 'job_cmd' =>
'SMW\RefreshJob' ), __METHOD__ );
$wgOut->addHTML( '<p>' . wfMessage(
'smw_smwadmin_updatestopped', '<a href="' . htmlspecialchars(
$title->getFullURL() ) . '">Special:SMWAdmin</a>' )->text() . '</p>' );
} else {
$wgOut->addHTML( '<p>' . wfMessage(
'smw_smwadmin_updatenotstopped', '<a href="' . htmlspecialchars(
$title->getFullURL() ) . '">Special:SMWAdmin</a>' )->text() . '</p>' );
diff --git a/tests/phpunit/MockObjectRepository.php
b/tests/phpunit/MockObjectRepository.php
index 6288627..2f46fc1 100644
--- a/tests/phpunit/MockObjectRepository.php
+++ b/tests/phpunit/MockObjectRepository.php
@@ -653,6 +653,10 @@
->will( $this->builder->setCallback( 'getSemanticData'
) );
$store->expects( $this->any() )
+ ->method( 'refreshData' )
+ ->will( $this->builder->setCallback( 'refreshData' ) );
+
+ $store->expects( $this->any() )
->method( 'getUnusedPropertiesSpecial' )
->will( $this->returnValue( $this->builder->setValue(
'getUnusedPropertiesSpecial' ) ) );
diff --git a/tests/phpunit/includes/SetupTest.php
b/tests/phpunit/includes/SetupTest.php
index 3a39e8c..812b072 100644
--- a/tests/phpunit/includes/SetupTest.php
+++ b/tests/phpunit/includes/SetupTest.php
@@ -414,7 +414,7 @@
$jobs = array(
'SMW\UpdateJob',
- 'SMWRefreshJob',
+ 'SMW\RefreshJob',
'SMW\UpdateDispatcherJob',
);
diff --git a/tests/phpunit/includes/jobs/RefreshJobTest.php
b/tests/phpunit/includes/jobs/RefreshJobTest.php
new file mode 100644
index 0000000..f935995
--- /dev/null
+++ b/tests/phpunit/includes/jobs/RefreshJobTest.php
@@ -0,0 +1,177 @@
+<?php
+
+namespace SMW\Test;
+
+use SMW\EmptyContext;
+use SMW\RefreshJob;
+
+use Title;
+
+/**
+ * @covers \SMW\RefreshJob
+ * @covers \SMW\JobBase
+ *
+ * @ingroup Test
+ *
+ * @group SMW
+ * @group SMWExtension
+ *
+ * @licence GNU GPL v2+
+ * @since 1.9
+ *
+ * @author mwjames
+ */
+class RefreshJobTest extends SemanticMediaWikiTestCase {
+
+ /** @var integer */
+ protected $controlRefreshDataIndex;
+
+ /**
+ * @return string|false
+ */
+ public function getClass() {
+ return '\SMW\RefreshJob';
+ }
+
+ /**
+ * @since 1.9
+ *
+ * @return RefreshJob
+ */
+ private function newInstance( Title $title = null, $parameters =
array() ) {
+
+ if ( $title === null ) {
+ $title = $this->newTitle();
+ }
+
+ $mockStore = $this->newMockBuilder()->newObject( 'Store', array(
+ 'refreshData' => array( $this, 'refreshDataCallback' )
+ ) );
+
+ $context = new EmptyContext();
+ $container = $context->getDependencyBuilder()->getContainer();
+ $container->registerObject( 'Store', $mockStore );
+
+ $instance = new RefreshJob( $title, $parameters );
+ $instance->invokeContext( $context );
+
+ return $instance;
+ }
+
+ /**
+ * FIXME Delete SMWRefreshJob assertion after all references to
+ * SMWRefreshJob have been removed
+ *
+ * @since 1.9
+ */
+ public function testConstructor() {
+ $this->assertInstanceOf( $this->getClass(),
$this->newInstance() );
+ $this->assertInstanceOf( $this->getClass(), new \SMWRefreshJob(
$this->newTitle() ) );
+ }
+
+ /**
+ * @dataProvider parameterDataProvider
+ *
+ * @since 1.9
+ */
+ public function testRun( $parameter, $expected ) {
+
+ $instance = $this->newInstance( null, $parameter );
+
+ $this->assertTrue(
+ $instance->disable()->run(),
+ 'Asserts that the run() returns true'
+ );
+
+ $this->assertEquals(
+ $expected['progress'],
+ $instance->getProgress(),
+ "Asserts that the getProgress() returns
{$expected['progress']}"
+ );
+
+ $this->assertEquals(
+ $expected['spos'],
+ $this->controlRefreshDataIndex,
+ "Asserts that the refreshData() received a spos
{$expected['spos']}"
+ );
+
+ unset( $this->controlRefreshDataIndex );
+
+ }
+
+ /**
+ * @return array
+ */
+ public function parameterDataProvider() {
+
+ $provider = array();
+
+ // #0 Empty
+ $provider[] = array(
+ array(),
+ array(
+ 'progress' => 0,
+ 'spos' => null
+ )
+ );
+
+ // #1 Initial
+ $provider[] = array(
+ array(
+ 'spos' => 1,
+ 'prog' => 0,
+ 'rc' => 1
+ ),
+ array(
+ 'progress' => 0,
+ 'spos' => 1
+ )
+ );
+
+ // #2
+ $provider[] = array(
+ array(
+ 'spos' => 1,
+ 'run' => 1,
+ 'prog' => 10,
+ 'rc' => 1
+ ),
+ array(
+ 'progress' => 10,
+ 'spos' => 1
+ )
+ );
+
+ // #3 Initiates another run from the beginning
+ $provider[] = array(
+ array(
+ 'spos' => 0,
+ 'run' => 1,
+ 'prog' => 10,
+ 'rc' => 2
+ ),
+ array(
+ 'progress' => 5,
+ 'spos' => 0
+ )
+ );
+
+ return $provider;
+
+ }
+
+ /**
+ * @see Store::refreshData
+ *
+ * @since 1.9
+ *
+ * @param integer $index
+ * @param integer $count
+ * @param mixed $namespaces Array or false
+ * @param boolean $usejobs
+ */
+ public function refreshDataCallback( &$index, $count, $namespaces ) {
+ $this->controlRefreshDataIndex = $index;
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/91566
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I2405e4e908098fc40a2fa8c86ef2f24c2c0788c2
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[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