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