Fix issues identified by SpaceBeforeSingleLineComment sniff
[lhc/web/wiklou.git] / includes / deferred / DataUpdate.php
index 53990bf..f2421f4 100644 (file)
  * Abstract base class for update jobs that do something with some secondary
  * data extracted from article.
  *
- * @note: subclasses should NOT start or commit transactions in their doUpdate() method,
- *        a transaction will automatically be wrapped around the update. If need be,
- *        subclasses can override the beginTransaction() and commitTransaction() methods.
+ * @note subclasses should NOT start or commit transactions in their doUpdate() method,
+ *       a transaction will automatically be wrapped around the update. If need be,
+ *       subclasses can override the beginTransaction() and commitTransaction() methods.
  */
 abstract class DataUpdate implements DeferrableUpdate {
-       /**
-        * Constructor
-        */
        public function __construct() {
-               noop
+               //noop
        }
 
        /**
@@ -42,7 +39,7 @@ abstract class DataUpdate implements DeferrableUpdate {
         * This default implementation does nothing.
         */
        public function beginTransaction() {
-               //noop
+               // noop
        }
 
        /**
@@ -50,7 +47,7 @@ abstract class DataUpdate implements DeferrableUpdate {
         * This default implementation does nothing.
         */
        public function commitTransaction() {
-               //noop
+               // noop
        }
 
        /**
@@ -58,7 +55,7 @@ abstract class DataUpdate implements DeferrableUpdate {
         * This default implementation does nothing.
         */
        public function rollbackTransaction() {
-               //noop
+               // noop
        }
 
        /**
@@ -73,22 +70,23 @@ abstract class DataUpdate implements DeferrableUpdate {
         * This allows for limited transactional logic across multiple backends for storing
         * secondary data.
         *
-        * @param array $updates a list of DataUpdate instances
+        * @param DataUpdate[] $updates A list of DataUpdate instances
+        * @param string $mode Use "enqueue" to use the job queue when possible [Default: run]
         * @throws Exception|null
         */
-       public static function runUpdates( $updates ) {
-               if ( empty( $updates ) ) {
-                       return; # nothing to do
+       public static function runUpdates( array $updates, $mode = 'run' ) {
+               if ( $mode === 'enqueue' ) {
+                       // When possible, push updates as jobs instead of calling doUpdate()
+                       $updates = self::enqueueUpdates( $updates );
+               }
+
+               if ( !count( $updates ) ) {
+                       return; // nothing to do
                }
 
                $open_transactions = array();
                $exception = null;
 
-               /**
-                * @var $update DataUpdate
-                * @var $trans DataUpdate
-                */
-
                try {
                        // begin transactions
                        foreach ( $updates as $update ) {
@@ -122,4 +120,36 @@ abstract class DataUpdate implements DeferrableUpdate {
                        throw $exception; // rethrow after cleanup
                }
        }
+
+       /**
+        * Enqueue jobs for every DataUpdate that support enqueueUpdate()
+        * and return the remaining DataUpdate objects (those that do not)
+        *
+        * @param DataUpdate[] $updates A list of DataUpdate instances
+        * @return DataUpdate[]
+        * @since 1.26
+        */
+       protected static function enqueueUpdates( array $updates ) {
+               $remaining = array();
+
+               foreach ( $updates as $update ) {
+                       if ( $update instanceof EnqueueableDataUpdate ) {
+                               $update->enqueueUpdate();
+                       } else {
+                               $remaining[] = $update;
+                       }
+               }
+
+               return $remaining;
+       }
+}
+
+/**
+ * @since 1.26
+ */
+interface EnqueueableDataUpdate {
+       /**
+        * Push the update into the job queue
+        */
+       public function enqueueUpdate();
 }