| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Deferred; |
| 4 |
|
| 5 |
use Closure; |
| 6 |
use Wikimedia\Rdbms\IDatabase; |
| 7 |
use Wikimedia\Rdbms\Platform\ISQLPlatform; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class MWCallableUpdate |
| 15 |
implements DeferrableUpdate, DeferrableCallback, TransactionRoundAwareUpdate |
| 16 |
{ |
| 17 |
|
| 18 |
private $callback; |
| 19 |
|
| 20 |
private $fname; |
| 21 |
|
| 22 |
private $trxRoundRequirement = self::TRX_ROUND_PRESENT; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function __construct( |
| 35 |
callable $callback, |
| 36 |
$fname = ISQLPlatform::CALLER_UNKNOWN, |
| 37 |
$dependeeDbws = [] |
| 38 |
) { |
| 39 |
$this->callback = $callback; |
| 40 |
$this->fname = $fname; |
| 41 |
|
| 42 |
$dependeeDbws = is_array( $dependeeDbws ) ? $dependeeDbws : [ $dependeeDbws ]; |
| 43 |
foreach ( $dependeeDbws as $dbw ) { |
| 44 |
if ( $dbw->trxLevel() ) { |
| 45 |
$dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname ); |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
public function doUpdate() { |
| 51 |
if ( $this->callback instanceof Closure ) { |
| 52 |
( $this->callback )( $this->fname ); |
| 53 |
} elseif ( $this->callback ) { |
| 54 |
|
| 55 |
|
| 56 |
( $this->callback )(); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function cancelOnRollback( $trigger ) { |
| 65 |
if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) { |
| 66 |
$this->callback = null; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
public function getOrigin() { |
| 71 |
return $this->fname; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function setTransactionRoundRequirement( $mode ) { |
| 78 |
$this->trxRoundRequirement = $mode; |
| 79 |
} |
| 80 |
|
| 81 |
public function getTransactionRoundRequirement() { |
| 82 |
return $this->trxRoundRequirement; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
class_alias( MWCallableUpdate::class, 'MWCallableUpdate' ); |