Return jqXHR from mw.Api calls
[lhc/web/wiklou.git] / includes / WatchedItem.php
1 <?php
2 /**
3 * Accessor and mutator for watchlist entries.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Watchlist
22 */
23
24 /**
25 * Representation of a pair of user and title for watchlist entries.
26 *
27 * @ingroup Watchlist
28 */
29 class WatchedItem {
30 /**
31 * Constant to specify that user rights 'editmywatchlist' and
32 * 'viewmywatchlist' should not be checked.
33 * @since 1.22
34 */
35 const IGNORE_USER_RIGHTS = 0;
36
37 /**
38 * Constant to specify that user rights 'editmywatchlist' and
39 * 'viewmywatchlist' should be checked.
40 * @since 1.22
41 */
42 const CHECK_USER_RIGHTS = 1;
43
44 var $mTitle, $mUser, $mCheckRights;
45 private $loaded = false, $watched, $timestamp;
46
47 /**
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 $checkRights int: Whether to check the 'viewmywatchlist' and 'editmywatchlist' rights.
53 * Pass either WatchedItem::IGNORE_USER_RIGHTS or WatchedItem::CHECK_USER_RIGHTS.
54 * @return WatchedItem object
55 */
56 public static function fromUserTitle( $user, $title, $checkRights = WatchedItem::CHECK_USER_RIGHTS ) {
57 $wl = new WatchedItem;
58 $wl->mUser = $user;
59 $wl->mTitle = $title;
60 $wl->mCheckRights = $checkRights;
61
62 return $wl;
63 }
64
65 /**
66 * Title being watched
67 * @return Title
68 */
69 protected function getTitle() {
70 return $this->mTitle;
71 }
72
73 /** Helper to retrieve the title namespace */
74 protected function getTitleNs() {
75 return $this->getTitle()->getNamespace();
76 }
77
78 /** Helper to retrieve the title DBkey */
79 protected function getTitleDBkey() {
80 return $this->getTitle()->getDBkey();
81 }
82 /** Helper to retrieve the user id */
83 protected function getUserId() {
84 return $this->mUser->getId();
85 }
86
87 /**
88 * Return an array of conditions to select or update the appropriate database
89 * row.
90 *
91 * @return array
92 */
93 private function dbCond() {
94 return array(
95 'wl_user' => $this->getUserId(),
96 'wl_namespace' => $this->getTitleNs(),
97 'wl_title' => $this->getTitleDBkey(),
98 );
99 }
100
101 /**
102 * Load the object from the database
103 */
104 private function load() {
105 if ( $this->loaded ) {
106 return;
107 }
108 $this->loaded = true;
109
110 // Only loggedin user can have a watchlist
111 if ( $this->mUser->isAnon() ) {
112 $this->watched = false;
113 return;
114 }
115
116 // some pages cannot be watched
117 if ( !$this->getTitle()->isWatchable() ) {
118 $this->watched = false;
119 return;
120 }
121
122 # Pages and their talk pages are considered equivalent for watching;
123 # remember that talk namespaces are numbered as page namespace+1.
124
125 $dbr = wfGetDB( DB_SLAVE );
126 $row = $dbr->selectRow( 'watchlist', 'wl_notificationtimestamp',
127 $this->dbCond(), __METHOD__ );
128
129 if ( $row === false ) {
130 $this->watched = false;
131 } else {
132 $this->watched = true;
133 $this->timestamp = $row->wl_notificationtimestamp;
134 }
135 }
136
137 /**
138 * Check permissions
139 * @param $what string: 'viewmywatchlist' or 'editmywatchlist'
140 */
141 private function isAllowed( $what ) {
142 return !$this->mCheckRights || $this->mUser->isAllowed( $what );
143 }
144
145 /**
146 * Is mTitle being watched by mUser?
147 * @return bool
148 */
149 public function isWatched() {
150 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
151 return false;
152 }
153
154 $this->load();
155 return $this->watched;
156 }
157
158 /**
159 * Get the notification timestamp of this entry.
160 *
161 * @return false|null|string: false if the page is not watched, the value of
162 * the wl_notificationtimestamp field otherwise
163 */
164 public function getNotificationTimestamp() {
165 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
166 return false;
167 }
168
169 $this->load();
170 if ( $this->watched ) {
171 return $this->timestamp;
172 } else {
173 return false;
174 }
175 }
176
177 /**
178 * Reset the notification timestamp of this entry
179 *
180 * @param $force Whether to force the write query to be executed even if the
181 * page is not watched or the notification timestamp is already NULL.
182 * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed.
183 */
184 public function resetNotificationTimestamp( $force = '', $oldid = 0 ) {
185 // Only loggedin user can have a watchlist
186 if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
187 return;
188 }
189
190 if ( $force != 'force' ) {
191 $this->load();
192 if ( !$this->watched || $this->timestamp === null ) {
193 return;
194 }
195 }
196
197 $title = $this->getTitle();
198 if ( !$oldid ) {
199 // No oldid given, assuming latest revision; clear the timestamp.
200 $notificationTimestamp = null;
201 } elseif ( !$title->getNextRevisionID( $oldid ) ) {
202 // Oldid given and is the latest revision for this title; clear the timestamp.
203 $notificationTimestamp = null;
204 } else {
205 // See if the version marked as read is more recent than the one we're viewing.
206 // Call load() if it wasn't called before due to $force.
207 $this->load();
208
209 if ( $this->timestamp === null ) {
210 // This can only happen if $force is enabled.
211 $notificationTimestamp = null;
212 } else {
213 // Oldid given and isn't the latest; update the timestamp.
214 // This will result in no further notification emails being sent!
215 $dbr = wfGetDB( DB_SLAVE );
216 $notificationTimestamp = $dbr->selectField(
217 'revision', 'rev_timestamp',
218 array( 'rev_page' => $title->getArticleID(), 'rev_id' => $oldid )
219 );
220 // We need to go one second to the future because of various strict comparisons
221 // throughout the codebase
222 $ts = new MWTimestamp( $notificationTimestamp );
223 $ts->timestamp->add( new DateInterval( 'PT1S' ) );
224 $notificationTimestamp = $ts->getTimestamp( TS_MW );
225
226 if ( $notificationTimestamp < $this->timestamp ) {
227 if ( $force != 'force' ) {
228 return;
229 } else {
230 // This is a little silly…
231 $notificationTimestamp = $this->timestamp;
232 }
233 }
234 }
235 }
236
237 // If the page is watched by the user (or may be watched), update the timestamp on any
238 // any matching rows
239 $dbw = wfGetDB( DB_MASTER );
240 $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => $notificationTimestamp ),
241 $this->dbCond(), __METHOD__ );
242 $this->timestamp = null;
243 }
244
245 /**
246 * Given a title and user (assumes the object is setup), add the watch to the
247 * database.
248 * @return bool
249 */
250 public function addWatch() {
251 wfProfileIn( __METHOD__ );
252
253 // Only loggedin user can have a watchlist
254 if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
255 wfProfileOut( __METHOD__ );
256 return false;
257 }
258
259 // Use INSERT IGNORE to avoid overwriting the notification timestamp
260 // if there's already an entry for this page
261 $dbw = wfGetDB( DB_MASTER );
262 $dbw->insert( 'watchlist',
263 array(
264 'wl_user' => $this->getUserId(),
265 'wl_namespace' => MWNamespace::getSubject( $this->getTitleNs() ),
266 'wl_title' => $this->getTitleDBkey(),
267 'wl_notificationtimestamp' => null
268 ), __METHOD__, 'IGNORE' );
269
270 // Every single watched page needs now to be listed in watchlist;
271 // namespace:page and namespace_talk:page need separate entries:
272 $dbw->insert( 'watchlist',
273 array(
274 'wl_user' => $this->getUserId(),
275 'wl_namespace' => MWNamespace::getTalk( $this->getTitleNs() ),
276 'wl_title' => $this->getTitleDBkey(),
277 'wl_notificationtimestamp' => null
278 ), __METHOD__, 'IGNORE' );
279
280 $this->watched = true;
281
282 wfProfileOut( __METHOD__ );
283 return true;
284 }
285
286 /**
287 * Same as addWatch, only the opposite.
288 * @return bool
289 */
290 public function removeWatch() {
291 wfProfileIn( __METHOD__ );
292
293 // Only loggedin user can have a watchlist
294 if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
295 wfProfileOut( __METHOD__ );
296 return false;
297 }
298
299 $success = false;
300 $dbw = wfGetDB( DB_MASTER );
301 $dbw->delete( 'watchlist',
302 array(
303 'wl_user' => $this->getUserId(),
304 'wl_namespace' => MWNamespace::getSubject( $this->getTitleNs() ),
305 'wl_title' => $this->getTitleDBkey(),
306 ), __METHOD__
307 );
308 if ( $dbw->affectedRows() ) {
309 $success = true;
310 }
311
312 # the following code compensates the new behavior, introduced by the
313 # enotif patch, that every single watched page needs now to be listed
314 # in watchlist namespace:page and namespace_talk:page had separate
315 # entries: clear them
316 $dbw->delete( 'watchlist',
317 array(
318 'wl_user' => $this->getUserId(),
319 'wl_namespace' => MWNamespace::getTalk( $this->getTitleNs() ),
320 'wl_title' => $this->getTitleDBkey(),
321 ), __METHOD__
322 );
323
324 if ( $dbw->affectedRows() ) {
325 $success = true;
326 }
327
328 $this->watched = false;
329
330 wfProfileOut( __METHOD__ );
331 return $success;
332 }
333
334 /**
335 * Check if the given title already is watched by the user, and if so
336 * add watches on a new title. To be used for page renames and such.
337 *
338 * @param $ot Title: page title to duplicate entries from, if present
339 * @param $nt Title: page title to add watches on
340 */
341 public static function duplicateEntries( $ot, $nt ) {
342 WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
343 WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
344 }
345
346 /**
347 * Handle duplicate entries. Backend for duplicateEntries().
348 *
349 * @param $ot Title
350 * @param $nt Title
351 *
352 * @return bool
353 */
354 private static function doDuplicateEntries( $ot, $nt ) {
355 $oldnamespace = $ot->getNamespace();
356 $newnamespace = $nt->getNamespace();
357 $oldtitle = $ot->getDBkey();
358 $newtitle = $nt->getDBkey();
359
360 $dbw = wfGetDB( DB_MASTER );
361 $res = $dbw->select( 'watchlist', 'wl_user',
362 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
363 __METHOD__, 'FOR UPDATE'
364 );
365 # Construct array to replace into the watchlist
366 $values = array();
367 foreach ( $res as $s ) {
368 $values[] = array(
369 'wl_user' => $s->wl_user,
370 'wl_namespace' => $newnamespace,
371 'wl_title' => $newtitle
372 );
373 }
374
375 if ( empty( $values ) ) {
376 // Nothing to do
377 return true;
378 }
379
380 # Perform replace
381 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
382 # some other DBMSes, mostly due to poor simulation by us
383 $dbw->replace( 'watchlist', array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ), $values, __METHOD__ );
384 return true;
385 }
386 }