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;
44 var $mTitle, $mUser, $mCheckRights;
45 private $loaded = false, $watched, $timestamp;
48 * Create a WatchedItem object with the given user and title
49 * @since 1.22 $checkRights parameter added
50 * @param User $user The user to use for (un)watching
51 * @param Title $title The title we're going to (un)watch
52 * @param int $checkRights Whether to check the 'viewmywatchlist' and 'editmywatchlist' rights.
53 * Pass either WatchedItem::IGNORE_USER_RIGHTS or WatchedItem::CHECK_USER_RIGHTS.
56 public static function fromUserTitle( $user, $title, $checkRights = WatchedItem
::CHECK_USER_RIGHTS
) {
57 $wl = new WatchedItem
;
60 $wl->mCheckRights
= $checkRights;
69 protected function getTitle() {
74 * Helper to retrieve the title namespace
77 protected function getTitleNs() {
78 return $this->getTitle()->getNamespace();
82 * Helper to retrieve the title DBkey
85 protected function getTitleDBkey() {
86 return $this->getTitle()->getDBkey();
90 * Helper to retrieve the user id
93 protected function getUserId() {
94 return $this->mUser
->getId();
98 * Return an array of conditions to select or update the appropriate database
103 private function dbCond() {
105 'wl_user' => $this->getUserId(),
106 'wl_namespace' => $this->getTitleNs(),
107 'wl_title' => $this->getTitleDBkey(),
112 * Load the object from the database
114 private function load() {
115 if ( $this->loaded
) {
118 $this->loaded
= true;
120 // Only loggedin user can have a watchlist
121 if ( $this->mUser
->isAnon() ) {
122 $this->watched
= false;
126 // some pages cannot be watched
127 if ( !$this->getTitle()->isWatchable() ) {
128 $this->watched
= false;
132 # Pages and their talk pages are considered equivalent for watching;
133 # remember that talk namespaces are numbered as page namespace+1.
135 $dbr = wfGetDB( DB_SLAVE
);
136 $row = $dbr->selectRow( 'watchlist', 'wl_notificationtimestamp',
137 $this->dbCond(), __METHOD__
);
139 if ( $row === false ) {
140 $this->watched
= false;
142 $this->watched
= true;
143 $this->timestamp
= $row->wl_notificationtimestamp
;
149 * @param string $what 'viewmywatchlist' or 'editmywatchlist'
151 private function isAllowed( $what ) {
152 return !$this->mCheckRights ||
$this->mUser
->isAllowed( $what );
156 * Is mTitle being watched by mUser?
159 public function isWatched() {
160 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
165 return $this->watched
;
169 * Get the notification timestamp of this entry.
171 * @return bool|null|string False if the page is not watched, the value of
172 * the wl_notificationtimestamp field otherwise
174 public function getNotificationTimestamp() {
175 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
180 if ( $this->watched
) {
181 return $this->timestamp
;
188 * Reset the notification timestamp of this entry
190 * @param bool $force Whether to force the write query to be executed even if the
191 * page is not watched or the notification timestamp is already NULL.
192 * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed.
194 public function resetNotificationTimestamp( $force = '', $oldid = 0 ) {
195 // Only loggedin user can have a watchlist
196 if ( wfReadOnly() ||
$this->mUser
->isAnon() ||
!$this->isAllowed( 'editmywatchlist' ) ) {
200 if ( $force != 'force' ) {
202 if ( !$this->watched ||
$this->timestamp
=== null ) {
207 $title = $this->getTitle();
209 // No oldid given, assuming latest revision; clear the timestamp.
210 $notificationTimestamp = null;
211 } elseif ( !$title->getNextRevisionID( $oldid ) ) {
212 // Oldid given and is the latest revision for this title; clear the timestamp.
213 $notificationTimestamp = null;
215 // See if the version marked as read is more recent than the one we're viewing.
216 // Call load() if it wasn't called before due to $force.
219 if ( $this->timestamp
=== null ) {
220 // This can only happen if $force is enabled.
221 $notificationTimestamp = null;
223 // Oldid given and isn't the latest; update the timestamp.
224 // This will result in no further notification emails being sent!
225 $dbr = wfGetDB( DB_SLAVE
);
226 $notificationTimestamp = $dbr->selectField(
227 'revision', 'rev_timestamp',
228 array( 'rev_page' => $title->getArticleID(), 'rev_id' => $oldid )
230 // We need to go one second to the future because of various strict comparisons
231 // throughout the codebase
232 $ts = new MWTimestamp( $notificationTimestamp );
233 $ts->timestamp
->add( new DateInterval( 'PT1S' ) );
234 $notificationTimestamp = $ts->getTimestamp( TS_MW
);
236 if ( $notificationTimestamp < $this->timestamp
) {
237 if ( $force != 'force' ) {
240 // This is a little silly…
241 $notificationTimestamp = $this->timestamp
;
247 // If the page is watched by the user (or may be watched), update the timestamp on any
249 $dbw = wfGetDB( DB_MASTER
);
250 $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => $notificationTimestamp ),
251 $this->dbCond(), __METHOD__
);
252 $this->timestamp
= null;
256 * Given a title and user (assumes the object is setup), add the watch to the database.
259 public function addWatch() {
260 wfProfileIn( __METHOD__
);
262 // Only loggedin user can have a watchlist
263 if ( wfReadOnly() ||
$this->mUser
->isAnon() ||
!$this->isAllowed( 'editmywatchlist' ) ) {
264 wfProfileOut( __METHOD__
);
268 // Use INSERT IGNORE to avoid overwriting the notification timestamp
269 // if there's already an entry for this page
270 $dbw = wfGetDB( DB_MASTER
);
271 $dbw->insert( 'watchlist',
273 'wl_user' => $this->getUserId(),
274 'wl_namespace' => MWNamespace
::getSubject( $this->getTitleNs() ),
275 'wl_title' => $this->getTitleDBkey(),
276 'wl_notificationtimestamp' => null
277 ), __METHOD__
, 'IGNORE' );
279 // Every single watched page needs now to be listed in watchlist;
280 // namespace:page and namespace_talk:page need separate entries:
281 $dbw->insert( 'watchlist',
283 'wl_user' => $this->getUserId(),
284 'wl_namespace' => MWNamespace
::getTalk( $this->getTitleNs() ),
285 'wl_title' => $this->getTitleDBkey(),
286 'wl_notificationtimestamp' => null
287 ), __METHOD__
, 'IGNORE' );
289 $this->watched
= true;
291 wfProfileOut( __METHOD__
);
296 * Same as addWatch, only the opposite.
299 public function removeWatch() {
300 wfProfileIn( __METHOD__
);
302 // Only loggedin user can have a watchlist
303 if ( wfReadOnly() ||
$this->mUser
->isAnon() ||
!$this->isAllowed( 'editmywatchlist' ) ) {
304 wfProfileOut( __METHOD__
);
309 $dbw = wfGetDB( DB_MASTER
);
310 $dbw->delete( 'watchlist',
312 'wl_user' => $this->getUserId(),
313 'wl_namespace' => MWNamespace
::getSubject( $this->getTitleNs() ),
314 'wl_title' => $this->getTitleDBkey(),
317 if ( $dbw->affectedRows() ) {
321 # the following code compensates the new behavior, introduced by the
322 # enotif patch, that every single watched page needs now to be listed
323 # in watchlist namespace:page and namespace_talk:page had separate
324 # entries: clear them
325 $dbw->delete( 'watchlist',
327 'wl_user' => $this->getUserId(),
328 'wl_namespace' => MWNamespace
::getTalk( $this->getTitleNs() ),
329 'wl_title' => $this->getTitleDBkey(),
333 if ( $dbw->affectedRows() ) {
337 $this->watched
= false;
339 wfProfileOut( __METHOD__
);
344 * Check if the given title already is watched by the user, and if so
345 * add watches on a new title. To be used for page renames and such.
347 * @param Title $ot Page title to duplicate entries from, if present
348 * @param Title $nt Page title to add watches on
350 public static function duplicateEntries( $ot, $nt ) {
351 WatchedItem
::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
352 WatchedItem
::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
356 * Handle duplicate entries. Backend for duplicateEntries().
363 private static function doDuplicateEntries( $ot, $nt ) {
364 $oldnamespace = $ot->getNamespace();
365 $newnamespace = $nt->getNamespace();
366 $oldtitle = $ot->getDBkey();
367 $newtitle = $nt->getDBkey();
369 $dbw = wfGetDB( DB_MASTER
);
370 $res = $dbw->select( 'watchlist', 'wl_user',
371 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
372 __METHOD__
, 'FOR UPDATE'
374 # Construct array to replace into the watchlist
376 foreach ( $res as $s ) {
378 'wl_user' => $s->wl_user
,
379 'wl_namespace' => $newnamespace,
380 'wl_title' => $newtitle
384 if ( empty( $values ) ) {
390 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
391 # some other DBMSes, mostly due to poor simulation by us
392 $dbw->replace( 'watchlist', array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ), $values, __METHOD__
);