Merge "Fix exception in Import, when import of a revision fails"
[lhc/web/wiklou.git] / includes / deferred / DeferredUpdates.php
1 <?php
2 /**
3 * Interface and manager for deferred updates.
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 * @file
21 */
22
23 /**
24 * Interface that deferrable updates should implement. Basically required so we
25 * can validate input on DeferredUpdates::addUpdate()
26 *
27 * @since 1.19
28 */
29 interface DeferrableUpdate {
30 /**
31 * Perform the actual work
32 */
33 function doUpdate();
34 }
35
36 /**
37 * Class for managing the deferred updates
38 *
39 * Deferred updates can be run at the end of the request,
40 * after the HTTP response has been sent. In CLI mode, updates
41 * are only deferred until there is no local master DB transaction.
42 * When updates are deferred, they go into a simple FIFO queue.
43 *
44 * @since 1.19
45 */
46 class DeferredUpdates {
47 /**
48 * @var array Updates to be deferred until the end of the request.
49 */
50 private static $updates = array();
51
52 /**
53 * Add an update to the deferred list
54 * @param DeferrableUpdate $update Some object that implements doUpdate()
55 */
56 public static function addUpdate( DeferrableUpdate $update ) {
57 global $wgCommandLineMode;
58
59 array_push( self::$updates, $update );
60
61 // CLI scripts may forget to periodically flush these updates,
62 // so try to handle that rather than OOMing and losing them.
63 // Try to run the updates as soon as there is no local transaction.
64 static $waitingOnTrx = false; // de-duplicate callback
65 if ( $wgCommandLineMode && !$waitingOnTrx ) {
66 $lb = wfGetLB();
67 $dbw = $lb->getAnyOpenConnection( $lb->getWriterIndex() );
68 // Do the update as soon as there is no transaction
69 if ( $dbw && $dbw->trxLevel() ) {
70 $waitingOnTrx = true;
71 $dbw->onTransactionIdle( function() use ( &$waitingOnTrx ) {
72 DeferredUpdates::doUpdates();
73 $waitingOnTrx = false;
74 } );
75 } else {
76 self::doUpdates();
77 }
78 }
79 }
80
81 /**
82 * Add a callable update. In a lot of cases, we just need a callback/closure,
83 * defining a new DeferrableUpdate object is not necessary
84 * @see MWCallableUpdate::__construct()
85 * @param callable $callable
86 */
87 public static function addCallableUpdate( $callable ) {
88 self::addUpdate( new MWCallableUpdate( $callable ) );
89 }
90
91 /**
92 * Do any deferred updates and clear the list
93 *
94 * @param string $commit Set to 'commit' to commit after every update to
95 * prevent lock contention
96 */
97 public static function doUpdates( $commit = '' ) {
98 $updates = self::$updates;
99
100 while ( count( $updates ) ) {
101 self::clearPendingUpdates();
102
103 /** @var DeferrableUpdate $update */
104 foreach ( $updates as $update ) {
105 try {
106 $update->doUpdate();
107
108 if ( $commit === 'commit' ) {
109 wfGetLBFactory()->commitMasterChanges();
110 }
111 } catch ( Exception $e ) {
112 // We don't want exceptions thrown during deferred updates to
113 // be reported to the user since the output is already sent.
114 // Instead we just log them.
115 if ( !$e instanceof ErrorPageError ) {
116 MWExceptionHandler::logException( $e );
117 }
118 }
119 }
120
121 $updates = self::$updates;
122 }
123 }
124
125 /**
126 * Clear all pending updates without performing them. Generally, you don't
127 * want or need to call this. Unit tests need it though.
128 */
129 public static function clearPendingUpdates() {
130 self::$updates = array();
131 }
132 }