2011-09-10 06:50:30 +00:00
|
|
|
<?php
|
2012-05-20 15:56:43 +00:00
|
|
|
/**
|
|
|
|
|
* Interface and manager for deferred updates.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
2011-09-10 06:50:30 +00:00
|
|
|
/**
|
|
|
|
|
* Interface that deferrable updates should implement. Basically required so we
|
|
|
|
|
* can validate input on DeferredUpdates::addUpdate()
|
2011-09-10 17:16:41 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.19
|
2011-09-10 06:50:30 +00:00
|
|
|
*/
|
|
|
|
|
interface DeferrableUpdate {
|
|
|
|
|
/**
|
|
|
|
|
* Perform the actual work
|
|
|
|
|
*/
|
|
|
|
|
function doUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-05-18 21:20:35 +00:00
|
|
|
* Class for managing the deferred updates
|
|
|
|
|
*
|
|
|
|
|
* Deferred updates can be run at the end of the request,
|
|
|
|
|
* after the HTTP response has been sent. In CLI mode, updates
|
|
|
|
|
* are only deferred until there is no local master DB transaction.
|
2015-05-27 19:40:02 +00:00
|
|
|
* When updates are deferred, they go into a simple FIFO queue.
|
2011-09-10 17:16:41 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.19
|
2011-09-10 06:50:30 +00:00
|
|
|
*/
|
|
|
|
|
class DeferredUpdates {
|
2015-09-20 04:09:26 +00:00
|
|
|
/** @var array Updates to be deferred until the end of the request */
|
2011-09-10 06:50:30 +00:00
|
|
|
private static $updates = array();
|
2015-09-20 04:09:26 +00:00
|
|
|
/** @var bool Defer updates fully even in CLI mode */
|
|
|
|
|
private static $forceDeferral = false;
|
2011-09-10 06:50:30 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add an update to the deferred list
|
2013-11-20 18:43:39 +00:00
|
|
|
* @param DeferrableUpdate $update Some object that implements doUpdate()
|
2011-09-10 06:50:30 +00:00
|
|
|
*/
|
|
|
|
|
public static function addUpdate( DeferrableUpdate $update ) {
|
2015-05-18 21:20:35 +00:00
|
|
|
global $wgCommandLineMode;
|
|
|
|
|
|
2011-09-10 06:50:30 +00:00
|
|
|
array_push( self::$updates, $update );
|
2015-09-20 04:09:26 +00:00
|
|
|
if ( self::$forceDeferral ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-05-18 21:20:35 +00:00
|
|
|
|
|
|
|
|
// CLI scripts may forget to periodically flush these updates,
|
|
|
|
|
// so try to handle that rather than OOMing and losing them.
|
|
|
|
|
// Try to run the updates as soon as there is no local transaction.
|
|
|
|
|
static $waitingOnTrx = false; // de-duplicate callback
|
|
|
|
|
if ( $wgCommandLineMode && !$waitingOnTrx ) {
|
|
|
|
|
$lb = wfGetLB();
|
|
|
|
|
$dbw = $lb->getAnyOpenConnection( $lb->getWriterIndex() );
|
|
|
|
|
// Do the update as soon as there is no transaction
|
2015-05-21 06:06:05 +00:00
|
|
|
if ( $dbw && $dbw->trxLevel() ) {
|
2015-05-18 21:20:35 +00:00
|
|
|
$waitingOnTrx = true;
|
|
|
|
|
$dbw->onTransactionIdle( function() use ( &$waitingOnTrx ) {
|
|
|
|
|
DeferredUpdates::doUpdates();
|
|
|
|
|
$waitingOnTrx = false;
|
|
|
|
|
} );
|
|
|
|
|
} else {
|
|
|
|
|
self::doUpdates();
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-09-10 06:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
2013-09-03 20:43:40 +00:00
|
|
|
/**
|
|
|
|
|
* Add a callable update. In a lot of cases, we just need a callback/closure,
|
|
|
|
|
* defining a new DeferrableUpdate object is not necessary
|
|
|
|
|
* @see MWCallableUpdate::__construct()
|
|
|
|
|
* @param callable $callable
|
|
|
|
|
*/
|
|
|
|
|
public static function addCallableUpdate( $callable ) {
|
|
|
|
|
self::addUpdate( new MWCallableUpdate( $callable ) );
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-10 06:50:30 +00:00
|
|
|
/**
|
|
|
|
|
* Do any deferred updates and clear the list
|
|
|
|
|
*
|
2014-07-24 17:42:45 +00:00
|
|
|
* @param string $commit Set to 'commit' to commit after every update to
|
2013-11-20 18:43:39 +00:00
|
|
|
* prevent lock contention
|
2011-09-10 06:50:30 +00:00
|
|
|
*/
|
|
|
|
|
public static function doUpdates( $commit = '' ) {
|
2015-09-21 23:25:47 +00:00
|
|
|
$updates = self::$updates;
|
2011-09-10 06:50:30 +00:00
|
|
|
|
2015-05-18 21:20:35 +00:00
|
|
|
while ( count( $updates ) ) {
|
2014-08-19 22:23:05 +00:00
|
|
|
self::clearPendingUpdates();
|
2011-09-10 06:50:30 +00:00
|
|
|
|
2014-08-19 22:23:05 +00:00
|
|
|
/** @var DeferrableUpdate $update */
|
|
|
|
|
foreach ( $updates as $update ) {
|
|
|
|
|
try {
|
|
|
|
|
$update->doUpdate();
|
|
|
|
|
|
2015-05-18 21:20:35 +00:00
|
|
|
if ( $commit === 'commit' ) {
|
|
|
|
|
wfGetLBFactory()->commitMasterChanges();
|
2014-08-19 22:23:05 +00:00
|
|
|
}
|
2015-01-09 23:44:47 +00:00
|
|
|
} catch ( Exception $e ) {
|
2014-08-19 22:23:05 +00:00
|
|
|
// We don't want exceptions thrown during deferred updates to
|
|
|
|
|
// be reported to the user since the output is already sent.
|
|
|
|
|
// Instead we just log them.
|
|
|
|
|
if ( !$e instanceof ErrorPageError ) {
|
|
|
|
|
MWExceptionHandler::logException( $e );
|
|
|
|
|
}
|
2012-07-12 21:07:56 +00:00
|
|
|
}
|
2011-09-10 06:50:30 +00:00
|
|
|
}
|
2015-05-18 21:20:35 +00:00
|
|
|
|
2015-09-21 23:25:47 +00:00
|
|
|
$updates = self::$updates;
|
2011-09-10 06:50:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear all pending updates without performing them. Generally, you don't
|
|
|
|
|
* want or need to call this. Unit tests need it though.
|
|
|
|
|
*/
|
|
|
|
|
public static function clearPendingUpdates() {
|
2015-09-21 23:25:47 +00:00
|
|
|
self::$updates = array();
|
2011-09-10 06:50:30 +00:00
|
|
|
}
|
2015-09-20 04:09:26 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @note This method is intended for testing purposes
|
|
|
|
|
* @param bool $value Whether to *always* defer updates, even in CLI mode
|
|
|
|
|
* @since 1.26
|
|
|
|
|
*/
|
|
|
|
|
public static function forceDeferral( $value ) {
|
|
|
|
|
self::$forceDeferral = $value;
|
|
|
|
|
}
|
2011-09-10 06:50:30 +00:00
|
|
|
}
|