Merge "resourceloader: Implement mwLoadEnd marker"
[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 * HTMLCacheUpdates are the most common deferred update people use. This
83 * is a shortcut method for that.
84 * @see HTMLCacheUpdate::__construct()
85 * @param Title $title
86 * @param string $table
87 */
88 public static function addHTMLCacheUpdate( $title, $table ) {
89 self::addUpdate( new HTMLCacheUpdate( $title, $table ) );
90 }
91
92 /**
93 * Add a callable update. In a lot of cases, we just need a callback/closure,
94 * defining a new DeferrableUpdate object is not necessary
95 * @see MWCallableUpdate::__construct()
96 * @param callable $callable
97 */
98 public static function addCallableUpdate( $callable ) {
99 self::addUpdate( new MWCallableUpdate( $callable ) );
100 }
101
102 /**
103 * Do any deferred updates and clear the list
104 *
105 * @param string $commit Set to 'commit' to commit after every update to
106 * prevent lock contention
107 */
108 public static function doUpdates( $commit = '' ) {
109 global $wgDeferredUpdateList;
110
111 $updates = array_merge( $wgDeferredUpdateList, self::$updates );
112
113 while ( count( $updates ) ) {
114 self::clearPendingUpdates();
115
116 /** @var DeferrableUpdate $update */
117 foreach ( $updates as $update ) {
118 try {
119 $update->doUpdate();
120
121 if ( $commit === 'commit' ) {
122 wfGetLBFactory()->commitMasterChanges();
123 }
124 } catch ( Exception $e ) {
125 // We don't want exceptions thrown during deferred updates to
126 // be reported to the user since the output is already sent.
127 // Instead we just log them.
128 if ( !$e instanceof ErrorPageError ) {
129 MWExceptionHandler::logException( $e );
130 }
131 }
132 }
133
134 $updates = array_merge( $wgDeferredUpdateList, self::$updates );
135 }
136 }
137
138 /**
139 * Clear all pending updates without performing them. Generally, you don't
140 * want or need to call this. Unit tests need it though.
141 */
142 public static function clearPendingUpdates() {
143 global $wgDeferredUpdateList;
144 $wgDeferredUpdateList = self::$updates = array();
145 }
146 }