SessionManager: Change behavior of getSessionById()
[mediawiki.git] / includes / WatchedItem.php
blob0ef2373783de4715f5521606532462d466e2285c
1 <?php
2 /**
3 * Accessor and mutator for watchlist entries.
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.
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.
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
20 * @file
21 * @ingroup Watchlist
24 /**
25 * Representation of a pair of user and title for watchlist entries.
27 * @ingroup Watchlist
29 class WatchedItem {
30 /** @var Title */
31 public $mTitle;
33 /** @var User */
34 public $mUser;
36 /** @var int */
37 public $mCheckRights;
39 /** @var bool */
40 private $loaded = false;
42 /** @var bool */
43 private $watched;
45 /** @var string */
46 private $timestamp;
48 /**
49 * Constant to specify that user rights 'editmywatchlist' and
50 * 'viewmywatchlist' should not be checked.
51 * @since 1.22
53 const IGNORE_USER_RIGHTS = 0;
55 /**
56 * Constant to specify that user rights 'editmywatchlist' and
57 * 'viewmywatchlist' should be checked.
58 * @since 1.22
60 const CHECK_USER_RIGHTS = 1;
62 /**
63 * Do DB master updates right now
64 * @since 1.26
66 const IMMEDIATE = 0;
67 /**
68 * Do DB master updates via the job queue
69 * @since 1.26
71 const DEFERRED = 1;
73 /**
74 * Create a WatchedItem object with the given user and title
75 * @since 1.22 $checkRights parameter added
76 * @param User $user The user to use for (un)watching
77 * @param Title $title The title we're going to (un)watch
78 * @param int $checkRights Whether to check the 'viewmywatchlist' and 'editmywatchlist' rights.
79 * Pass either WatchedItem::IGNORE_USER_RIGHTS or WatchedItem::CHECK_USER_RIGHTS.
80 * @return WatchedItem
82 public static function fromUserTitle( $user, $title,
83 $checkRights = WatchedItem::CHECK_USER_RIGHTS
84 ) {
85 $wl = new WatchedItem;
86 $wl->mUser = $user;
87 $wl->mTitle = $title;
88 $wl->mCheckRights = $checkRights;
90 return $wl;
93 /**
94 * Title being watched
95 * @return Title
97 protected function getTitle() {
98 return $this->mTitle;
102 * Helper to retrieve the title namespace
103 * @return int
105 protected function getTitleNs() {
106 return $this->getTitle()->getNamespace();
110 * Helper to retrieve the title DBkey
111 * @return string
113 protected function getTitleDBkey() {
114 return $this->getTitle()->getDBkey();
118 * Helper to retrieve the user id
119 * @return int
121 protected function getUserId() {
122 return $this->mUser->getId();
126 * Return an array of conditions to select or update the appropriate database
127 * row.
129 * @return array
131 private function dbCond() {
132 return array(
133 'wl_user' => $this->getUserId(),
134 'wl_namespace' => $this->getTitleNs(),
135 'wl_title' => $this->getTitleDBkey(),
140 * Load the object from the database
142 private function load() {
143 if ( $this->loaded ) {
144 return;
146 $this->loaded = true;
148 // Only loggedin user can have a watchlist
149 if ( $this->mUser->isAnon() ) {
150 $this->watched = false;
151 return;
154 // some pages cannot be watched
155 if ( !$this->getTitle()->isWatchable() ) {
156 $this->watched = false;
157 return;
160 # Pages and their talk pages are considered equivalent for watching;
161 # remember that talk namespaces are numbered as page namespace+1.
163 $dbr = wfGetDB( DB_SLAVE );
164 $row = $dbr->selectRow( 'watchlist', 'wl_notificationtimestamp',
165 $this->dbCond(), __METHOD__ );
167 if ( $row === false ) {
168 $this->watched = false;
169 } else {
170 $this->watched = true;
171 $this->timestamp = $row->wl_notificationtimestamp;
176 * Check permissions
177 * @param string $what 'viewmywatchlist' or 'editmywatchlist'
178 * @return bool
180 private function isAllowed( $what ) {
181 return !$this->mCheckRights || $this->mUser->isAllowed( $what );
185 * Is mTitle being watched by mUser?
186 * @return bool
188 public function isWatched() {
189 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
190 return false;
193 $this->load();
194 return $this->watched;
198 * Get the notification timestamp of this entry.
200 * @return bool|null|string False if the page is not watched, the value of
201 * the wl_notificationtimestamp field otherwise
203 public function getNotificationTimestamp() {
204 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
205 return false;
208 $this->load();
209 if ( $this->watched ) {
210 return $this->timestamp;
211 } else {
212 return false;
217 * Reset the notification timestamp of this entry
219 * @param bool $force Whether to force the write query to be executed even if the
220 * page is not watched or the notification timestamp is already NULL.
221 * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed.
222 * @mode int $mode WatchedItem::DEFERRED/IMMEDIATE
224 public function resetNotificationTimestamp(
225 $force = '', $oldid = 0, $mode = self::IMMEDIATE
227 // Only loggedin user can have a watchlist
228 if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
229 return;
232 if ( $force != 'force' ) {
233 $this->load();
234 if ( !$this->watched || $this->timestamp === null ) {
235 return;
239 $title = $this->getTitle();
240 if ( !$oldid ) {
241 // No oldid given, assuming latest revision; clear the timestamp.
242 $notificationTimestamp = null;
243 } elseif ( !$title->getNextRevisionID( $oldid ) ) {
244 // Oldid given and is the latest revision for this title; clear the timestamp.
245 $notificationTimestamp = null;
246 } else {
247 // See if the version marked as read is more recent than the one we're viewing.
248 // Call load() if it wasn't called before due to $force.
249 $this->load();
251 if ( $this->timestamp === null ) {
252 // This can only happen if $force is enabled.
253 $notificationTimestamp = null;
254 } else {
255 // Oldid given and isn't the latest; update the timestamp.
256 // This will result in no further notification emails being sent!
257 $notificationTimestamp = Revision::getTimestampFromId( $title, $oldid );
258 // We need to go one second to the future because of various strict comparisons
259 // throughout the codebase
260 $ts = new MWTimestamp( $notificationTimestamp );
261 $ts->timestamp->add( new DateInterval( 'PT1S' ) );
262 $notificationTimestamp = $ts->getTimestamp( TS_MW );
264 if ( $notificationTimestamp < $this->timestamp ) {
265 if ( $force != 'force' ) {
266 return;
267 } else {
268 // This is a little silly…
269 $notificationTimestamp = $this->timestamp;
275 // If the page is watched by the user (or may be watched), update the timestamp
276 if ( $mode === self::DEFERRED ) {
277 $job = new ActivityUpdateJob(
278 $title,
279 array(
280 'type' => 'updateWatchlistNotification',
281 'userid' => $this->getUserId(),
282 'notifTime' => $notificationTimestamp,
283 'curTime' => time()
286 // Try to run this post-send
287 DeferredUpdates::addCallableUpdate( function() use ( $job ) {
288 $job->run();
289 } );
290 } else {
291 $dbw = wfGetDB( DB_MASTER );
292 $dbw->update( 'watchlist',
293 array( 'wl_notificationtimestamp' => $dbw->timestampOrNull( $notificationTimestamp ) ),
294 $this->dbCond(),
295 __METHOD__
299 $this->timestamp = null;
303 * @param WatchedItem[] $items
304 * @return bool
306 public static function batchAddWatch( array $items ) {
308 if ( wfReadOnly() ) {
309 return false;
312 $rows = array();
313 foreach ( $items as $item ) {
314 // Only loggedin user can have a watchlist
315 if ( $item->mUser->isAnon() || !$item->isAllowed( 'editmywatchlist' ) ) {
316 continue;
318 $rows[] = array(
319 'wl_user' => $item->getUserId(),
320 'wl_namespace' => MWNamespace::getSubject( $item->getTitleNs() ),
321 'wl_title' => $item->getTitleDBkey(),
322 'wl_notificationtimestamp' => null,
324 // Every single watched page needs now to be listed in watchlist;
325 // namespace:page and namespace_talk:page need separate entries:
326 $rows[] = array(
327 'wl_user' => $item->getUserId(),
328 'wl_namespace' => MWNamespace::getTalk( $item->getTitleNs() ),
329 'wl_title' => $item->getTitleDBkey(),
330 'wl_notificationtimestamp' => null
332 $item->watched = true;
335 if ( !$rows ) {
336 return false;
339 $dbw = wfGetDB( DB_MASTER );
340 foreach ( array_chunk( $rows, 100 ) as $toInsert ) {
341 // Use INSERT IGNORE to avoid overwriting the notification timestamp
342 // if there's already an entry for this page
343 $dbw->insert( 'watchlist', $toInsert, __METHOD__, 'IGNORE' );
346 return true;
350 * Given a title and user (assumes the object is setup), add the watch to the database.
351 * @return bool
353 public function addWatch() {
354 return self::batchAddWatch( array( $this ) );
358 * Same as addWatch, only the opposite.
359 * @return bool
361 public function removeWatch() {
363 // Only loggedin user can have a watchlist
364 if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
365 return false;
368 $success = false;
369 $dbw = wfGetDB( DB_MASTER );
370 $dbw->delete( 'watchlist',
371 array(
372 'wl_user' => $this->getUserId(),
373 'wl_namespace' => MWNamespace::getSubject( $this->getTitleNs() ),
374 'wl_title' => $this->getTitleDBkey(),
375 ), __METHOD__
377 if ( $dbw->affectedRows() ) {
378 $success = true;
381 # the following code compensates the new behavior, introduced by the
382 # enotif patch, that every single watched page needs now to be listed
383 # in watchlist namespace:page and namespace_talk:page had separate
384 # entries: clear them
385 $dbw->delete( 'watchlist',
386 array(
387 'wl_user' => $this->getUserId(),
388 'wl_namespace' => MWNamespace::getTalk( $this->getTitleNs() ),
389 'wl_title' => $this->getTitleDBkey(),
390 ), __METHOD__
393 if ( $dbw->affectedRows() ) {
394 $success = true;
397 $this->watched = false;
399 return $success;
403 * Check if the given title already is watched by the user, and if so
404 * add watches on a new title. To be used for page renames and such.
406 * @param Title $ot Page title to duplicate entries from, if present
407 * @param Title $nt Page title to add watches on
409 public static function duplicateEntries( $ot, $nt ) {
410 WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
411 WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
415 * Handle duplicate entries. Backend for duplicateEntries().
417 * @param Title $ot
418 * @param Title $nt
420 * @return bool
422 private static function doDuplicateEntries( $ot, $nt ) {
423 $oldnamespace = $ot->getNamespace();
424 $newnamespace = $nt->getNamespace();
425 $oldtitle = $ot->getDBkey();
426 $newtitle = $nt->getDBkey();
428 $dbw = wfGetDB( DB_MASTER );
429 $res = $dbw->select( 'watchlist',
430 array( 'wl_user', 'wl_notificationtimestamp' ),
431 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
432 __METHOD__, 'FOR UPDATE'
434 # Construct array to replace into the watchlist
435 $values = array();
436 foreach ( $res as $s ) {
437 $values[] = array(
438 'wl_user' => $s->wl_user,
439 'wl_namespace' => $newnamespace,
440 'wl_title' => $newtitle,
441 'wl_notificationtimestamp' => $s->wl_notificationtimestamp,
445 if ( empty( $values ) ) {
446 // Nothing to do
447 return true;
450 # Perform replace
451 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
452 # some other DBMSes, mostly due to poor simulation by us
453 $dbw->replace(
454 'watchlist',
455 array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ),
456 $values,
457 __METHOD__
460 return true;