Merge "Added a separate error message for mkdir failures"
[lhc/web/wiklou.git] / includes / deferred / AtomicSectionUpdate.php
1 <?php
2
3 use Wikimedia\Rdbms\IDatabase;
4
5 /**
6 * Deferrable Update for closure/callback updates via IDatabase::doAtomicSection()
7 * @since 1.27
8 */
9 class AtomicSectionUpdate 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
21 * @see IDatabase::doAtomicSection()
22 */
23 public function __construct( IDatabase $dbw, $fname, callable $callback ) {
24 $this->dbw = $dbw;
25 $this->fname = $fname;
26 $this->callback = $callback;
27
28 if ( $this->dbw->trxLevel() ) {
29 $this->dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
30 }
31 }
32
33 public function doUpdate() {
34 if ( $this->callback ) {
35 $this->dbw->doAtomicSection( $this->fname, $this->callback );
36 }
37 }
38
39 public function cancelOnRollback( $trigger ) {
40 if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
41 $this->callback = null;
42 }
43 }
44
45 public function getOrigin() {
46 return $this->fname;
47 }
48 }