- * 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
- */
- 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
- }
- }
-
- /**
- * Enqueue jobs for every DataUpdate that support enqueueUpdate()
- * and return the remaining DataUpdate objects (those that do not)
- *