summaryrefslogtreecommitdiff
path: root/www/wiki/includes/deferred/MWCallableUpdate.php
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/includes/deferred/MWCallableUpdate.php')
-rw-r--r--www/wiki/includes/deferred/MWCallableUpdate.php47
1 files changed, 47 insertions, 0 deletions
diff --git a/www/wiki/includes/deferred/MWCallableUpdate.php b/www/wiki/includes/deferred/MWCallableUpdate.php
new file mode 100644
index 00000000..9803b7a4
--- /dev/null
+++ b/www/wiki/includes/deferred/MWCallableUpdate.php
@@ -0,0 +1,47 @@
+<?php
+
+use Wikimedia\Rdbms\IDatabase;
+
+/**
+ * Deferrable Update for closure/callback
+ */
+class MWCallableUpdate implements DeferrableUpdate, DeferrableCallback {
+ /** @var callable|null */
+ private $callback;
+ /** @var string */
+ private $fname;
+
+ /**
+ * @param callable $callback
+ * @param string $fname Calling method
+ * @param IDatabase|IDatabase[]|null $dbws Abort if any of the specified DB handles have
+ * a currently pending transaction which later gets rolled back [optional] (since 1.28)
+ */
+ public function __construct( callable $callback, $fname = 'unknown', $dbws = [] ) {
+ $this->callback = $callback;
+ $this->fname = $fname;
+
+ $dbws = is_array( $dbws ) ? $dbws : [ $dbws ];
+ foreach ( $dbws as $dbw ) {
+ if ( $dbw && $dbw->trxLevel() ) {
+ $dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
+ }
+ }
+ }
+
+ public function doUpdate() {
+ if ( $this->callback ) {
+ call_user_func( $this->callback );
+ }
+ }
+
+ public function cancelOnRollback( $trigger ) {
+ if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
+ $this->callback = null;
+ }
+ }
+
+ public function getOrigin() {
+ return $this->fname;
+ }
+}