2013-09-03 13:43:40 -07:00
|
|
|
<?php
|
|
|
|
|
|
2023-11-21 16:08:14 -05:00
|
|
|
namespace MediaWiki\Deferred;
|
|
|
|
|
|
2024-08-07 10:24:36 -07:00
|
|
|
use Closure;
|
2017-02-10 10:09:05 -08:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2024-02-09 18:46:37 +01:00
|
|
|
use Wikimedia\Rdbms\Platform\ISQLPlatform;
|
2017-02-10 10:09:05 -08:00
|
|
|
|
2013-09-03 13:43:40 -07:00
|
|
|
/**
|
2023-08-25 00:21:19 +01:00
|
|
|
* DeferrableUpdate for closure/callable
|
|
|
|
|
*
|
|
|
|
|
* @internal Use DeferredUpdates::addCallableUpdate instead
|
2013-09-03 13:43:40 -07:00
|
|
|
*/
|
2019-05-29 14:07:03 -07:00
|
|
|
class MWCallableUpdate
|
|
|
|
|
implements DeferrableUpdate, DeferrableCallback, TransactionRoundAwareUpdate
|
|
|
|
|
{
|
|
|
|
|
/** @var callable|null Callback, or null if it was cancelled */
|
2013-09-03 13:43:40 -07:00
|
|
|
private $callback;
|
2019-05-29 14:07:03 -07:00
|
|
|
/** @var string Calling method name */
|
2016-07-21 19:32:09 -07:00
|
|
|
private $fname;
|
2019-05-29 14:07:03 -07:00
|
|
|
/** @var int One of the class TRX_ROUND_* constants */
|
|
|
|
|
private $trxRoundRequirement = self::TRX_ROUND_PRESENT;
|
2013-09-03 13:43:40 -07:00
|
|
|
|
|
|
|
|
/**
|
2024-08-07 10:24:36 -07:00
|
|
|
* @param callable $callback One of the following:
|
|
|
|
|
* - A Closure callback that takes the caller name as its argument
|
|
|
|
|
* - A non-Closure callback that takes no arguments
|
2016-07-21 19:32:09 -07:00
|
|
|
* @param string $fname Calling method
|
2024-08-07 12:23:32 -07:00
|
|
|
* @param IDatabase|IDatabase[] $dependeeDbws DB handles which might have pending writes
|
2024-08-07 10:24:36 -07:00
|
|
|
* upon which this update depends. If any of the handles already has an open transaction,
|
|
|
|
|
* a rollback thereof will cause this update to be cancelled (if it has not already run).
|
|
|
|
|
* [optional]
|
2013-09-03 13:43:40 -07:00
|
|
|
*/
|
2024-08-07 10:24:36 -07:00
|
|
|
public function __construct(
|
|
|
|
|
callable $callback,
|
|
|
|
|
$fname = ISQLPlatform::CALLER_UNKNOWN,
|
|
|
|
|
$dependeeDbws = []
|
|
|
|
|
) {
|
2013-09-03 13:43:40 -07:00
|
|
|
$this->callback = $callback;
|
2016-07-21 19:32:09 -07:00
|
|
|
$this->fname = $fname;
|
2016-08-13 12:28:53 -07:00
|
|
|
|
2024-08-07 10:24:36 -07:00
|
|
|
$dependeeDbws = is_array( $dependeeDbws ) ? $dependeeDbws : [ $dependeeDbws ];
|
|
|
|
|
foreach ( $dependeeDbws as $dbw ) {
|
2024-08-07 12:23:32 -07:00
|
|
|
if ( $dbw->trxLevel() ) {
|
2018-01-23 16:06:32 +01:00
|
|
|
$dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
|
|
|
|
|
}
|
2016-08-13 12:28:53 -07:00
|
|
|
}
|
2013-09-03 13:43:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function doUpdate() {
|
2024-08-07 10:24:36 -07:00
|
|
|
if ( $this->callback instanceof Closure ) {
|
|
|
|
|
( $this->callback )( $this->fname );
|
|
|
|
|
} elseif ( $this->callback ) {
|
|
|
|
|
// For backwards-compatibility with [$classOrObject, 'func'] style callbacks
|
|
|
|
|
// where the function happened to already take an optional parameter.
|
|
|
|
|
( $this->callback )();
|
2016-08-13 12:28:53 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-21 14:05:35 -07:00
|
|
|
/**
|
2020-06-26 14:14:23 +02:00
|
|
|
* @internal This method is public so that it works with onTransactionResolution()
|
2019-03-21 14:05:35 -07:00
|
|
|
* @param int $trigger
|
|
|
|
|
*/
|
2016-08-13 12:28:53 -07:00
|
|
|
public function cancelOnRollback( $trigger ) {
|
|
|
|
|
if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
|
|
|
|
|
$this->callback = null;
|
|
|
|
|
}
|
2013-09-03 13:43:40 -07:00
|
|
|
}
|
2016-07-21 19:32:09 -07:00
|
|
|
|
|
|
|
|
public function getOrigin() {
|
|
|
|
|
return $this->fname;
|
|
|
|
|
}
|
2019-05-29 14:07:03 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $mode One of the class TRX_ROUND_* constants
|
|
|
|
|
*/
|
|
|
|
|
public function setTransactionRoundRequirement( $mode ) {
|
|
|
|
|
$this->trxRoundRequirement = $mode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getTransactionRoundRequirement() {
|
|
|
|
|
return $this->trxRoundRequirement;
|
|
|
|
|
}
|
2013-09-03 13:43:40 -07:00
|
|
|
}
|
2023-11-21 16:08:14 -05:00
|
|
|
|
2024-03-07 16:56:58 -05:00
|
|
|
/** @deprecated class alias since 1.42 */
|
2023-11-21 16:08:14 -05:00
|
|
|
class_alias( MWCallableUpdate::class, 'MWCallableUpdate' );
|