Enable users to watch category membership changes
[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
22 */
23
24 /**
25 * A special page that lists last changes made to the wiki,
26 * limited to user-defined list of titles.
27 *
28 * @ingroup SpecialPage
29 */
30 class SpecialWatchlist extends ChangesListSpecialPage {
31 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
32 parent::__construct( $page, $restriction );
33 }
34
35 /**
36 * Main execution point
37 *
38 * @param string $subpage
39 */
40 function execute( $subpage ) {
41 // Anons don't get a watchlist
42 $this->requireLogin( 'watchlistanontext' );
43
44 $output = $this->getOutput();
45 $request = $this->getRequest();
46 $this->addHelpLink( 'Help:Watching pages' );
47
48 $mode = SpecialEditWatchlist::getMode( $request, $subpage );
49 if ( $mode !== false ) {
50 if ( $mode === SpecialEditWatchlist::EDIT_RAW ) {
51 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'raw' );
52 } elseif ( $mode === SpecialEditWatchlist::EDIT_CLEAR ) {
53 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'clear' );
54 } else {
55 $title = SpecialPage::getTitleFor( 'EditWatchlist' );
56 }
57
58 $output->redirect( $title->getLocalURL() );
59
60 return;
61 }
62
63 $this->checkPermissions();
64
65 $user = $this->getUser();
66 $opts = $this->getOptions();
67
68 $config = $this->getConfig();
69 if ( ( $config->get( 'EnotifWatchlist' ) || $config->get( 'ShowUpdatedMarker' ) )
70 && $request->getVal( 'reset' )
71 && $request->wasPosted()
72 ) {
73 $user->clearAllNotifications();
74 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
75
76 return;
77 }
78
79 parent::execute( $subpage );
80 }
81
82 /**
83 * Return an array of subpages that this special page will accept.
84 *
85 * @see also SpecialEditWatchlist::getSubpagesForPrefixSearch
86 * @return string[] subpages
87 */
88 public function getSubpagesForPrefixSearch() {
89 return array(
90 'clear',
91 'edit',
92 'raw',
93 );
94 }
95
96 /**
97 * Get a FormOptions object containing the default options
98 *
99 * @return FormOptions
100 */
101 public function getDefaultOptions() {
102 $opts = parent::getDefaultOptions();
103 $user = $this->getUser();
104
105 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions::FLOAT );
106
107 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
108 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
109 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
110 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
111 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
112 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
113 $opts->add( 'hidecategorization', $user->getBoolOption( 'watchlisthidecategorization' ) );
114
115 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
116
117 return $opts;
118 }
119
120 /**
121 * Get custom show/hide filters
122 *
123 * @return array Map of filter URL param names to properties (msg/default)
124 */
125 protected function getCustomFilters() {
126 if ( $this->customFilters === null ) {
127 $this->customFilters = parent::getCustomFilters();
128 Hooks::run( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ), '1.23' );
129 }
130
131 return $this->customFilters;
132 }
133
134 /**
135 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
136 *
137 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
138 * to the current ones.
139 *
140 * @param FormOptions $opts
141 * @return FormOptions
142 */
143 protected function fetchOptionsFromRequest( $opts ) {
144 static $compatibilityMap = array(
145 'hideMinor' => 'hideminor',
146 'hideBots' => 'hidebots',
147 'hideAnons' => 'hideanons',
148 'hideLiu' => 'hideliu',
149 'hidePatrolled' => 'hidepatrolled',
150 'hideOwn' => 'hidemyself',
151 );
152
153 $params = $this->getRequest()->getValues();
154 foreach ( $compatibilityMap as $from => $to ) {
155 if ( isset( $params[$from] ) ) {
156 $params[$to] = $params[$from];
157 unset( $params[$from] );
158 }
159 }
160
161 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
162 // methods defined on WebRequest and removing this dependency would cause some code duplication.
163 $request = new DerivativeRequest( $this->getRequest(), $params );
164 $opts->fetchValuesFromRequest( $request );
165
166 return $opts;
167 }
168
169 /**
170 * Return an array of conditions depending of options set in $opts
171 *
172 * @param FormOptions $opts
173 * @return array
174 */
175 public function buildMainQueryConds( FormOptions $opts ) {
176 $dbr = $this->getDB();
177 $conds = parent::buildMainQueryConds( $opts );
178
179 // Calculate cutoff
180 if ( $opts['days'] > 0 ) {
181 $conds[] = 'rc_timestamp > ' .
182 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
183 }
184
185 return $conds;
186 }
187
188 /**
189 * Process the query
190 *
191 * @param array $conds
192 * @param FormOptions $opts
193 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
194 */
195 public function doMainQuery( $conds, $opts ) {
196 $dbr = $this->getDB();
197 $user = $this->getUser();
198
199 # Toggle watchlist content (all recent edits or just the latest)
200 if ( $opts['extended'] ) {
201 $limitWatchlist = $user->getIntOption( 'wllimit' );
202 $usePage = false;
203 } else {
204 # Top log Ids for a page are not stored
205 $nonRevisionTypes = array( RC_LOG );
206 Hooks::run( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
207 if ( $nonRevisionTypes ) {
208 $conds[] = $dbr->makeList(
209 array(
210 'rc_this_oldid=page_latest',
211 'rc_type' => $nonRevisionTypes,
212 ),
213 LIST_OR
214 );
215 }
216 $limitWatchlist = 0;
217 $usePage = true;
218 }
219
220 $tables = array( 'recentchanges', 'watchlist' );
221 $fields = RecentChange::selectFields();
222 $query_options = array( 'ORDER BY' => 'rc_timestamp DESC' );
223 $join_conds = array(
224 'watchlist' => array(
225 'INNER JOIN',
226 array(
227 'wl_user' => $user->getId(),
228 'wl_namespace=rc_namespace',
229 'wl_title=rc_title'
230 ),
231 ),
232 );
233
234 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
235 $fields[] = 'wl_notificationtimestamp';
236 }
237 if ( $limitWatchlist ) {
238 $query_options['LIMIT'] = $limitWatchlist;
239 }
240
241 $rollbacker = $user->isAllowed( 'rollback' );
242 if ( $usePage || $rollbacker ) {
243 $tables[] = 'page';
244 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
245 if ( $rollbacker ) {
246 $fields[] = 'page_latest';
247 }
248 }
249
250 // Log entries with DELETED_ACTION must not show up unless the user has
251 // the necessary rights.
252 if ( !$user->isAllowed( 'deletedhistory' ) ) {
253 $bitmask = LogPage::DELETED_ACTION;
254 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
255 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
256 } else {
257 $bitmask = 0;
258 }
259 if ( $bitmask ) {
260 $conds[] = $dbr->makeList( array(
261 'rc_type != ' . RC_LOG,
262 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
263 ), LIST_OR );
264 }
265
266 ChangeTags::modifyDisplayQuery(
267 $tables,
268 $fields,
269 $conds,
270 $join_conds,
271 $query_options,
272 ''
273 );
274
275 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
276
277 return $dbr->select(
278 $tables,
279 $fields,
280 $conds,
281 __METHOD__,
282 $query_options,
283 $join_conds
284 );
285 }
286
287 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
288 &$join_conds, $opts
289 ) {
290 return parent::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
291 && Hooks::run(
292 'SpecialWatchlistQuery',
293 array( &$conds, &$tables, &$join_conds, &$fields, $opts ),
294 '1.23'
295 );
296 }
297
298 /**
299 * Return a IDatabase object for reading
300 *
301 * @return IDatabase
302 */
303 protected function getDB() {
304 return wfGetDB( DB_SLAVE, 'watchlist' );
305 }
306
307 /**
308 * Output feed links.
309 */
310 public function outputFeedLinks() {
311 $user = $this->getUser();
312 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
313 if ( $wlToken ) {
314 $this->addFeedLinks( array(
315 'action' => 'feedwatchlist',
316 'allrev' => 1,
317 'wlowner' => $user->getName(),
318 'wltoken' => $wlToken,
319 ) );
320 }
321 }
322
323 /**
324 * Build and output the actual changes list.
325 *
326 * @param ResultWrapper $rows Database rows
327 * @param FormOptions $opts
328 */
329 public function outputChangesList( $rows, $opts ) {
330 $dbr = $this->getDB();
331 $user = $this->getUser();
332 $output = $this->getOutput();
333
334 # Show a message about slave lag, if applicable
335 $lag = wfGetLB()->safeGetLag( $dbr );
336 if ( $lag > 0 ) {
337 $output->showLagWarning( $lag );
338 }
339
340 # If no rows to display, show message before try to render the list
341 if ( $rows->numRows() == 0 ) {
342 $output->wrapWikiMsg(
343 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
344 );
345 return;
346 }
347
348 $dbr->dataSeek( $rows, 0 );
349
350 $list = ChangesList::newFromContext( $this->getContext() );
351 $list->setWatchlistDivs();
352 $list->initChangesListRows( $rows );
353 $dbr->dataSeek( $rows, 0 );
354
355 $s = $list->beginRecentChangesList();
356 $counter = 1;
357 foreach ( $rows as $obj ) {
358 # Make RC entry
359 $rc = RecentChange::newFromRow( $obj );
360 $rc->counter = $counter++;
361
362 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
363 $updated = $obj->wl_notificationtimestamp;
364 } else {
365 $updated = false;
366 }
367
368 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
369 && $user->getOption( 'shownumberswatching' )
370 ) {
371 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
372 'COUNT(*)',
373 array(
374 'wl_namespace' => $obj->rc_namespace,
375 'wl_title' => $obj->rc_title,
376 ),
377 __METHOD__ );
378 } else {
379 $rc->numberofWatchingusers = 0;
380 }
381
382 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
383 if ( $changeLine !== false ) {
384 $s .= $changeLine;
385 }
386 }
387 $s .= $list->endRecentChangesList();
388
389 $output->addHTML( $s );
390 }
391
392 /**
393 * Set the text to be displayed above the changes
394 *
395 * @param FormOptions $opts
396 * @param int $numRows Number of rows in the result to show after this header
397 */
398 public function doHeader( $opts, $numRows ) {
399 $user = $this->getUser();
400
401 $this->getOutput()->addSubtitle(
402 $this->msg( 'watchlistfor2', $user->getName() )
403 ->rawParams( SpecialEditWatchlist::buildTools( null ) )
404 );
405
406 $this->setTopText( $opts );
407
408 $lang = $this->getLanguage();
409 $wlInfo = '';
410 if ( $opts['days'] > 0 ) {
411 $timestamp = wfTimestampNow();
412 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $opts['days'] * 24 ) )->params(
413 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
414 )->parse() . "<br />\n";
415 }
416
417 $nondefaults = $opts->getChangedValues();
418 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
419
420 # Spit out some control panel links
421 $filters = array(
422 'hideminor' => 'rcshowhideminor',
423 'hidebots' => 'rcshowhidebots',
424 'hideanons' => 'rcshowhideanons',
425 'hideliu' => 'rcshowhideliu',
426 'hidemyself' => 'rcshowhidemine',
427 'hidepatrolled' => 'rcshowhidepatr',
428 'hidecategorization' => 'rcshowhidecategorization',
429 );
430 foreach ( $this->getCustomFilters() as $key => $params ) {
431 $filters[$key] = $params['msg'];
432 }
433 // Disable some if needed
434 if ( !$user->useRCPatrol() ) {
435 unset( $filters['hidepatrolled'] );
436 }
437
438 $links = array();
439 foreach ( $filters as $name => $msg ) {
440 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
441 }
442
443 $hiddenFields = $nondefaults;
444 unset( $hiddenFields['namespace'] );
445 unset( $hiddenFields['invert'] );
446 unset( $hiddenFields['associated'] );
447
448 # Create output
449 $form = '';
450
451 # Namespace filter and put the whole form together.
452 $form .= $wlInfo;
453 $form .= $cutofflinks;
454 $form .= $lang->pipeList( $links ) . "\n";
455 $form .= "<hr />\n<p>";
456 $form .= Html::namespaceSelector(
457 array(
458 'selected' => $opts['namespace'],
459 'all' => '',
460 'label' => $this->msg( 'namespace' )->text()
461 ), array(
462 'name' => 'namespace',
463 'id' => 'namespace',
464 'class' => 'namespaceselector',
465 )
466 ) . '&#160;';
467 $form .= Xml::checkLabel(
468 $this->msg( 'invert' )->text(),
469 'invert',
470 'nsinvert',
471 $opts['invert'],
472 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
473 ) . '&#160;';
474 $form .= Xml::checkLabel(
475 $this->msg( 'namespace_association' )->text(),
476 'associated',
477 'nsassociated',
478 $opts['associated'],
479 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
480 ) . '&#160;';
481 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
482 foreach ( $hiddenFields as $key => $value ) {
483 $form .= Html::hidden( $key, $value ) . "\n";
484 }
485 $form .= Xml::closeElement( 'fieldset' ) . "\n";
486 $form .= Xml::closeElement( 'form' ) . "\n";
487 $this->getOutput()->addHTML( $form );
488
489 $this->setBottomText( $opts );
490 }
491
492 function setTopText( FormOptions $opts ) {
493 $nondefaults = $opts->getChangedValues();
494 $form = "";
495 $user = $this->getUser();
496
497 $dbr = $this->getDB();
498 $numItems = $this->countItems( $dbr );
499 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
500
501 // Show watchlist header
502 $form .= "<p>";
503 if ( $numItems == 0 ) {
504 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
505 } else {
506 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
507 if ( $this->getConfig()->get( 'EnotifWatchlist' )
508 && $user->getOption( 'enotifwatchlistpages' )
509 ) {
510 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
511 }
512 if ( $showUpdatedMarker ) {
513 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
514 }
515 }
516 $form .= "</p>";
517
518 if ( $numItems > 0 && $showUpdatedMarker ) {
519 $form .= Xml::openElement( 'form', array( 'method' => 'post',
520 'action' => $this->getPageTitle()->getLocalURL(),
521 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
522 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
523 Html::hidden( 'reset', 'all' ) . "\n";
524 foreach ( $nondefaults as $key => $value ) {
525 $form .= Html::hidden( $key, $value ) . "\n";
526 }
527 $form .= Xml::closeElement( 'form' ) . "\n";
528 }
529
530 $form .= Xml::openElement( 'form', array(
531 'method' => 'post',
532 'action' => $this->getPageTitle()->getLocalURL(),
533 'id' => 'mw-watchlist-form'
534 ) );
535 $form .= Xml::fieldset(
536 $this->msg( 'watchlist-options' )->text(),
537 false,
538 array( 'id' => 'mw-watchlist-options' )
539 );
540
541 $form .= SpecialRecentChanges::makeLegend( $this->getContext() );
542
543 $this->getOutput()->addHTML( $form );
544 }
545
546 protected function showHideLink( $options, $message, $name, $value ) {
547 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
548 $options[$name] = 1 - (int)$value;
549
550 return $this->msg( $message )
551 ->rawParams( Linker::linkKnown( $this->getPageTitle(), $label, array(), $options ) )
552 ->escaped();
553 }
554
555 protected function hoursLink( $h, $options = array() ) {
556 $options['days'] = ( $h / 24.0 );
557
558 return Linker::linkKnown(
559 $this->getPageTitle(),
560 $this->getLanguage()->formatNum( $h ),
561 array(),
562 $options
563 );
564 }
565
566 protected function daysLink( $d, $options = array() ) {
567 $options['days'] = $d;
568
569 return Linker::linkKnown(
570 $this->getPageTitle(),
571 $this->getLanguage()->formatNum( $d ),
572 array(),
573 $options
574 );
575 }
576
577 /**
578 * Returns html
579 *
580 * @param int $days This gets overwritten, so is not used
581 * @param array $options Query parameters for URL
582 * @return string
583 */
584 protected function cutoffLinks( $days, $options = array() ) {
585 global $wgRCMaxAge;
586 $watchlistMaxDays = ceil( $wgRCMaxAge / ( 3600 * 24 ) );
587
588 $hours = array( 1, 2, 6, 12 );
589 $days = array( 1, 3, 7, $watchlistMaxDays );
590 $i = 0;
591 foreach ( $hours as $h ) {
592 $hours[$i++] = $this->hoursLink( $h, $options );
593 }
594 $i = 0;
595 foreach ( $days as $d ) {
596 $days[$i++] = $this->daysLink( $d, $options );
597 }
598
599 return $this->msg( 'wlshowlast' )->rawParams(
600 $this->getLanguage()->pipeList( $hours ),
601 $this->getLanguage()->pipeList( $days ) )->parse();
602 }
603
604 /**
605 * Count the number of items on a user's watchlist
606 *
607 * @param IDatabase $dbr A database connection
608 * @return int
609 */
610 protected function countItems( $dbr ) {
611 # Fetch the raw count
612 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
613 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
614 $row = $dbr->fetchObject( $rows );
615 $count = $row->count;
616
617 return floor( $count / 2 );
618 }
619 }