Merge "TableDiffFormatter: Don't repeatedly call array_shift()"
[lhc/web/wiklou.git] / includes / specials / SpecialEditWatchlist.php
index 1a9d096..c1abd6e 100644 (file)
@@ -49,10 +49,26 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
 
        private $badItems = [];
 
+       /**
+        * @var TitleParser
+        */
+       private $titleParser;
+
        public function __construct() {
                parent::__construct( 'EditWatchlist', 'editmywatchlist' );
        }
 
+       /**
+        * Initialize any services we'll need (unless it has already been provided via a setter).
+        * This allows for dependency injection even though we don't control object creation.
+        */
+       private function initServices() {
+               if ( !$this->titleParser ) {
+                       $lang = $this->getContext()->getLanguage();
+                       $this->titleParser = new MediaWikiTitleCodec( $lang, GenderCache::singleton() );
+               }
+       }
+
        public function doesWrites() {
                return true;
        }
@@ -63,6 +79,7 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
         * @param int $mode
         */
        public function execute( $mode ) {
+               $this->initServices();
                $this->setHeaders();
 
                # Anons don't get a watchlist
@@ -307,32 +324,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 );
 
@@ -354,22 +364,18 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
         */
        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;
                        }
                }
 
@@ -413,22 +419,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( (int)$namespace, $dbKey ) );
 
                        // Can't just do an UPDATE instead of DELETE/INSERT due to unique index
                        if ( $title ) {
@@ -450,39 +449,32 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
        }
 
        /**
-        * Add a list of titles to a user's watchlist
+        * Add a list of targets to a user's watchlist
         *
-        * $titles can be an array of strings or Title objects; the former
-        * is preferred, since Titles are very memory-heavy
-        *
-        * @param array $titles Array of strings, or Title objects
+        * @param string[]|LinkTarget[] $targets
         */
-       private function watchTitles( $titles ) {
-               $dbw = wfGetDB( DB_MASTER );
-               $rows = [];
-
-               foreach ( $titles as $title ) {
-                       if ( !$title instanceof Title ) {
-                               $title = Title::newFromText( $title );
+       private function watchTitles( $targets ) {
+               $expandedTargets = [];
+               foreach ( $targets as $target ) {
+                       if ( !$target instanceof LinkTarget ) {
+                               try {
+                                       $target = $this->titleParser->parseTitle( $target, NS_MAIN );
+                               }
+                               catch ( MalformedTitleException $e ) {
+                                       continue;
+                               }
                        }
 
-                       if ( $title instanceof Title ) {
-                               $rows[] = [
-                                       'wl_user' => $this->getUser()->getId(),
-                                       'wl_namespace' => MWNamespace::getSubject( $title->getNamespace() ),
-                                       'wl_title' => $title->getDBkey(),
-                                       'wl_notificationtimestamp' => null,
-                               ];
-                               $rows[] = [
-                                       'wl_user' => $this->getUser()->getId(),
-                                       'wl_namespace' => MWNamespace::getTalk( $title->getNamespace() ),
-                                       'wl_title' => $title->getDBkey(),
-                                       'wl_notificationtimestamp' => null,
-                               ];
-                       }
+                       $ns = $target->getNamespace();
+                       $dbKey = $target->getDBkey();
+                       $expandedTargets[] = new TitleValue( MWNamespace::getSubject( $ns ), $dbKey );
+                       $expandedTargets[] = new TitleValue( MWNamespace::getTalk( $ns ), $dbKey );
                }
 
-               $dbw->insert( 'watchlist', $rows, __METHOD__, 'IGNORE' );
+               WatchedItemStore::getDefaultInstance()->addWatchBatchForUser(
+                       $this->getUser(),
+                       $expandedTargets
+               );
        }
 
        /**
@@ -494,7 +486,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 +494,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 ] );