Merge "Less wild whitespace"
[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 * @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.
31 */
32 abstract class DataUpdate implements DeferrableUpdate {
33
34 /**
35 * Constructor
36 */
37 public function __construct( ) {
38 # noop
39 }
40
41 /**
42 * Begin an appropriate transaction, if any.
43 * This default implementation does nothing.
44 */
45 public function beginTransaction() {
46 //noop
47 }
48
49 /**
50 * Commit the transaction started via beginTransaction, if any.
51 * This default implementation does nothing.
52 */
53 public function commitTransaction() {
54 //noop
55 }
56
57 /**
58 * Abort / roll back the transaction started via beginTransaction, if any.
59 * This default implementation does nothing.
60 */
61 public function rollbackTransaction() {
62 //noop
63 }
64
65 /**
66 * Convenience method, calls doUpdate() on every DataUpdate in the array.
67 *
68 * This methods supports transactions logic by first calling beginTransaction()
69 * on all updates in the array, then calling doUpdate() on each, and, if all goes well,
70 * then calling commitTransaction() on each update. If an error occurrs,
71 * rollbackTransaction() will be called on any update object that had beginTranscation()
72 * called but not yet commitTransaction().
73 *
74 * This allows for limited transactional logic across multiple backends for storing
75 * secondary data.
76 *
77 * @param $updates array a list of DataUpdate instances
78 * @throws Exception|null
79 */
80 public static function runUpdates( $updates ) {
81 if ( empty( $updates ) ) return; # nothing to do
82
83 $open_transactions = array();
84 $exception = null;
85
86 /**
87 * @var $update DataUpdate
88 * @var $trans DataUpdate
89 */
90
91 try {
92 // begin transactions
93 foreach ( $updates as $update ) {
94 $update->beginTransaction();
95 $open_transactions[] = $update;
96 }
97
98 // do work
99 foreach ( $updates as $update ) {
100 $update->doUpdate();
101 }
102
103 // commit transactions
104 while ( count( $open_transactions ) > 0 ) {
105 $trans = array_pop( $open_transactions );
106 $trans->commitTransaction();
107 }
108 } catch ( Exception $ex ) {
109 $exception = $ex;
110 wfDebug( "Caught exception, will rethrow after rollback: " . $ex->getMessage() );
111 }
112
113 // rollback remaining transactions
114 while ( count( $open_transactions ) > 0 ) {
115 $trans = array_pop( $open_transactions );
116 $trans->rollbackTransaction();
117 }
118
119 if ( $exception ) {
120 throw $exception; // rethrow after cleanup
121 }
122 }
123
124 }