$this->loadBalancer->reuseConnection( $connection );
}
+ /**
+ * Count the number of individual items that are watched by the user.
+ * If a subject and corresponding talk page are watched this will return 2.
+ *
+ * @param User $user
+ *
+ * @return int
+ */
+ public function countWatchedItems( User $user ) {
+ $dbr = $this->getConnection( DB_SLAVE );
+ $return = (int)$dbr->selectField(
+ 'watchlist',
+ 'COUNT(*)',
+ [
+ 'wl_user' => $user->getId()
+ ],
+ __METHOD__
+ );
+ $this->reuseConnection( $dbr );
+
+ return $return;
+ }
+
/**
* @param LinkTarget $target
*
$form = "";
$user = $this->getUser();
- $dbr = $this->getDB();
- $numItems = $this->countItems( $dbr );
+ $numItems = $this->countItems();
$showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
// Show watchlist header
}
/**
- * Count the number of items on a user's watchlist
+ * Count the number of paired items on a user's watchlist.
+ * The assumption made here is that when a subject page is watched a talk page is also watched.
+ * Hence the number of individual items is halved.
*
- * @param IDatabase $dbr A database connection
* @return int
*/
- protected function countItems( $dbr ) {
- # Fetch the raw count
- $rows = $dbr->select( 'watchlist', [ 'count' => 'COUNT(*)' ],
- [ 'wl_user' => $this->getUser()->getId() ], __METHOD__ );
- $row = $dbr->fetchObject( $rows );
- $count = $row->count;
-
+ protected function countItems() {
+ $count = WatchedItemStore::getDefaultInstance()->countWatchedItems( $this->getUser() );
return floor( $count / 2 );
}
}
// Cleanup after previous tests
$store->removeWatch( $user, $title );
$initialWatchers = $store->countWatchers( $title );
+ $initialUserWatchedItems = $store->countWatchedItems( $user );
$this->assertFalse(
$store->isWatched( $user, $title ),
$store->isWatched( $user, $title ),
'Page should be watched'
);
+ $this->assertEquals( $initialUserWatchedItems + 1, $store->countWatchedItems( $user ) );
$this->assertEquals( $initialWatchers + 1, $store->countWatchers( $title ) );
$this->assertEquals(
$initialWatchers + 1,
$store->isWatched( $user, $title ),
'Page should be unwatched'
);
+ $this->assertEquals( $initialUserWatchedItems, $store->countWatchedItems( $user ) );
$this->assertEquals( $initialWatchers, $store->countWatchers( $title ) );
$this->assertEquals(
$initialWatchers,
$this->assertSame( $instance, WatchedItemStore::getDefaultInstance() );
}
+ public function testCountWatchedItems() {
+ $user = $this->getMockNonAnonUserWithId( 1 );
+
+ $mockDb = $this->getMockDb();
+ $mockDb->expects( $this->exactly( 1 ) )
+ ->method( 'selectField' )
+ ->with(
+ 'watchlist',
+ 'COUNT(*)',
+ [
+ 'wl_user' => $user->getId(),
+ ],
+ $this->isType( 'string' )
+ )
+ ->will( $this->returnValue( 12 ) );
+
+ $mockCache = $this->getMockCache();
+ $mockCache->expects( $this->never() )->method( 'get' );
+ $mockCache->expects( $this->never() )->method( 'set' );
+ $mockCache->expects( $this->never() )->method( 'delete' );
+
+ $store = new WatchedItemStore(
+ $this->getMockLoadBalancer( $mockDb ),
+ $mockCache
+ );
+
+ $this->assertEquals( 12, $store->countWatchedItems( $user ) );
+ }
+
public function testCountWatchers() {
$titleValue = new TitleValue( 0, 'SomeDbKey' );