]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - includes/deferred/AtomicSectionUpdate.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / includes / deferred / AtomicSectionUpdate.php
diff --git a/includes/deferred/AtomicSectionUpdate.php b/includes/deferred/AtomicSectionUpdate.php
new file mode 100644 (file)
index 0000000..8b62989
--- /dev/null
@@ -0,0 +1,48 @@
+<?php
+
+use Wikimedia\Rdbms\IDatabase;
+
+/**
+ * Deferrable Update for closure/callback updates via IDatabase::doAtomicSection()
+ * @since 1.27
+ */
+class AtomicSectionUpdate implements DeferrableUpdate, DeferrableCallback {
+       /** @var IDatabase */
+       private $dbw;
+       /** @var string */
+       private $fname;
+       /** @var callable|null */
+       private $callback;
+
+       /**
+        * @param IDatabase $dbw
+        * @param string $fname Caller name (usually __METHOD__)
+        * @param callable $callback
+        * @see IDatabase::doAtomicSection()
+        */
+       public function __construct( IDatabase $dbw, $fname, callable $callback ) {
+               $this->dbw = $dbw;
+               $this->fname = $fname;
+               $this->callback = $callback;
+
+               if ( $this->dbw->trxLevel() ) {
+                       $this->dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
+               }
+       }
+
+       public function doUpdate() {
+               if ( $this->callback ) {
+                       $this->dbw->doAtomicSection( $this->fname, $this->callback );
+               }
+       }
+
+       public function cancelOnRollback( $trigger ) {
+               if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
+                       $this->callback = null;
+               }
+       }
+
+       public function getOrigin() {
+               return $this->fname;
+       }
+}