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 
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 );
33  }
34  }
35  }
36 
37  public function doUpdate() {
38  if ( !$this->callback ) {
39  return;
40  }
41 
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 );
49  }
50  }
51  }
52 
57  public function cancelOnRollback( $trigger ) {
58  if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
59  $this->callback = null;
60  }
61  }
62 
63  public function getOrigin() {
64  return $this->fname;
65  }
66 }
Deferrable Update for closure/callback updates that should use auto-commit mode.
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:36
onTransactionResolution(callable $callback, $fname=__METHOD__)
Run a callback when the current transaction commits or rolls back.
const DBO_TRX
Definition: defines.php:12