Use commit-and-wait when processing more than updateRowsPerQuery
authorKosta Harlan <kharlan@wikimedia.org>
Mon, 5 Nov 2018 20:44:40 +0000 (15:44 -0500)
committerKosta Harlan <kharlan@wikimedia.org>
Mon, 5 Nov 2018 20:49:33 +0000 (15:49 -0500)
This is needed to avoid triggering "does not have outer scope" errors when
addWatchBatchForUser() is invoked from WatchAction::doWatch() or
WatchAction::doUnwatch() in the context of uploading a file, moving a page, or
other instances when we don't have outer scope.

Bug: T208003
Change-Id: Ice5cb8fced64883476daea5cdac36e47dfcccb61

includes/watcheditem/WatchedItemStore.php

index f9435a1..a9bba7a 100644 (file)
@@ -389,7 +389,8 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
                $this->uncacheTitlesForUser( $user, $titles );
 
                $dbw = $this->getConnectionRef( DB_MASTER );
-               $ticket = $this->lbFactory->getEmptyTransactionTicket( __METHOD__ );
+               $ticket = count( $titles ) > $this->updateRowsPerQuery ?
+                       $this->lbFactory->getEmptyTransactionTicket( __METHOD__ ) : null;
                $affectedRows = 0;
 
                // Batch delete items per namespace.
@@ -402,7 +403,9 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
                                        'wl_title' => $toDelete
                                ], __METHOD__ );
                                $affectedRows += $dbw->affectedRows();
-                               $this->lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
+                               if ( $ticket ) {
+                                       $this->lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
+                               }
                        }
                }
 
@@ -714,7 +717,7 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
                if ( $this->readOnlyMode->isReadOnly() ) {
                        return false;
                }
-               // Only loggedin user can have a watchlist
+               // Only logged-in user can have a watchlist
                if ( $user->isAnon() ) {
                        return false;
                }
@@ -741,7 +744,8 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
                }
 
                $dbw = $this->getConnectionRef( DB_MASTER );
-               $ticket = $this->lbFactory->getEmptyTransactionTicket( __METHOD__ );
+               $ticket = count( $targets ) > $this->updateRowsPerQuery ?
+                       $this->lbFactory->getEmptyTransactionTicket( __METHOD__ ) : null;
                $affectedRows = 0;
                $rowBatches = array_chunk( $rows, $this->updateRowsPerQuery );
                foreach ( $rowBatches as $toInsert ) {
@@ -749,7 +753,9 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
                        // if there's already an entry for this page
                        $dbw->insert( 'watchlist', $toInsert, __METHOD__, 'IGNORE' );
                        $affectedRows += $dbw->affectedRows();
-                       $this->lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
+                       if ( $ticket ) {
+                               $this->lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
+                       }
                }
                // Update process cache to ensure skin doesn't claim that the current
                // page is unwatched in the response of action=watch itself (T28292).