Update RELEASE-NOTES-1.34
[lhc/web/wiklou.git] / includes / deferred / DataUpdate.php
index 2865461..ed9a746 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.
  */
 abstract class DataUpdate implements DeferrableUpdate {
+       /** @var mixed Result from LBFactory::getEmptyTransactionTicket() */
+       protected $ticket;
+       /** @var string Short update cause action description */
+       protected $causeAction = 'unknown';
+       /** @var string Short update cause user description */
+       protected $causeAgent = 'unknown';
+
        public function __construct() {
                // noop
        }
 
        /**
-        * Begin an appropriate transaction, if any.
-        * This default implementation does nothing.
+        * @param mixed $ticket Result of getEmptyTransactionTicket()
+        * @since 1.28
         */
-       public function beginTransaction() {
-               // noop
+       public function setTransactionTicket( $ticket ) {
+               $this->ticket = $ticket;
        }
 
        /**
-        * Commit the transaction started via beginTransaction, if any.
-        * This default implementation does nothing.
+        * @param string $action Action type
+        * @param string $user User name
         */
-       public function commitTransaction() {
-               // noop
+       public function setCause( $action, $user ) {
+               $this->causeAction = $action;
+               $this->causeAgent = $user;
        }
 
        /**
-        * Abort / roll back the transaction started via beginTransaction, if any.
-        * This default implementation does nothing.
+        * @return string
         */
-       public function rollbackTransaction() {
-               // noop
+       public function getCauseAction() {
+               return $this->causeAction;
        }
 
        /**
-        * Convenience method, calls doUpdate() on every DataUpdate in the array.
-        *
-        * This methods supports transactions logic by first calling beginTransaction()
-        * on all updates in the array, then calling doUpdate() on each, and, if all goes well,
-        * then calling commitTransaction() on each update. If an error occurs,
-        * rollbackTransaction() will be called on any update object that had beginTransaction()
-        * called but not yet commitTransaction().
-        *
-        * This allows for limited transactional logic across multiple backends for storing
-        * secondary data.
-        *
-        * @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
+        * @return string
         */
-       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 = [];
-               $exception = null;
-
-               try {
-                       // begin transactions
-                       foreach ( $updates as $update ) {
-                               $update->beginTransaction();
-                               $open_transactions[] = $update;
-                       }
-
-                       // do work
-                       foreach ( $updates as $update ) {
-                               $update->doUpdate();
-                       }
-
-                       // commit transactions
-                       while ( count( $open_transactions ) > 0 ) {
-                               $trans = array_pop( $open_transactions );
-                               $trans->commitTransaction();
-                       }
-               } catch ( Exception $ex ) {
-                       $exception = $ex;
-                       wfDebug( "Caught exception, will rethrow after rollback: " .
-                               $ex->getMessage() . "\n" );
-               }
-
-               // rollback remaining transactions
-               while ( count( $open_transactions ) > 0 ) {
-                       $trans = array_pop( $open_transactions );
-                       $trans->rollbackTransaction();
-               }
-
-               if ( $exception ) {
-                       throw $exception; // rethrow after cleanup
-               }
+       public function getCauseAgent() {
+               return $this->causeAgent;
        }
 
        /**
-        * Enqueue jobs for every DataUpdate that support enqueueUpdate()
-        * and return the remaining DataUpdate objects (those that do not)
+        * Convenience method, calls doUpdate() on every DataUpdate in the array.
         *
         * @param DataUpdate[] $updates A list of DataUpdate instances
-        * @return DataUpdate[]
-        * @since 1.27
+        * @throws Exception
+        * @deprecated Since 1.28 Use DeferredUpdates::execute()
         */
-       protected static function enqueueUpdates( array $updates ) {
-               $remaining = [];
-
+       public static function runUpdates( array $updates ) {
                foreach ( $updates as $update ) {
-                       if ( $update instanceof EnqueueableDataUpdate ) {
-                               $spec = $update->getAsJobSpecification();
-                               JobQueueGroup::singleton( $spec['wiki'] )->push( $spec['job'] );
-                       } else {
-                               $remaining[] = $update;
-                       }
+                       $update->doUpdate();
                }
-
-               return $remaining;
        }
 }
-
-/**
- * Interface that marks a DataUpdate as enqueuable via the JobQueue
- *
- * Such updates must be representable using IJobSpecification, so that
- * they can be serialized into jobs and enqueued for later execution
- *
- * @since 1.27
- */
-interface EnqueueableDataUpdate {
-       /**
-        * @return array (wiki => wiki ID, job => IJobSpecification)
-        */
-       public function getAsJobSpecification();
-}