Merge "EditPage: Show a different label for the button on create vs. modify"
[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 /** @var mixed Result from LBFactory::getEmptyTransactionTicket() */
34 protected $ticket;
35
36 public function __construct() {
37 // noop
38 }
39
40 /**
41 * @param mixed $ticket Result of getEmptyTransactionTicket()
42 * @since 1.28
43 */
44 public function setTransactionTicket( $ticket ) {
45 $this->ticket = $ticket;
46 }
47
48 /**
49 * Begin an appropriate transaction, if any.
50 * This default implementation does nothing.
51 */
52 public function beginTransaction() {
53 // noop
54 }
55
56 /**
57 * Commit the transaction started via beginTransaction, if any.
58 * This default implementation does nothing.
59 */
60 public function commitTransaction() {
61 // noop
62 }
63
64 /**
65 * Abort / roll back the transaction started via beginTransaction, if any.
66 * This default implementation does nothing.
67 */
68 public function rollbackTransaction() {
69 // noop
70 }
71
72 /**
73 * Convenience method, calls doUpdate() on every DataUpdate in the array.
74 *
75 * This methods supports transactions logic by first calling beginTransaction()
76 * on all updates in the array, then calling doUpdate() on each, and, if all goes well,
77 * then calling commitTransaction() on each update. If an error occurs,
78 * rollbackTransaction() will be called on any update object that had beginTransaction()
79 * called but not yet commitTransaction().
80 *
81 * This allows for limited transactional logic across multiple backends for storing
82 * secondary data.
83 *
84 * @param DataUpdate[] $updates A list of DataUpdate instances
85 * @param string $mode Use "enqueue" to use the job queue when possible [Default: run]
86 * @throws Exception|null
87 */
88 public static function runUpdates( array $updates, $mode = 'run' ) {
89 if ( $mode === 'enqueue' ) {
90 // When possible, push updates as jobs instead of calling doUpdate()
91 $updates = self::enqueueUpdates( $updates );
92 }
93
94 if ( !count( $updates ) ) {
95 return; // nothing to do
96 }
97
98 $open_transactions = [];
99 $exception = null;
100
101 try {
102 // begin transactions
103 foreach ( $updates as $update ) {
104 $update->beginTransaction();
105 $open_transactions[] = $update;
106 }
107
108 // do work
109 foreach ( $updates as $update ) {
110 $update->doUpdate();
111 }
112
113 // commit transactions
114 while ( count( $open_transactions ) > 0 ) {
115 $trans = array_pop( $open_transactions );
116 $trans->commitTransaction();
117 }
118 } catch ( Exception $ex ) {
119 $exception = $ex;
120 wfDebug( "Caught exception, will rethrow after rollback: " .
121 $ex->getMessage() . "\n" );
122 }
123
124 // rollback remaining transactions
125 while ( count( $open_transactions ) > 0 ) {
126 $trans = array_pop( $open_transactions );
127 $trans->rollbackTransaction();
128 }
129
130 if ( $exception ) {
131 throw $exception; // rethrow after cleanup
132 }
133 }
134
135 /**
136 * Enqueue jobs for every DataUpdate that support enqueueUpdate()
137 * and return the remaining DataUpdate objects (those that do not)
138 *
139 * @param DataUpdate[] $updates A list of DataUpdate instances
140 * @return DataUpdate[]
141 * @since 1.27
142 */
143 protected static function enqueueUpdates( array $updates ) {
144 $remaining = [];
145
146 foreach ( $updates as $update ) {
147 if ( $update instanceof EnqueueableDataUpdate ) {
148 $spec = $update->getAsJobSpecification();
149 JobQueueGroup::singleton( $spec['wiki'] )->push( $spec['job'] );
150 } else {
151 $remaining[] = $update;
152 }
153 }
154
155 return $remaining;
156 }
157 }