Add a way for packagers to override some installation details
[mediawiki.git] / includes / DataUpdate.php
blob7203c3b3c3e2f3b3a2af206cfbd4295f019b030b
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 abstract class DataUpdate implements DeferrableUpdate {
30 /**
31 * Constructor
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 occurrs,
67 * rollbackTransaction() will be called on any update object that had beginTranscation()
68 * called but not yet commitTransaction().
70 * This allows for limited transactional logic across multiple backends for storing
71 * secondary data.
73 * @static
74 * @param $updates array a list of DataUpdate instances
76 public static function runUpdates( $updates ) {
77 if ( empty( $updates ) ) return; # nothing to do
79 $open_transactions = array();
80 $exception = null;
82 /**
83 * @var $update StorageUpdate
84 * @var $trans StorageUpdate
87 try {
88 // begin transactions
89 foreach ( $updates as $update ) {
90 $update->beginTransaction();
91 $open_transactions[] = $update;
94 // do work
95 foreach ( $updates as $update ) {
96 $update->doUpdate();
99 // commit transactions
100 while ( count( $open_transactions ) > 0 ) {
101 $trans = array_pop( $open_transactions );
102 $trans->commitTransaction();
104 } catch ( Exception $ex ) {
105 $exception = $ex;
106 wfDebug( "Caught exception, will rethrow after rollback: " . $ex->getMessage() );
109 // rollback remaining transactions
110 while ( count( $open_transactions ) > 0 ) {
111 $trans = array_pop( $open_transactions );
112 $trans->rollbackTransaction();
115 if ( $exception ) {
116 throw $exception; // rethrow after cleanup