aboutsummaryrefslogtreecommitdiffstats
path: root/includes/deferred/MWCallableUpdate.php
blob: 91fb53db4ebaa0bbdb69e7492a36c41fe24da8e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php

use Wikimedia\Rdbms\IDatabase;

/**
 * Deferrable Update for closure/callback
 */
class MWCallableUpdate
	implements DeferrableUpdate, DeferrableCallback, TransactionRoundAwareUpdate
{
	/** @var callable|null Callback, or null if it was cancelled */
	private $callback;
	/** @var string Calling method name */
	private $fname;
	/** @var int One of the class TRX_ROUND_* constants */
	private $trxRoundRequirement = self::TRX_ROUND_PRESENT;

	/**
	 * @param callable $callback
	 * @param string $fname Calling method
	 * @param IDatabase|IDatabase[]|null $dbws Abort if any of the specified DB handles have
	 *   a currently pending transaction which later gets rolled back [optional] (since 1.28)
	 */
	public function __construct( callable $callback, $fname = 'unknown', $dbws = [] ) {
		$this->callback = $callback;
		$this->fname = $fname;

		$dbws = is_array( $dbws ) ? $dbws : [ $dbws ];
		foreach ( $dbws as $dbw ) {
			if ( $dbw && $dbw->trxLevel() ) {
				$dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
			}
		}
	}

	public function doUpdate() {
		if ( $this->callback ) {
			call_user_func( $this->callback );
		}
	}

	/**
	 * @internal This method is public so that it works with onTransactionResolution()
	 * @param int $trigger
	 */
	public function cancelOnRollback( $trigger ) {
		if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
			$this->callback = null;
		}
	}

	public function getOrigin() {
		return $this->fname;
	}

	/**
	 * @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;
	}
}