| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Deferred; |
| 4 |
|
| 5 |
use Wikimedia\Rdbms\IDatabase; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class AtomicSectionUpdate implements DeferrableUpdate, DeferrableCallback { |
| 12 |
|
| 13 |
private $dbw; |
| 14 |
|
| 15 |
private $fname; |
| 16 |
|
| 17 |
private $callback; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public function __construct( IDatabase $dbw, $fname, callable $callback, array $conns = [] ) { |
| 27 |
$this->dbw = $dbw; |
| 28 |
$this->fname = $fname; |
| 29 |
$this->callback = $callback; |
| 30 |
|
| 31 |
$conns[] = $dbw; |
| 32 |
foreach ( $conns as $conn ) { |
| 33 |
if ( $conn->trxLevel() ) { |
| 34 |
$conn->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname ); |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
public function doUpdate() { |
| 40 |
if ( $this->callback ) { |
| 41 |
$this->dbw->doAtomicSection( $this->fname, $this->callback ); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function cancelOnRollback( $trigger ) { |
| 50 |
if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) { |
| 51 |
$this->callback = null; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
public function getOrigin() { |
| 56 |
return $this->fname; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
class_alias( AtomicSectionUpdate::class, 'AtomicSectionUpdate' ); |