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.
37 private $mCheckRights;
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 * 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.
71 public static function fromUserTitle( $user, $title,
72 $checkRights = WatchedItem
::CHECK_USER_RIGHTS
74 $wl = new WatchedItem
;
77 $wl->mCheckRights
= $checkRights;
86 protected function getTitle() {
91 * Helper to retrieve the title namespace
94 protected function getTitleNs() {
95 return $this->getTitle()->getNamespace();
99 * Helper to retrieve the title DBkey
102 protected function getTitleDBkey() {
103 return $this->getTitle()->getDBkey();
107 * Helper to retrieve the user id
110 protected function getUserId() {
111 return $this->mUser
->getId();
115 * Return an array of conditions to select or update the appropriate database
120 private function dbCond() {
122 'wl_user' => $this->getUserId(),
123 'wl_namespace' => $this->getTitleNs(),
124 'wl_title' => $this->getTitleDBkey(),
129 * Load the object from the database
131 private function load() {
132 if ( $this->loaded
) {
135 $this->loaded
= true;
137 // Only loggedin user can have a watchlist
138 if ( $this->mUser
->isAnon() ) {
139 $this->watched
= false;
143 // some pages cannot be watched
144 if ( !$this->getTitle()->isWatchable() ) {
145 $this->watched
= false;
149 # Pages and their talk pages are considered equivalent for watching;
150 # remember that talk namespaces are numbered as page namespace+1.
152 $dbr = wfGetDB( DB_SLAVE
);
153 $row = $dbr->selectRow( 'watchlist', 'wl_notificationtimestamp',
154 $this->dbCond(), __METHOD__
);
156 if ( $row === false ) {
157 $this->watched
= false;
159 $this->watched
= true;
160 $this->timestamp
= $row->wl_notificationtimestamp
;
166 * @param string $what 'viewmywatchlist' or 'editmywatchlist'
169 private function isAllowed( $what ) {
170 return !$this->mCheckRights ||
$this->mUser
->isAllowed( $what );
174 * Is mTitle being watched by mUser?
177 public function isWatched() {
178 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
183 return $this->watched
;
187 * Get the notification timestamp of this entry.
189 * @return bool|null|string False if the page is not watched, the value of
190 * the wl_notificationtimestamp field otherwise
192 public function getNotificationTimestamp() {
193 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
198 if ( $this->watched
) {
199 return $this->timestamp
;
206 * Reset the notification timestamp of this entry
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.
212 public function resetNotificationTimestamp(
213 $force = '', $oldid = 0
215 // Only loggedin user can have a watchlist
216 if ( wfReadOnly() ||
$this->mUser
->isAnon() ||
!$this->isAllowed( 'editmywatchlist' ) ) {
220 if ( $force != 'force' ) {
222 if ( !$this->watched ||
$this->timestamp
=== null ) {
227 $title = $this->getTitle();
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;
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.
239 if ( $this->timestamp
=== null ) {
240 // This can only happen if $force is enabled.
241 $notificationTimestamp = null;
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
);
252 if ( $notificationTimestamp < $this->timestamp
) {
253 if ( $force != 'force' ) {
256 // This is a little silly…
257 $notificationTimestamp = $this->timestamp
;
263 // If the page is watched by the user (or may be watched), update the timestamp
264 $job = new ActivityUpdateJob(
267 'type' => 'updateWatchlistNotification',
268 'userid' => $this->getUserId(),
269 'notifTime' => $notificationTimestamp,
273 // Try to run this post-send
274 DeferredUpdates
::addCallableUpdate( function() use ( $job ) {
278 $this->timestamp
= null;
282 * @param WatchedItem[] $items
285 public static function batchAddWatch( array $items ) {
287 if ( wfReadOnly() ) {
292 foreach ( $items as $item ) {
293 // Only loggedin user can have a watchlist
294 if ( $item->mUser
->isAnon() ||
!$item->isAllowed( 'editmywatchlist' ) ) {
298 'wl_user' => $item->getUserId(),
299 'wl_namespace' => MWNamespace
::getSubject( $item->getTitleNs() ),
300 'wl_title' => $item->getTitleDBkey(),
301 'wl_notificationtimestamp' => null,
303 // Every single watched page needs now to be listed in watchlist;
304 // namespace:page and namespace_talk:page need separate entries:
306 'wl_user' => $item->getUserId(),
307 'wl_namespace' => MWNamespace
::getTalk( $item->getTitleNs() ),
308 'wl_title' => $item->getTitleDBkey(),
309 'wl_notificationtimestamp' => null
311 $item->watched
= true;
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' );
329 * Given a title and user (assumes the object is setup), add the watch to the database.
332 public function addWatch() {
333 return self
::batchAddWatch( [ $this ] );
337 * Same as addWatch, only the opposite.
340 public function removeWatch() {
342 // Only loggedin user can have a watchlist
343 if ( wfReadOnly() ||
$this->mUser
->isAnon() ||
!$this->isAllowed( 'editmywatchlist' ) ) {
348 $dbw = wfGetDB( DB_MASTER
);
349 $dbw->delete( 'watchlist',
351 'wl_user' => $this->getUserId(),
352 'wl_namespace' => MWNamespace
::getSubject( $this->getTitleNs() ),
353 'wl_title' => $this->getTitleDBkey(),
356 if ( $dbw->affectedRows() ) {
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',
366 'wl_user' => $this->getUserId(),
367 'wl_namespace' => MWNamespace
::getTalk( $this->getTitleNs() ),
368 'wl_title' => $this->getTitleDBkey(),
372 if ( $dbw->affectedRows() ) {
376 $this->watched
= false;
382 * @deprecated since 1.27. See WatchedItemStore::duplicateEntry
384 * @param Title $oldTitle
385 * @param Title $newTitle
387 public static function duplicateEntries( Title
$oldTitle, Title
$newTitle ) {
388 $store = WatchedItemStore
::getDefaultInstance();
389 $store->duplicateEntry( $oldTitle->getSubjectPage(), $newTitle->getSubjectPage() );
390 $store->duplicateEntry( $oldTitle->getTalkPage(), $newTitle->getTalkPage() );