3 use Wikimedia\Rdbms\IDatabase
;
6 * Deferrable Update for closure/callback
9 implements DeferrableUpdate
, DeferrableCallback
, TransactionRoundAwareUpdate
11 /** @var callable|null Callback, or null if it was cancelled */
13 /** @var string Calling method name */
15 /** @var int One of the class TRX_ROUND_* constants */
16 private $trxRoundRequirement = self
::TRX_ROUND_PRESENT
;
19 * @param callable $callback
20 * @param string $fname Calling method
21 * @param IDatabase|IDatabase[]|null $dbws Abort if any of the specified DB handles have
22 * a currently pending transaction which later gets rolled back [optional] (since 1.28)
24 public function __construct( callable
$callback, $fname = 'unknown', $dbws = [] ) {
25 $this->callback
= $callback;
26 $this->fname
= $fname;
28 $dbws = is_array( $dbws ) ?
$dbws : [ $dbws ];
29 foreach ( $dbws as $dbw ) {
30 if ( $dbw && $dbw->trxLevel() ) {
31 $dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
36 public function doUpdate() {
37 if ( $this->callback
) {
38 call_user_func( $this->callback
);
43 * @private This method is public so that it works with onTransactionResolution()
46 public function cancelOnRollback( $trigger ) {
47 if ( $trigger === IDatabase
::TRIGGER_ROLLBACK
) {
48 $this->callback
= null;
52 public function getOrigin() {
58 * @param int $mode One of the class TRX_ROUND_* constants
60 public function setTransactionRoundRequirement( $mode ) {
61 $this->trxRoundRequirement
= $mode;
64 public function getTransactionRoundRequirement() {
65 return $this->trxRoundRequirement
;