wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / specials / SpecialWatchlist.php
blobc894a793120e76c0d11d6104492a4c098b39a751
1 <?php
2 /**
3 * Implements Special:Watchlist
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * A special page that lists last changes made to the wiki,
26 * limited to user-defined list of titles.
28 * @ingroup SpecialPage
30 class SpecialWatchlist extends ChangesListSpecialPage {
31 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
32 parent::__construct( $page, $restriction );
35 /**
36 * Main execution point
38 * @param string $subpage
40 function execute( $subpage ) {
41 // Anons don't get a watchlist
42 $this->requireLogin( 'watchlistanontext' );
44 $output = $this->getOutput();
45 $request = $this->getRequest();
46 $this->addHelpLink( 'Help:Watching pages' );
47 $output->addModules( array(
48 'mediawiki.special.changeslist.visitedstatus',
49 ) );
51 $mode = SpecialEditWatchlist::getMode( $request, $subpage );
52 if ( $mode !== false ) {
53 if ( $mode === SpecialEditWatchlist::EDIT_RAW ) {
54 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'raw' );
55 } elseif ( $mode === SpecialEditWatchlist::EDIT_CLEAR ) {
56 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'clear' );
57 } else {
58 $title = SpecialPage::getTitleFor( 'EditWatchlist' );
61 $output->redirect( $title->getLocalURL() );
63 return;
66 $this->checkPermissions();
68 $user = $this->getUser();
69 $opts = $this->getOptions();
71 $config = $this->getConfig();
72 if ( ( $config->get( 'EnotifWatchlist' ) || $config->get( 'ShowUpdatedMarker' ) )
73 && $request->getVal( 'reset' )
74 && $request->wasPosted()
75 ) {
76 $user->clearAllNotifications();
77 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
79 return;
82 parent::execute( $subpage );
85 /**
86 * Return an array of subpages that this special page will accept.
88 * @see also SpecialEditWatchlist::getSubpagesForPrefixSearch
89 * @return string[] subpages
91 public function getSubpagesForPrefixSearch() {
92 return array(
93 'clear',
94 'edit',
95 'raw',
99 /**
100 * Get a FormOptions object containing the default options
102 * @return FormOptions
104 public function getDefaultOptions() {
105 $opts = parent::getDefaultOptions();
106 $user = $this->getUser();
108 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions::FLOAT );
109 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
110 if ( $this->getRequest()->getVal( 'action' ) == 'submit' ) {
111 // The user has submitted the form, so we dont need the default values
112 return $opts;
115 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
116 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
117 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
118 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
119 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
120 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
122 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
123 $opts->add( 'hidecategorization', $user->getBoolOption( 'watchlisthidecategorization' ) );
126 return $opts;
130 * Get custom show/hide filters
132 * @return array Map of filter URL param names to properties (msg/default)
134 protected function getCustomFilters() {
135 if ( $this->customFilters === null ) {
136 $this->customFilters = parent::getCustomFilters();
137 Hooks::run( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ), '1.23' );
140 return $this->customFilters;
144 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
146 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
147 * to the current ones.
149 * @param FormOptions $opts
150 * @return FormOptions
152 protected function fetchOptionsFromRequest( $opts ) {
153 static $compatibilityMap = array(
154 'hideMinor' => 'hideminor',
155 'hideBots' => 'hidebots',
156 'hideAnons' => 'hideanons',
157 'hideLiu' => 'hideliu',
158 'hidePatrolled' => 'hidepatrolled',
159 'hideOwn' => 'hidemyself',
162 $params = $this->getRequest()->getValues();
163 foreach ( $compatibilityMap as $from => $to ) {
164 if ( isset( $params[$from] ) ) {
165 $params[$to] = $params[$from];
166 unset( $params[$from] );
170 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
171 // methods defined on WebRequest and removing this dependency would cause some code duplication.
172 $request = new DerivativeRequest( $this->getRequest(), $params );
173 $opts->fetchValuesFromRequest( $request );
175 return $opts;
179 * Return an array of conditions depending of options set in $opts
181 * @param FormOptions $opts
182 * @return array
184 public function buildMainQueryConds( FormOptions $opts ) {
185 $dbr = $this->getDB();
186 $conds = parent::buildMainQueryConds( $opts );
188 // Calculate cutoff
189 if ( $opts['days'] > 0 ) {
190 $conds[] = 'rc_timestamp > ' .
191 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
194 return $conds;
198 * Process the query
200 * @param array $conds
201 * @param FormOptions $opts
202 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
204 public function doMainQuery( $conds, $opts ) {
205 $dbr = $this->getDB();
206 $user = $this->getUser();
208 # Toggle watchlist content (all recent edits or just the latest)
209 if ( $opts['extended'] ) {
210 $limitWatchlist = $user->getIntOption( 'wllimit' );
211 $usePage = false;
212 } else {
213 # Top log Ids for a page are not stored
214 $nonRevisionTypes = array( RC_LOG );
215 Hooks::run( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
216 if ( $nonRevisionTypes ) {
217 $conds[] = $dbr->makeList(
218 array(
219 'rc_this_oldid=page_latest',
220 'rc_type' => $nonRevisionTypes,
222 LIST_OR
225 $limitWatchlist = 0;
226 $usePage = true;
229 $tables = array( 'recentchanges', 'watchlist' );
230 $fields = RecentChange::selectFields();
231 $query_options = array( 'ORDER BY' => 'rc_timestamp DESC' );
232 $join_conds = array(
233 'watchlist' => array(
234 'INNER JOIN',
235 array(
236 'wl_user' => $user->getId(),
237 'wl_namespace=rc_namespace',
238 'wl_title=rc_title'
243 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
244 $fields[] = 'wl_notificationtimestamp';
246 if ( $limitWatchlist ) {
247 $query_options['LIMIT'] = $limitWatchlist;
250 $rollbacker = $user->isAllowed( 'rollback' );
251 if ( $usePage || $rollbacker ) {
252 $tables[] = 'page';
253 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
254 if ( $rollbacker ) {
255 $fields[] = 'page_latest';
259 // Log entries with DELETED_ACTION must not show up unless the user has
260 // the necessary rights.
261 if ( !$user->isAllowed( 'deletedhistory' ) ) {
262 $bitmask = LogPage::DELETED_ACTION;
263 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
264 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
265 } else {
266 $bitmask = 0;
268 if ( $bitmask ) {
269 $conds[] = $dbr->makeList( array(
270 'rc_type != ' . RC_LOG,
271 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
272 ), LIST_OR );
275 ChangeTags::modifyDisplayQuery(
276 $tables,
277 $fields,
278 $conds,
279 $join_conds,
280 $query_options,
284 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
286 return $dbr->select(
287 $tables,
288 $fields,
289 $conds,
290 __METHOD__,
291 $query_options,
292 $join_conds
296 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
297 &$join_conds, $opts
299 return parent::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
300 && Hooks::run(
301 'SpecialWatchlistQuery',
302 array( &$conds, &$tables, &$join_conds, &$fields, $opts ),
303 '1.23'
308 * Return a IDatabase object for reading
310 * @return IDatabase
312 protected function getDB() {
313 return wfGetDB( DB_SLAVE, 'watchlist' );
317 * Output feed links.
319 public function outputFeedLinks() {
320 $user = $this->getUser();
321 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
322 if ( $wlToken ) {
323 $this->addFeedLinks( array(
324 'action' => 'feedwatchlist',
325 'allrev' => 1,
326 'wlowner' => $user->getName(),
327 'wltoken' => $wlToken,
328 ) );
333 * Build and output the actual changes list.
335 * @param ResultWrapper $rows Database rows
336 * @param FormOptions $opts
338 public function outputChangesList( $rows, $opts ) {
339 $dbr = $this->getDB();
340 $user = $this->getUser();
341 $output = $this->getOutput();
343 # Show a message about slave lag, if applicable
344 $lag = wfGetLB()->safeGetLag( $dbr );
345 if ( $lag > 0 ) {
346 $output->showLagWarning( $lag );
349 # If no rows to display, show message before try to render the list
350 if ( $rows->numRows() == 0 ) {
351 $output->wrapWikiMsg(
352 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
354 return;
357 $dbr->dataSeek( $rows, 0 );
359 $list = ChangesList::newFromContext( $this->getContext() );
360 $list->setWatchlistDivs();
361 $list->initChangesListRows( $rows );
362 $dbr->dataSeek( $rows, 0 );
364 $s = $list->beginRecentChangesList();
365 $counter = 1;
366 foreach ( $rows as $obj ) {
367 # Make RC entry
368 $rc = RecentChange::newFromRow( $obj );
369 $rc->counter = $counter++;
371 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
372 $updated = $obj->wl_notificationtimestamp;
373 } else {
374 $updated = false;
377 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
378 && $user->getOption( 'shownumberswatching' )
380 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
381 'COUNT(*)',
382 array(
383 'wl_namespace' => $obj->rc_namespace,
384 'wl_title' => $obj->rc_title,
386 __METHOD__ );
387 } else {
388 $rc->numberofWatchingusers = 0;
391 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
392 if ( $changeLine !== false ) {
393 $s .= $changeLine;
396 $s .= $list->endRecentChangesList();
398 $output->addHTML( $s );
402 * Set the text to be displayed above the changes
404 * @param FormOptions $opts
405 * @param int $numRows Number of rows in the result to show after this header
407 public function doHeader( $opts, $numRows ) {
408 $user = $this->getUser();
409 $out = $this->getOutput();
411 // if the user wishes, that the watchlist is reloaded, whenever a filter changes,
412 // add the module for that
413 if ( $user->getBoolOption( 'watchlistreloadautomatically' ) ) {
414 $out->addModules( array( 'mediawiki.special.watchlist' ) );
417 $out->addSubtitle(
418 $this->msg( 'watchlistfor2', $user->getName() )
419 ->rawParams( SpecialEditWatchlist::buildTools( null ) )
422 $this->setTopText( $opts );
424 $lang = $this->getLanguage();
425 $wlInfo = '';
426 if ( $opts['days'] > 0 ) {
427 $timestamp = wfTimestampNow();
428 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $opts['days'] * 24 ) )->params(
429 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
430 )->parse() . "<br />\n";
433 $nondefaults = $opts->getChangedValues();
434 $cutofflinks = $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts );
436 # Spit out some control panel links
437 $filters = array(
438 'hideminor' => 'wlshowhideminor',
439 'hidebots' => 'wlshowhidebots',
440 'hideanons' => 'wlshowhideanons',
441 'hideliu' => 'wlshowhideliu',
442 'hidemyself' => 'wlshowhidemine',
443 'hidepatrolled' => 'wlshowhidepatr'
446 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
447 $filters['hidecategorization'] = 'wlshowhidecategorization';
450 foreach ( $this->getCustomFilters() as $key => $params ) {
451 $filters[$key] = $params['msg'];
453 // Disable some if needed
454 if ( !$user->useRCPatrol() ) {
455 unset( $filters['hidepatrolled'] );
458 $links = array();
459 foreach ( $filters as $name => $msg ) {
460 $links[] = $this->showHideCheck( $nondefaults, $msg, $name, $opts[$name] );
463 $hiddenFields = $nondefaults;
464 $hiddenFields['action'] = 'submit';
465 unset( $hiddenFields['namespace'] );
466 unset( $hiddenFields['invert'] );
467 unset( $hiddenFields['associated'] );
468 unset( $hiddenFields['days'] );
469 foreach ( $filters as $key => $value ) {
470 unset( $hiddenFields[$key] );
473 # Create output
474 $form = '';
476 # Namespace filter and put the whole form together.
477 $form .= $wlInfo;
478 $form .= $cutofflinks;
479 $form .= $this->msg( 'watchlist-hide' ) .
480 $this->msg( 'colon-separator' )->escaped() .
481 implode( ' ', $links );
482 $form .= "\n<br />\n";
483 $form .= Html::namespaceSelector(
484 array(
485 'selected' => $opts['namespace'],
486 'all' => '',
487 'label' => $this->msg( 'namespace' )->text()
488 ), array(
489 'name' => 'namespace',
490 'id' => 'namespace',
491 'class' => 'namespaceselector',
493 ) . "\n";
494 $form .= '<span class="mw-input-with-label">' . Xml::checkLabel(
495 $this->msg( 'invert' )->text(),
496 'invert',
497 'nsinvert',
498 $opts['invert'],
499 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
500 ) . "</span>\n";
501 $form .= '<span class="mw-input-with-label">' . Xml::checkLabel(
502 $this->msg( 'namespace_association' )->text(),
503 'associated',
504 'nsassociated',
505 $opts['associated'],
506 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
507 ) . "</span>\n";
508 $form .= Xml::submitButton( $this->msg( 'watchlist-submit' )->text() ) . "\n";
509 foreach ( $hiddenFields as $key => $value ) {
510 $form .= Html::hidden( $key, $value ) . "\n";
512 $form .= Xml::closeElement( 'fieldset' ) . "\n";
513 $form .= Xml::closeElement( 'form' ) . "\n";
514 $this->getOutput()->addHTML( $form );
516 $this->setBottomText( $opts );
519 function cutoffselector( $options ) {
520 // Cast everything to strings immediately, so that we know all of the values have the same
521 // precision, and can be compared with '==='. 2/24 has a few more decimal places than its
522 // default string representation, for example, and would confuse comparisons.
524 // Misleadingly, the 'days' option supports hours too.
525 $days = array_map( 'strval', array( 1/24, 2/24, 6/24, 12/24, 1, 3, 7 ) );
527 $userWatchlistOption = (string)$this->getUser()->getOption( 'watchlistdays' );
528 // add the user preference, if it isn't available already
529 if ( !in_array( $userWatchlistOption, $days ) && $userWatchlistOption !== '0' ) {
530 $days[] = $userWatchlistOption;
533 $selected = (string)$options['days'];
534 // add the currently selected value, if it isn't available already
535 if ( !in_array( $selected, $days ) && $selected !== '0' ) {
536 $days[] = $selected;
539 $select = new XmlSelect( 'days', 'days', $selected );
541 asort( $days );
542 foreach ( $days as $value ) {
543 if ( $value < 1 ) {
544 $name = $this->msg( 'hours' )->numParams( $value * 24 )->text();
545 } else {
546 $name = $this->msg( 'days' )->numParams( $value )->text();
548 $select->addOption( $name, $value );
551 // 'all' option
552 $name = $this->msg( 'watchlistall2' )->text();
553 $value = '0';
554 $select->addOption( $name, $value );
556 return $select->getHTML() . "\n<br />\n";
559 function setTopText( FormOptions $opts ) {
560 $nondefaults = $opts->getChangedValues();
561 $form = "";
562 $user = $this->getUser();
564 $dbr = $this->getDB();
565 $numItems = $this->countItems( $dbr );
566 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
568 // Show watchlist header
569 $form .= "<p>";
570 if ( $numItems == 0 ) {
571 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
572 } else {
573 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
574 if ( $this->getConfig()->get( 'EnotifWatchlist' )
575 && $user->getOption( 'enotifwatchlistpages' )
577 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
579 if ( $showUpdatedMarker ) {
580 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
583 $form .= "</p>";
585 if ( $numItems > 0 && $showUpdatedMarker ) {
586 $form .= Xml::openElement( 'form', array( 'method' => 'post',
587 'action' => $this->getPageTitle()->getLocalURL(),
588 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
589 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
590 Html::hidden( 'reset', 'all' ) . "\n";
591 foreach ( $nondefaults as $key => $value ) {
592 $form .= Html::hidden( $key, $value ) . "\n";
594 $form .= Xml::closeElement( 'form' ) . "\n";
597 $form .= Xml::openElement( 'form', array(
598 'method' => 'get',
599 'action' => $this->getPageTitle()->getLocalURL(),
600 'id' => 'mw-watchlist-form'
601 ) );
602 $form .= Xml::fieldset(
603 $this->msg( 'watchlist-options' )->text(),
604 false,
605 array( 'id' => 'mw-watchlist-options' )
608 $form .= SpecialRecentChanges::makeLegend( $this->getContext() );
610 $this->getOutput()->addHTML( $form );
613 protected function showHideCheck( $options, $message, $name, $value ) {
614 $options[$name] = 1 - (int)$value;
616 return '<span class="mw-input-with-label">' . Xml::checkLabel(
617 $this->msg( $message, '' )->text(),
618 $name,
619 $name,
620 (int)$value
621 ) . '</span>';
625 * Count the number of items on a user's watchlist
627 * @param IDatabase $dbr A database connection
628 * @return int
630 protected function countItems( $dbr ) {
631 # Fetch the raw count
632 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
633 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
634 $row = $dbr->fetchObject( $rows );
635 $count = $row->count;
637 return floor( $count / 2 );