Merge "REST: Properly handle HEAD requests"
[lhc/web/wiklou.git] / includes / specials / SpecialEditWatchlist.php
index 083b3c0..717edc3 100644 (file)
@@ -159,7 +159,6 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                        $out->addReturnTo( SpecialPage::getTitleFor( 'Watchlist' ) );
                } elseif ( $this->toc !== false ) {
                        $out->prependHTML( $this->toc );
-                       $out->addModules( 'mediawiki.toc' );
                        $out->addModuleStyles( 'mediawiki.toc.styles' );
                }
        }
@@ -381,8 +380,9 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
         */
        protected function getWatchlistInfo() {
                $titles = [];
+               $services = MediaWikiServices::getInstance();
 
-               $watchedItems = MediaWikiServices::getInstance()->getWatchedItemStore()
+               $watchedItems = $services->getWatchedItemStore()
                        ->getWatchedItemsForUser( $this->getUser(), [ 'sort' => WatchedItemStore::SORT_ASC ] );
 
                $lb = new LinkBatch();
@@ -391,7 +391,7 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                        $namespace = $watchedItem->getLinkTarget()->getNamespace();
                        $dbKey = $watchedItem->getLinkTarget()->getDBkey();
                        $lb->add( $namespace, $dbKey );
-                       if ( !MWNamespace::isTalk( $namespace ) ) {
+                       if ( !$services->getNamespaceInfo()->isTalk( $namespace ) ) {
                                $titles[$namespace][$dbKey] = 1;
                        }
                }
@@ -432,7 +432,7 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
         * Attempts to clean up broken items
         */
        private function cleanupWatchlist() {
-               if ( !count( $this->badItems ) ) {
+               if ( $this->badItems === [] ) {
                        return; // nothing to do
                }
 
@@ -459,9 +459,60 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
         * Add a list of targets to a user's watchlist
         *
         * @param string[]|LinkTarget[] $targets
+        * @return bool
+        * @throws FatalError
+        * @throws MWException
         */
-       private function watchTitles( $targets ) {
+       private function watchTitles( array $targets ) {
+               return MediaWikiServices::getInstance()->getWatchedItemStore()
+                       ->addWatchBatchForUser( $this->getUser(), $this->getExpandedTargets( $targets ) )
+                       && $this->runWatchUnwatchCompleteHook( 'Watch', $targets );
+       }
+
+       /**
+        * Remove a list of titles from 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 string[]|LinkTarget[] $targets
+        *
+        * @return bool
+        * @throws FatalError
+        * @throws MWException
+        */
+       private function unwatchTitles( array $targets ) {
+               return MediaWikiServices::getInstance()->getWatchedItemStore()
+                       ->removeWatchBatchForUser( $this->getUser(), $this->getExpandedTargets( $targets ) )
+                       && $this->runWatchUnwatchCompleteHook( 'Unwatch', $targets );
+       }
+
+       /**
+        * @param string $action
+        *   Can be "Watch" or "Unwatch"
+        * @param string[]|LinkTarget[] $targets
+        * @return bool
+        * @throws FatalError
+        * @throws MWException
+        */
+       private function runWatchUnwatchCompleteHook( $action, $targets ) {
+               foreach ( $targets as $target ) {
+                       $title = $target instanceof TitleValue ?
+                               Title::newFromTitleValue( $target ) :
+                               Title::newFromText( $target );
+                       $page = WikiPage::factory( $title );
+                       Hooks::run( $action . 'ArticleComplete', [ $this->getUser(), &$page ] );
+               }
+               return true;
+       }
+
+       /**
+        * @param string[]|LinkTarget[] $targets
+        * @return TitleValue[]
+        */
+       private function getExpandedTargets( array $targets ) {
                $expandedTargets = [];
+               $services = MediaWikiServices::getInstance();
                foreach ( $targets as $target ) {
                        if ( !$target instanceof LinkTarget ) {
                                try {
@@ -474,40 +525,12 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
 
                        $ns = $target->getNamespace();
                        $dbKey = $target->getDBkey();
-                       $expandedTargets[] = new TitleValue( MWNamespace::getSubject( $ns ), $dbKey );
-                       $expandedTargets[] = new TitleValue( MWNamespace::getTalk( $ns ), $dbKey );
-               }
-
-               MediaWikiServices::getInstance()->getWatchedItemStore()->addWatchBatchForUser(
-                       $this->getUser(),
-                       $expandedTargets
-               );
-       }
-
-       /**
-        * Remove a list of titles from 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
-        */
-       private function unwatchTitles( $titles ) {
-               $store = MediaWikiServices::getInstance()->getWatchedItemStore();
-
-               foreach ( $titles as $title ) {
-                       if ( !$title instanceof Title ) {
-                               $title = Title::newFromText( $title );
-                       }
-
-                       if ( $title instanceof Title ) {
-                               $store->removeWatch( $this->getUser(), $title->getSubjectPage() );
-                               $store->removeWatch( $this->getUser(), $title->getTalkPage() );
-
-                               $page = WikiPage::factory( $title );
-                               Hooks::run( 'UnwatchArticleComplete', [ $this->getUser(), &$page ] );
-                       }
+                       $expandedTargets[] =
+                               new TitleValue( $services->getNamespaceInfo()->getSubject( $ns ), $dbKey );
+                       $expandedTargets[] =
+                               new TitleValue( $services->getNamespaceInfo()->getTalk( $ns ), $dbKey );
                }
+               return $expandedTargets;
        }
 
        public function submitNormal( $data ) {
@@ -616,6 +639,7 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                $linkRenderer = $this->getLinkRenderer();
                $link = $linkRenderer->makeLink( $title );
 
+               $tools = [];
                $tools['talk'] = $linkRenderer->makeLink(
                        $title->getTalkPage(),
                        $this->msg( 'talkpagelinktext' )->text()