Generalizing LinksUpdate to allow extensions to add arbitrary update handlers.
[lhc/web/wiklou.git] / includes / DataUpdate.php
1 <?php
2 /**
3 * See docs/deferred.txt
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * Abstract base class for update jobs that do something with some secondary
21 * data extracted from article.
22 */
23 abstract class DataUpdate implements DeferrableUpdate {
24
25 /**
26 * Constructor
27 */
28 public function __construct( ) {
29 # noop
30 }
31
32 /**
33 * Begin an appropriate transaction, if any.
34 * This default implementation does nothing.
35 */
36 public function beginTransaction() {
37 //noop
38 }
39
40 /**
41 * Commit the transaction started via beginTransaction, if any.
42 * This default implementation does nothing.
43 */
44 public function commitTransaction() {
45 //noop
46 }
47
48 /**
49 * Abort / roll back the transaction started via beginTransaction, if any.
50 * This default implementation does nothing.
51 */
52 public function rollbackTransaction() {
53 //noop
54 }
55
56 /**
57 * Convenience method, calls doUpdate() on every DataUpdate in the array.
58 *
59 * This methods supports transactions logic by first calling beginTransaction()
60 * on all updates in the array, then calling doUpdate() on each, and, if all goes well,
61 * then calling commitTransaction() on each update. If an error occurrs,
62 * rollbackTransaction() will be called on any update object that had beginTranscation()
63 * called but not yet commitTransaction().
64 *
65 * This allows for limited transactional logic across multiple backends for storing
66 * secondary data.
67 *
68 * @static
69 * @param $updates array a list of DataUpdate instances
70 */
71 public static function runUpdates( $updates ) {
72 if ( empty( $updates ) ) return; # nothing to do
73
74 $open_transactions = array();
75 $exception = null;
76
77 /**
78 * @var $update StorageUpdate
79 * @var $trans StorageUpdate
80 */
81
82 try {
83 // begin transactions
84 foreach ( $updates as $update ) {
85 $update->beginTransaction();
86 $open_transactions[] = $update;
87 }
88
89 // do work
90 foreach ( $updates as $update ) {
91 $update->doUpdate();
92 }
93
94 // commit transactions
95 while ( count( $open_transactions ) > 0 ) {
96 $trans = array_pop( $open_transactions );
97 $trans->commitTransaction();
98 }
99 } catch ( Exception $ex ) {
100 $exception = $ex;
101 wfDebug( "Caught exception, will rethrow after rollback: " . $ex->getMessage() );
102 }
103
104 // rollback remaining transactions
105 while ( count( $open_transactions ) > 0 ) {
106 $trans = array_pop( $open_transactions );
107 $trans->rollbackTransaction();
108 }
109
110 if ( $exception ) {
111 throw $exception; // rethrow after cleanup
112 }
113 }
114
115 }