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
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() {
38 * Begin an appropriate transaction, if any.
39 * This default implementation does nothing.
41 public function beginTransaction() {
46 * Commit the transaction started via beginTransaction, if any.
47 * This default implementation does nothing.
49 public function commitTransaction() {
54 * Abort / roll back the transaction started via beginTransaction, if any.
55 * This default implementation does nothing.
57 public function rollbackTransaction() {
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
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 = [];
92 foreach ( $updates as $update ) {
93 $update->beginTransaction();
94 $open_transactions[] = $update;
98 foreach ( $updates as $update ) {
102 // commit transactions
103 while ( count( $open_transactions ) > 0 ) {
104 $trans = array_pop( $open_transactions );
105 $trans->commitTransaction();
107 } catch ( 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();
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[]
132 protected static function enqueueUpdates( array $updates ) {
135 foreach ( $updates as $update ) {
136 if ( $update instanceof EnqueueableDataUpdate
) {
137 $spec = $update->getAsJobSpecification();
138 JobQueueGroup
::singleton( $spec['wiki'] )->push( $spec['job'] );
140 $remaining[] = $update;
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
156 interface EnqueueableDataUpdate
{
158 * @return array (wiki => wiki ID, job => IJobSpecification)
160 public function getAsJobSpecification();