Remove WatchedItemStore::getDefaultInstance
[lhc/web/wiklou.git] / includes / WatchedItem.php
index 5b4a4fc..50c79dc 100644 (file)
@@ -18,6 +18,8 @@
  * @file
  * @ingroup Watchlist
  */
+use MediaWiki\MediaWikiServices;
+use MediaWiki\Linker\LinkTarget;
 
 /**
  * Representation of a pair of user and title for watchlist entries.
@@ -117,7 +119,7 @@ class WatchedItem {
                        if ( $this->checkRights && !$this->user->isAllowed( 'viewmywatchlist' ) ) {
                                return false;
                        }
-                       $item = WatchedItemStore::getDefaultInstance()
+                       $item = MediaWikiServices::getInstance()->getWatchedItemStore()
                                ->loadWatchedItem( $this->user, $this->linkTarget );
                        if ( $item ) {
                                $this->notificationTimestamp = $item->getNotificationTimestamp();
@@ -140,11 +142,7 @@ class WatchedItem {
         */
        public function getTitle() {
                if ( !$this->title ) {
-                       if ( $this->linkTarget instanceof Title ) {
-                               $this->title = $this->linkTarget;
-                       } else {
-                               $this->title = Title::newFromLinkTarget( $this->linkTarget );
-                       }
+                       $this->title = Title::newFromLinkTarget( $this->linkTarget );
                }
                return $this->title;
        }
@@ -166,7 +164,7 @@ class WatchedItem {
                if ( $this->checkRights && !$this->user->isAllowed( 'editmywatchlist' ) ) {
                        return;
                }
-               WatchedItemStore::getDefaultInstance()->resetNotificationTimestamp(
+               MediaWikiServices::getInstance()->getWatchedItemStore()->resetNotificationTimestamp(
                        $this->user,
                        $this->getTitle(),
                        $force,
@@ -179,23 +177,31 @@ class WatchedItem {
         */
        public static function batchAddWatch( array $items ) {
                // wfDeprecated( __METHOD__, '1.27' );
-               $userTargetCombinations = [];
+               if ( !$items ) {
+                       return false;
+               }
+
+               $targets = [];
+               $users = [];
                /** @var WatchedItem $watchedItem */
                foreach ( $items as $watchedItem ) {
-                       if ( $watchedItem->checkRights && !$watchedItem->getUser()->isAllowed( 'editmywatchlist' ) ) {
+                       $user = $watchedItem->getUser();
+                       if ( $watchedItem->checkRights && !$user->isAllowed( 'editmywatchlist' ) ) {
                                continue;
                        }
-                       $userTargetCombinations[] = [
-                               $watchedItem->getUser(),
-                               $watchedItem->getTitle()->getSubjectPage()
-                       ];
-                       $userTargetCombinations[] = [
-                               $watchedItem->getUser(),
-                               $watchedItem->getTitle()->getTalkPage()
-                       ];
+                       $userId = $user->getId();
+                       $users[$userId] = $user;
+                       $targets[$userId][] = $watchedItem->getTitle()->getSubjectPage();
+                       $targets[$userId][] = $watchedItem->getTitle()->getTalkPage();
+               }
+
+               $store = MediaWikiServices::getInstance()->getWatchedItemStore();
+               $success = true;
+               foreach ( $users as $userId => $user ) {
+                       $success &= $store->addWatchBatchForUser( $user, $targets[$userId] );
                }
-               $store = WatchedItemStore::getDefaultInstance();
-               return $store->addWatchBatch( $userTargetCombinations );
+
+               return $success;
        }
 
        /**
@@ -235,7 +241,7 @@ class WatchedItem {
         */
        public static function duplicateEntries( Title $oldTitle, Title $newTitle ) {
                // wfDeprecated( __METHOD__, '1.27' );
-               $store = WatchedItemStore::getDefaultInstance();
+               $store = MediaWikiServices::getInstance()->getWatchedItemStore();
                $store->duplicateAllAssociatedEntries( $oldTitle, $newTitle );
        }