Add WatchedItemStore::getWatchedItemsForUser
[lhc/web/wiklou.git] / includes / specials / SpecialEditWatchlist.php
index 1a9d096..dd440b9 100644 (file)
@@ -307,32 +307,25 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
        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 );
 
@@ -413,22 +406,15 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                        return; // nothing to do
                }
 
-               $dbw = wfGetDB( DB_MASTER );
                $user = $this->getUser();
+               $store = WatchedItemStore::getDefaultInstance();
 
                foreach ( $this->badItems as $row ) {
                        list( $title, $namespace, $dbKey ) = $row;
                        $action = $title ? 'cleaning up' : 'deleting';
                        wfDebug( "User {$user->getName()} has broken watchlist item ns($namespace):$dbKey, $action.\n" );
 
-                       $dbw->delete( 'watchlist',
-                               [
-                                       'wl_user' => $user->getId(),
-                                       'wl_namespace' => $namespace,
-                                       'wl_title' => $dbKey,
-                               ],
-                               __METHOD__
-                       );
+                       $store->removeWatch( $user, new TitleValue( $namespace, $dbKey ) );
 
                        // Can't just do an UPDATE instead of DELETE/INSERT due to unique index
                        if ( $title ) {
@@ -494,7 +480,7 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
         * @param array $titles Array of strings, or Title objects
         */
        private function unwatchTitles( $titles ) {
-               $dbw = wfGetDB( DB_MASTER );
+               $store = WatchedItemStore::getDefaultInstance();
 
                foreach ( $titles as $title ) {
                        if ( !$title instanceof Title ) {
@@ -502,25 +488,8 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                        }
 
                        if ( $title instanceof Title ) {
-                               $dbw->delete(
-                                       'watchlist',
-                                       [
-                                               'wl_user' => $this->getUser()->getId(),
-                                               'wl_namespace' => MWNamespace::getSubject( $title->getNamespace() ),
-                                               'wl_title' => $title->getDBkey(),
-                                       ],
-                                       __METHOD__
-                               );
-
-                               $dbw->delete(
-                                       'watchlist',
-                                       [
-                                               'wl_user' => $this->getUser()->getId(),
-                                               'wl_namespace' => MWNamespace::getTalk( $title->getNamespace() ),
-                                               'wl_title' => $title->getDBkey(),
-                                       ],
-                                       __METHOD__
-                               );
+                               $store->removeWatch( $this->getUser(), $title->getSubjectPage() );
+                               $store->removeWatch( $this->getUser(), $title->getTalkPage() );
 
                                $page = WikiPage::factory( $title );
                                Hooks::run( 'UnwatchArticleComplete', [ $this->getUser(), &$page ] );