Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / includes / deferred / MWCallableUpdate.php
1 <?php
2
3 use Wikimedia\Rdbms\IDatabase;
4
5 /**
6 * Deferrable Update for closure/callback
7 */
8 class MWCallableUpdate
9 implements DeferrableUpdate, DeferrableCallback, TransactionRoundAwareUpdate
10 {
11 /** @var callable|null Callback, or null if it was cancelled */
12 private $callback;
13 /** @var string Calling method name */
14 private $fname;
15 /** @var int One of the class TRX_ROUND_* constants */
16 private $trxRoundRequirement = self::TRX_ROUND_PRESENT;
17
18 /**
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)
23 */
24 public function __construct( callable $callback, $fname = 'unknown', $dbws = [] ) {
25 $this->callback = $callback;
26 $this->fname = $fname;
27
28 $dbws = is_array( $dbws ) ? $dbws : [ $dbws ];
29 foreach ( $dbws as $dbw ) {
30 if ( $dbw && $dbw->trxLevel() ) {
31 $dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
32 }
33 }
34 }
35
36 public function doUpdate() {
37 if ( $this->callback ) {
38 call_user_func( $this->callback );
39 }
40 }
41
42 /**
43 * @private This method is public so that it works with onTransactionResolution()
44 * @param int $trigger
45 */
46 public function cancelOnRollback( $trigger ) {
47 if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
48 $this->callback = null;
49 }
50 }
51
52 public function getOrigin() {
53 return $this->fname;
54 }
55
56 /**
57 * @since 1.34
58 * @param int $mode One of the class TRX_ROUND_* constants
59 */
60 public function setTransactionRoundRequirement( $mode ) {
61 $this->trxRoundRequirement = $mode;
62 }
63
64 public function getTransactionRoundRequirement() {
65 return $this->trxRoundRequirement;
66 }
67 }