Make DeferredUpdates::doUpdates use DataUpdate::runUpdates
[lhc/web/wiklou.git] / includes / deferred / DeferredUpdates.php
index 29bb8d7..8b3582d 100644 (file)
@@ -39,14 +39,15 @@ interface DeferrableUpdate {
  * Deferred updates can be run at the end of the request,
  * after the HTTP response has been sent. In CLI mode, updates
  * are only deferred until there is no local master DB transaction.
+ * When updates are deferred, they go into a simple FIFO queue.
  *
  * @since 1.19
  */
 class DeferredUpdates {
-       /**
-        * @var array Updates to be deferred until the end of the request.
-        */
+       /** @var DeferrableUpdate[] Updates to be deferred until the end of the request */
        private static $updates = array();
+       /** @var bool Defer updates fully even in CLI mode */
+       private static $forceDeferral = false;
 
        /**
         * Add an update to the deferred list
@@ -56,6 +57,9 @@ class DeferredUpdates {
                global $wgCommandLineMode;
 
                array_push( self::$updates, $update );
+               if ( self::$forceDeferral ) {
+                       return;
+               }
 
                // CLI scripts may forget to periodically flush these updates,
                // so try to handle that rather than OOMing and losing them.
@@ -65,7 +69,7 @@ class DeferredUpdates {
                        $lb = wfGetLB();
                        $dbw = $lb->getAnyOpenConnection( $lb->getWriterIndex() );
                        // Do the update as soon as there is no transaction
-                       if ( $dbw->trxLevel() ) {
+                       if ( $dbw && $dbw->trxLevel() ) {
                                $waitingOnTrx = true;
                                $dbw->onTransactionIdle( function() use ( &$waitingOnTrx ) {
                                        DeferredUpdates::doUpdates();
@@ -77,17 +81,6 @@ class DeferredUpdates {
                }
        }
 
-       /**
-        * HTMLCacheUpdates are the most common deferred update people use. This
-        * is a shortcut method for that.
-        * @see HTMLCacheUpdate::__construct()
-        * @param Title $title
-        * @param string $table
-        */
-       public static function addHTMLCacheUpdate( $title, $table ) {
-               self::addUpdate( new HTMLCacheUpdate( $title, $table ) );
-       }
-
        /**
         * Add a callable update.  In a lot of cases, we just need a callback/closure,
         * defining a new DeferrableUpdate object is not necessary
@@ -105,15 +98,26 @@ class DeferredUpdates {
         *   prevent lock contention
         */
        public static function doUpdates( $commit = '' ) {
-               global $wgDeferredUpdateList;
-
-               $updates = array_merge( $wgDeferredUpdateList, self::$updates );
+               $updates = self::$updates;
 
                while ( count( $updates ) ) {
                        self::clearPendingUpdates();
-
-                       /** @var DeferrableUpdate $update */
+                       /** @var DataUpdate[] $dataUpdates */
+                       $dataUpdates = array();
+                       /** @var DeferrableUpdate[] $otherUpdates */
+                       $otherUpdates = array();
                        foreach ( $updates as $update ) {
+                               if ( $update instanceof DataUpdate ) {
+                                       $dataUpdates[] = $update;
+                               } else {
+                                       $otherUpdates[] = $update;
+                               }
+                       }
+
+                       // Delegate DataUpdate execution to the DataUpdate class
+                       DataUpdate::runUpdates( $dataUpdates, 'run' );
+                       // Execute the non-DataUpdate tasks
+                       foreach ( $otherUpdates as $update ) {
                                try {
                                        $update->doUpdate();
 
@@ -130,7 +134,7 @@ class DeferredUpdates {
                                }
                        }
 
-                       $updates = array_merge( $wgDeferredUpdateList, self::$updates );
+                       $updates = self::$updates;
                }
        }
 
@@ -139,7 +143,15 @@ class DeferredUpdates {
         * want or need to call this. Unit tests need it though.
         */
        public static function clearPendingUpdates() {
-               global $wgDeferredUpdateList;
-               $wgDeferredUpdateList = self::$updates = array();
+               self::$updates = array();
+       }
+
+       /**
+        * @note This method is intended for testing purposes
+        * @param bool $value Whether to *always* defer updates, even in CLI mode
+        * @since 1.26
+        */
+       public static function forceDeferral( $value ) {
+               self::$forceDeferral = $value;
        }
 }