Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.50% |
14 / 16 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| MWCallableUpdate | |
93.33% |
14 / 15 |
|
83.33% |
5 / 6 |
12.04 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| doUpdate | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| cancelOnRollback | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getOrigin | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setTransactionRoundRequirement | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTransactionRoundRequirement | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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 | * DeferrableUpdate for closure/callable |
| 11 | * |
| 12 | * @internal Use DeferredUpdates::addCallableUpdate instead |
| 13 | */ |
| 14 | class MWCallableUpdate |
| 15 | implements DeferrableUpdate, DeferrableCallback, TransactionRoundAwareUpdate |
| 16 | { |
| 17 | /** @var callable|null Callback, or null if it was cancelled */ |
| 18 | private $callback; |
| 19 | /** @var string Calling method name */ |
| 20 | private $fname; |
| 21 | /** @var int One of the class TRX_ROUND_* constants */ |
| 22 | private $trxRoundRequirement = self::TRX_ROUND_PRESENT; |
| 23 | |
| 24 | /** |
| 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). |
| 32 | * [optional] |
| 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 | // For backwards-compatibility with [$classOrObject, 'func'] style callbacks |
| 55 | // where the function happened to already take an optional parameter. |
| 56 | ( $this->callback )(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @internal This method is public so that it works with onTransactionResolution() |
| 62 | * @param int $trigger |
| 63 | */ |
| 64 | public function cancelOnRollback( $trigger ) { |
| 65 | if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) { |
| 66 | $this->callback = null; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** @inheritDoc */ |
| 71 | public function getOrigin() { |
| 72 | return $this->fname; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @param int $mode One of the class TRX_ROUND_* constants |
| 77 | */ |
| 78 | public function setTransactionRoundRequirement( $mode ) { |
| 79 | $this->trxRoundRequirement = $mode; |
| 80 | } |
| 81 | |
| 82 | /** @inheritDoc */ |
| 83 | public function getTransactionRoundRequirement() { |
| 84 | return $this->trxRoundRequirement; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** @deprecated class alias since 1.42 */ |
| 89 | class_alias( MWCallableUpdate::class, 'MWCallableUpdate' ); |