In ApiMain, gather Vary headers in OutputPage
[lhc/web/wiklou.git] / includes / DataUpdate.php
1 <?php
2 /**
3 * Base code for update jobs that do something with some secondary
4 * data extracted from article.
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 */
23
24 /**
25 * Abstract base class for update jobs that do something with some secondary
26 * data extracted from article.
27 */
28 abstract class DataUpdate implements DeferrableUpdate {
29
30 /**
31 * Constructor
32 */
33 public function __construct( ) {
34 # noop
35 }
36
37 /**
38 * Begin an appropriate transaction, if any.
39 * This default implementation does nothing.
40 */
41 public function beginTransaction() {
42 //noop
43 }
44
45 /**
46 * Commit the transaction started via beginTransaction, if any.
47 * This default implementation does nothing.
48 */
49 public function commitTransaction() {
50 //noop
51 }
52
53 /**
54 * Abort / roll back the transaction started via beginTransaction, if any.
55 * This default implementation does nothing.
56 */
57 public function rollbackTransaction() {
58 //noop
59 }
60
61 /**
62 * Convenience method, calls doUpdate() on every DataUpdate in the array.
63 *
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().
69 *
70 * This allows for limited transactional logic across multiple backends for storing
71 * secondary data.
72 *
73 * @static
74 * @param $updates array a list of DataUpdate instances
75 */
76 public static function runUpdates( $updates ) {
77 if ( empty( $updates ) ) return; # nothing to do
78
79 $open_transactions = array();
80 $exception = null;
81
82 /**
83 * @var $update StorageUpdate
84 * @var $trans StorageUpdate
85 */
86
87 try {
88 // begin transactions
89 foreach ( $updates as $update ) {
90 $update->beginTransaction();
91 $open_transactions[] = $update;
92 }
93
94 // do work
95 foreach ( $updates as $update ) {
96 $update->doUpdate();
97 }
98
99 // commit transactions
100 while ( count( $open_transactions ) > 0 ) {
101 $trans = array_pop( $open_transactions );
102 $trans->commitTransaction();
103 }
104 } catch ( Exception $ex ) {
105 $exception = $ex;
106 wfDebug( "Caught exception, will rethrow after rollback: " . $ex->getMessage() );
107 }
108
109 // rollback remaining transactions
110 while ( count( $open_transactions ) > 0 ) {
111 $trans = array_pop( $open_transactions );
112 $trans->rollbackTransaction();
113 }
114
115 if ( $exception ) {
116 throw $exception; // rethrow after cleanup
117 }
118 }
119
120 }