Don't rely on $wgTitle in WebRequest
[mediawiki.git] / includes / deferred / DataUpdate.php
blobed12c6012eed4bb3b2abfae72e3a508b77ed4ba1
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 /**
34 * Constructor
36 public function __construct() {
37 # noop
40 /**
41 * Begin an appropriate transaction, if any.
42 * This default implementation does nothing.
44 public function beginTransaction() {
45 //noop
48 /**
49 * Commit the transaction started via beginTransaction, if any.
50 * This default implementation does nothing.
52 public function commitTransaction() {
53 //noop
56 /**
57 * Abort / roll back the transaction started via beginTransaction, if any.
58 * This default implementation does nothing.
60 public function rollbackTransaction() {
61 //noop
64 /**
65 * Convenience method, calls doUpdate() on every DataUpdate in the array.
67 * This methods supports transactions logic by first calling beginTransaction()
68 * on all updates in the array, then calling doUpdate() on each, and, if all goes well,
69 * then calling commitTransaction() on each update. If an error occurs,
70 * rollbackTransaction() will be called on any update object that had beginTransaction()
71 * called but not yet commitTransaction().
73 * This allows for limited transactional logic across multiple backends for storing
74 * secondary data.
76 * @param array $updates A list of DataUpdate instances
77 * @throws Exception|null
79 public static function runUpdates( $updates ) {
80 if ( empty( $updates ) ) {
81 return; # nothing to do
84 $open_transactions = array();
85 $exception = null;
87 /**
88 * @var $update DataUpdate
89 * @var $trans DataUpdate
92 try {
93 // begin transactions
94 foreach ( $updates as $update ) {
95 $update->beginTransaction();
96 $open_transactions[] = $update;
99 // do work
100 foreach ( $updates as $update ) {
101 $update->doUpdate();
104 // commit transactions
105 while ( count( $open_transactions ) > 0 ) {
106 $trans = array_pop( $open_transactions );
107 $trans->commitTransaction();
109 } catch ( Exception $ex ) {
110 $exception = $ex;
111 wfDebug( "Caught exception, will rethrow after rollback: " .
112 $ex->getMessage() . "\n" );
115 // rollback remaining transactions
116 while ( count( $open_transactions ) > 0 ) {
117 $trans = array_pop( $open_transactions );
118 $trans->rollbackTransaction();
121 if ( $exception ) {
122 throw $exception; // rethrow after cleanup