Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / deferred / 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 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 occurs,
67 * rollbackTransaction() will be called on any update object that had beginTransaction()
68 * called but not yet commitTransaction().
69 *
70 * This allows for limited transactional logic across multiple backends for storing
71 * secondary data.
72 *
73 * @param DataUpdate[] $updates A list of DataUpdate instances
74 * @param string $mode Use "enqueue" to use the job queue when possible [Default: run]
75 * @throws Exception|null
76 */
77 public static function runUpdates( array $updates, $mode = 'run' ) {
78 if ( $mode === 'enqueue' ) {
79 // When possible, push updates as jobs instead of calling doUpdate()
80 $updates = self::enqueueUpdates( $updates );
81 }
82
83 if ( !count( $updates ) ) {
84 return; // nothing to do
85 }
86
87 $open_transactions = [];
88 $exception = null;
89
90 try {
91 // begin transactions
92 foreach ( $updates as $update ) {
93 $update->beginTransaction();
94 $open_transactions[] = $update;
95 }
96
97 // do work
98 foreach ( $updates as $update ) {
99 $update->doUpdate();
100 }
101
102 // commit transactions
103 while ( count( $open_transactions ) > 0 ) {
104 $trans = array_pop( $open_transactions );
105 $trans->commitTransaction();
106 }
107 } catch ( Exception $ex ) {
108 $exception = $ex;
109 wfDebug( "Caught exception, will rethrow after rollback: " .
110 $ex->getMessage() . "\n" );
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 /**
125 * Enqueue jobs for every DataUpdate that support enqueueUpdate()
126 * and return the remaining DataUpdate objects (those that do not)
127 *
128 * @param DataUpdate[] $updates A list of DataUpdate instances
129 * @return DataUpdate[]
130 * @since 1.27
131 */
132 protected static function enqueueUpdates( array $updates ) {
133 $remaining = [];
134
135 foreach ( $updates as $update ) {
136 if ( $update instanceof EnqueueableDataUpdate ) {
137 $spec = $update->getAsJobSpecification();
138 JobQueueGroup::singleton( $spec['wiki'] )->push( $spec['job'] );
139 } else {
140 $remaining[] = $update;
141 }
142 }
143
144 return $remaining;
145 }
146 }
147
148 /**
149 * Interface that marks a DataUpdate as enqueuable via the JobQueue
150 *
151 * Such updates must be representable using IJobSpecification, so that
152 * they can be serialized into jobs and enqueued for later execution
153 *
154 * @since 1.27
155 */
156 interface EnqueueableDataUpdate {
157 /**
158 * @return array (wiki => wiki ID, job => IJobSpecification)
159 */
160 public function getAsJobSpecification();
161 }