Run maintenance/generateLocalAutoload.php
[mediawiki.git] / includes / WatchedItem.php
blobdb6ce8744a77db705221c2a79803a81953bc7ebe
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\Linker\LinkTarget;
23 /**
24 * Representation of a pair of user and title for watchlist entries.
26 * @author Tim Starling
27 * @author Addshore
29 * @ingroup Watchlist
31 class WatchedItem {
33 /**
34 * @deprecated since 1.27, see User::IGNORE_USER_RIGHTS
36 const IGNORE_USER_RIGHTS = User::IGNORE_USER_RIGHTS;
38 /**
39 * @deprecated since 1.27, see User::CHECK_USER_RIGHTS
41 const CHECK_USER_RIGHTS = User::CHECK_USER_RIGHTS;
43 /**
44 * @deprecated Internal class use only
46 const DEPRECATED_USAGE_TIMESTAMP = -100;
48 /**
49 * @var bool
50 * @deprecated Internal class use only
52 public $checkRights = User::CHECK_USER_RIGHTS;
54 /**
55 * @var Title
56 * @deprecated Internal class use only
58 private $title;
60 /**
61 * @var LinkTarget
63 private $linkTarget;
65 /**
66 * @var User
68 private $user;
70 /**
71 * @var null|string the value of the wl_notificationtimestamp field
73 private $notificationTimestamp;
75 /**
76 * @param User $user
77 * @param LinkTarget $linkTarget
78 * @param null|string $notificationTimestamp the value of the wl_notificationtimestamp field
79 * @param bool|null $checkRights DO NOT USE - used internally for backward compatibility
81 public function __construct(
82 User $user,
83 LinkTarget $linkTarget,
84 $notificationTimestamp,
85 $checkRights = null
86 ) {
87 $this->user = $user;
88 $this->linkTarget = $linkTarget;
89 $this->notificationTimestamp = $notificationTimestamp;
90 if ( $checkRights !== null ) {
91 $this->checkRights = $checkRights;
95 /**
96 * @return User
98 public function getUser() {
99 return $this->user;
103 * @return LinkTarget
105 public function getLinkTarget() {
106 return $this->linkTarget;
110 * Get the notification timestamp of this entry.
112 * @return bool|null|string
114 public function getNotificationTimestamp() {
115 // Back compat for objects constructed using self::fromUserTitle
116 if ( $this->notificationTimestamp === self::DEPRECATED_USAGE_TIMESTAMP ) {
117 // wfDeprecated( __METHOD__, '1.27' );
118 if ( $this->checkRights && !$this->user->isAllowed( 'viewmywatchlist' ) ) {
119 return false;
121 $item = WatchedItemStore::getDefaultInstance()
122 ->loadWatchedItem( $this->user, $this->linkTarget );
123 if ( $item ) {
124 $this->notificationTimestamp = $item->getNotificationTimestamp();
125 } else {
126 $this->notificationTimestamp = false;
129 return $this->notificationTimestamp;
133 * Back compat pre 1.27 with the WatchedItemStore introduction
134 * @todo remove in 1.28/9
135 * -------------------------------------------------
139 * @return Title
140 * @deprecated Internal class use only
142 public function getTitle() {
143 if ( !$this->title ) {
144 $this->title = Title::newFromLinkTarget( $this->linkTarget );
146 return $this->title;
150 * @deprecated since 1.27 Use the constructor, WatchedItemStore::getWatchedItem()
151 * or WatchedItemStore::loadWatchedItem()
153 public static function fromUserTitle( $user, $title, $checkRights = User::CHECK_USER_RIGHTS ) {
154 // wfDeprecated( __METHOD__, '1.27' );
155 return new self( $user, $title, self::DEPRECATED_USAGE_TIMESTAMP, (bool)$checkRights );
159 * @deprecated since 1.27 Use WatchedItemStore::resetNotificationTimestamp()
161 public function resetNotificationTimestamp( $force = '', $oldid = 0 ) {
162 // wfDeprecated( __METHOD__, '1.27' );
163 if ( $this->checkRights && !$this->user->isAllowed( 'editmywatchlist' ) ) {
164 return;
166 WatchedItemStore::getDefaultInstance()->resetNotificationTimestamp(
167 $this->user,
168 $this->getTitle(),
169 $force,
170 $oldid
175 * @deprecated since 1.27 Use WatchedItemStore::addWatchBatch()
177 public static function batchAddWatch( array $items ) {
178 // wfDeprecated( __METHOD__, '1.27' );
179 if ( !$items ) {
180 return false;
183 $targets = [];
184 $users = [];
185 /** @var WatchedItem $watchedItem */
186 foreach ( $items as $watchedItem ) {
187 $user = $watchedItem->getUser();
188 if ( $watchedItem->checkRights && !$user->isAllowed( 'editmywatchlist' ) ) {
189 continue;
191 $userId = $user->getId();
192 $users[$userId] = $user;
193 $targets[$userId][] = $watchedItem->getTitle()->getSubjectPage();
194 $targets[$userId][] = $watchedItem->getTitle()->getTalkPage();
197 $store = WatchedItemStore::getDefaultInstance();
198 $success = true;
199 foreach ( $users as $userId => $user ) {
200 $success &= $store->addWatchBatchForUser( $user, $targets[$userId] );
203 return $success;
207 * @deprecated since 1.27 Use User::addWatch()
208 * @return bool
210 public function addWatch() {
211 // wfDeprecated( __METHOD__, '1.27' );
212 $this->user->addWatch( $this->getTitle(), $this->checkRights );
213 return true;
217 * @deprecated since 1.27 Use User::removeWatch()
218 * @return bool
220 public function removeWatch() {
221 // wfDeprecated( __METHOD__, '1.27' );
222 if ( $this->checkRights && !$this->user->isAllowed( 'editmywatchlist' ) ) {
223 return false;
225 $this->user->removeWatch( $this->getTitle(), $this->checkRights );
226 return true;
230 * @deprecated since 1.27 Use User::isWatched()
231 * @return bool
233 public function isWatched() {
234 // wfDeprecated( __METHOD__, '1.27' );
235 return $this->user->isWatched( $this->getTitle(), $this->checkRights );
239 * @deprecated since 1.27 Use WatchedItemStore::duplicateAllAssociatedEntries()
241 public static function duplicateEntries( Title $oldTitle, Title $newTitle ) {
242 // wfDeprecated( __METHOD__, '1.27' );
243 $store = WatchedItemStore::getDefaultInstance();
244 $store->duplicateAllAssociatedEntries( $oldTitle, $newTitle );