2013-09-03 20:43:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
2023-11-21 21:08:14 +00:00
|
|
|
namespace MediaWiki\Deferred;
|
|
|
|
|
|
2024-08-07 17:24:36 +00:00
|
|
|
use Closure;
|
2017-02-10 18:09:05 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2024-02-09 17:46:37 +00:00
|
|
|
use Wikimedia\Rdbms\Platform\ISQLPlatform;
|
2017-02-10 18:09:05 +00:00
|
|
|
|
2013-09-03 20:43:40 +00:00
|
|
|
/**
|
2023-08-24 23:21:19 +00:00
|
|
|
* DeferrableUpdate for closure/callable
|
|
|
|
|
*
|
|
|
|
|
* @internal Use DeferredUpdates::addCallableUpdate instead
|
2013-09-03 20:43:40 +00:00
|
|
|
*/
|
2019-05-29 21:07:03 +00:00
|
|
|
class MWCallableUpdate
|
|
|
|
|
implements DeferrableUpdate, DeferrableCallback, TransactionRoundAwareUpdate
|
|
|
|
|
{
|
|
|
|
|
/** @var callable|null Callback, or null if it was cancelled */
|
2013-09-03 20:43:40 +00:00
|
|
|
private $callback;
|
2019-05-29 21:07:03 +00:00
|
|
|
/** @var string Calling method name */
|
2016-07-22 02:32:09 +00:00
|
|
|
private $fname;
|
2019-05-29 21:07:03 +00:00
|
|
|
/** @var int One of the class TRX_ROUND_* constants */
|
|
|
|
|
private $trxRoundRequirement = self::TRX_ROUND_PRESENT;
|
2013-09-03 20:43:40 +00:00
|
|
|
|
|
|
|
|
/**
|
2024-08-07 17:24:36 +00: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
|
2024-10-11 18:46:13 +00:00
|
|
|
* @param string $fname Calling method @phan-mandatory-param
|
2024-08-07 19:23:32 +00:00
|
|
|
* @param IDatabase|IDatabase[] $dependeeDbws DB handles which might have pending writes
|
2024-08-07 17:24:36 +00: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 20:43:40 +00:00
|
|
|
*/
|
2024-08-07 17:24:36 +00:00
|
|
|
public function __construct(
|
|
|
|
|
callable $callback,
|
|
|
|
|
$fname = ISQLPlatform::CALLER_UNKNOWN,
|
|
|
|
|
$dependeeDbws = []
|
|
|
|
|
) {
|
2013-09-03 20:43:40 +00:00
|
|
|
$this->callback = $callback;
|
2016-07-22 02:32:09 +00:00
|
|
|
$this->fname = $fname;
|
2016-08-13 19:28:53 +00:00
|
|
|
|
2024-08-07 17:24:36 +00:00
|
|
|
$dependeeDbws = is_array( $dependeeDbws ) ? $dependeeDbws : [ $dependeeDbws ];
|
|
|
|
|
foreach ( $dependeeDbws as $dbw ) {
|
2024-08-07 19:23:32 +00:00
|
|
|
if ( $dbw->trxLevel() ) {
|
2018-01-23 15:06:32 +00:00
|
|
|
$dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
|
|
|
|
|
}
|
2016-08-13 19:28:53 +00:00
|
|
|
}
|
2013-09-03 20:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function doUpdate() {
|
2024-08-07 17:24:36 +00: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 19:28:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-21 21:05:35 +00:00
|
|
|
/**
|
2020-06-26 12:14:23 +00:00
|
|
|
* @internal This method is public so that it works with onTransactionResolution()
|
2019-03-21 21:05:35 +00:00
|
|
|
* @param int $trigger
|
|
|
|
|
*/
|
2016-08-13 19:28:53 +00:00
|
|
|
public function cancelOnRollback( $trigger ) {
|
|
|
|
|
if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
|
|
|
|
|
$this->callback = null;
|
|
|
|
|
}
|
2013-09-03 20:43:40 +00:00
|
|
|
}
|
2016-07-22 02:32:09 +00:00
|
|
|
|
|
|
|
|
public function getOrigin() {
|
|
|
|
|
return $this->fname;
|
|
|
|
|
}
|
2019-05-29 21:07:03 +00: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 20:43:40 +00:00
|
|
|
}
|
2023-11-21 21:08:14 +00:00
|
|
|
|
2024-03-07 21:56:58 +00:00
|
|
|
/** @deprecated class alias since 1.42 */
|
2023-11-21 21:08:14 +00:00
|
|
|
class_alias( MWCallableUpdate::class, 'MWCallableUpdate' );
|