Merge "Make 'subnet' feature of $wgRateLimits work with IPv6"
[lhc/web/wiklou.git] / includes / specials / SpecialWatchlist.php
1 <?php
2 /**
3 * Implements Special:Watchlist
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 SpecialPage Watchlist
22 */
23 class SpecialWatchlist extends SpecialPage {
24 protected $customFilters;
25
26 /**
27 * Constructor
28 */
29 public function __construct( $page = 'Watchlist' ) {
30 parent::__construct( $page );
31 }
32
33 /**
34 * Execute
35 * @param $par Parameter passed to the page
36 */
37 function execute( $par ) {
38 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
39
40 $user = $this->getUser();
41 $output = $this->getOutput();
42
43 # Anons don't get a watchlist
44 if ( $user->isAnon() ) {
45 $output->setPageTitle( $this->msg( 'watchnologin' ) );
46 $output->setRobotPolicy( 'noindex,nofollow' );
47 $llink = Linker::linkKnown(
48 SpecialPage::getTitleFor( 'Userlogin' ),
49 $this->msg( 'loginreqlink' )->escaped(),
50 array(),
51 array( 'returnto' => $this->getTitle()->getPrefixedText() )
52 );
53 $output->addHTML( $this->msg( 'watchlistanontext' )->rawParams( $llink )->parse() );
54 return;
55 }
56
57 // Add feed links
58 $wlToken = $user->getOption( 'watchlisttoken' );
59 if ( !$wlToken ) {
60 $wlToken = MWCryptRand::generateHex( 40 );
61 $user->setOption( 'watchlisttoken', $wlToken );
62 $user->saveSettings();
63 }
64
65 $this->addFeedLinks( array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
66 'wlowner' => $user->getName(), 'wltoken' => $wlToken ) );
67
68 $this->setHeaders();
69 $this->outputHeader();
70
71 $output->addSubtitle( $this->msg( 'watchlistfor2', $user->getName()
72 )->rawParams( SpecialEditWatchlist::buildTools( null ) ) );
73
74 $request = $this->getRequest();
75
76 $mode = SpecialEditWatchlist::getMode( $request, $par );
77 if ( $mode !== false ) {
78 # TODO: localise?
79 switch ( $mode ) {
80 case SpecialEditWatchlist::EDIT_CLEAR:
81 $mode = 'clear';
82 break;
83 case SpecialEditWatchlist::EDIT_RAW:
84 $mode = 'raw';
85 break;
86 default:
87 $mode = null;
88 }
89 $title = SpecialPage::getTitleFor( 'EditWatchlist', $mode );
90 $output->redirect( $title->getLocalURL() );
91 return;
92 }
93
94 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
95
96 $nitems = $this->countItems( $dbr );
97 if ( $nitems == 0 ) {
98 $output->addWikiMsg( 'nowatchlist' );
99 return;
100 }
101
102 // @todo use FormOptions!
103 $defaults = array(
104 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
105 /* bool */ 'hideMinor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
106 /* bool */ 'hideBots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
107 /* bool */ 'hideAnons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
108 /* bool */ 'hideLiu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
109 /* bool */ 'hidePatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
110 /* bool */ 'hideOwn' => (int)$user->getBoolOption( 'watchlisthideown' ),
111 /* bool */ 'extended' => (int)$user->getBoolOption( 'extendwatchlist' ),
112 /* ? */ 'namespace' => '', //means all
113 /* ? */ 'invert' => false,
114 /* bool */ 'associated' => false,
115 );
116 $this->customFilters = array();
117 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ) );
118 foreach ( $this->customFilters as $key => $params ) {
119 $defaults[$key] = $params['default'];
120 }
121
122 # Extract variables from the request, falling back to user preferences or
123 # other default values if these don't exist
124 $values = array();
125 $values['days'] = $request->getVal( 'days', $defaults['days'] );
126 $values['hideMinor'] = (int)$request->getBool( 'hideMinor', $defaults['hideMinor'] );
127 $values['hideBots'] = (int)$request->getBool( 'hideBots', $defaults['hideBots'] );
128 $values['hideAnons'] = (int)$request->getBool( 'hideAnons', $defaults['hideAnons'] );
129 $values['hideLiu'] = (int)$request->getBool( 'hideLiu', $defaults['hideLiu'] );
130 $values['hideOwn'] = (int)$request->getBool( 'hideOwn', $defaults['hideOwn'] );
131 $values['hidePatrolled'] = (int)$request->getBool( 'hidePatrolled', $defaults['hidePatrolled'] );
132 $values['extended'] = (int)$request->getBool( 'extended', $defaults['extended'] );
133 foreach ( $this->customFilters as $key => $params ) {
134 $values[$key] = (int)$request->getBool( $key, $defaults[$key] );
135 }
136
137 # Get namespace value, if supplied, and prepare a WHERE fragment
138 $nameSpace = $request->getIntOrNull( 'namespace' );
139 $invert = $request->getBool( 'invert' );
140 $associated = $request->getBool( 'associated' );
141 if ( !is_null( $nameSpace ) ) {
142 $eq_op = $invert ? '!=' : '=';
143 $bool_op = $invert ? 'AND' : 'OR';
144 $nameSpace = intval( $nameSpace ); // paranioa
145 if ( !$associated ) {
146 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
147 } else {
148 $associatedNS = MWNamespace::getAssociated( $nameSpace );
149 $nameSpaceClause =
150 "rc_namespace $eq_op $nameSpace " .
151 $bool_op .
152 " rc_namespace $eq_op $associatedNS";
153 }
154 } else {
155 $nameSpace = '';
156 $nameSpaceClause = '';
157 }
158 $values['namespace'] = $nameSpace;
159 $values['invert'] = $invert;
160 $values['associated'] = $associated;
161
162 if ( is_null( $values['days'] ) || !is_numeric( $values['days'] ) ) {
163 $big = 1000; /* The magical big */
164 if ( $nitems > $big ) {
165 # Set default cutoff shorter
166 $values['days'] = $defaults['days'] = ( 12.0 / 24.0 ); # 12 hours...
167 } else {
168 $values['days'] = $defaults['days']; # default cutoff for shortlisters
169 }
170 } else {
171 $values['days'] = floatval( $values['days'] );
172 }
173
174 // Dump everything here
175 $nondefaults = array();
176 foreach ( $defaults as $name => $defValue ) {
177 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
178 }
179
180 if ( ( $wgEnotifWatchlist || $wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
181 $request->wasPosted() )
182 {
183 $user->clearAllNotifications();
184 $output->redirect( $this->getTitle()->getFullURL( $nondefaults ) );
185 return;
186 }
187
188 # Possible where conditions
189 $conds = array();
190
191 if ( $values['days'] > 0 ) {
192 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $values['days'] * 86400 ) ) );
193 }
194
195 # If the watchlist is relatively short, it's simplest to zip
196 # down its entirety and then sort the results.
197
198 # If it's relatively long, it may be worth our while to zip
199 # through the time-sorted page list checking for watched items.
200
201 # Up estimate of watched items by 15% to compensate for talk pages...
202
203 # Toggles
204 if ( $values['hideOwn'] ) {
205 $conds[] = 'rc_user != ' . $user->getId();
206 }
207 if ( $values['hideBots'] ) {
208 $conds[] = 'rc_bot = 0';
209 }
210 if ( $values['hideMinor'] ) {
211 $conds[] = 'rc_minor = 0';
212 }
213 if ( $values['hideLiu'] ) {
214 $conds[] = 'rc_user = 0';
215 }
216 if ( $values['hideAnons'] ) {
217 $conds[] = 'rc_user != 0';
218 }
219 if ( $user->useRCPatrol() && $values['hidePatrolled'] ) {
220 $conds[] = 'rc_patrolled != 1';
221 }
222 if ( $nameSpaceClause ) {
223 $conds[] = $nameSpaceClause;
224 }
225
226 # Toggle watchlist content (all recent edits or just the latest)
227 if ( $values['extended'] ) {
228 $limitWatchlist = $user->getIntOption( 'wllimit' );
229 $usePage = false;
230 } else {
231 # Top log Ids for a page are not stored
232 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG;
233 $limitWatchlist = 0;
234 $usePage = true;
235 }
236
237 # Show a message about slave lag, if applicable
238 $lag = wfGetLB()->safeGetLag( $dbr );
239 if ( $lag > 0 ) {
240 $output->showLagWarning( $lag );
241 }
242
243 # Create output form
244 $form = Xml::fieldset(
245 $this->msg( 'watchlist-options' )->text(),
246 false,
247 array( 'id' => 'mw-watchlist-options' )
248 );
249
250 # Show watchlist header
251 $form .= "<p>";
252 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
253 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
254 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
255 }
256 if ( $wgShowUpdatedMarker ) {
257 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
258 }
259 $form .= "</p>";
260
261 if ( $wgShowUpdatedMarker ) {
262 $form .= Xml::openElement( 'form', array( 'method' => 'post',
263 'action' => $this->getTitle()->getLocalURL(),
264 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
265 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
266 Html::hidden( 'reset', 'all' ) . "\n";
267 foreach ( $nondefaults as $key => $value ) {
268 $form .= Html::hidden( $key, $value ) . "\n";
269 }
270 $form .= Xml::closeElement( 'form' ) . "\n";
271 }
272
273 $form .= "<hr />\n";
274
275 $tables = array( 'recentchanges', 'watchlist' );
276 $fields = RecentChange::selectFields();
277 $join_conds = array(
278 'watchlist' => array(
279 'INNER JOIN',
280 array(
281 'wl_user' => $user->getId(),
282 'wl_namespace=rc_namespace',
283 'wl_title=rc_title'
284 ),
285 ),
286 );
287 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
288 if ( $wgShowUpdatedMarker ) {
289 $fields[] = 'wl_notificationtimestamp';
290 }
291 if ( $limitWatchlist ) {
292 $options['LIMIT'] = $limitWatchlist;
293 }
294
295 $rollbacker = $user->isAllowed( 'rollback' );
296 if ( $usePage || $rollbacker ) {
297 $tables[] = 'page';
298 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
299 if ( $rollbacker ) {
300 $fields[] = 'page_latest';
301 }
302 }
303
304 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
305 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $values ) );
306
307 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
308 $numRows = $res->numRows();
309
310 /* Start bottom header */
311
312 $lang = $this->getLanguage();
313 $wlInfo = '';
314 if ( $values['days'] > 0 ) {
315 $timestamp = wfTimestampNow();
316 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
317 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
318 }
319
320 $cutofflinks = $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
321
322 # Spit out some control panel links
323 $filters = array(
324 'hideMinor' => 'rcshowhideminor',
325 'hideBots' => 'rcshowhidebots',
326 'hideAnons' => 'rcshowhideanons',
327 'hideLiu' => 'rcshowhideliu',
328 'hideOwn' => 'rcshowhidemine',
329 'hidePatrolled' => 'rcshowhidepatr'
330 );
331 foreach ( $this->customFilters as $key => $params ) {
332 $filters[$key] = $params['msg'];
333 }
334 // Disable some if needed
335 if ( !$user->useNPPatrol() ) {
336 unset( $filters['hidePatrolled'] );
337 }
338
339 $links = array();
340 foreach ( $filters as $name => $msg ) {
341 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
342 }
343
344 $hiddenFields = $nondefaults;
345 unset( $hiddenFields['namespace'] );
346 unset( $hiddenFields['invert'] );
347 unset( $hiddenFields['associated'] );
348
349 # Namespace filter and put the whole form together.
350 $form .= $wlInfo;
351 $form .= $cutofflinks;
352 $form .= $lang->pipeList( $links ) . "\n";
353 $form .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'id' => 'mw-watchlist-form-namespaceselector' ) ) . "\n";
354 $form .= "<hr />\n<p>";
355 $form .= Html::namespaceSelector(
356 array(
357 'selected' => $nameSpace,
358 'all' => '',
359 'label' => $this->msg( 'namespace' )->text()
360 ), array(
361 'name' => 'namespace',
362 'id' => 'namespace',
363 'class' => 'namespaceselector',
364 )
365 ) . '&#160;';
366 $form .= Xml::checkLabel(
367 $this->msg( 'invert' )->text(),
368 'invert',
369 'nsinvert',
370 $invert,
371 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
372 ) . '&#160;';
373 $form .= Xml::checkLabel(
374 $this->msg( 'namespace_association' )->text(),
375 'associated',
376 'associated',
377 $associated,
378 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
379 ) . '&#160;';
380 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
381 foreach ( $hiddenFields as $key => $value ) {
382 $form .= Html::hidden( $key, $value ) . "\n";
383 }
384 $form .= Xml::closeElement( 'form' ) . "\n";
385 $form .= Xml::closeElement( 'fieldset' ) . "\n";
386 $output->addHTML( $form );
387
388 # If there's nothing to show, stop here
389 if ( $numRows == 0 ) {
390 $output->addWikiMsg( 'watchnochange' );
391 return;
392 }
393
394 /* End bottom header */
395
396 /* Do link batch query */
397 $linkBatch = new LinkBatch;
398 foreach ( $res as $row ) {
399 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
400 if ( $row->rc_user != 0 ) {
401 $linkBatch->add( NS_USER, $userNameUnderscored );
402 }
403 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
404
405 $linkBatch->add( $row->rc_namespace, $row->rc_title );
406 }
407 $linkBatch->execute();
408 $dbr->dataSeek( $res, 0 );
409
410 $list = ChangesList::newFromContext( $this->getContext() );
411 $list->setWatchlistDivs();
412
413 $s = $list->beginRecentChangesList();
414 $counter = 1;
415 foreach ( $res as $obj ) {
416 # Make RC entry
417 $rc = RecentChange::newFromRow( $obj );
418 $rc->counter = $counter++;
419
420 if ( $wgShowUpdatedMarker ) {
421 $updated = $obj->wl_notificationtimestamp;
422 } else {
423 $updated = false;
424 }
425
426 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
427 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
428 'COUNT(*)',
429 array(
430 'wl_namespace' => $obj->rc_namespace,
431 'wl_title' => $obj->rc_title,
432 ),
433 __METHOD__ );
434 } else {
435 $rc->numberofWatchingusers = 0;
436 }
437
438 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
439 if ( $changeLine !== false ) {
440 $s .= $changeLine;
441 }
442 }
443 $s .= $list->endRecentChangesList();
444
445 $output->addHTML( $s );
446 }
447
448 protected function showHideLink( $options, $message, $name, $value ) {
449 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
450 $options[$name] = 1 - (int) $value;
451
452 return $this->msg( $message )->rawParams( Linker::linkKnown( $this->getTitle(), $label, array(), $options ) )->escaped();
453 }
454
455 protected function hoursLink( $h, $options = array() ) {
456 $options['days'] = ( $h / 24.0 );
457
458 return Linker::linkKnown(
459 $this->getTitle(),
460 $this->getLanguage()->formatNum( $h ),
461 array(),
462 $options
463 );
464 }
465
466 protected function daysLink( $d, $options = array() ) {
467 $options['days'] = $d;
468 $message = ( $d ? $this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
469
470 return Linker::linkKnown(
471 $this->getTitle(),
472 $message,
473 array(),
474 $options
475 );
476 }
477
478 /**
479 * Returns html
480 *
481 * @param int $days This gets overwritten, so is not used
482 * @param array $options Query parameters for URL
483 * @return string
484 */
485 protected function cutoffLinks( $days, $options = array() ) {
486 $hours = array( 1, 2, 6, 12 );
487 $days = array( 1, 3, 7 );
488 $i = 0;
489 foreach ( $hours as $h ) {
490 $hours[$i++] = $this->hoursLink( $h, $options );
491 }
492 $i = 0;
493 foreach ( $days as $d ) {
494 $days[$i++] = $this->daysLink( $d, $options );
495 }
496 return $this->msg( 'wlshowlast' )->rawParams(
497 $this->getLanguage()->pipeList( $hours ),
498 $this->getLanguage()->pipeList( $days ),
499 $this->daysLink( 0, $options ) )->parse();
500 }
501
502 /**
503 * Count the number of items on a user's watchlist
504 *
505 * @param DatabaseBase $dbr A database connection
506 * @return Integer
507 */
508 protected function countItems( $dbr ) {
509 # Fetch the raw count
510 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
511 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
512 $row = $dbr->fetchObject( $res );
513 $count = $row->count;
514
515 return floor( $count / 2 );
516 }
517
518 protected function getGroupName() {
519 return 'changes';
520 }
521 }