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
25 * Representation of a pair of user and title for watchlist entries.
40 private $loaded = false;
49 * Constant to specify that user rights 'editmywatchlist' and
50 * 'viewmywatchlist' should not be checked.
53 const IGNORE_USER_RIGHTS
= 0;
56 * Constant to specify that user rights 'editmywatchlist' and
57 * 'viewmywatchlist' should be checked.
60 const CHECK_USER_RIGHTS
= 1;
63 * Do DB master updates right now
68 * Do DB master updates via the job queue
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.
82 public static function fromUserTitle( $user, $title,
83 $checkRights = WatchedItem
::CHECK_USER_RIGHTS
85 $wl = new WatchedItem
;
88 $wl->mCheckRights
= $checkRights;
97 protected function getTitle() {
102 * Helper to retrieve the title namespace
105 protected function getTitleNs() {
106 return $this->getTitle()->getNamespace();
110 * Helper to retrieve the title DBkey
113 protected function getTitleDBkey() {
114 return $this->getTitle()->getDBkey();
118 * Helper to retrieve the user id
121 protected function getUserId() {
122 return $this->mUser
->getId();
126 * Return an array of conditions to select or update the appropriate database
131 private function dbCond() {
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
) {
146 $this->loaded
= true;
148 // Only loggedin user can have a watchlist
149 if ( $this->mUser
->isAnon() ) {
150 $this->watched
= false;
154 // some pages cannot be watched
155 if ( !$this->getTitle()->isWatchable() ) {
156 $this->watched
= false;
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;
170 $this->watched
= true;
171 $this->timestamp
= $row->wl_notificationtimestamp
;
177 * @param string $what 'viewmywatchlist' or 'editmywatchlist'
180 private function isAllowed( $what ) {
181 return !$this->mCheckRights ||
$this->mUser
->isAllowed( $what );
185 * Is mTitle being watched by mUser?
188 public function isWatched() {
189 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
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' ) ) {
209 if ( $this->watched
) {
210 return $this->timestamp
;
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' ) ) {
232 if ( $force != 'force' ) {
234 if ( !$this->watched ||
$this->timestamp
=== null ) {
239 $title = $this->getTitle();
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;
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.
251 if ( $this->timestamp
=== null ) {
252 // This can only happen if $force is enabled.
253 $notificationTimestamp = null;
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' ) {
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(
280 'type' => 'updateWatchlistNotification',
281 'userid' => $this->getUserId(),
282 'notifTime' => $notificationTimestamp,
286 // Try to run this post-send
287 DeferredUpdates
::addCallableUpdate( function() use ( $job ) {
291 $dbw = wfGetDB( DB_MASTER
);
292 $dbw->update( 'watchlist',
293 array( 'wl_notificationtimestamp' => $dbw->timestampOrNull( $notificationTimestamp ) ),
299 $this->timestamp
= null;
303 * @param WatchedItem[] $items
306 public static function batchAddWatch( array $items ) {
308 if ( wfReadOnly() ) {
313 foreach ( $items as $item ) {
314 // Only loggedin user can have a watchlist
315 if ( $item->mUser
->isAnon() ||
!$item->isAllowed( 'editmywatchlist' ) ) {
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:
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;
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' );
350 * Given a title and user (assumes the object is setup), add the watch to the database.
353 public function addWatch() {
354 return self
::batchAddWatch( array( $this ) );
358 * Same as addWatch, only the opposite.
361 public function removeWatch() {
363 // Only loggedin user can have a watchlist
364 if ( wfReadOnly() ||
$this->mUser
->isAnon() ||
!$this->isAllowed( 'editmywatchlist' ) ) {
369 $dbw = wfGetDB( DB_MASTER
);
370 $dbw->delete( 'watchlist',
372 'wl_user' => $this->getUserId(),
373 'wl_namespace' => MWNamespace
::getSubject( $this->getTitleNs() ),
374 'wl_title' => $this->getTitleDBkey(),
377 if ( $dbw->affectedRows() ) {
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',
387 'wl_user' => $this->getUserId(),
388 'wl_namespace' => MWNamespace
::getTalk( $this->getTitleNs() ),
389 'wl_title' => $this->getTitleDBkey(),
393 if ( $dbw->affectedRows() ) {
397 $this->watched
= false;
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().
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
436 foreach ( $res as $s ) {
438 'wl_user' => $s->wl_user
,
439 'wl_namespace' => $newnamespace,
440 'wl_title' => $newtitle,
441 'wl_notificationtimestamp' => $s->wl_notificationtimestamp
,
445 if ( empty( $values ) ) {
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
455 array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ),