Mwjames has uploaded a new change for review.

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


Change subject: \SMW\ChangeAgent a general purpose change Observer that acts on 
invoked object notifications
......................................................................

\SMW\ChangeAgent a general purpose change Observer that acts on invoked object 
notifications

This enforce loose coupling between object that send change notifications
to the ChangeAgent.

Before this ChangeAgent, PropertyDisparityDetector class would have been
coupled to the UpdateDispatcher while now the ChangeAgent acts on behalf
of PropertyDisparityDetector to invoke the necessary object.

This will allow [1] to use the same ChangeAgent

[1] https://gerrit.wikimedia.org/r/#/c/75825/

Change-Id: Ifd8604d8ed551a62e3aa5c160e536c5cb1d0e718
---
M includes/ParserData.php
M includes/PropertyDisparityDetector.php
M includes/Setup.php
A includes/utilities/ChangeAgent.php
A includes/utilities/MediaWikiInterfaceProvider.php
M tests/phpunit/includes/PropertyDisparityDetectorTest.php
6 files changed, 119 insertions(+), 35 deletions(-)


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

diff --git a/includes/ParserData.php b/includes/ParserData.php
index 0acc9c8..a475f21 100644
--- a/includes/ParserData.php
+++ b/includes/ParserData.php
@@ -387,8 +387,6 @@
                        return true;
                }
 
-               $dispatcherJob = null;
-
                $namespace = $this->title->getNamespace();
                $wikiPage  = WikiPage::factory( $this->title );
                $revision  = $wikiPage->getRevision();
@@ -422,7 +420,8 @@
                // even finding uses of a property fails after its type was 
changed.
                if ( $this->updateJobs ) {
                        $disparityDetector = new PropertyDisparityDetector( 
$store, $this->semanticData, Settings::newFromGlobals() );
-                       $dispatcherJob = 
$disparityDetector->detectDisparity()->getDispatcherJob();
+                       $disparityDetector->attach( new ChangeAgent() );
+                       $disparityDetector->detectDisparity();
                }
 
                // Actually store semantic data, or at least clear it if needed
@@ -432,14 +431,7 @@
                        $store->clearData( $this->semanticData->getSubject() );
                }
 
-               // Job::batchInsert was deprecated in MW 1.21
-               // @see JobQueueGroup::singleton()->push( $job );
-               if ( $dispatcherJob !== null ) {
-                       Job::batchInsert( $dispatcherJob );
-               }
-
                Profiler::Out( __METHOD__, true );
-
                return true;
        }
 
diff --git a/includes/PropertyDisparityDetector.php 
b/includes/PropertyDisparityDetector.php
index bed5dc9..9defbc2 100644
--- a/includes/PropertyDisparityDetector.php
+++ b/includes/PropertyDisparityDetector.php
@@ -37,7 +37,7 @@
  *
  * @ingroup SMW
  */
