2016-07-19 20:43:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
2017-02-10 18:09:05 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
|
|
2016-07-19 20:43:17 +00:00
|
|
|
/**
|
|
|
|
|
* Deferrable Update for closure/callback updates that should use auto-commit mode
|
|
|
|
|
* @since 1.28
|
|
|
|
|
*/
|
2016-07-22 02:32:09 +00:00
|
|
|
class AutoCommitUpdate implements DeferrableUpdate, DeferrableCallback {
|
2016-07-19 20:43:17 +00:00
|
|
|
/** @var IDatabase */
|
|
|
|
|
private $dbw;
|
|
|
|
|
/** @var string */
|
|
|
|
|
private $fname;
|
2016-08-13 19:28:53 +00:00
|
|
|
/** @var callable|null */
|
2016-07-19 20:43:17 +00:00
|
|
|
private $callback;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-03-21 21:05:35 +00:00
|
|
|
* @param IDatabase $dbw DB handle; update aborts if a transaction now this rolls back
|
2016-07-19 20:43:17 +00:00
|
|
|
* @param string $fname Caller name (usually __METHOD__)
|
|
|
|
|
* @param callable $callback Callback that takes (IDatabase, method name string)
|
2019-03-21 21:05:35 +00:00
|
|
|
* @param IDatabase[] $conns Abort if a transaction now on one of these rolls back [optional]
|
2016-07-19 20:43:17 +00:00
|
|
|
*/
|
2019-03-21 21:05:35 +00:00
|
|
|
public function __construct( IDatabase $dbw, $fname, callable $callback, array $conns = [] ) {
|
2016-07-19 20:43:17 +00:00
|
|
|
$this->dbw = $dbw;
|
|
|
|
|
$this->fname = $fname;
|
|
|
|
|
$this->callback = $callback;
|
2019-03-21 21:05:35 +00:00
|
|
|
// Register DB connections for which uncommitted changes are related to this update
|
|
|
|
|
$conns[] = $dbw;
|
|
|
|
|
foreach ( $conns as $conn ) {
|
|
|
|
|
if ( $conn->trxLevel() ) {
|
|
|
|
|
$conn->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
|
|
|
|
|
}
|
2016-07-19 20:43:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function doUpdate() {
|
|
|
|
|
if ( !$this->callback ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$autoTrx = $this->dbw->getFlag( DBO_TRX );
|
|
|
|
|
$this->dbw->clearFlag( DBO_TRX );
|
|
|
|
|
try {
|
|
|
|
|
/** @var Exception $e */
|
|
|
|
|
$e = null;
|
2018-06-05 06:24:34 +00:00
|
|
|
( $this->callback )( $this->dbw, $this->fname );
|
2016-07-19 20:43:17 +00:00
|
|
|
} catch ( Exception $e ) {
|
|
|
|
|
}
|
|
|
|
|
if ( $autoTrx ) {
|
|
|
|
|
$this->dbw->setFlag( DBO_TRX );
|
|
|
|
|
}
|
|
|
|
|
if ( $e ) {
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-21 21:05:35 +00:00
|
|
|
/**
|
|
|
|
|
* @private This method is public so that it works with onTransactionResolution()
|
|
|
|
|
* @param int $trigger
|
|
|
|
|
*/
|
2016-07-19 20:43:17 +00:00
|
|
|
public function cancelOnRollback( $trigger ) {
|
|
|
|
|
if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
|
|
|
|
|
$this->callback = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-22 02:32:09 +00:00
|
|
|
|
|
|
|
|
public function getOrigin() {
|
|
|
|
|
return $this->fname;
|
|
|
|
|
}
|
2016-07-19 20:43:17 +00:00
|
|
|
}
|