AuthManager: Commit transaction after auto-creating a user
[mediawiki.git] / includes / deferred / DataUpdate.php
blob2865461e5e6768bd32a487938266440e88b37660
1 <?php
2 /**
3 * Base code for update jobs that do something with some secondary
4 * data extracted from article.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @file
24 /**
25 * Abstract base class for update jobs that do something with some secondary
26 * data extracted from article.
28 * @note subclasses should NOT start or commit transactions in their doUpdate() method,
29 * a transaction will automatically be wrapped around the update. If need be,
30 * subclasses can override the beginTransaction() and commitTransaction() methods.
32 abstract class DataUpdate implements DeferrableUpdate {
33 public function __construct() {
34 // noop
37 /**
38 * Begin an appropriate transaction, if any.
39 * This default implementation does nothing.
41 public function beginTransaction() {
42 // noop
45 /**
46 * Commit the transaction started via beginTransaction, if any.
47 * This default implementation does nothing.
49 public function commitTransaction() {
50 // noop
53 /**
54 * Abort / roll back the transaction started via beginTransaction, if any.
55 * This default implementation does nothing.
57 public function rollbackTransaction() {
58 // noop
61 /**
62 * Convenience method, calls doUpdate() on every DataUpdate in the array.
64 * This methods supports transactions logic by first calling beginTransaction()
65 * on all updates in the array, then calling doUpdate() on each, and, if all goes well,
66 * then calling commitTransaction() on each update. If an error occurs,
67 * rollbackTransaction() will be called on any update object that had beginTransaction()
68 * called but not yet commitTransaction().
70 * This allows for limited transactional logic across multiple backends for storing
71 * secondary data.
73 * @param DataUpdate[] $updates A list of DataUpdate instances
74 * @param string $mode Use "enqueue" to use the job queue when possible [Default: run]
75 * @throws Exception|null
77 public static function runUpdates( array $updates, $mode = 'run' ) {
78 if ( $mode === 'enqueue' ) {
79 // When possible, push updates as jobs instead of calling doUpdate()
80 $updates = self::enqueueUpdates( $updates );
83 if ( !count( $updates ) ) {
84 return; // nothing to do
87 $open_transactions = [];
88 $exception = null;
90 try {
91 // begin transactions
92 foreach ( $updates as $update ) {
93 $update->beginTransaction();
94 $open_transactions[] = $update;
97 // do work
98 foreach ( $updates as $update ) {
99 $update->doUpdate();
102 // commit transactions
103 while ( count( $open_transactions ) > 0 ) {
104 $trans = array_pop( $open_transactions );
105 $trans->commitTransaction();
107 } catch ( Exception $ex ) {
108 $exception = $ex;
109 wfDebug( "Caught exception, will rethrow after rollback: " .
110 $ex->getMessage() . "\n" );
113 // rollback remaining transactions
114 while ( count( $open_transactions ) > 0 ) {
115 $trans = array_pop( $open_transactions );
116 $trans->rollbackTransaction();
119 if ( $exception ) {
120 throw $exception; // rethrow after cleanup
125 * Enqueue jobs for every DataUpdate that support enqueueUpdate()
126 * and return the remaining DataUpdate objects (those that do not)
128 * @param DataUpdate[] $updates A list of DataUpdate instances
129 * @return DataUpdate[]
130 * @since 1.27
132 protected static function enqueueUpdates( array $updates ) {
133 $remaining = [];
135 foreach ( $updates as $update ) {
136 if ( $update instanceof EnqueueableDataUpdate ) {
137 $spec = $update->getAsJobSpecification();
138 JobQueueGroup::singleton( $spec['wiki'] )->push( $spec['job'] );
139 } else {
140 $remaining[] = $update;
144 return $remaining;
149 * Interface that marks a DataUpdate as enqueuable via the JobQueue
151 * Such updates must be representable using IJobSpecification, so that
152 * they can be serialized into jobs and enqueued for later execution
154 * @since 1.27
156 interface EnqueueableDataUpdate {
158 * @return array (wiki => wiki ID, job => IJobSpecification)
160 public function getAsJobSpecification();