Merge "Fix edit link for messages in $wgForceUIMsgAsContentMsg"
[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 *
43 * @since 1.19
44 */
45 class DeferredUpdates {
46 /**
47 * @var array Updates to be deferred until the end of the request.
48 */
49 private static $updates = array();
50
51 /**
52 * Add an update to the deferred list
53 * @param DeferrableUpdate $update Some object that implements doUpdate()
54 */
55 public static function addUpdate( DeferrableUpdate $update ) {
56 global $wgCommandLineMode;
57
58 array_push( self::$updates, $update );
59
60 // CLI scripts may forget to periodically flush these updates,
61 // so try to handle that rather than OOMing and losing them.
62 // Try to run the updates as soon as there is no local transaction.
63 static $waitingOnTrx = false; // de-duplicate callback
64 if ( $wgCommandLineMode && !$waitingOnTrx ) {
65 $lb = wfGetLB();
66 $dbw = $lb->getAnyOpenConnection( $lb->getWriterIndex() );
67 // Do the update as soon as there is no transaction
68 if ( $dbw && $dbw->trxLevel() ) {
69 $waitingOnTrx = true;
70 $dbw->onTransactionIdle( function() use ( &$waitingOnTrx ) {
71 DeferredUpdates::doUpdates();
72 $waitingOnTrx = false;
73 } );
74 } else {
75 self::doUpdates();
76 }
77 }
78 }
79
80 /**
81 * HTMLCacheUpdates are the most common deferred update people use. This
82 * is a shortcut method for that.
83 * @see HTMLCacheUpdate::__construct()
84 * @param Title $title
85 * @param string $table
86 */
87 public static function addHTMLCacheUpdate( $title, $table ) {
88 self::addUpdate( new HTMLCacheUpdate( $title, $table ) );
89 }
90
91 /**
92 * Add a callable update. In a lot of cases, we just need a callback/closure,
93 * defining a new DeferrableUpdate object is not necessary
94 * @see MWCallableUpdate::__construct()
95 * @param callable $callable
96 */
97 public static function addCallableUpdate( $callable ) {
98 self::addUpdate( new MWCallableUpdate( $callable ) );
99 }
100
101 /**
102 * Do any deferred updates and clear the list
103 *
104 * @param string $commit Set to 'commit' to commit after every update to
105 * prevent lock contention
106 */
107 public static function doUpdates( $commit = '' ) {
108 global $wgDeferredUpdateList;
109
110 $updates = array_merge( $wgDeferredUpdateList, self::$updates );
111
112 while ( count( $updates ) ) {
113 self::clearPendingUpdates();
114
115 /** @var DeferrableUpdate $update */
116 foreach ( $updates as $update ) {
117 try {
118 $update->doUpdate();
119
120 if ( $commit === 'commit' ) {
121 wfGetLBFactory()->commitMasterChanges();
122 }
123 } catch ( Exception $e ) {
124 // We don't want exceptions thrown during deferred updates to
125 // be reported to the user since the output is already sent.
126 // Instead we just log them.
127 if ( !$e instanceof ErrorPageError ) {
128 MWExceptionHandler::logException( $e );
129 }
130 }
131 }
132
133 $updates = array_merge( $wgDeferredUpdateList, self::$updates );
134 }
135 }
136
137 /**
138 * Clear all pending updates without performing them. Generally, you don't
139 * want or need to call this. Unit tests need it though.
140 */
141 public static function clearPendingUpdates() {
142 global $wgDeferredUpdateList;
143 $wgDeferredUpdateList = self::$updates = array();
144 }
145 }