Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[lhc/web/wiklou.git] / includes / deferred / AtomicSectionUpdate.php
index 6585575..69f09e3 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use Wikimedia\Rdbms\IDatabase;
+
 /**
  * Deferrable Update for closure/callback updates via IDatabase::doAtomicSection()
  * @since 1.27
@@ -13,18 +15,22 @@ class AtomicSectionUpdate implements DeferrableUpdate, DeferrableCallback {
        private $callback;
 
        /**
-        * @param IDatabase $dbw
+        * @param IDatabase $dbw DB handle; update aborts if a transaction now this rolls back
         * @param string $fname Caller name (usually __METHOD__)
         * @param callable $callback
+        * @param IDatabase[] $conns Abort if a transaction now on one of these rolls back [optional]
         * @see IDatabase::doAtomicSection()
         */
-       public function __construct( IDatabase $dbw, $fname, callable $callback ) {
+       public function __construct( IDatabase $dbw, $fname, callable $callback, array $conns = [] ) {
                $this->dbw = $dbw;
                $this->fname = $fname;
                $this->callback = $callback;
-
-               if ( $this->dbw->trxLevel() ) {
-                       $this->dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
+               // 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 );
+                       }
                }
        }
 
@@ -34,6 +40,10 @@ class AtomicSectionUpdate implements DeferrableUpdate, DeferrableCallback {
                }
        }
 
+       /**
+        * @private This method is public so that it works with onTransactionResolution()
+        * @param int $trigger
+        */
        public function cancelOnRollback( $trigger ) {
                if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
                        $this->callback = null;