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.
31 * Constant to specify that user rights 'editmywatchlist' and
32 * 'viewmywatchlist' should not be checked.
35 const IGNORE_USER_RIGHTS
= 0;
38 * Constant to specify that user rights 'editmywatchlist' and
39 * 'viewmywatchlist' should be checked.
42 const CHECK_USER_RIGHTS
= 1;
54 private $loaded = false;
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'
168 private function isAllowed( $what ) {
169 return !$this->mCheckRights ||
$this->mUser
->isAllowed( $what );
173 * Is mTitle being watched by mUser?
176 public function isWatched() {
177 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
182 return $this->watched
;
186 * Get the notification timestamp of this entry.
188 * @return bool|null|string False if the page is not watched, the value of
189 * the wl_notificationtimestamp field otherwise
191 public function getNotificationTimestamp() {
192 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
197 if ( $this->watched
) {
198 return $this->timestamp
;
205 * Reset the notification timestamp of this entry
207 * @param bool $force Whether to force the write query to be executed even if the
208 * page is not watched or the notification timestamp is already NULL.
209 * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed.
211 public function resetNotificationTimestamp( $force = '', $oldid = 0 ) {
212 // Only loggedin user can have a watchlist
213 if ( wfReadOnly() ||
$this->mUser
->isAnon() ||
!$this->isAllowed( 'editmywatchlist' ) ) {
217 if ( $force != 'force' ) {
219 if ( !$this->watched ||
$this->timestamp
=== null ) {
224 $title = $this->getTitle();
226 // No oldid given, assuming latest revision; clear the timestamp.
227 $notificationTimestamp = null;
228 } elseif ( !$title->getNextRevisionID( $oldid ) ) {
229 // Oldid given and is the latest revision for this title; clear the timestamp.
230 $notificationTimestamp = null;
232 // See if the version marked as read is more recent than the one we're viewing.
233 // Call load() if it wasn't called before due to $force.
236 if ( $this->timestamp
=== null ) {
237 // This can only happen if $force is enabled.
238 $notificationTimestamp = null;
240 // Oldid given and isn't the latest; update the timestamp.
241 // This will result in no further notification emails being sent!
242 $dbr = wfGetDB( DB_SLAVE
);
243 $notificationTimestamp = $dbr->selectField(
244 'revision', 'rev_timestamp',
245 array( 'rev_page' => $title->getArticleID(), 'rev_id' => $oldid )
247 // We need to go one second to the future because of various strict comparisons
248 // throughout the codebase
249 $ts = new MWTimestamp( $notificationTimestamp );
250 $ts->timestamp
->add( new DateInterval( 'PT1S' ) );
251 $notificationTimestamp = $ts->getTimestamp( TS_MW
);
253 if ( $notificationTimestamp < $this->timestamp
) {
254 if ( $force != 'force' ) {
257 // This is a little silly…
258 $notificationTimestamp = $this->timestamp
;
264 // If the page is watched by the user (or may be watched), update the timestamp on any
266 $dbw = wfGetDB( DB_MASTER
);
267 $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => $notificationTimestamp ),
268 $this->dbCond(), __METHOD__
);
269 $this->timestamp
= null;
273 * Given a title and user (assumes the object is setup), add the watch to the database.
276 public function addWatch() {
277 wfProfileIn( __METHOD__
);
279 // Only loggedin user can have a watchlist
280 if ( wfReadOnly() ||
$this->mUser
->isAnon() ||
!$this->isAllowed( 'editmywatchlist' ) ) {
281 wfProfileOut( __METHOD__
);
285 // Use INSERT IGNORE to avoid overwriting the notification timestamp
286 // if there's already an entry for this page
287 $dbw = wfGetDB( DB_MASTER
);
288 $dbw->insert( 'watchlist',
290 'wl_user' => $this->getUserId(),
291 'wl_namespace' => MWNamespace
::getSubject( $this->getTitleNs() ),
292 'wl_title' => $this->getTitleDBkey(),
293 'wl_notificationtimestamp' => null
294 ), __METHOD__
, 'IGNORE' );
296 // Every single watched page needs now to be listed in watchlist;
297 // namespace:page and namespace_talk:page need separate entries:
298 $dbw->insert( 'watchlist',
300 'wl_user' => $this->getUserId(),
301 'wl_namespace' => MWNamespace
::getTalk( $this->getTitleNs() ),
302 'wl_title' => $this->getTitleDBkey(),
303 'wl_notificationtimestamp' => null
304 ), __METHOD__
, 'IGNORE' );
306 $this->watched
= true;
308 wfProfileOut( __METHOD__
);
313 * Same as addWatch, only the opposite.
316 public function removeWatch() {
317 wfProfileIn( __METHOD__
);
319 // Only loggedin user can have a watchlist
320 if ( wfReadOnly() ||
$this->mUser
->isAnon() ||
!$this->isAllowed( 'editmywatchlist' ) ) {
321 wfProfileOut( __METHOD__
);
326 $dbw = wfGetDB( DB_MASTER
);
327 $dbw->delete( 'watchlist',
329 'wl_user' => $this->getUserId(),
330 'wl_namespace' => MWNamespace
::getSubject( $this->getTitleNs() ),
331 'wl_title' => $this->getTitleDBkey(),
334 if ( $dbw->affectedRows() ) {
338 # the following code compensates the new behavior, introduced by the
339 # enotif patch, that every single watched page needs now to be listed
340 # in watchlist namespace:page and namespace_talk:page had separate
341 # entries: clear them
342 $dbw->delete( 'watchlist',
344 'wl_user' => $this->getUserId(),
345 'wl_namespace' => MWNamespace
::getTalk( $this->getTitleNs() ),
346 'wl_title' => $this->getTitleDBkey(),
350 if ( $dbw->affectedRows() ) {
354 $this->watched
= false;
356 wfProfileOut( __METHOD__
);
361 * Check if the given title already is watched by the user, and if so
362 * add watches on a new title. To be used for page renames and such.
364 * @param Title $ot Page title to duplicate entries from, if present
365 * @param Title $nt Page title to add watches on
367 public static function duplicateEntries( $ot, $nt ) {
368 WatchedItem
::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
369 WatchedItem
::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
373 * Handle duplicate entries. Backend for duplicateEntries().
380 private static function doDuplicateEntries( $ot, $nt ) {
381 $oldnamespace = $ot->getNamespace();
382 $newnamespace = $nt->getNamespace();
383 $oldtitle = $ot->getDBkey();
384 $newtitle = $nt->getDBkey();
386 $dbw = wfGetDB( DB_MASTER
);
387 $res = $dbw->select( 'watchlist', 'wl_user',
388 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
389 __METHOD__
, 'FOR UPDATE'
391 # Construct array to replace into the watchlist
393 foreach ( $res as $s ) {
395 'wl_user' => $s->wl_user
,
396 'wl_namespace' => $newnamespace,
397 'wl_title' => $newtitle
401 if ( empty( $values ) ) {
407 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
408 # some other DBMSes, mostly due to poor simulation by us
411 array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ),