Update git submodules
[mediawiki.git] / includes / deferred / AutoCommitUpdate.php
blobc1da75cff7fb1d4ec5b297f30810a06f2ca00ad4
1 <?php
3 use Wikimedia\Rdbms\IDatabase;
5 /**
6 * Deferrable Update for closure/callback updates that should use auto-commit mode
7 * @since 1.28
8 */
9 class AutoCommitUpdate implements DeferrableUpdate, DeferrableCallback {
10 /** @var IDatabase */
11 private $dbw;
12 /** @var string */
13 private $fname;
14 /** @var callable|null */
15 private $callback;
17 /**
18 * @param IDatabase $dbw DB handle; update aborts if a transaction now this rolls back
19 * @param string $fname Caller name (usually __METHOD__)
20 * @param callable $callback Callback that takes (IDatabase, method name string)
21 * @param IDatabase[] $conns Cancel the update if a transaction on these
22 * connections is rolled back [optional]
24 public function __construct( IDatabase $dbw, $fname, callable $callback, array $conns = [] ) {
25 $this->dbw = $dbw;
26 $this->fname = $fname;
27 $this->callback = $callback;
28 // Register DB connections for which uncommitted changes are related to this update
29 $conns[] = $dbw;
30 foreach ( $conns as $conn ) {
31 if ( $conn->trxLevel() ) {
32 $conn->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
37 public function doUpdate() {
38 if ( !$this->callback ) {
39 return;
42 $autoTrx = $this->dbw->getFlag( DBO_TRX );
43 $this->dbw->clearFlag( DBO_TRX );
44 try {
45 ( $this->callback )( $this->dbw, $this->fname );
46 } finally {
47 if ( $autoTrx ) {
48 $this->dbw->setFlag( DBO_TRX );
53 /**
54 * @internal This method is public so that it works with onTransactionResolution()
55 * @param int $trigger
57 public function cancelOnRollback( $trigger ) {
58 if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
59 $this->callback = null;
63 public function getOrigin() {
64 return $this->fname;