-class PropertyDisparityDetector {
+class PropertyDisparityDetector extends Subject implements MediaWikiTitle {
 
        /** @var Store */
        protected $store;
@@ -48,8 +48,8 @@
        /** @var Settings */
        protected $settings;
 
-       /** @var Job */
-       protected $dispatcherJob = null;
+       /** @var boolean */
+       protected $hasDisparity = false;
 
        /**
         * @since 1.9
@@ -65,14 +65,14 @@
        }
 
        /**
-        * Returns update jobs as a result of the data comparison
+        * Returns a Title object
         *
         * @since 1.9
         *
-        * @return PropertyDisparityDispatcherJob|null
+        * @return Title
         */
-       public function getDispatcherJob() {
-               return $this->dispatcherJob;
+       public function getTitle() {
+               return $this->semanticData->getSubject()->getTitle();
        }
 
        /**
@@ -83,7 +83,7 @@
         * @return boolean
         */
        public function hasDisparity() {
-               return $this->getDispatcherJob() !== null;
+               return $this->hasDisparity;
        }
 
        /**
@@ -179,11 +179,9 @@
         * @param boolean $addJob
         */
        protected function addDispatchJob( $addJob = true ) {
-               if ( $addJob && $this->dispatcherJob === null ) {
-                       $this->dispatcherJob[] = new 
PropertySubjectsUpdateDispatcherJob(
-                               $this->semanticData->getSubject()->getTitle(),
-                               array( 'store' => get_class( $this->store ) )
-                       );
+               if ( $addJob && !$this->hasDisparity ) {
+                       $this->setState( 'useUpdateDispatcher' );
+                       $this->hasDisparity = true;
                }
        }
 
diff --git a/includes/Setup.php b/includes/Setup.php
index 19beeb8..7b793b5 100644
--- a/includes/Setup.php
+++ b/includes/Setup.php
@@ -166,6 +166,8 @@
        $wgAutoloadClasses['SMW\ObservableMessageReporter'] = $incDir . 
'/utilities/MessageReporter.php';
        $wgAutoloadClasses['SMW\RedirectBuilder']           = $incDir . 
'/utilities/RedirectBuilder.php';
        $wgAutoloadClasses['SMW\ParserOutputGenerator']     = $incDir . 
'/utilities/ParserOutputGenerator.php';
+       $wgAutoloadClasses['SMW\ChangeAgent']               = $incDir . 
'/utilities/ChangeAgent.php';
+       $wgAutoloadClasses['SMW\MediaWikiTitle']            = $incDir . 
'/utilities/MediaWikiInterfaceProvider.php';
 
        $wgAutoloadClasses['SMW\Publisher']                 = $incDir . 
'/utilities/ObserverInterfaceProvider.php';
        $wgAutoloadClasses['SMW\Subject']                   = $incDir . 
'/utilities/ObserverInterfaceProvider.php';
diff --git a/includes/utilities/ChangeAgent.php 
b/includes/utilities/ChangeAgent.php
new file mode 100644
index 0000000..7d85c3a
--- /dev/null
+++ b/includes/utilities/ChangeAgent.php
@@ -0,0 +1,49 @@
+<?php
+
+namespace SMW;
+
+/**
+ * General purpose change agent to enforce loose coupling by having
+ * a Publisher (subject) sent a change notification to this agent
+ *
+ * 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
+ */
+
+/**
+ * General purpose change agent to enforce loose coupling by having
+ * a Publisher (subject) sent a change notification to this agent
+ *
+ * @ingroup Observer
+ */
+class ChangeAgent extends Observer {
+
+       /**
+        * Insert a DispatcherJob to the JobQueue
+        *
+        * @since 1.9
+        */
+       public function useUpdateDispatcher( MediaWikiTitle $subject ) {
+               $instance = new PropertySubjectsUpdateDispatcherJob( 
$subject->getTitle() );
+               $instance->insert();
+       }
+}
diff --git a/includes/utilities/MediaWikiInterfaceProvider.php 
b/includes/utilities/MediaWikiInterfaceProvider.php
new file mode 100644
index 0000000..fb24c99
--- /dev/null
+++ b/includes/utilities/MediaWikiInterfaceProvider.php
@@ -0,0 +1,45 @@
+<?php
+
+namespace SMW;
+
+/**
+ * Provides interfaces to acess MediaWiki specific objects
+ *
+ * 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
+ */
+
+/**
+ * Interface specifying access to a Title object
+ */
+interface MediaWikiTitle {
+
+       /**
+        * Returns a Title object
+        *
+        * @since 1.9
+        *
+        * @return boolean
+        */
+       public function getTitle();
+
+}
diff --git a/tests/phpunit/includes/PropertyDisparityDetectorTest.php 
b/tests/phpunit/includes/PropertyDisparityDetectorTest.php
index 5bf0b4e..90035e8 100644
--- a/tests/phpunit/includes/PropertyDisparityDetectorTest.php
+++ b/tests/phpunit/includes/PropertyDisparityDetectorTest.php
@@ -110,9 +110,7 @@
                $instance = $this->getInstance( $store, $data, $settings );
 
                $this->assertInstanceOf( $this->getClass(), 
$instance->detectDisparity() );
-               $this->assertEquals( $expected['disp'], 
$instance->hasDisparity() );
-               $this->assertInternalType( $expected['type'], 
$instance->getDispatcherJob() );
-               $this->assertEquals( $expected['count'], count( 
$instance->getDispatcherJob() ) );
+               $this->assertEquals( $expected['disparity'], 
$instance->hasDisparity() );
 
        }
 
@@ -135,15 +133,15 @@
 
                return array(
                        //  $storeValues, $dataValues, $settings,               
$expected
-                       array( $subjects, array(),   array( '_PVAL', '_LIST' ), 
array( 'disp' => true,  'type' => 'array', 'count' => 1 ) ),
-                       array( array(),   $subjects, array( '_PVAL', '_LIST' ), 
array( 'disp' => true,  'type' => 'array', 'count' => 1 ) ),
-                       array( $subject,  $subjects, array( '_PVAL', '_LIST' ), 
array( 'disp' => true,  'type' => 'array', 'count' => 1 ) ),
-                       array( $subject,  array(),   array( '_PVAL', '_LIST' ), 
array( 'disp' => true,  'type' => 'array', 'count' => 1 ) ),
-                       array( $subject,  array(),   array( '_PVAL' ),          
array( 'disp' => true,  'type' => 'array', 'count' => 1 ) ),
-                       array( $subjects, $subjects, array( '_PVAL' ),          
array( 'disp' => false, 'type' => 'null',  'count' => 0 ) ),
-                       array( $subject,  $subject,  array( '_PVAL' ),          
array( 'disp' => false, 'type' => 'null',  'count' => 0 ) ),
-                       array( $subjects, $subjects, array( '_PVAL', '_LIST' ), 
array( 'disp' => true,  'type' => 'array', 'count' => 1 ) ),
-                       array( $subject,  $subject,  array( '_PVAL', '_LIST' ), 
array( 'disp' => true,  'type' => 'array', 'count' => 1 ) )
+                       array( $subjects, array(),   array( '_PVAL', '_LIST' ), 
array( 'disparity' => true  ) ),
+                       array( array(),   $subjects, array( '_PVAL', '_LIST' ), 
array( 'disparity' => true  ) ),
+                       array( $subject,  $subjects, array( '_PVAL', '_LIST' ), 
array( 'disparity' => true  ) ),
+                       array( $subject,  array(),   array( '_PVAL', '_LIST' ), 
array( 'disparity' => true  ) ),
+                       array( $subject,  array(),   array( '_PVAL' ),          
array( 'disparity' => true  ) ),
+                       array( $subjects, $subjects, array( '_PVAL' ),          
array( 'disparity' => false ) ),
+                       array( $subject,  $subject,  array( '_PVAL' ),          
array( 'disparity' => false ) ),
+                       array( $subjects, $subjects, array( '_PVAL', '_LIST' ), 
array( 'disparity' => true  ) ),
+                       array( $subject,  $subject,  array( '_PVAL', '_LIST' ), 
array( 'disparity' => true  ) )
                );
        }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifd8604d8ed551a62e3aa5c160e536c5cb1d0e718
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <jamesin.hongkon...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to