MediaWiki  master
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 {
44  ( $this->callback )( $this->dbw, $this->fname );
45  } finally {
46  if ( $autoTrx ) {
47  $this->dbw->setFlag( DBO_TRX );
48  }
49  }
50  }
51 
56  public function cancelOnRollback( $trigger ) {
57  if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
58  $this->callback = null;
59  }
60  }
61 
62  public function getOrigin() {
63  return $this->fname;
64  }
65 }
Deferrable Update for closure/callback updates that should use auto-commit mode.
callable null $callback
doUpdate()
Perform the actual work.
cancelOnRollback( $trigger)
__construct(IDatabase $dbw, $fname, callable $callback, array $conns=[])
Callback wrapper that has an originating method.
Interface that deferrable updates should implement.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:39
onTransactionResolution(callable $callback, $fname=__METHOD__)
Run a callback when the current transaction commits or rolls back.
const DBO_TRX
Definition: defines.php:12