SessionManager: Ignore Session object destruction during global shutdown
[mediawiki.git] / includes / WatchedItem.php
blob4dbe0f674f9bae615a425da338c310c62810d9c8
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
22 /**
23 * Representation of a pair of user and title for watchlist entries.
25 * @author Tim Starling
26 * @author Addshore
28 * @ingroup Watchlist
30 class WatchedItem {
32 /**
33 * @deprecated since 1.27, see User::IGNORE_USER_RIGHTS
35 const IGNORE_USER_RIGHTS = User::IGNORE_USER_RIGHTS;
37 /**
38 * @deprecated since 1.27, see User::CHECK_USER_RIGHTS
40 const CHECK_USER_RIGHTS = User::CHECK_USER_RIGHTS;
42 /**
43 * @deprecated Internal class use only
45 const DEPRECATED_USAGE_TIMESTAMP = -100;
47 /**
48 * @var bool
49 * @deprecated Internal class use only
51 public $checkRights = User::CHECK_USER_RIGHTS;
53 /**
54 * @var Title
55 * @deprecated Internal class use only
57 private $title;
59 /**
60 * @var LinkTarget
62 private $linkTarget;
64 /**
65 * @var User
67 private $user;
69 /**
70 * @var null|string the value of the wl_notificationtimestamp field
72 private $notificationTimestamp;
74 /**
75 * @param User $user
76 * @param LinkTarget $linkTarget
77 * @param null|string $notificationTimestamp the value of the wl_notificationtimestamp field
78 * @param bool|null $checkRights DO NOT USE - used internally for backward compatibility
80 public function __construct(
81 User $user,
82 LinkTarget $linkTarget,
83 $notificationTimestamp,
84 $checkRights = null
85 ) {
86 $this->user = $user;
87 $this->linkTarget = $linkTarget;
88 $this->notificationTimestamp = $notificationTimestamp;
89 if ( $checkRights !== null ) {
90 $this->checkRights = $checkRights;
94 /**
95 * @return User
97 public function getUser() {
98 return $this->user;
102 * @return LinkTarget
104 public function getLinkTarget() {
105 return $this->linkTarget;
109 * Get the notification timestamp of this entry.
111 * @return bool|null|string
113 public function getNotificationTimestamp() {
114 // Back compat for objects constructed using self::fromUserTitle
115 if ( $this->notificationTimestamp === self::DEPRECATED_USAGE_TIMESTAMP ) {
116 // wfDeprecated( __METHOD__, '1.27' );
117 if ( $this->checkRights && !$this->user->isAllowed( 'viewmywatchlist' ) ) {
118 return false;
120 $item = WatchedItemStore::getDefaultInstance()
121 ->loadWatchedItem( $this->user, $this->linkTarget );
122 if ( $item ) {
123 $this->notificationTimestamp = $item->getNotificationTimestamp();
124 } else {
125 $this->notificationTimestamp = false;
128 return $this->notificationTimestamp;
132 * Back compat pre 1.27 with the WatchedItemStore introduction
133 * @todo remove in 1.28/9
134 * -------------------------------------------------
138 * @return Title
139 * @deprecated Internal class use only
141 public function getTitle() {
142 if ( !$this->title ) {
143 $this->title = Title::newFromLinkTarget( $this->linkTarget );
145 return $this->title;
149 * @deprecated since 1.27 Use the constructor, WatchedItemStore::getWatchedItem()
150 * or WatchedItemStore::loadWatchedItem()
152 public static function fromUserTitle( $user, $title, $checkRights = User::CHECK_USER_RIGHTS ) {
153 // wfDeprecated( __METHOD__, '1.27' );
154 return new self( $user, $title, self::DEPRECATED_USAGE_TIMESTAMP, (bool)$checkRights );
158 * @deprecated since 1.27 Use WatchedItemStore::resetNotificationTimestamp()
160 public function resetNotificationTimestamp( $force = '', $oldid = 0 ) {
161 // wfDeprecated( __METHOD__, '1.27' );
162 if ( $this->checkRights && !$this->user->isAllowed( 'editmywatchlist' ) ) {
163 return;
165 WatchedItemStore::getDefaultInstance()->resetNotificationTimestamp(
166 $this->user,
167 $this->getTitle(),
168 $force,
169 $oldid
174 * @deprecated since 1.27 Use WatchedItemStore::addWatchBatch()
176 public static function batchAddWatch( array $items ) {
177 // wfDeprecated( __METHOD__, '1.27' );
178 if ( !$items ) {
179 return false;
182 $targets = [];
183 $users = [];
184 /** @var WatchedItem $watchedItem */
185 foreach ( $items as $watchedItem ) {
186 $user = $watchedItem->getUser();
187 if ( $watchedItem->checkRights && !$user->isAllowed( 'editmywatchlist' ) ) {
188 continue;
190 $userId = $user->getId();
191 $users[$userId] = $user;
192 $targets[$userId][] = $watchedItem->getTitle()->getSubjectPage();
193 $targets[$userId][] = $watchedItem->getTitle()->getTalkPage();
196 $store = WatchedItemStore::getDefaultInstance();
197 $success = true;
198 foreach ( $users as $userId => $user ) {
199 $success &= $store->addWatchBatchForUser( $user, $targets[$userId] );
202 return $success;
206 * @deprecated since 1.27 Use User::addWatch()
207 * @return bool
209 public function addWatch() {
210 // wfDeprecated( __METHOD__, '1.27' );
211 $this->user->addWatch( $this->getTitle(), $this->checkRights );
212 return true;
216 * @deprecated since 1.27 Use User::removeWatch()
217 * @return bool
219 public function removeWatch() {
220 // wfDeprecated( __METHOD__, '1.27' );
221 if ( $this->checkRights && !$this->user->isAllowed( 'editmywatchlist' ) ) {
222 return false;
224 $this->user->removeWatch( $this->getTitle(), $this->checkRights );
225 return true;
229 * @deprecated since 1.27 Use User::isWatched()
230 * @return bool
232 public function isWatched() {
233 // wfDeprecated( __METHOD__, '1.27' );
234 return $this->user->isWatched( $this->getTitle(), $this->checkRights );
238 * @deprecated since 1.27 Use WatchedItemStore::duplicateAllAssociatedEntries()
240 public static function duplicateEntries( Title $oldTitle, Title $newTitle ) {
241 // wfDeprecated( __METHOD__, '1.27' );
242 $store = WatchedItemStore::getDefaultInstance();
243 $store->duplicateAllAssociatedEntries( $oldTitle, $newTitle );