2013-09-03 20:43:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
2023-11-21 21:08:14 +00:00
|
|
|
namespace MediaWiki\Deferred;
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param callable $callback
|
2016-07-22 02:32:09 +00:00
|
|
|
* @param string $fname Calling method
|
2023-08-24 23:21:19 +00:00
|
|
|
* @param IDatabase|IDatabase[]|null $dbws Cancel the update if a DB transaction
|
|
|
|
|
* is rolled back [optional] (since 1.28)
|
2013-09-03 20:43:40 +00:00
|
|
|
*/
|
2024-02-09 17:46:37 +00:00
|
|
|
public function __construct( callable $callback, $fname = ISQLPlatform::CALLER_UNKNOWN, $dbws = [] ) {
|
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
|
|
|
|
2018-01-23 15:06:32 +00:00
|
|
|
$dbws = is_array( $dbws ) ? $dbws : [ $dbws ];
|
|
|
|
|
foreach ( $dbws as $dbw ) {
|
|
|
|
|
if ( $dbw && $dbw->trxLevel() ) {
|
|
|
|
|
$dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
|
|
|
|
|
}
|
2016-08-13 19:28:53 +00:00
|
|
|
}
|
2013-09-03 20:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function doUpdate() {
|
2016-08-13 19:28:53 +00:00
|
|
|
if ( $this->callback ) {
|
|
|
|
|
call_user_func( $this->callback );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 1.34
|
|
|
|
|
* @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' );
|