Http::getProxy() method to get proxy configuration
[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 /** @var Title */
31 public $mTitle;
32
33 /** @var User */
34 public $mUser;
35
36 /** @var int */
37 public $mCheckRights;
38
39 /** @var bool */
40 private $loaded = false;
41
42 /** @var bool */
43 private $watched;
44
45 /** @var string */
46 private $timestamp;
47
48 /**
49 * Constant to specify that user rights 'editmywatchlist' and
50 * 'viewmywatchlist' should not be checked.
51 * @since 1.22
52 */
53 const IGNORE_USER_RIGHTS = 0;
54
55 /**
56 * Constant to specify that user rights 'editmywatchlist' and
57 * 'viewmywatchlist' should be checked.
58 * @since 1.22
59 */
60 const CHECK_USER_RIGHTS = 1;
61
62 /**
63 * Do DB master updates right now
64 * @since 1.26
65 */
66 const IMMEDIATE = 0;
67 /**
68 * Do DB master updates via the job queue
69 * @since 1.26
70 */
71 const DEFERRED = 1;
72
73 /**
74 * Create a WatchedItem object with the given user and title
75 * @since 1.22 $checkRights parameter added
76 * @param User $user The user to use for (un)watching
77 * @param Title $title The title we're going to (un)watch
78 * @param int $checkRights Whether to check the 'viewmywatchlist' and 'editmywatchlist' rights.
79 * Pass either WatchedItem::IGNORE_USER_RIGHTS or WatchedItem::CHECK_USER_RIGHTS.
80 * @return WatchedItem
81 */
82 public static function fromUserTitle( $user, $title,
83 $checkRights = WatchedItem::CHECK_USER_RIGHTS
84 ) {
85 $wl = new WatchedItem;
86 $wl->mUser = $user;
87 $wl->mTitle = $title;
88 $wl->mCheckRights = $checkRights;
89
90 return $wl;
91 }
92
93 /**
94 * Title being watched
95 * @return Title
96 */
97 protected function getTitle() {
98 return $this->mTitle;
99 }
100
101 /**
102 * Helper to retrieve the title namespace
103 * @return int
104 */
105 protected function getTitleNs() {
106 return $this->getTitle()->getNamespace();
107 }
108
109 /**
110 * Helper to retrieve the title DBkey
111 * @return string
112 */
113 protected function getTitleDBkey() {
114 return $this->getTitle()->getDBkey();
115 }
116
117 /**
118 * Helper to retrieve the user id
119 * @return int
120 */
121 protected function getUserId() {
122 return $this->mUser->getId();
123 }
124
125 /**
126 * Return an array of conditions to select or update the appropriate database
127 * row.
128 *
129 * @return array
130 */
131 private function dbCond() {
132 return array(
133 'wl_user' => $this->getUserId(),
134 'wl_namespace' => $this->getTitleNs(),
135 'wl_title' => $this->getTitleDBkey(),
136 );
137 }
138
139 /**
140 * Load the object from the database
141 */
142 private function load() {
143 if ( $this->loaded ) {
144 return;
145 }
146 $this->loaded = true;
147
148 // Only loggedin user can have a watchlist
149 if ( $this->mUser->isAnon() ) {
150 $this->watched = false;
151 return;
152 }
153
154 // some pages cannot be watched
155 if ( !$this->getTitle()->isWatchable() ) {
156 $this->watched = false;
157 return;
158 }
159
160 # Pages and their talk pages are considered equivalent for watching;
161 # remember that talk namespaces are numbered as page namespace+1.
162
163 $dbr = wfGetDB( DB_SLAVE );
164 $row = $dbr->selectRow( 'watchlist', 'wl_notificationtimestamp',
165 $this->dbCond(), __METHOD__ );
166
167 if ( $row === false ) {
168 $this->watched = false;
169 } else {
170 $this->watched = true;
171 $this->timestamp = $row->wl_notificationtimestamp;
172 }
173 }
174
175 /**
176 * Check permissions
177 * @param string $what 'viewmywatchlist' or 'editmywatchlist'
178 * @return bool
179 */
180 private function isAllowed( $what ) {
181 return !$this->mCheckRights || $this->mUser->isAllowed( $what );
182 }
183
184 /**
185 * Is mTitle being watched by mUser?
186 * @return bool
187 */
188 public function isWatched() {
189 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
190 return false;
191 }
192
193 $this->load();
194 return $this->watched;
195 }
196
197 /**
198 * Get the notification timestamp of this entry.
199 *
200 * @return bool|null|string False if the page is not watched, the value of
201 * the wl_notificationtimestamp field otherwise
202 */
203 public function getNotificationTimestamp() {
204 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
205 return false;
206 }
207
208 $this->load();
209 if ( $this->watched ) {
210 return $this->timestamp;
211 } else {
212 return false;
213 }
214 }
215
216 /**
217 * Reset the notification timestamp of this entry
218 *
219 * @param bool $force Whether to force the write query to be executed even if the
220 * page is not watched or the notification timestamp is already NULL.
221 * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed.
222 * @mode int $mode WatchedItem::DEFERRED/IMMEDIATE
223 */
224 public function resetNotificationTimestamp(
225 $force = '', $oldid = 0, $mode = self::IMMEDIATE
226 ) {
227 // Only loggedin user can have a watchlist
228 if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
229 return;
230 }
231
232 if ( $force != 'force' ) {
233 $this->load();
234 if ( !$this->watched || $this->timestamp === null ) {
235 return;
236 }
237 }
238
239 $title = $this->getTitle();
240 if ( !$oldid ) {
241 // No oldid given, assuming latest revision; clear the timestamp.
242 $notificationTimestamp = null;
243 } elseif ( !$title->getNextRevisionID( $oldid ) ) {
244 // Oldid given and is the latest revision for this title; clear the timestamp.
245 $notificationTimestamp = null;
246 } else {
247 // See if the version marked as read is more recent than the one we're viewing.
248 // Call load() if it wasn't called before due to $force.
249 $this->load();
250
251 if ( $this->timestamp === null ) {
252 // This can only happen if $force is enabled.
253 $notificationTimestamp = null;
254 } else {
255 // Oldid given and isn't the latest; update the timestamp.
256 // This will result in no further notification emails being sent!
257 $notificationTimestamp = Revision::getTimestampFromId( $title, $oldid );
258 // We need to go one second to the future because of various strict comparisons
259 // throughout the codebase
260 $ts = new MWTimestamp( $notificationTimestamp );
261 $ts->timestamp->add( new DateInterval( 'PT1S' ) );
262 $notificationTimestamp = $ts->getTimestamp( TS_MW );
263
264 if ( $notificationTimestamp < $this->timestamp ) {
265 if ( $force != 'force' ) {
266 return;
267 } else {
268 // This is a little silly…
269 $notificationTimestamp = $this->timestamp;
270 }
271 }
272 }
273 }
274
275 // If the page is watched by the user (or may be watched), update the timestamp
276 if ( $mode === self::DEFERRED ) {
277 $job = new ActivityUpdateJob(
278 $title,
279 array(
280 'type' => 'updateWatchlistNotification',
281 'userid' => $this->getUserId(),
282 'notifTime' => $notificationTimestamp,
283 'curTime' => time()
284 )
285 );
286 // Try to run this post-send
287 DeferredUpdates::addCallableUpdate( function() use ( $job ) {
288 $job->run();
289 } );
290 } else {
291 $dbw = wfGetDB( DB_MASTER );
292 $dbw->update( 'watchlist',
293 array( 'wl_notificationtimestamp' => $notificationTimestamp ),
294 $this->dbCond(),
295 __METHOD__
296 );
297 }
298
299 $this->timestamp = null;
300 }
301
302 /**
303 * @param WatchedItem[] $items
304 * @return bool
305 */
306 public static function batchAddWatch( array $items ) {
307
308 if ( wfReadOnly() ) {
309 return false;
310 }
311
312 $rows = array();
313 foreach ( $items as $item ) {
314 // Only loggedin user can have a watchlist
315 if ( $item->mUser->isAnon() || !$item->isAllowed( 'editmywatchlist' ) ) {
316 continue;
317 }
318 $rows[] = array(
319 'wl_user' => $item->getUserId(),
320 'wl_namespace' => MWNamespace::getSubject( $item->getTitleNs() ),
321 'wl_title' => $item->getTitleDBkey(),
322 'wl_notificationtimestamp' => null,
323 );
324 // Every single watched page needs now to be listed in watchlist;
325 // namespace:page and namespace_talk:page need separate entries:
326 $rows[] = array(
327 'wl_user' => $item->getUserId(),
328 'wl_namespace' => MWNamespace::getTalk( $item->getTitleNs() ),
329 'wl_title' => $item->getTitleDBkey(),
330 'wl_notificationtimestamp' => null
331 );
332 $item->watched = true;
333 }
334
335 if ( !$rows ) {
336 return false;
337 }
338
339 $dbw = wfGetDB( DB_MASTER );
340 foreach ( array_chunk( $rows, 100 ) as $toInsert ) {
341 // Use INSERT IGNORE to avoid overwriting the notification timestamp
342 // if there's already an entry for this page
343 $dbw->insert( 'watchlist', $toInsert, __METHOD__, 'IGNORE' );
344 }
345
346 return true;
347 }
348
349 /**
350 * Given a title and user (assumes the object is setup), add the watch to the database.
351 * @return bool
352 */
353 public function addWatch() {
354 return self::batchAddWatch( array( $this ) );
355 }
356
357 /**
358 * Same as addWatch, only the opposite.
359 * @return bool
360 */
361 public function removeWatch() {
362
363 // Only loggedin user can have a watchlist
364 if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
365 return false;
366 }
367
368 $success = false;
369 $dbw = wfGetDB( DB_MASTER );
370 $dbw->delete( 'watchlist',
371 array(
372 'wl_user' => $this->getUserId(),
373 'wl_namespace' => MWNamespace::getSubject( $this->getTitleNs() ),
374 'wl_title' => $this->getTitleDBkey(),
375 ), __METHOD__
376 );
377 if ( $dbw->affectedRows() ) {
378 $success = true;
379 }
380
381 # the following code compensates the new behavior, introduced by the
382 # enotif patch, that every single watched page needs now to be listed
383 # in watchlist namespace:page and namespace_talk:page had separate
384 # entries: clear them
385 $dbw->delete( 'watchlist',
386 array(
387 'wl_user' => $this->getUserId(),
388 'wl_namespace' => MWNamespace::getTalk( $this->getTitleNs() ),
389 'wl_title' => $this->getTitleDBkey(),
390 ), __METHOD__
391 );
392
393 if ( $dbw->affectedRows() ) {
394 $success = true;
395 }
396
397 $this->watched = false;
398
399 return $success;
400 }
401
402 /**
403 * Check if the given title already is watched by the user, and if so
404 * add watches on a new title. To be used for page renames and such.
405 *
406 * @param Title $ot Page title to duplicate entries from, if present
407 * @param Title $nt Page title to add watches on
408 */
409 public static function duplicateEntries( $ot, $nt ) {
410 WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
411 WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
412 }
413
414 /**
415 * Handle duplicate entries. Backend for duplicateEntries().
416 *
417 * @param Title $ot
418 * @param Title $nt
419 *
420 * @return bool
421 */
422 private static function doDuplicateEntries( $ot, $nt ) {
423 $oldnamespace = $ot->getNamespace();
424 $newnamespace = $nt->getNamespace();
425 $oldtitle = $ot->getDBkey();
426 $newtitle = $nt->getDBkey();
427
428 $dbw = wfGetDB( DB_MASTER );
429 $res = $dbw->select( 'watchlist',
430 array( 'wl_user', 'wl_notificationtimestamp' ),
431 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
432 __METHOD__, 'FOR UPDATE'
433 );
434 # Construct array to replace into the watchlist
435 $values = array();
436 foreach ( $res as $s ) {
437 $values[] = array(
438 'wl_user' => $s->wl_user,
439 'wl_namespace' => $newnamespace,
440 'wl_title' => $newtitle,
441 'wl_notificationtimestamp' => $s->wl_notificationtimestamp,
442 );
443 }
444
445 if ( empty( $values ) ) {
446 // Nothing to do
447 return true;
448 }
449
450 # Perform replace
451 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
452 # some other DBMSes, mostly due to poor simulation by us
453 $dbw->replace(
454 'watchlist',
455 array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ),
456 $values,
457 __METHOD__
458 );
459
460 return true;
461 }
462 }