Mwjames has uploaded a new change for review.

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


Change subject: \SMW\RefreshJob
......................................................................

\SMW\RefreshJob

Contains the same functionality

* Put the code in some sane order
* Replace array_key_exists with isset because of performance
* Add rudimentary test but needs some improvement to verify what's
expected of getProgress()

Change-Id: I2405e4e908098fc40a2fa8c86ef2f24c2c0788c2
---
M SemanticMediaWiki.classes.php
M SemanticMediaWiki.php
M includes/Aliases.php
M includes/Setup.php
A includes/jobs/RefreshJob.php
D includes/jobs/SMW_RefreshJob.php
M includes/jobs/UpdateDispatcherJob.php
M includes/jobs/UpdateJob.php
M tests/phpunit/includes/SetupTest.php
A tests/phpunit/includes/jobs/RefreshJobTest.php
10 files changed, 291 insertions(+), 88 deletions(-)


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

diff --git a/SemanticMediaWiki.classes.php b/SemanticMediaWiki.classes.php
index e3fdd45..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/SMW_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/RefreshJob.php b/includes/jobs/RefreshJob.php
new file mode 100644
index 0000000..a9b4e98
--- /dev/null
+++ b/includes/jobs/RefreshJob.php
@@ -0,0 +1,141 @@
+<?php
+
+namespace SMW;
+
+/**
+ * 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.
+ *
+ * @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.
+ *
+ * @ingroup SMW
+ *
+ * @licence GNU GPL v2+
+ * @since 1.9
+ *
+ * @author Markus Krötzsch
+ * @author mwjames
+ */
+class RefreshJob extends JobBase {
+
+       /** $var boolean */
+       protected $enabled = true;
+
+       /**
+        * Constructor. The parameters optionally specified in the second
+        * argument of this constructor use the following array keys:  'spos'
+        * (start index, default 1), 'prog' (progress indicator, default 0),
+        * ('rc' (number of runs to be done, default 1). If more than one run
+        * is done, then the first run will restrict to properties and types.
+        * The progress indication refers to the current run, not to the
+        * overall job.
+        *
+        * @param $title Title not relevant but needed for MW jobs
+        * @param $params array (associative) as explained above
+        */
+       public function __construct( $title, $params = array( 'spos' => 1, 
'prog' => 0, 'rc' => 1 ) ) {
+               parent::__construct( 'SMW\RefreshJob', $title, $params );
+       }
+
+       /**
+        * Report the estimated progress status of this job as a number between
+        * 0 and 1 (0% to 100%). The progress refers to the state before
+        * processing this job.
+        *
+        * @return double
+        */
+       public function getProgress() {
+
+               $prog = isset( $this->params['prog'] ) ? $this->params['prog'] 
: 0;
+               $run = isset( $this->params['run'] ) ? $this->params['run'] : 1;
+
+               return ( $run - 1 + $prog ) / $this->params['rc'];
+       }
+
+       /**
+        * Disables ability to insert jobs into the JobQueue and is normally
+        * only executed when running unit tests
+        *
+        * @since 1.9
+        */
+       public function disable() {
+               $this->enabled = false;
+               return $this;
+       }
+
+       /**
+        * Run job
+        *
+        * @return boolean success
+        */
+       public function run() {
+               return isset( $this->params['spos'] ) ? $this->interate( 
$this->params['spos'] ) : true;
+       }
+
+       /**
+        * @since 1.9
+        *
+        * @return boolean success
+        */
+       protected function interate( $spos ) {
+               Profiler::In();
+
+               $run = isset( $this->params['run'] ) ? $this->params['run'] : 1;
+
+               if ( $spos > 0 ) {
+
+                       $progress = 
$this->withContext()->getStore()->refreshData( $spos, 20, $this->getNamespace( 
$run ) );
+
+                       $this->createNextJob( array(
+                               'spos' => $spos,
+                               'prog' => $progress,
+                               'rc'   => $this->params['rc'],
+                               'run'  => $run
+                       ) );
+
+               } elseif ( $this->params['rc'] > $run ) { // do another run 
from the beginning
+
+                       $this->createNextJob( array(
+                               'spos' => 1,
+                               'prog' => 0,
+                               'rc'   => $this->params['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->params['rc'] > 1 ) && ( $run == 1 ) ) ? 
array( SMW_NS_PROPERTY, SMW_NS_TYPE ) : false;
+       }
+
+       /**
+        * @codeCoverageIgnore
+        * @see Job::insert
+        */
+       public function insert() {
+               if ( $this->enabled ) {
+                       parent::insert();
+               }
+       }
+
+}
\ No newline at end of file
diff --git a/includes/jobs/SMW_RefreshJob.php b/includes/jobs/SMW_RefreshJob.php
deleted file mode 100644
index 5199565..0000000
--- a/includes/jobs/SMW_RefreshJob.php
+++ /dev/null
@@ -1,83 +0,0 @@
-<?php
-/**
- * @file
- * @ingroup SMW
- */
-
-/**
- * SMWRefreshJob 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).
- *
- * @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
- */
-class SMWRefreshJob extends Job {
-
-       /**
-        * Constructor. The parameters optionally specified in the second
-        * argument of this constructor use the following array keys:  'spos'
-        * (start index, default 1), 'prog' (progress indicator, default 0),
-        * ('rc' (number of runs to be done, default 1). If more than one run
-        * is done, then the first run will restrict to properties and types.
-        * The progress indication refers to the current run, not to the
-        * overall job.
-        *
-        * @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 );
-       }
-
-       /**
-        * Run job
-        *
-        * @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;
-       }
-
-       /**
-        * Report the estimated progress status of this job as a number between
-        * 0 and 1 (0% to 100%). The progress refers to the state before
-        * processing this job.
-        *
-        * @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'];
-       }
-       
-}
\ 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/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..7b1a161
--- /dev/null
+++ b/tests/phpunit/includes/jobs/RefreshJobTest.php
@@ -0,0 +1,137 @@
+<?php
+
+namespace SMW\Test;
+
+use SMW\EmptyContext;
+use SMW\RefreshJob;
+
+use Title;
+
+/**
+ * @covers \SMW\RefreshJob
+ *
+ * @ingroup Test
+ *
+ * @group SMW
+ * @group SMWExtension
+ *
+ * @licence GNU GPL v2+
+ * @since 1.9
+ *
+ * @author mwjames
+ */
+class RefreshJobTest extends SemanticMediaWikiTestCase {
+
+       /**
+        * @return string|false
+        */
+       public function getClass() {
+               return '\SMW\RefreshJob';
+       }
+
+       /**
+        * @since 1.9
+        *
+        * @return UpdateJob
+        */
+       private function newInstance( Title $title = null, $parameters = 
array() ) {
+
+               if ( $title === null ) {
+                       $title = $this->newTitle();
+               }
+
+               $mockStore = $this->newMockBuilder()->newObject( 'Store' );
+               $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']}"
+               );
+
+       }
+
+       /**
+        * @return array
+        */
+       public function parameterDataProvider() {
+
+               $provider = array();
+
+               // #0 Initial
+               $provider[] = array(
+                       array(
+                               'spos' => 1,
+                               'prog' => 0,
+                               'rc'   => 1
+                       ),
+                       array(
+                               'progress' => 0
+                       )
+               );
+
+               // #1
+               $provider[] = array(
+                       array(
+                               'spos' => 1,
+                               'run'  => 1,
+                               'prog' => 10,
+                               'rc'   => 1
+                       ),
+                       array(
+                               'progress' => 10
+                       )
+               );
+
+               // #2 Initiates another run from the beginning
+               $provider[] = array(
+                       array(
+                               'spos' => 0,
+                               'run'  => 1,
+                               'prog' => 10,
+                               'rc'   => 2
+                       ),
+                       array(
+                               'progress' => 5
+                       )
+               );
+
+               return $provider;
+
+       }
+
+}

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

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