Merge "build: Ignore phpcs in /skins but not /includes/skins"
[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 Wikimedia\Assert\Assert;
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 if ( $this->linkTarget instanceof Title ) {
145 $this->title = $this->linkTarget;
146 } else {
147 $this->title = Title::newFromLinkTarget( $this->linkTarget );
148 }
149 }
150 return $this->title;
151 }
152
153 /**
154 * @deprecated since 1.27 Use the constructor, WatchedItemStore::getWatchedItem()
155 * or WatchedItemStore::loadWatchedItem()
156 */
157 public static function fromUserTitle( $user, $title, $checkRights = User::CHECK_USER_RIGHTS ) {
158 // wfDeprecated( __METHOD__, '1.27' );
159 return new self( $user, $title, self::DEPRECATED_USAGE_TIMESTAMP, (bool)$checkRights );
160 }
161
162 /**
163 * @deprecated since 1.27 Use WatchedItemStore::resetNotificationTimestamp()
164 */
165 public function resetNotificationTimestamp( $force = '', $oldid = 0 ) {
166 // wfDeprecated( __METHOD__, '1.27' );
167 if ( $this->checkRights && !$this->user->isAllowed( 'editmywatchlist' ) ) {
168 return;
169 }
170 WatchedItemStore::getDefaultInstance()->resetNotificationTimestamp(
171 $this->user,
172 $this->getTitle(),
173 $force,
174 $oldid
175 );
176 }
177
178 /**
179 * @deprecated since 1.27 Use WatchedItemStore::addWatchBatch()
180 */
181 public static function batchAddWatch( array $items ) {
182 // wfDeprecated( __METHOD__, '1.27' );
183 $userTargetCombinations = [];
184 /** @var WatchedItem $watchedItem */
185 foreach ( $items as $watchedItem ) {
186 if ( $watchedItem->checkRights && !$watchedItem->getUser()->isAllowed( 'editmywatchlist' ) ) {
187 continue;
188 }
189 $userTargetCombinations[] = [
190 $watchedItem->getUser(),
191 $watchedItem->getTitle()->getSubjectPage()
192 ];
193 $userTargetCombinations[] = [
194 $watchedItem->getUser(),
195 $watchedItem->getTitle()->getTalkPage()
196 ];
197 }
198 $store = WatchedItemStore::getDefaultInstance();
199 return $store->addWatchBatch( $userTargetCombinations );
200 }
201
202 /**
203 * @deprecated since 1.27 Use User::addWatch()
204 * @return bool
205 */
206 public function addWatch() {
207 // wfDeprecated( __METHOD__, '1.27' );
208 $this->user->addWatch( $this->getTitle(), $this->checkRights );
209 return true;
210 }
211
212 /**
213 * @deprecated since 1.27 Use User::removeWatch()
214 * @return bool
215 */
216 public function removeWatch() {
217 // wfDeprecated( __METHOD__, '1.27' );
218 if ( $this->checkRights && !$this->user->isAllowed( 'editmywatchlist' ) ) {
219 return false;
220 }
221 $this->user->removeWatch( $this->getTitle(), $this->checkRights );
222 return true;
223 }
224
225 /**
226 * @deprecated since 1.27 Use User::isWatched()
227 * @return bool
228 */
229 public function isWatched() {
230 // wfDeprecated( __METHOD__, '1.27' );
231 return $this->user->isWatched( $this->getTitle(), $this->checkRights );
232 }
233
234 /**
235 * @deprecated since 1.27 Use WatchedItemStore::duplicateAllAssociatedEntries()
236 */
237 public static function duplicateEntries( Title $oldTitle, Title $newTitle ) {
238 // wfDeprecated( __METHOD__, '1.27' );
239 $store = WatchedItemStore::getDefaultInstance();
240 $store->duplicateAllAssociatedEntries( $oldTitle, $newTitle );
241 }
242
243 }