Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
AutoCommitUpdate
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 4
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 doUpdate
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 cancelOnRollback
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getOrigin
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Deferred;
4
5use Wikimedia\Rdbms\IDatabase;
6
7/**
8 * Deferrable Update for closure/callback updates that should use auto-commit mode
9 * @since 1.28
10 */
11class AutoCommitUpdate implements DeferrableUpdate, DeferrableCallback {
12    /** @var IDatabase */
13    private $dbw;
14    /** @var string */
15    private $fname;
16    /** @var callable|null */
17    private $callback;
18
19    /**
20     * @param IDatabase $dbw DB handle; update aborts if a transaction now this rolls back
21     * @param string $fname Caller name (usually __METHOD__)
22     * @param callable $callback Callback that takes (IDatabase, method name string)
23     * @param IDatabase[] $conns Cancel the update if a transaction on these
24     * connections is rolled back [optional]
25     */
26    public function __construct( IDatabase $dbw, $fname, callable $callback, array $conns = [] ) {
27        $this->dbw = $dbw;
28        $this->fname = $fname;
29        $this->callback = $callback;
30        // Register DB connections for which uncommitted changes are related to this update
31        $conns[] = $dbw;
32        foreach ( $conns as $conn ) {
33            if ( $conn->trxLevel() ) {
34                $conn->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
35            }
36        }
37    }
38
39    public function doUpdate() {
40        if ( !$this->callback ) {
41            return;
42        }
43
44        $autoTrx = $this->dbw->getFlag( DBO_TRX );
45        $this->dbw->clearFlag( DBO_TRX );
46        try {
47            ( $this->callback )( $this->dbw, $this->fname );
48        } finally {
49            if ( $autoTrx ) {
50                $this->dbw->setFlag( DBO_TRX );
51            }
52        }
53    }
54
55    /**
56     * @internal This method is public so that it works with onTransactionResolution()
57     * @param int $trigger
58     */
59    public function cancelOnRollback( $trigger ) {
60        if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
61            $this->callback = null;
62        }
63    }
64
65    public function getOrigin() {
66        return $this->fname;
67    }
68}
69
70/** @deprecated class alias since 1.42 */
71class_alias( AutoCommitUpdate::class, 'AutoCommitUpdate' );