jenkins-bot has submitted this change and it was merged.

Change subject: Use WIS::getWatchedItemsForUser in SpecialEditWatchlist
......................................................................


Use WIS::getWatchedItemsForUser in SpecialEditWatchlist

This also adds a order option to:
WacthedItemStore::getWatchedItemsForUser

Tests are also updated

Change-Id: Ia683b92846ad79bde3f37068a8e168c5a8bdc201
---
M includes/WatchedItemStore.php
M includes/specials/SpecialEditWatchlist.php
M tests/phpunit/includes/WatchedItemStoreUnitTest.php
3 files changed, 45 insertions(+), 15 deletions(-)

Approvals:
  WMDE-Fisch: Looks good to me, but someone else must approve
  Daniel Kinzler: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/WatchedItemStore.php b/includes/WatchedItemStore.php
index 20ec592..4eea54d 100644
--- a/includes/WatchedItemStore.php
+++ b/includes/WatchedItemStore.php
@@ -12,6 +12,9 @@
  */
 class WatchedItemStore {
 
+       const SORT_DESC = 'DESC';
+       const SORT_ASC = 'ASC';
+
        /**
         * @var LoadBalancer
         */
@@ -494,18 +497,34 @@
         * @param User $user
         * @param array $options Allowed keys:
         *        'forWrite' => bool defaults to false
+        *        'sort' => string optional sorting by namespace ID and title
+        *                     one of the self::SORT_* constants
         *
         * @return WatchedItem[]
         */
        public function getWatchedItemsForUser( User $user, array $options = [] 
) {
                $options += [ 'forWrite' => false ];
 
+               $dbOptions = [];
+               if ( array_key_exists( 'sort', $options ) ) {
+                       Assert::parameter(
+                               ( in_array( $options['sort'], [ self::SORT_ASC, 
self::SORT_DESC ] ) ),
+                               '$options[\'sort\']',
+                               'must be SORT_ASC or SORT_DESC'
+                       );
+                       $dbOptions['ORDER BY'] = [
+                               "wl_namespace {$options['sort']}",
+                               "wl_title {$options['sort']}"
+                       ];
+               }
                $db = $this->getConnection( $options['forWrite'] ? DB_MASTER : 
DB_SLAVE );
+
                $res = $db->select(
                        'watchlist',
                        [ 'wl_namespace', 'wl_title', 
'wl_notificationtimestamp' ],
                        [ 'wl_user' => $user->getId() ],
-                       __METHOD__
+                       __METHOD__,
+                       $dbOptions
                );
                $this->reuseConnection( $db );
 
diff --git a/includes/specials/SpecialEditWatchlist.php 
b/includes/specials/SpecialEditWatchlist.php
index dd440b9..290ae8c 100644
--- a/includes/specials/SpecialEditWatchlist.php
+++ b/includes/specials/SpecialEditWatchlist.php
@@ -347,22 +347,18 @@
         */
        protected function getWatchlistInfo() {
                $titles = [];
-               $dbr = wfGetDB( DB_SLAVE );
 
-               $res = $dbr->select(
-                       [ 'watchlist' ],
-                       [ 'wl_namespace', 'wl_title' ],
-                       [ 'wl_user' => $this->getUser()->getId() ],
-                       __METHOD__,
-                       [ 'ORDER BY' => [ 'wl_namespace', 'wl_title' ] ]
-               );
+               $watchedItems = WatchedItemStore::getDefaultInstance()
+                       ->getWatchedItemsForUser( $this->getUser(), [ 'sort' => 
WatchedItemStore::SORT_ASC ] );
 
                $lb = new LinkBatch();
 
-               foreach ( $res as $row ) {
-                       $lb->add( $row->wl_namespace, $row->wl_title );
-                       if ( !MWNamespace::isTalk( $row->wl_namespace ) ) {
-                               $titles[$row->wl_namespace][$row->wl_title] = 1;
+               foreach ( $watchedItems as $watchedItem ) {
+                       $namespace = 
$watchedItem->getLinkTarget()->getNamespace();
+                       $dbKey = $watchedItem->getLinkTarget()->getDBkey();
+                       $lb->add( $namespace, $dbKey );
+                       if ( !MWNamespace::isTalk( $namespace ) ) {
+                               $titles[$namespace][$dbKey] = 1;
                        }
                }
 
diff --git a/tests/phpunit/includes/WatchedItemStoreUnitTest.php 
b/tests/phpunit/includes/WatchedItemStoreUnitTest.php
index 1354b1c..afde88c 100644
--- a/tests/phpunit/includes/WatchedItemStoreUnitTest.php
+++ b/tests/phpunit/includes/WatchedItemStoreUnitTest.php
@@ -1458,7 +1458,9 @@
                        ->with(
                                'watchlist',
                                [ 'wl_namespace', 'wl_title', 
'wl_notificationtimestamp' ],
-                               [ 'wl_user' => 1 ]
+                               [ 'wl_user' => 1 ],
+                               $this->isType( 'string' ),
+                               [ 'ORDER BY' => [ 'wl_namespace ASC', 'wl_title 
ASC' ] ]
                        )
                        ->will( $this->returnValue( [] ) );
 
@@ -1469,11 +1471,24 @@
 
                $watchedItems = $store->getWatchedItemsForUser(
                        $user,
-                       [ 'forWrite' => $forWrite ]
+                       [ 'forWrite' => $forWrite, 'sort' => 
WatchedItemStore::SORT_ASC ]
                );
                $this->assertEquals( [], $watchedItems );
        }
 
+       public function 
testGetWatchedItemsForUser_badSortOptionThrowsException() {
+               $store = new WatchedItemStore(
+                       $this->getMockLoadBalancer( $this->getMockDb() ),
+                       $this->getMockCache()
+               );
+
+               $this->setExpectedException( 'InvalidArgumentException' );
+               $store->getWatchedItemsForUser(
+                       $this->getMockNonAnonUserWithId( 1 ),
+                       [ 'sort' => 'foo' ]
+               );
+       }
+
        public function testIsWatchedItem_existingItem() {
                $mockDb = $this->getMockDb();
                $mockDb->expects( $this->once() )

-- 
To view, visit https://gerrit.wikimedia.org/r/277770
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ia683b92846ad79bde3f37068a8e168c5a8bdc201
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Anomie <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Florianschmidtwelzow <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: WMDE-Fisch <[email protected]>
Gerrit-Reviewer: WMDE-leszek <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to