Merge "Perform a permission check on the title when changing the page language"
[lhc/web/wiklou.git] / includes / deferred / AutoCommitUpdate.php
1 <?php
2
3 use Wikimedia\Rdbms\IDatabase;
4
5 /**
6 * Deferrable Update for closure/callback updates that should use auto-commit mode
7 * @since 1.28
8 */
9 class AutoCommitUpdate implements DeferrableUpdate, DeferrableCallback {
10 /** @var IDatabase */
11 private $dbw;
12 /** @var string */
13 private $fname;
14 /** @var callable|null */
15 private $callback;
16
17 /**
18 * @param IDatabase $dbw
19 * @param string $fname Caller name (usually __METHOD__)
20 * @param callable $callback Callback that takes (IDatabase, method name string)
21 */
22 public function __construct( IDatabase $dbw, $fname, callable $callback ) {
23 $this->dbw = $dbw;
24 $this->fname = $fname;
25 $this->callback = $callback;
26
27 if ( $this->dbw->trxLevel() ) {
28 $this->dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
29 }
30 }
31
32 public function doUpdate() {
33 if ( !$this->callback ) {
34 return;
35 }
36
37 $autoTrx = $this->dbw->getFlag( DBO_TRX );
38 $this->dbw->clearFlag( DBO_TRX );
39 try {
40 /** @var Exception $e */
41 $e = null;
42 call_user_func_array( $this->callback, [ $this->dbw, $this->fname ] );
43 } catch ( Exception $e ) {
44 }
45 if ( $autoTrx ) {
46 $this->dbw->setFlag( DBO_TRX );
47 }
48 if ( $e ) {
49 throw $e;
50 }
51 }
52
53 public function cancelOnRollback( $trigger ) {
54 if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
55 $this->callback = null;
56 }
57 }
58
59 public function getOrigin() {
60 return $this->fname;
61 }
62 }