Merge "Add LoadBalancer::safeWaitForPos()"
[lhc/web/wiklou.git] / includes / WatchedItem.php
1 <?php
2 /**
3 * Accessor and mutator for watchlist entries.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Watchlist
22 */
23
24 /**
25 * Representation of a pair of user and title for watchlist entries.
26 *
27 * @ingroup Watchlist
28 */
29 class WatchedItem {
30 /** @var Title */
31 private $mTitle;
32
33 /** @var User */
34 private $mUser;
35
36 /** @var int */
37 private $mCheckRights;
38
39 /** @var bool */
40 private $loaded = false;
41
42 /** @var bool */
43 private $watched;
44
45 /** @var string */
46 private $timestamp;
47
48 /**
49 * Constant to specify that user rights 'editmywatchlist' and
50 * 'viewmywatchlist' should not be checked.
51 * @since 1.22
52 */
53 const IGNORE_USER_RIGHTS = 0;
54
55 /**
56 * Constant to specify that user rights 'editmywatchlist' and
57 * 'viewmywatchlist' should be checked.
58 * @since 1.22
59 */
60 const CHECK_USER_RIGHTS = 1;
61
62 /**
63 * Create a WatchedItem object with the given user and title
64 * @since 1.22 $checkRights parameter added
65 * @param User $user The user to use for (un)watching
66 * @param Title $title The title we're going to (un)watch
67 * @param int $checkRights Whether to check the 'viewmywatchlist' and 'editmywatchlist' rights.
68 * Pass either WatchedItem::IGNORE_USER_RIGHTS or WatchedItem::CHECK_USER_RIGHTS.
69 * @return WatchedItem
70 */
71 public static function fromUserTitle( $user, $title,
72 $checkRights = WatchedItem::CHECK_USER_RIGHTS
73 ) {
74 $wl = new WatchedItem;
75 $wl->mUser = $user;
76 $wl->mTitle = $title;
77 $wl->mCheckRights = $checkRights;
78
79 return $wl;
80 }
81
82 /**
83 * Title being watched
84 * @return Title
85 */
86 protected function getTitle() {
87 return $this->mTitle;
88 }
89
90 /**
91 * Helper to retrieve the title namespace
92 * @return int
93 */
94 protected function getTitleNs() {
95 return $this->getTitle()->getNamespace();
96 }
97
98 /**
99 * Helper to retrieve the title DBkey
100 * @return string
101 */
102 protected function getTitleDBkey() {
103 return $this->getTitle()->getDBkey();
104 }
105
106 /**
107 * Helper to retrieve the user id
108 * @return int
109 */
110 protected function getUserId() {
111 return $this->mUser->getId();
112 }
113
114 /**
115 * Return an array of conditions to select or update the appropriate database
116 * row.
117 *
118 * @return array
119 */
120 private function dbCond() {
121 return array(
122 'wl_user' => $this->getUserId(),
123 'wl_namespace' => $this->getTitleNs(),
124 'wl_title' => $this->getTitleDBkey(),
125 );
126 }
127
128 /**
129 * Load the object from the database
130 */
131 private function load() {
132 if ( $this->loaded ) {
133 return;
134 }
135 $this->loaded = true;
136
137 // Only loggedin user can have a watchlist
138 if ( $this->mUser->isAnon() ) {
139 $this->watched = false;
140 return;
141 }
142
143 // some pages cannot be watched
144 if ( !$this->getTitle()->isWatchable() ) {
145 $this->watched = false;
146 return;
147 }
148
149 # Pages and their talk pages are considered equivalent for watching;
150 # remember that talk namespaces are numbered as page namespace+1.
151
152 $dbr = wfGetDB( DB_SLAVE );
153 $row = $dbr->selectRow( 'watchlist', 'wl_notificationtimestamp',
154 $this->dbCond(), __METHOD__ );
155
156 if ( $row === false ) {
157 $this->watched = false;
158 } else {
159 $this->watched = true;
160 $this->timestamp = $row->wl_notificationtimestamp;
161 }
162 }
163
164 /**
165 * Check permissions
166 * @param string $what 'viewmywatchlist' or 'editmywatchlist'
167 * @return bool
168 */
169 private function isAllowed( $what ) {
170 return !$this->mCheckRights || $this->mUser->isAllowed( $what );
171 }
172
173 /**
174 * Is mTitle being watched by mUser?
175 * @return bool
176 */
177 public function isWatched() {
178 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
179 return false;
180 }
181
182 $this->load();
183 return $this->watched;
184 }
185
186 /**
187 * Get the notification timestamp of this entry.
188 *
189 * @return bool|null|string False if the page is not watched, the value of
190 * the wl_notificationtimestamp field otherwise
191 */
192 public function getNotificationTimestamp() {
193 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
194 return false;
195 }
196
197 $this->load();
198 if ( $this->watched ) {
199 return $this->timestamp;
200 } else {
201 return false;
202 }
203 }
204
205 /**
206 * Reset the notification timestamp of this entry
207 *
208 * @param bool $force Whether to force the write query to be executed even if the
209 * page is not watched or the notification timestamp is already NULL.
210 * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed.
211 */
212 public function resetNotificationTimestamp(
213 $force = '', $oldid = 0
214 ) {
215 // Only loggedin user can have a watchlist
216 if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
217 return;
218 }
219
220 if ( $force != 'force' ) {
221 $this->load();
222 if ( !$this->watched || $this->timestamp === null ) {
223 return;
224 }
225 }
226
227 $title = $this->getTitle();
228 if ( !$oldid ) {
229 // No oldid given, assuming latest revision; clear the timestamp.
230 $notificationTimestamp = null;
231 } elseif ( !$title->getNextRevisionID( $oldid ) ) {
232 // Oldid given and is the latest revision for this title; clear the timestamp.
233 $notificationTimestamp = null;
234 } else {
235 // See if the version marked as read is more recent than the one we're viewing.
236 // Call load() if it wasn't called before due to $force.
237 $this->load();
238
239 if ( $this->timestamp === null ) {
240 // This can only happen if $force is enabled.
241 $notificationTimestamp = null;
242 } else {
243 // Oldid given and isn't the latest; update the timestamp.
244 // This will result in no further notification emails being sent!
245 $notificationTimestamp = Revision::getTimestampFromId( $title, $oldid );
246 // We need to go one second to the future because of various strict comparisons
247 // throughout the codebase
248 $ts = new MWTimestamp( $notificationTimestamp );
249 $ts->timestamp->add( new DateInterval( 'PT1S' ) );
250 $notificationTimestamp = $ts->getTimestamp( TS_MW );
251
252 if ( $notificationTimestamp < $this->timestamp ) {
253 if ( $force != 'force' ) {
254 return;
255 } else {
256 // This is a little silly…
257 $notificationTimestamp = $this->timestamp;
258 }
259 }
260 }
261 }
262
263 // If the page is watched by the user (or may be watched), update the timestamp
264 $job = new ActivityUpdateJob(
265 $title,
266 array(
267 'type' => 'updateWatchlistNotification',
268 'userid' => $this->getUserId(),
269 'notifTime' => $notificationTimestamp,
270 'curTime' => time()
271 )
272 );
273 // Try to run this post-send
274 DeferredUpdates::addCallableUpdate( function() use ( $job ) {
275 $job->run();
276 } );
277
278 $this->timestamp = null;
279 }
280
281 /**
282 * @param WatchedItem[] $items
283 * @return bool
284 */
285 public static function batchAddWatch( array $items ) {
286
287 if ( wfReadOnly() ) {
288 return false;
289 }
290
291 $rows = array();
292 foreach ( $items as $item ) {
293 // Only loggedin user can have a watchlist
294 if ( $item->mUser->isAnon() || !$item->isAllowed( 'editmywatchlist' ) ) {
295 continue;
296 }
297 $rows[] = array(
298 'wl_user' => $item->getUserId(),
299 'wl_namespace' => MWNamespace::getSubject( $item->getTitleNs() ),
300 'wl_title' => $item->getTitleDBkey(),
301 'wl_notificationtimestamp' => null,
302 );
303 // Every single watched page needs now to be listed in watchlist;
304 // namespace:page and namespace_talk:page need separate entries:
305 $rows[] = array(
306 'wl_user' => $item->getUserId(),
307 'wl_namespace' => MWNamespace::getTalk( $item->getTitleNs() ),
308 'wl_title' => $item->getTitleDBkey(),
309 'wl_notificationtimestamp' => null
310 );
311 $item->watched = true;
312 }
313
314 if ( !$rows ) {
315 return false;
316 }
317
318 $dbw = wfGetDB( DB_MASTER );
319 foreach ( array_chunk( $rows, 100 ) as $toInsert ) {
320 // Use INSERT IGNORE to avoid overwriting the notification timestamp
321 // if there's already an entry for this page
322 $dbw->insert( 'watchlist', $toInsert, __METHOD__, 'IGNORE' );
323 }
324
325 return true;
326 }
327
328 /**
329 * Given a title and user (assumes the object is setup), add the watch to the database.
330 * @return bool
331 */
332 public function addWatch() {
333 return self::batchAddWatch( array( $this ) );
334 }
335
336 /**
337 * Same as addWatch, only the opposite.
338 * @return bool
339 */
340 public function removeWatch() {
341
342 // Only loggedin user can have a watchlist
343 if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
344 return false;
345 }
346
347 $success = false;
348 $dbw = wfGetDB( DB_MASTER );
349 $dbw->delete( 'watchlist',
350 array(
351 'wl_user' => $this->getUserId(),
352 'wl_namespace' => MWNamespace::getSubject( $this->getTitleNs() ),
353 'wl_title' => $this->getTitleDBkey(),
354 ), __METHOD__
355 );
356 if ( $dbw->affectedRows() ) {
357 $success = true;
358 }
359
360 # the following code compensates the new behavior, introduced by the
361 # enotif patch, that every single watched page needs now to be listed
362 # in watchlist namespace:page and namespace_talk:page had separate
363 # entries: clear them
364 $dbw->delete( 'watchlist',
365 array(
366 'wl_user' => $this->getUserId(),
367 'wl_namespace' => MWNamespace::getTalk( $this->getTitleNs() ),
368 'wl_title' => $this->getTitleDBkey(),
369 ), __METHOD__
370 );
371
372 if ( $dbw->affectedRows() ) {
373 $success = true;
374 }
375
376 $this->watched = false;
377
378 return $success;
379 }
380
381 /**
382 * Check if the given title already is watched by the user, and if so
383 * add watches on a new title. To be used for page renames and such.
384 *
385 * @param Title $ot Page title to duplicate entries from, if present
386 * @param Title $nt Page title to add watches on
387 */
388 public static function duplicateEntries( $ot, $nt ) {
389 WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
390 WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
391 }
392
393 /**
394 * Handle duplicate entries. Backend for duplicateEntries().
395 *
396 * @param Title $ot
397 * @param Title $nt
398 *
399 * @return bool
400 */
401 private static function doDuplicateEntries( $ot, $nt ) {
402 $oldnamespace = $ot->getNamespace();
403 $newnamespace = $nt->getNamespace();
404 $oldtitle = $ot->getDBkey();
405 $newtitle = $nt->getDBkey();
406
407 $dbw = wfGetDB( DB_MASTER );
408 $res = $dbw->select( 'watchlist',
409 array( 'wl_user', 'wl_notificationtimestamp' ),
410 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
411 __METHOD__, 'FOR UPDATE'
412 );
413 # Construct array to replace into the watchlist
414 $values = array();
415 foreach ( $res as $s ) {
416 $values[] = array(
417 'wl_user' => $s->wl_user,
418 'wl_namespace' => $newnamespace,
419 'wl_title' => $newtitle,
420 'wl_notificationtimestamp' => $s->wl_notificationtimestamp,
421 );
422 }
423
424 if ( empty( $values ) ) {
425 // Nothing to do
426 return true;
427 }
428
429 # Perform replace
430 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
431 # some other DBMSes, mostly due to poor simulation by us
432 $dbw->replace(
433 'watchlist',
434 array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ),
435 $values,
436 __METHOD__
437 );
438
439 return true;
440 }
441 }