3 namespace MediaWiki\Deferred
;
6 use Wikimedia\Rdbms\IDatabase
;
7 use Wikimedia\Rdbms\Platform\ISQLPlatform
;
10 * DeferrableUpdate for closure/callable
12 * @internal Use DeferredUpdates::addCallableUpdate instead
14 class MWCallableUpdate
15 implements DeferrableUpdate
, DeferrableCallback
, TransactionRoundAwareUpdate
17 /** @var callable|null Callback, or null if it was cancelled */
19 /** @var string Calling method name */
21 /** @var int One of the class TRX_ROUND_* constants */
22 private $trxRoundRequirement = self
::TRX_ROUND_PRESENT
;
25 * @param callable $callback One of the following:
26 * - A Closure callback that takes the caller name as its argument
27 * - A non-Closure callback that takes no arguments
28 * @param string $fname Calling method @phan-mandatory-param
29 * @param IDatabase|IDatabase[] $dependeeDbws DB handles which might have pending writes
30 * upon which this update depends. If any of the handles already has an open transaction,
31 * a rollback thereof will cause this update to be cancelled (if it has not already run).
34 public function __construct(
36 $fname = ISQLPlatform
::CALLER_UNKNOWN
,
39 $this->callback
= $callback;
40 $this->fname
= $fname;
42 $dependeeDbws = is_array( $dependeeDbws ) ?
$dependeeDbws : [ $dependeeDbws ];
43 foreach ( $dependeeDbws as $dbw ) {
44 if ( $dbw->trxLevel() ) {
45 $dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
50 public function doUpdate() {
51 if ( $this->callback
instanceof Closure
) {
52 ( $this->callback
)( $this->fname
);
53 } elseif ( $this->callback
) {
54 // For backwards-compatibility with [$classOrObject, 'func'] style callbacks
55 // where the function happened to already take an optional parameter.
56 ( $this->callback
)();
61 * @internal This method is public so that it works with onTransactionResolution()
64 public function cancelOnRollback( $trigger ) {
65 if ( $trigger === IDatabase
::TRIGGER_ROLLBACK
) {
66 $this->callback
= null;
70 public function getOrigin() {
75 * @param int $mode One of the class TRX_ROUND_* constants
77 public function setTransactionRoundRequirement( $mode ) {
78 $this->trxRoundRequirement
= $mode;
81 public function getTransactionRoundRequirement() {
82 return $this->trxRoundRequirement
;
86 /** @deprecated class alias since 1.42 */
87 class_alias( MWCallableUpdate
::class, 'MWCallableUpdate' );