2016-01-13 16:54:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
2017-02-10 18:09:05 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
|
|
2016-01-13 16:54:48 +00:00
|
|
|
/**
|
|
|
|
|
* Deferrable Update for closure/callback updates via IDatabase::doAtomicSection()
|
|
|
|
|
* @since 1.27
|
|
|
|
|
*/
|
2016-07-22 02:32:09 +00:00
|
|
|
class AtomicSectionUpdate implements DeferrableUpdate, DeferrableCallback {
|
2016-01-13 16:54:48 +00:00
|
|
|
/** @var IDatabase */
|
|
|
|
|
private $dbw;
|
|
|
|
|
/** @var string */
|
|
|
|
|
private $fname;
|
2016-08-13 19:28:53 +00:00
|
|
|
/** @var callable|null */
|
2016-01-13 16:54:48 +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-01-13 16:54:48 +00:00
|
|
|
* @param string $fname Caller name (usually __METHOD__)
|
|
|
|
|
* @param callable $callback
|
2019-03-21 21:05:35 +00:00
|
|
|
* @param IDatabase[] $conns Abort if a transaction now on one of these rolls back [optional]
|
2016-01-13 16:54:48 +00:00
|
|
|
* @see IDatabase::doAtomicSection()
|
|
|
|
|
*/
|
2019-03-21 21:05:35 +00:00
|
|
|
public function __construct( IDatabase $dbw, $fname, callable $callback, array $conns = [] ) {
|
2016-01-13 16:54:48 +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
|
|
|
}
|
2016-01-13 16:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function doUpdate() {
|
2016-07-19 20:43:17 +00:00
|
|
|
if ( $this->callback ) {
|
|
|
|
|
$this->dbw->doAtomicSection( $this->fname, $this->callback );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-21 21:05:35 +00:00
|
|
|
/**
|
2020-06-26 12:14:23 +00:00
|
|
|
* @internal This method is public so that it works with onTransactionResolution()
|
2019-03-21 21:05:35 +00:00
|
|
|
* @param int $trigger
|
|
|
|
|
*/
|
2016-07-19 20:43:17 +00:00
|
|
|
public function cancelOnRollback( $trigger ) {
|
|
|
|
|
if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
|
|
|
|
|
$this->callback = null;
|
|
|
|
|
}
|
2016-01-13 16:54:48 +00:00
|
|
|
}
|
2016-07-22 02:32:09 +00:00
|
|
|
|
|
|
|
|
public function getOrigin() {
|
|
|
|
|
return $this->fname;
|
|
|
|
|
}
|
2016-01-13 16:54:48 +00:00
|
|
|
}
|