MediaWiki  1.34.4
AutoCommitUpdate.php
Go to the documentation of this file.
1 <?php
2 
4 
11  private $dbw;
13  private $fname;
15  private $callback;
16 
23  public function __construct( IDatabase $dbw, $fname, callable $callback, array $conns = [] ) {
24  $this->dbw = $dbw;
25  $this->fname = $fname;
26  $this->callback = $callback;
27  // Register DB connections for which uncommitted changes are related to this update
28  $conns[] = $dbw;
29  foreach ( $conns as $conn ) {
30  if ( $conn->trxLevel() ) {
31  $conn->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
32  }
33  }
34  }
35 
36  public function doUpdate() {
37  if ( !$this->callback ) {
38  return;
39  }
40 
41  $autoTrx = $this->dbw->getFlag( DBO_TRX );
42  $this->dbw->clearFlag( DBO_TRX );
43  try {
45  $e = null;
46  ( $this->callback )( $this->dbw, $this->fname );
47  } catch ( Exception $e ) {
48  }
49  if ( $autoTrx ) {
50  $this->dbw->setFlag( DBO_TRX );
51  }
52  if ( $e ) {
53  throw $e;
54  }
55  }
56 
61  public function cancelOnRollback( $trigger ) {
62  if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
63  $this->callback = null;
64  }
65  }
66 
67  public function getOrigin() {
68  return $this->fname;
69  }
70 }
AutoCommitUpdate\cancelOnRollback
cancelOnRollback( $trigger)
Definition: AutoCommitUpdate.php:61
AutoCommitUpdate
Deferrable Update for closure/callback updates that should use auto-commit mode.
Definition: AutoCommitUpdate.php:9
AutoCommitUpdate\__construct
__construct(IDatabase $dbw, $fname, callable $callback, array $conns=[])
Definition: AutoCommitUpdate.php:23
DeferrableCallback
Callback wrapper that has an originating method.
Definition: DeferrableCallback.php:8
DBO_TRX
const DBO_TRX
Definition: defines.php:12
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
AutoCommitUpdate\$fname
string $fname
Definition: AutoCommitUpdate.php:13
AutoCommitUpdate\$dbw
IDatabase $dbw
Definition: AutoCommitUpdate.php:11
AutoCommitUpdate\getOrigin
getOrigin()
Definition: AutoCommitUpdate.php:67
DeferrableUpdate
Interface that deferrable updates should implement.
Definition: DeferrableUpdate.php:9
Wikimedia\Rdbms\IDatabase\onTransactionResolution
onTransactionResolution(callable $callback, $fname=__METHOD__)
Run a callback as soon as the current transaction commits or rolls back.
AutoCommitUpdate\$callback
callable null $callback
Definition: AutoCommitUpdate.php:15
AutoCommitUpdate\doUpdate
doUpdate()
Perform the actual work.
Definition: AutoCommitUpdate.php:36