return $item;
}
+ /**
+ * @param User $user
+ * @param array $options Allowed keys:
+ * 'forWrite' => bool defaults to false
+ *
+ * @return WatchedItem[]
+ */
+ public function getWatchedItemsForUser( User $user, array $options = [] ) {
+ $options += [ 'forWrite' => false ];
+
+ $db = $this->getConnection( $options['forWrite'] ? DB_MASTER : DB_SLAVE );
+ $res = $db->select(
+ 'watchlist',
+ [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
+ [ 'wl_user' => $user->getId() ],
+ __METHOD__
+ );
+ $this->reuseConnection( $db );
+
+ $watchedItems = [];
+ foreach ( $res as $row ) {
+ // todo these could all be cached at some point?
+ $watchedItems[] = new WatchedItem(
+ $user,
+ new TitleValue( (int)$row->wl_namespace, $row->wl_title ),
+ $row->wl_notificationtimestamp
+ );
+ }
+
+ return $watchedItems;
+ }
+
/**
* Must be called separately for Subject & Talk namespaces
*
private function getWatchlist() {
$list = [];
- $index = $this->getRequest()->wasPosted() ? DB_MASTER : DB_SLAVE;
- $dbr = wfGetDB( $index );
-
- $res = $dbr->select(
- 'watchlist',
- [
- 'wl_namespace', 'wl_title'
- ], [
- 'wl_user' => $this->getUser()->getId(),
- ],
- __METHOD__
+ $watchedItems = WatchedItemStore::getDefaultInstance()->getWatchedItemsForUser(
+ $this->getUser(),
+ [ 'forWrite' => $this->getRequest()->wasPosted() ]
);
- if ( $res->numRows() > 0 ) {
+ if ( $watchedItems ) {
/** @var Title[] $titles */
$titles = [];
- foreach ( $res as $row ) {
- $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
+ foreach ( $watchedItems as $watchedItem ) {
+ $namespace = $watchedItem->getLinkTarget()->getNamespace();
+ $dbKey = $watchedItem->getLinkTarget()->getDBkey();
+ $title = Title::makeTitleSafe( $namespace, $dbKey );
- if ( $this->checkTitle( $title, $row->wl_namespace, $row->wl_title )
+ if ( $this->checkTitle( $title, $namespace, $dbKey )
&& !$title->isTalkPage()
) {
$titles[] = $title;
}
}
- $res->free();
GenderCache::singleton()->doTitlesArray( $titles );
'Page should be watched'
);
$this->assertEquals( $initialUserWatchedItems + 1, $store->countWatchedItems( $user ) );
+ $watchedItemsForUser = $store->getWatchedItemsForUser( $user );
+ $this->assertCount( $initialUserWatchedItems + 1, $watchedItemsForUser );
+ $watchedItemsForUserHasExpectedItem = false;
+ foreach ( $watchedItemsForUser as $watchedItem ) {
+ if (
+ $watchedItem->getUser()->equals( $user ) &&
+ $watchedItem->getLinkTarget() == $title->getTitleValue()
+ ) {
+ $watchedItemsForUserHasExpectedItem = true;
+ }
+ }
+ $this->assertTrue(
+ $watchedItemsForUserHasExpectedItem,
+ 'getWatchedItemsForUser should contain the page'
+ );
$this->assertEquals( $initialWatchers + 1, $store->countWatchers( $title ) );
$this->assertEquals(
$initialWatchers + 1,
'Page should be unwatched'
);
$this->assertEquals( $initialUserWatchedItems, $store->countWatchedItems( $user ) );
+ $watchedItemsForUser = $store->getWatchedItemsForUser( $user );
+ $this->assertCount( $initialUserWatchedItems, $watchedItemsForUser );
+ $watchedItemsForUserHasExpectedItem = false;
+ foreach ( $watchedItemsForUser as $watchedItem ) {
+ if (
+ $watchedItem->getUser()->equals( $user ) &&
+ $watchedItem->getLinkTarget() == $title->getTitleValue()
+ ) {
+ $watchedItemsForUserHasExpectedItem = true;
+ }
+ }
+ $this->assertFalse(
+ $watchedItemsForUserHasExpectedItem,
+ 'getWatchedItemsForUser should not contain the page'
+ );
$this->assertEquals( $initialWatchers, $store->countWatchers( $title ) );
$this->assertEquals(
$initialWatchers,
/**
* @return PHPUnit_Framework_MockObject_MockObject|LoadBalancer
*/
- private function getMockLoadBalancer( $mockDb ) {
+ private function getMockLoadBalancer( $mockDb, $expectedConnectionType = null ) {
$mock = $this->getMockBuilder( LoadBalancer::class )
->disableOriginalConstructor()
->getMock();
- $mock->expects( $this->any() )
- ->method( 'getConnection' )
- ->will( $this->returnValue( $mockDb ) );
+ if ( $expectedConnectionType !== null ) {
+ $mock->expects( $this->any() )
+ ->method( 'getConnection' )
+ ->with( $expectedConnectionType )
+ ->will( $this->returnValue( $mockDb ) );
+ } else {
+ $mock->expects( $this->any() )
+ ->method( 'getConnection' )
+ ->will( $this->returnValue( $mockDb ) );
+ }
$mock->expects( $this->any() )
->method( 'getReadOnlyReason' )
->will( $this->returnValue( false ) );
);
}
+ public function testGetWatchedItemsForUser() {
+ $mockDb = $this->getMockDb();
+ $mockDb->expects( $this->once() )
+ ->method( 'select' )
+ ->with(
+ 'watchlist',
+ [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
+ [ 'wl_user' => 1 ]
+ )
+ ->will( $this->returnValue( [
+ $this->getFakeRow( [
+ 'wl_namespace' => 0,
+ 'wl_title' => 'Foo1',
+ 'wl_notificationtimestamp' => '20151212010101',
+ ] ),
+ $this->getFakeRow( [
+ 'wl_namespace' => 1,
+ 'wl_title' => 'Foo2',
+ 'wl_notificationtimestamp' => null,
+ ] ),
+ ] ) );
+
+ $mockCache = $this->getMockCache();
+ $mockCache->expects( $this->never() )->method( 'delete' );
+ $mockCache->expects( $this->never() )->method( 'get' );
+ $mockCache->expects( $this->never() )->method( 'set' );
+
+ $store = new WatchedItemStore(
+ $this->getMockLoadBalancer( $mockDb ),
+ $mockCache
+ );
+ $user = $this->getMockNonAnonUserWithId( 1 );
+
+ $watchedItems = $store->getWatchedItemsForUser( $user );
+
+ $this->assertInternalType( 'array', $watchedItems );
+ $this->assertCount( 2, $watchedItems );
+ foreach ( $watchedItems as $watchedItem ) {
+ $this->assertInstanceOf( 'WatchedItem', $watchedItem );
+ }
+ $this->assertEquals(
+ new WatchedItem( $user, new TitleValue( 0, 'Foo1' ), '20151212010101' ),
+ $watchedItems[0]
+ );
+ $this->assertEquals(
+ new WatchedItem( $user, new TitleValue( 1, 'Foo2' ), null ),
+ $watchedItems[1]
+ );
+ }
+
+ public function provideDbTypes() {
+ return [
+ [ false, DB_SLAVE ],
+ [ true, DB_MASTER ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideDbTypes
+ */
+ public function testGetWatchedItemsForUser_optionsAndEmptyResult( $forWrite, $dbType ) {
+ $mockDb = $this->getMockDb();
+ $mockCache = $this->getMockCache();
+ $mockLoadBalancer = $this->getMockLoadBalancer( $mockDb, $dbType );
+ $user = $this->getMockNonAnonUserWithId( 1 );
+
+ $mockDb->expects( $this->once() )
+ ->method( 'select' )
+ ->with(
+ 'watchlist',
+ [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
+ [ 'wl_user' => 1 ]
+ )
+ ->will( $this->returnValue( [] ) );
+
+ $store = new WatchedItemStore(
+ $mockLoadBalancer,
+ $mockCache
+ );
+
+ $watchedItems = $store->getWatchedItemsForUser(
+ $user,
+ [ 'forWrite' => $forWrite ]
+ );
+ $this->assertEquals( [], $watchedItems );
+ }
+
public function testIsWatchedItem_existingItem() {
$mockDb = $this->getMockDb();
$mockDb->expects( $this->once() )