Merge "Handle null data return in HTMLForm"
[lhc/web/wiklou.git] / includes / WatchedItem.php
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup Watchlist
20 */
21 use MediaWiki\Linker\LinkTarget;
22
23 /**
24 * Representation of a pair of user and title for watchlist entries.
25 *
26 * @author Tim Starling
27 * @author Addshore
28 *
29 * @ingroup Watchlist
30 */
31 class WatchedItem {
32
33 /**
34 * @deprecated since 1.27, see User::IGNORE_USER_RIGHTS
35 */
36 const IGNORE_USER_RIGHTS = User::IGNORE_USER_RIGHTS;
37
38 /**
39 * @deprecated since 1.27, see User::CHECK_USER_RIGHTS
40 */
41 const CHECK_USER_RIGHTS = User::CHECK_USER_RIGHTS;
42
43 /**
44 * @deprecated Internal class use only
45 */
46 const DEPRECATED_USAGE_TIMESTAMP = -100;
47
48 /**
49 * @var bool
50 * @deprecated Internal class use only
51 */
52 public $checkRights = User::CHECK_USER_RIGHTS;
53
54 /**
55 * @var Title
56 * @deprecated Internal class use only
57 */
58 private $title;
59
60 /**
61 * @var LinkTarget
62 */
63 private $linkTarget;
64
65 /**
66 * @var User
67 */
68 private $user;
69
70 /**
71 * @var null|string the value of the wl_notificationtimestamp field
72 */
73 private $notificationTimestamp;
74
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
80 */
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;
92 }
93 }
94
95 /**
96 * @return User
97 */
98 public function getUser() {
99 return $this->user;
100 }
101
102 /**
103 * @return LinkTarget
104 */
105 public function getLinkTarget() {
106 return $this->linkTarget;
107 }
108
109 /**
110 * Get the notification timestamp of this entry.
111 *
112 * @return bool|null|string
113 */
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;
120 }
121 $item = WatchedItemStore::getDefaultInstance()
122 ->loadWatchedItem( $this->user, $this->linkTarget );
123 if ( $item ) {
124 $this->notificationTimestamp = $item->getNotificationTimestamp();
125 } else {
126 $this->notificationTimestamp = false;
127 }
128 }
129 return $this->notificationTimestamp;
130 }
131
132 /**
133 * Back compat pre 1.27 with the WatchedItemStore introduction
134 * @todo remove in 1.28/9
135 * -------------------------------------------------
136 */
137
138 /**
139 * @return Title
140 * @deprecated Internal class use only
141 */
142 public function getTitle() {
143 if ( !$this->title ) {
144 $this->title = Title::newFromLinkTarget( $this->linkTarget );
145 }
146 return $this->title;
147 }
148
149 /**
150 * @deprecated since 1.27 Use the constructor, WatchedItemStore::getWatchedItem()
151 * or WatchedItemStore::loadWatchedItem()
152 */
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 );
156 }
157
158 /**
159 * @deprecated since 1.27 Use WatchedItemStore::resetNotificationTimestamp()
160 */
161 public function resetNotificationTimestamp( $force = '', $oldid = 0 ) {
162 // wfDeprecated( __METHOD__, '1.27' );
163 if ( $this->checkRights && !$this->user->isAllowed( 'editmywatchlist' ) ) {
164 return;
165 }
166 WatchedItemStore::getDefaultInstance()->resetNotificationTimestamp(
167 $this->user,
168 $this->getTitle(),
169 $force,
170 $oldid
171 );
172 }
173
174 /**
175 * @deprecated since 1.27 Use WatchedItemStore::addWatchBatch()
176 */
177 public static function batchAddWatch( array $items ) {
178 // wfDeprecated( __METHOD__, '1.27' );
179 if ( !$items ) {
180 return false;
181 }
182
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;
190 }
191 $userId = $user->getId();
192 $users[$userId] = $user;
193 $targets[$userId][] = $watchedItem->getTitle()->getSubjectPage();
194 $targets[$userId][] = $watchedItem->getTitle()->getTalkPage();
195 }
196
197 $store = WatchedItemStore::getDefaultInstance();
198 $success = true;
199 foreach ( $users as $userId => $user ) {
200 $success &= $store->addWatchBatchForUser( $user, $targets[$userId] );
201 }
202
203 return $success;
204 }
205
206 /**
207 * @deprecated since 1.27 Use User::addWatch()
208 * @return bool
209 */
210 public function addWatch() {
211 // wfDeprecated( __METHOD__, '1.27' );
212 $this->user->addWatch( $this->getTitle(), $this->checkRights );
213 return true;
214 }
215
216 /**
217 * @deprecated since 1.27 Use User::removeWatch()
218 * @return bool
219 */
220 public function removeWatch() {
221 // wfDeprecated( __METHOD__, '1.27' );
222 if ( $this->checkRights && !$this->user->isAllowed( 'editmywatchlist' ) ) {
223 return false;
224 }
225 $this->user->removeWatch( $this->getTitle(), $this->checkRights );
226 return true;
227 }
228
229 /**
230 * @deprecated since 1.27 Use User::isWatched()
231 * @return bool
232 */
233 public function isWatched() {
234 // wfDeprecated( __METHOD__, '1.27' );
235 return $this->user->isWatched( $this->getTitle(), $this->checkRights );
236 }
237
238 /**
239 * @deprecated since 1.27 Use WatchedItemStore::duplicateAllAssociatedEntries()
240 */
241 public static function duplicateEntries( Title $oldTitle, Title $newTitle ) {
242 // wfDeprecated( __METHOD__, '1.27' );
243 $store = WatchedItemStore::getDefaultInstance();
244 $store->duplicateAllAssociatedEntries( $oldTitle, $newTitle );
245 }
246
247 }