Add wfDeprecated calls to WatchedItem
[mediawiki.git] / includes / WatchedItem.php
blobb070e1eba6d35339a7160fe780899be49995c295
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
19 * @ingroup Watchlist
21 use MediaWiki\MediaWikiServices;
22 use MediaWiki\Linker\LinkTarget;
24 /**
25 * Representation of a pair of user and title for watchlist entries.
27 * @author Tim Starling
28 * @author Addshore
30 * @ingroup Watchlist
32 class WatchedItem {
34 /**
35 * @deprecated since 1.27, see User::IGNORE_USER_RIGHTS
37 const IGNORE_USER_RIGHTS = User::IGNORE_USER_RIGHTS;
39 /**
40 * @deprecated since 1.27, see User::CHECK_USER_RIGHTS
42 const CHECK_USER_RIGHTS = User::CHECK_USER_RIGHTS;
44 /**
45 * @deprecated Internal class use only
47 const DEPRECATED_USAGE_TIMESTAMP = -100;
49 /**
50 * @var bool
51 * @deprecated Internal class use only
53 public $checkRights = User::CHECK_USER_RIGHTS;
55 /**
56 * @var Title
57 * @deprecated Internal class use only
59 private $title;
61 /**
62 * @var LinkTarget
64 private $linkTarget;
66 /**
67 * @var User
69 private $user;
71 /**
72 * @var null|string the value of the wl_notificationtimestamp field
74 private $notificationTimestamp;
76 /**
77 * @param User $user
78 * @param LinkTarget $linkTarget
79 * @param null|string $notificationTimestamp the value of the wl_notificationtimestamp field
80 * @param bool|null $checkRights DO NOT USE - used internally for backward compatibility
82 public function __construct(
83 User $user,
84 LinkTarget $linkTarget,
85 $notificationTimestamp,
86 $checkRights = null
87 ) {
88 $this->user = $user;
89 $this->linkTarget = $linkTarget;
90 $this->notificationTimestamp = $notificationTimestamp;
91 if ( $checkRights !== null ) {
92 $this->checkRights = $checkRights;
96 /**
97 * @return User
99 public function getUser() {
100 return $this->user;
104 * @return LinkTarget
106 public function getLinkTarget() {
107 return $this->linkTarget;
111 * Get the notification timestamp of this entry.
113 * @return bool|null|string
115 public function getNotificationTimestamp() {
116 // Back compat for objects constructed using self::fromUserTitle
117 if ( $this->notificationTimestamp === self::DEPRECATED_USAGE_TIMESTAMP ) {
118 // wfDeprecated( __METHOD__, '1.27' );
119 if ( $this->checkRights && !$this->user->isAllowed( 'viewmywatchlist' ) ) {
120 return false;
122 $item = MediaWikiServices::getInstance()->getWatchedItemStore()
123 ->loadWatchedItem( $this->user, $this->linkTarget );
124 if ( $item ) {
125 $this->notificationTimestamp = $item->getNotificationTimestamp();
126 } else {
127 $this->notificationTimestamp = false;
130 return $this->notificationTimestamp;
134 * Back compat pre 1.27 with the WatchedItemStore introduction
135 * @todo remove in 1.28/9
136 * -------------------------------------------------
140 * @return Title
141 * @deprecated Internal class use only
143 public function getTitle() {
144 if ( !$this->title ) {
145 $this->title = Title::newFromLinkTarget( $this->linkTarget );
147 return $this->title;
151 * @deprecated since 1.27 Use the constructor, WatchedItemStore::getWatchedItem()
152 * or WatchedItemStore::loadWatchedItem()
154 public static function fromUserTitle( $user, $title, $checkRights = User::CHECK_USER_RIGHTS ) {
155 wfDeprecated( __METHOD__, '1.27' );
156 return new self( $user, $title, self::DEPRECATED_USAGE_TIMESTAMP, (bool)$checkRights );
160 * @deprecated since 1.27 Use WatchedItemStore::resetNotificationTimestamp()
162 public function resetNotificationTimestamp( $force = '', $oldid = 0 ) {
163 wfDeprecated( __METHOD__, '1.27' );
164 if ( $this->checkRights && !$this->user->isAllowed( 'editmywatchlist' ) ) {
165 return;
167 MediaWikiServices::getInstance()->getWatchedItemStore()->resetNotificationTimestamp(
168 $this->user,
169 $this->getTitle(),
170 $force,
171 $oldid
176 * @deprecated since 1.27 Use WatchedItemStore::addWatchBatch()
178 public static function batchAddWatch( array $items ) {
179 wfDeprecated( __METHOD__, '1.27' );
180 if ( !$items ) {
181 return false;
184 $targets = [];
185 $users = [];
186 /** @var WatchedItem $watchedItem */
187 foreach ( $items as $watchedItem ) {
188 $user = $watchedItem->getUser();
189 if ( $watchedItem->checkRights && !$user->isAllowed( 'editmywatchlist' ) ) {
190 continue;
192 $userId = $user->getId();
193 $users[$userId] = $user;
194 $targets[$userId][] = $watchedItem->getTitle()->getSubjectPage();
195 $targets[$userId][] = $watchedItem->getTitle()->getTalkPage();
198 $store = MediaWikiServices::getInstance()->getWatchedItemStore();
199 $success = true;
200 foreach ( $users as $userId => $user ) {
201 $success &= $store->addWatchBatchForUser( $user, $targets[$userId] );
204 return $success;
208 * @deprecated since 1.27 Use User::addWatch()
209 * @return bool
211 public function addWatch() {
212 wfDeprecated( __METHOD__, '1.27' );
213 $this->user->addWatch( $this->getTitle(), $this->checkRights );
214 return true;
218 * @deprecated since 1.27 Use User::removeWatch()
219 * @return bool
221 public function removeWatch() {
222 wfDeprecated( __METHOD__, '1.27' );
223 if ( $this->checkRights && !$this->user->isAllowed( 'editmywatchlist' ) ) {
224 return false;
226 $this->user->removeWatch( $this->getTitle(), $this->checkRights );
227 return true;
231 * @deprecated since 1.27 Use User::isWatched()
232 * @return bool
234 public function isWatched() {
235 wfDeprecated( __METHOD__, '1.27' );
236 return $this->user->isWatched( $this->getTitle(), $this->checkRights );
240 * @deprecated since 1.27 Use WatchedItemStore::duplicateAllAssociatedEntries()
242 public static function duplicateEntries( Title $oldTitle, Title $newTitle ) {
243 wfDeprecated( __METHOD__, '1.27' );
244 $store = MediaWikiServices::getInstance()->getWatchedItemStore();
245 $store->duplicateAllAssociatedEntries( $oldTitle, $newTitle );