Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / deferred / DeferredUpdates.php
blob1552777a800a96ad2b29d0628c4925349d6fdd75
1 <?php
2 /**
3 * Interface and manager for deferred updates.
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.
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.
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
20 * @file
23 /**
24 * Class for managing the deferred updates
26 * In web request mode, deferred updates can be run at the end of the request, either before or
27 * after the HTTP response has been sent. In either case, they run after the DB commit step. If
28 * an update runs after the response is sent, it will not block clients. If sent before, it will
29 * run synchronously. If such an update works via queueing, it will be more likely to complete by
30 * the time the client makes their next request after this one.
32 * In CLI mode, updates are only deferred until the current wiki has no DB write transaction
33 * active within this request.
35 * When updates are deferred, they use a FIFO queue (one for pre-send and one for post-send).
37 * @since 1.19
39 class DeferredUpdates {
40 /** @var DeferrableUpdate[] Updates to be deferred until before request end */
41 private static $preSendUpdates = [];
42 /** @var DeferrableUpdate[] Updates to be deferred until after request end */
43 private static $postSendUpdates = [];
45 const ALL = 0; // all updates
46 const PRESEND = 1; // for updates that should run before flushing output buffer
47 const POSTSEND = 2; // for updates that should run after flushing output buffer
49 /**
50 * Add an update to the deferred list
52 * @param DeferrableUpdate $update Some object that implements doUpdate()
53 * @param integer $type DeferredUpdates constant (PRESEND or POSTSEND) (since 1.27)
55 public static function addUpdate( DeferrableUpdate $update, $type = self::POSTSEND ) {
56 if ( $type === self::PRESEND ) {
57 self::push( self::$preSendUpdates, $update );
58 } else {
59 self::push( self::$postSendUpdates, $update );
63 /**
64 * Add a callable update. In a lot of cases, we just need a callback/closure,
65 * defining a new DeferrableUpdate object is not necessary
67 * @see MWCallableUpdate::__construct()
69 * @param callable $callable
70 * @param integer $type DeferredUpdates constant (PRESEND or POSTSEND) (since 1.27)
72 public static function addCallableUpdate( $callable, $type = self::POSTSEND ) {
73 self::addUpdate( new MWCallableUpdate( $callable ), $type );
76 /**
77 * Do any deferred updates and clear the list
79 * @param string $mode Use "enqueue" to use the job queue when possible [Default: "run"]
80 * @param integer $type DeferredUpdates constant (PRESEND, POSTSEND, or ALL) (since 1.27)
82 public static function doUpdates( $mode = 'run', $type = self::ALL ) {
83 if ( $type === self::ALL || $type == self::PRESEND ) {
84 self::execute( self::$preSendUpdates, $mode );
87 if ( $type === self::ALL || $type == self::POSTSEND ) {
88 self::execute( self::$postSendUpdates, $mode );
92 private static function push( array &$queue, DeferrableUpdate $update ) {
93 global $wgCommandLineMode;
95 if ( $update instanceof MergeableUpdate ) {
96 $class = get_class( $update ); // fully-qualified class
97 if ( isset( $queue[$class] ) ) {
98 /** @var $existingUpdate MergeableUpdate */
99 $existingUpdate = $queue[$class];
100 $existingUpdate->merge( $update );
101 } else {
102 $queue[$class] = $update;
104 } else {
105 $queue[] = $update;
108 // CLI scripts may forget to periodically flush these updates,
109 // so try to handle that rather than OOMing and losing them entirely.
110 // Try to run the updates as soon as there is no current wiki transaction.
111 static $waitingOnTrx = false; // de-duplicate callback
112 if ( $wgCommandLineMode && !$waitingOnTrx ) {
113 $lb = wfGetLB();
114 $dbw = $lb->getAnyOpenConnection( $lb->getWriterIndex() );
115 // Do the update as soon as there is no transaction
116 if ( $dbw && $dbw->trxLevel() ) {
117 $waitingOnTrx = true;
118 $dbw->onTransactionIdle( function() use ( &$waitingOnTrx ) {
119 DeferredUpdates::doUpdates();
120 $waitingOnTrx = false;
121 } );
122 } else {
123 self::doUpdates();
128 public static function execute( array &$queue, $mode ) {
129 $stats = \MediaWiki\MediaWikiServices::getInstance()->getStatsdDataFactory();
130 $method = RequestContext::getMain()->getRequest()->getMethod();
132 $updates = $queue; // snapshot of queue
133 // Keep doing rounds of updates until none get enqueued
134 while ( count( $updates ) ) {
135 $queue = []; // clear the queue
136 /** @var DataUpdate[] $dataUpdates */
137 $dataUpdates = [];
138 /** @var DeferrableUpdate[] $otherUpdates */
139 $otherUpdates = [];
140 foreach ( $updates as $update ) {
141 if ( $update instanceof DataUpdate ) {
142 $dataUpdates[] = $update;
143 } else {
144 $otherUpdates[] = $update;
146 $stats->increment( 'deferred_updates.' . $method . '.' . get_class( $update ) );
149 // Delegate DataUpdate execution to the DataUpdate class
150 try {
151 DataUpdate::runUpdates( $dataUpdates, $mode );
152 } catch ( Exception $e ) {
153 // Let the other updates occur if these had to rollback
154 MWExceptionHandler::logException( $e );
156 // Execute the non-DataUpdate tasks
157 foreach ( $otherUpdates as $update ) {
158 try {
159 $update->doUpdate();
160 wfGetLBFactory()->commitMasterChanges( __METHOD__ );
161 } catch ( Exception $e ) {
162 // We don't want exceptions thrown during deferred updates to
163 // be reported to the user since the output is already sent
164 if ( !$e instanceof ErrorPageError ) {
165 MWExceptionHandler::logException( $e );
167 // Make sure incomplete transactions are not committed and end any
168 // open atomic sections so that other DB updates have a chance to run
169 wfGetLBFactory()->rollbackMasterChanges( __METHOD__ );
173 $updates = $queue; // new snapshot of queue (check for new entries)
178 * Clear all pending updates without performing them. Generally, you don't
179 * want or need to call this. Unit tests need it though.
181 public static function clearPendingUpdates() {
182 self::$preSendUpdates = [];
183 self::$postSendUpdates = [];