Add AtomicSectionUpdate deferred update class
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 13 Jan 2016 16:54:48 +0000 (08:54 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Thu, 14 Jan 2016 10:32:39 +0000 (10:32 +0000)
This makes it easier to have deferred atomic DB transactions

Bug: T122115
Change-Id: I67afe335f03cc21fdce78abdf3f31fa67a368419

autoload.php
includes/Block.php
includes/Title.php
includes/deferred/AtomicSectionUpdate.php [new file with mode: 0644]

index 7dec6b7..a25b562 100644 (file)
@@ -140,6 +140,7 @@ $wgAutoloadLocalClasses = array(
        'Article' => __DIR__ . '/includes/page/Article.php',
        'AssembleUploadChunksJob' => __DIR__ . '/includes/jobqueue/jobs/AssembleUploadChunksJob.php',
        'AtomFeed' => __DIR__ . '/includes/Feed.php',
+       'AtomicSectionUpdate' => __DIR__ . '/includes/deferred/AtomicSectionUpdate.php',
        'AttachLatest' => __DIR__ . '/maintenance/attachLatest.php',
        'AuthPlugin' => __DIR__ . '/includes/AuthPlugin.php',
        'AuthPluginUser' => __DIR__ . '/includes/AuthPlugin.php',
index 7b287dc..c017c6c 100644 (file)
@@ -1021,12 +1021,17 @@ class Block {
                        return;
                }
 
-               $method = __METHOD__;
-               $dbw = wfGetDB( DB_MASTER );
-               $dbw->onTransactionIdle( function () use ( $dbw, $method ) {
-                       $dbw->delete( 'ipblocks',
-                               array( 'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ), $method );
-               } );
+               DeferredUpdates::addUpdate( new AtomicSectionUpdate(
+                       wfGetDB( DB_MASTER ),
+                       __METHOD__,
+                       function ( IDatabase $dbw, $fname ) {
+                               $dbw->delete(
+                                       'ipblocks',
+                                       array( 'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ),
+                                       $fname
+                               );
+                       }
+               ) );
        }
 
        /**
index 56ba4c7..5b86ed6 100644 (file)
@@ -3020,20 +3020,22 @@ class Title {
                        return;
                }
 
-               $method = __METHOD__;
-               $dbw = wfGetDB( DB_MASTER );
-               $dbw->onTransactionIdle( function () use ( $dbw, $method ) {
-                       $dbw->delete(
-                               'page_restrictions',
-                               array( 'pr_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ),
-                               $method
-                       );
-                       $dbw->delete(
-                               'protected_titles',
-                               array( 'pt_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ),
-                               $method
-                       );
-               } );
+               DeferredUpdates::addUpdate( new AtomicSectionUpdate(
+                       wfGetDB( DB_MASTER ),
+                       __METHOD__,
+                       function ( IDatabase $dbw, $fname ) {
+                               $dbw->delete(
+                                       'page_restrictions',
+                                       array( 'pr_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ),
+                                       $fname
+                               );
+                               $dbw->delete(
+                                       'protected_titles',
+                                       array( 'pt_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ),
+                                       $fname
+                               );
+                       }
+               ) );
        }
 
        /**
diff --git a/includes/deferred/AtomicSectionUpdate.php b/includes/deferred/AtomicSectionUpdate.php
new file mode 100644 (file)
index 0000000..a9921b3
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+
+/**
+ * Deferrable Update for closure/callback updates via IDatabase::doAtomicSection()
+ * @since 1.27
+ */
+class AtomicSectionUpdate implements DeferrableUpdate {
+       /** @var IDatabase */
+       private $dbw;
+       /** @var string */
+       private $fname;
+       /** @var Closure|callable */
+       private $callback;
+
+       /**
+        * @param IDatabase $dbw
+        * @param string $fname Caller name (usually __METHOD__)
+        * @param callable $callback
+        * @throws InvalidArgumentException
+        * @see IDatabase::doAtomicSection()
+        */
+       public function __construct( IDatabase $dbw, $fname, $callback ) {
+               $this->dbw = $dbw;
+               $this->fname = $fname;
+               if ( !is_callable( $callback ) ) {
+                       throw new InvalidArgumentException( 'Not a valid callback/closure!' );
+               }
+               $this->callback = $callback;
+       }
+
+       public function doUpdate() {
+               $this->dbw->doAtomicSection( $this->fname, $this->callback );
+       }
+}