Merge "Allow mobile to reduce image quality"
[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 int $checkRights Whether to check the 'viewmywatchlist' and 'editmywatchlist' rights.
53 * Pass either WatchedItem::IGNORE_USER_RIGHTS or WatchedItem::CHECK_USER_RIGHTS.
54 * @return WatchedItem
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 /**
74 * Helper to retrieve the title namespace
75 * @return int
76 */
77 protected function getTitleNs() {
78 return $this->getTitle()->getNamespace();
79 }
80
81 /**
82 * Helper to retrieve the title DBkey
83 * @return string
84 */
85 protected function getTitleDBkey() {
86 return $this->getTitle()->getDBkey();
87 }
88
89 /**
90 * Helper to retrieve the user id
91 * @return int
92 */
93 protected function getUserId() {
94 return $this->mUser->getId();
95 }
96
97 /**
98 * Return an array of conditions to select or update the appropriate database
99 * row.
100 *
101 * @return array
102 */
103 private function dbCond() {
104 return array(
105 'wl_user' => $this->getUserId(),
106 'wl_namespace' => $this->getTitleNs(),
107 'wl_title' => $this->getTitleDBkey(),
108 );
109 }
110
111 /**
112 * Load the object from the database
113 */
114 private function load() {
115 if ( $this->loaded ) {
116 return;
117 }
118 $this->loaded = true;
119
120 // Only loggedin user can have a watchlist
121 if ( $this->mUser->isAnon() ) {
122 $this->watched = false;
123 return;
124 }
125
126 // some pages cannot be watched
127 if ( !$this->getTitle()->isWatchable() ) {
128 $this->watched = false;
129 return;
130 }
131
132 # Pages and their talk pages are considered equivalent for watching;
133 # remember that talk namespaces are numbered as page namespace+1.
134
135 $dbr = wfGetDB( DB_SLAVE );
136 $row = $dbr->selectRow( 'watchlist', 'wl_notificationtimestamp',
137 $this->dbCond(), __METHOD__ );
138
139 if ( $row === false ) {
140 $this->watched = false;
141 } else {
142 $this->watched = true;
143 $this->timestamp = $row->wl_notificationtimestamp;
144 }
145 }
146
147 /**
148 * Check permissions
149 * @param string $what 'viewmywatchlist' or 'editmywatchlist'
150 */
151 private function isAllowed( $what ) {
152 return !$this->mCheckRights || $this->mUser->isAllowed( $what );
153 }
154
155 /**
156 * Is mTitle being watched by mUser?
157 * @return bool
158 */
159 public function isWatched() {
160 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
161 return false;
162 }
163
164 $this->load();
165 return $this->watched;
166 }
167
168 /**
169 * Get the notification timestamp of this entry.
170 *
171 * @return bool|null|string False if the page is not watched, the value of
172 * the wl_notificationtimestamp field otherwise
173 */
174 public function getNotificationTimestamp() {
175 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
176 return false;
177 }
178
179 $this->load();
180 if ( $this->watched ) {
181 return $this->timestamp;
182 } else {
183 return false;
184 }
185 }
186
187 /**
188 * Reset the notification timestamp of this entry
189 *
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.
193 */
194 public function resetNotificationTimestamp( $force = '', $oldid = 0 ) {
195 // Only loggedin user can have a watchlist
196 if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
197 return;
198 }
199
200 if ( $force != 'force' ) {
201 $this->load();
202 if ( !$this->watched || $this->timestamp === null ) {
203 return;
204 }
205 }
206
207 $title = $this->getTitle();
208 if ( !$oldid ) {
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;
214 } else {
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.
217 $this->load();
218
219 if ( $this->timestamp === null ) {
220 // This can only happen if $force is enabled.
221 $notificationTimestamp = null;
222 } else {
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 )
229 );
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 );
235
236 if ( $notificationTimestamp < $this->timestamp ) {
237 if ( $force != 'force' ) {
238 return;
239 } else {
240 // This is a little silly…
241 $notificationTimestamp = $this->timestamp;
242 }
243 }
244 }
245 }
246
247 // If the page is watched by the user (or may be watched), update the timestamp on any
248 // any matching rows
249 $dbw = wfGetDB( DB_MASTER );
250 $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => $notificationTimestamp ),
251 $this->dbCond(), __METHOD__ );
252 $this->timestamp = null;
253 }
254
255 /**
256 * Given a title and user (assumes the object is setup), add the watch to the database.
257 * @return bool
258 */
259 public function addWatch() {
260 wfProfileIn( __METHOD__ );
261
262 // Only loggedin user can have a watchlist
263 if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
264 wfProfileOut( __METHOD__ );
265 return false;
266 }
267
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',
272 array(
273 'wl_user' => $this->getUserId(),
274 'wl_namespace' => MWNamespace::getSubject( $this->getTitleNs() ),
275 'wl_title' => $this->getTitleDBkey(),
276 'wl_notificationtimestamp' => null
277 ), __METHOD__, 'IGNORE' );
278
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',
282 array(
283 'wl_user' => $this->getUserId(),
284 'wl_namespace' => MWNamespace::getTalk( $this->getTitleNs() ),
285 'wl_title' => $this->getTitleDBkey(),
286 'wl_notificationtimestamp' => null
287 ), __METHOD__, 'IGNORE' );
288
289 $this->watched = true;
290
291 wfProfileOut( __METHOD__ );
292 return true;
293 }
294
295 /**
296 * Same as addWatch, only the opposite.
297 * @return bool
298 */
299 public function removeWatch() {
300 wfProfileIn( __METHOD__ );
301
302 // Only loggedin user can have a watchlist
303 if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
304 wfProfileOut( __METHOD__ );
305 return false;
306 }
307
308 $success = false;
309 $dbw = wfGetDB( DB_MASTER );
310 $dbw->delete( 'watchlist',
311 array(
312 'wl_user' => $this->getUserId(),
313 'wl_namespace' => MWNamespace::getSubject( $this->getTitleNs() ),
314 'wl_title' => $this->getTitleDBkey(),
315 ), __METHOD__
316 );
317 if ( $dbw->affectedRows() ) {
318 $success = true;
319 }
320
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',
326 array(
327 'wl_user' => $this->getUserId(),
328 'wl_namespace' => MWNamespace::getTalk( $this->getTitleNs() ),
329 'wl_title' => $this->getTitleDBkey(),
330 ), __METHOD__
331 );
332
333 if ( $dbw->affectedRows() ) {
334 $success = true;
335 }
336
337 $this->watched = false;
338
339 wfProfileOut( __METHOD__ );
340 return $success;
341 }
342
343 /**
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.
346 *
347 * @param Title $ot Page title to duplicate entries from, if present
348 * @param Title $nt Page title to add watches on
349 */
350 public static function duplicateEntries( $ot, $nt ) {
351 WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
352 WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
353 }
354
355 /**
356 * Handle duplicate entries. Backend for duplicateEntries().
357 *
358 * @param Title $ot
359 * @param Title $nt
360 *
361 * @return bool
362 */
363 private static function doDuplicateEntries( $ot, $nt ) {
364 $oldnamespace = $ot->getNamespace();
365 $newnamespace = $nt->getNamespace();
366 $oldtitle = $ot->getDBkey();
367 $newtitle = $nt->getDBkey();
368
369 $dbw = wfGetDB( DB_MASTER );
370 $res = $dbw->select( 'watchlist', 'wl_user',
371 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
372 __METHOD__, 'FOR UPDATE'
373 );
374 # Construct array to replace into the watchlist
375 $values = array();
376 foreach ( $res as $s ) {
377 $values[] = array(
378 'wl_user' => $s->wl_user,
379 'wl_namespace' => $newnamespace,
380 'wl_title' => $newtitle
381 );
382 }
383
384 if ( empty( $values ) ) {
385 // Nothing to do
386 return true;
387 }
388
389 # Perform replace
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__ );
393 return true;
394 }
395 }