X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2FWatchedItemStore.php;h=6c47cae2abac9afc7535f85bc248cd98aef24447;hb=d21abb5d95a36c66947585bb703963d19e16999c;hp=a13609bf5dccbd8ca33afeac444863cba749eee9;hpb=d24af42ffff087a0824f6e9abefa33cab3e4e835;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/WatchedItemStore.php b/includes/WatchedItemStore.php index a13609bf5d..6c47cae2ab 100644 --- a/includes/WatchedItemStore.php +++ b/includes/WatchedItemStore.php @@ -3,6 +3,7 @@ use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface; use MediaWiki\Linker\LinkTarget; use Wikimedia\Assert\Assert; +use Wikimedia\ScopedCallback; /** * Storage layer class for WatchedItems. @@ -190,22 +191,13 @@ class WatchedItemStore implements StatsdAwareInterface { } /** - * @param int $slaveOrMaster DB_MASTER or DB_SLAVE + * @param int $dbIndex DB_MASTER or DB_REPLICA * - * @return DatabaseBase + * @return IDatabase * @throws MWException */ - private function getConnection( $slaveOrMaster ) { - return $this->loadBalancer->getConnection( $slaveOrMaster, [ 'watchlist' ] ); - } - - /** - * @param DatabaseBase $connection - * - * @throws MWException - */ - private function reuseConnection( $connection ) { - $this->loadBalancer->reuseConnection( $connection ); + private function getConnectionRef( $dbIndex ) { + return $this->loadBalancer->getConnectionRef( $dbIndex, [ 'watchlist' ] ); } /** @@ -217,7 +209,7 @@ class WatchedItemStore implements StatsdAwareInterface { * @return int */ public function countWatchedItems( User $user ) { - $dbr = $this->getConnection( DB_SLAVE ); + $dbr = $this->getConnectionRef( DB_REPLICA ); $return = (int)$dbr->selectField( 'watchlist', 'COUNT(*)', @@ -226,7 +218,6 @@ class WatchedItemStore implements StatsdAwareInterface { ], __METHOD__ ); - $this->reuseConnection( $dbr ); return $return; } @@ -237,7 +228,7 @@ class WatchedItemStore implements StatsdAwareInterface { * @return int */ public function countWatchers( LinkTarget $target ) { - $dbr = $this->getConnection( DB_SLAVE ); + $dbr = $this->getConnectionRef( DB_REPLICA ); $return = (int)$dbr->selectField( 'watchlist', 'COUNT(*)', @@ -247,7 +238,6 @@ class WatchedItemStore implements StatsdAwareInterface { ], __METHOD__ ); - $this->reuseConnection( $dbr ); return $return; } @@ -263,7 +253,7 @@ class WatchedItemStore implements StatsdAwareInterface { * @throws MWException */ public function countVisitingWatchers( LinkTarget $target, $threshold ) { - $dbr = $this->getConnection( DB_SLAVE ); + $dbr = $this->getConnectionRef( DB_REPLICA ); $visitingWatchers = (int)$dbr->selectField( 'watchlist', 'COUNT(*)', @@ -276,7 +266,6 @@ class WatchedItemStore implements StatsdAwareInterface { ], __METHOD__ ); - $this->reuseConnection( $dbr ); return $visitingWatchers; } @@ -293,7 +282,7 @@ class WatchedItemStore implements StatsdAwareInterface { public function countWatchersMultiple( array $targets, array $options = [] ) { $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ]; - $dbr = $this->getConnection( DB_SLAVE ); + $dbr = $this->getConnectionRef( DB_REPLICA ); if ( array_key_exists( 'minimumWatchers', $options ) ) { $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$options['minimumWatchers']; @@ -308,8 +297,6 @@ class WatchedItemStore implements StatsdAwareInterface { $dbOptions ); - $this->reuseConnection( $dbr ); - $watchCounts = []; foreach ( $targets as $linkTarget ) { $watchCounts[$linkTarget->getNamespace()][$linkTarget->getDBkey()] = 0; @@ -341,7 +328,7 @@ class WatchedItemStore implements StatsdAwareInterface { array $targetsWithVisitThresholds, $minimumWatchers = null ) { - $dbr = $this->getConnection( DB_SLAVE ); + $dbr = $this->getConnectionRef( DB_REPLICA ); $conds = $this->getVisitingWatchersCondition( $dbr, $targetsWithVisitThresholds ); @@ -357,8 +344,6 @@ class WatchedItemStore implements StatsdAwareInterface { $dbOptions ); - $this->reuseConnection( $dbr ); - $watcherCounts = []; foreach ( $targetsWithVisitThresholds as list( $target ) ) { /* @var LinkTarget $target */ @@ -452,14 +437,13 @@ class WatchedItemStore implements StatsdAwareInterface { return false; } - $dbr = $this->getConnection( DB_SLAVE ); + $dbr = $this->getConnectionRef( DB_REPLICA ); $row = $dbr->selectRow( 'watchlist', 'wl_notificationtimestamp', $this->dbCond( $user, $target ), __METHOD__ ); - $this->reuseConnection( $dbr ); if ( !$row ) { return false; @@ -499,7 +483,7 @@ class WatchedItemStore implements StatsdAwareInterface { "wl_title {$options['sort']}" ]; } - $db = $this->getConnection( $options['forWrite'] ? DB_MASTER : DB_SLAVE ); + $db = $this->getConnectionRef( $options['forWrite'] ? DB_MASTER : DB_REPLICA ); $res = $db->select( 'watchlist', @@ -508,7 +492,6 @@ class WatchedItemStore implements StatsdAwareInterface { __METHOD__, $dbOptions ); - $this->reuseConnection( $db ); $watchedItems = []; foreach ( $res as $row ) { @@ -569,7 +552,7 @@ class WatchedItemStore implements StatsdAwareInterface { return $timestamps; } - $dbr = $this->getConnection( DB_SLAVE ); + $dbr = $this->getConnectionRef( DB_REPLICA ); $lb = new LinkBatch( $targetsToLoad ); $res = $dbr->select( @@ -581,7 +564,6 @@ class WatchedItemStore implements StatsdAwareInterface { ], __METHOD__ ); - $this->reuseConnection( $dbr ); foreach ( $res as $row ) { $timestamps[$row->wl_namespace][$row->wl_title] = $row->wl_notificationtimestamp; @@ -630,13 +612,12 @@ class WatchedItemStore implements StatsdAwareInterface { $this->uncache( $user, $target ); } - $dbw = $this->getConnection( DB_MASTER ); + $dbw = $this->getConnectionRef( DB_MASTER ); foreach ( array_chunk( $rows, 100 ) as $toInsert ) { // Use INSERT IGNORE to avoid overwriting the notification timestamp // if there's already an entry for this page $dbw->insert( 'watchlist', $toInsert, __METHOD__, 'IGNORE' ); } - $this->reuseConnection( $dbw ); return true; } @@ -660,7 +641,7 @@ class WatchedItemStore implements StatsdAwareInterface { $this->uncache( $user, $target ); - $dbw = $this->getConnection( DB_MASTER ); + $dbw = $this->getConnectionRef( DB_MASTER ); $dbw->delete( 'watchlist', [ 'wl_user' => $user->getId(), @@ -669,7 +650,6 @@ class WatchedItemStore implements StatsdAwareInterface { ], __METHOD__ ); $success = (bool)$dbw->affectedRows(); - $this->reuseConnection( $dbw ); return $success; } @@ -687,7 +667,7 @@ class WatchedItemStore implements StatsdAwareInterface { return false; } - $dbw = $this->getConnection( DB_MASTER ); + $dbw = $this->getConnectionRef( DB_MASTER ); $conds = [ 'wl_user' => $user->getId() ]; if ( $targets ) { @@ -702,8 +682,6 @@ class WatchedItemStore implements StatsdAwareInterface { __METHOD__ ); - $this->reuseConnection( $dbw ); - $this->uncacheUser( $user ); return $success; @@ -718,7 +696,7 @@ class WatchedItemStore implements StatsdAwareInterface { * @return int[] Array of user IDs the timestamp has been updated for */ public function updateNotificationTimestamp( User $editor, LinkTarget $target, $timestamp ) { - $dbw = $this->getConnection( DB_MASTER ); + $dbw = $this->getConnectionRef( DB_MASTER ); $uids = $dbw->selectFieldValues( 'watchlist', 'wl_user', @@ -730,7 +708,6 @@ class WatchedItemStore implements StatsdAwareInterface { ], __METHOD__ ); - $this->reuseConnection( $dbw ); $watchers = array_map( 'intval', $uids ); if ( $watchers ) { @@ -740,7 +717,7 @@ class WatchedItemStore implements StatsdAwareInterface { function () use ( $timestamp, $watchers, $target, $fname ) { global $wgUpdateRowsPerQuery; - $dbw = $this->getConnection( DB_MASTER ); + $dbw = $this->getConnectionRef( DB_MASTER ); $factory = wfGetLBFactory(); $ticket = $factory->getEmptyTransactionTicket( __METHOD__ ); @@ -762,8 +739,6 @@ class WatchedItemStore implements StatsdAwareInterface { } } $this->uncacheLinkTarget( $target ); - - $this->reuseConnection( $dbw ); }, DeferredUpdates::POSTSEND, $dbw @@ -885,7 +860,7 @@ class WatchedItemStore implements StatsdAwareInterface { $queryOptions['LIMIT'] = $unreadLimit; } - $dbr = $this->getConnection( DB_SLAVE ); + $dbr = $this->getConnectionRef( DB_REPLICA ); $rowCount = $dbr->selectRowCount( 'watchlist', '1', @@ -896,7 +871,6 @@ class WatchedItemStore implements StatsdAwareInterface { __METHOD__, $queryOptions ); - $this->reuseConnection( $dbr ); if ( !isset( $unreadLimit ) ) { return $rowCount; @@ -937,7 +911,7 @@ class WatchedItemStore implements StatsdAwareInterface { * @param LinkTarget $newTarget */ public function duplicateEntry( LinkTarget $oldTarget, LinkTarget $newTarget ) { - $dbw = $this->getConnection( DB_MASTER ); + $dbw = $this->getConnectionRef( DB_MASTER ); $result = $dbw->select( 'watchlist', @@ -975,8 +949,6 @@ class WatchedItemStore implements StatsdAwareInterface { __METHOD__ ); } - - $this->reuseConnection( $dbw ); } }