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
21 * @ingroup SpecialPage
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 );
36 * Main execution point
38 * @param string $subpage
40 function execute( $subpage ) {
41 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
43 // Anons don't get a watchlist
44 $this->requireLogin( 'watchlistanontext' );
46 $output = $this->getOutput();
47 $request = $this->getRequest();
49 $mode = SpecialEditWatchlist
::getMode( $request, $subpage );
50 if ( $mode !== false ) {
51 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
52 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
53 } elseif ( $mode === SpecialEditWatchlist
::EDIT_CLEAR
) {
54 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'clear' );
56 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
59 $output->redirect( $title->getLocalURL() );
64 $this->checkPermissions();
66 $user = $this->getUser();
67 $opts = $this->getOptions();
69 if ( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker )
70 && $request->getVal( 'reset' )
71 && $request->wasPosted()
73 $user->clearAllNotifications();
74 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
79 parent
::execute( $subpage );
83 * Return an array of subpages beginning with $search that this special page will accept.
85 * @param string $search Prefix to search for
86 * @param int $limit Maximum number of results to return
87 * @return string[] Matching subpages
89 public function prefixSearchSubpages( $search, $limit = 10 ) {
90 // See also SpecialEditWatchlist::prefixSearchSubpages
91 return self
::prefixSearchArray(
103 * Get a FormOptions object containing the default options
105 * @return FormOptions
107 public function getDefaultOptions() {
108 $opts = parent
::getDefaultOptions();
109 $user = $this->getUser();
111 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
113 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
114 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
115 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
116 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
117 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
118 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
120 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
126 * Get custom show/hide filters
128 * @return array Map of filter URL param names to properties (msg/default)
130 protected function getCustomFilters() {
131 if ( $this->customFilters
=== null ) {
132 $this->customFilters
= parent
::getCustomFilters();
133 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
), '1.23' );
136 return $this->customFilters
;
140 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
142 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
143 * to the current ones.
145 * @param FormOptions $opts
146 * @return FormOptions
148 protected function fetchOptionsFromRequest( $opts ) {
149 static $compatibilityMap = array(
150 'hideMinor' => 'hideminor',
151 'hideBots' => 'hidebots',
152 'hideAnons' => 'hideanons',
153 'hideLiu' => 'hideliu',
154 'hidePatrolled' => 'hidepatrolled',
155 'hideOwn' => 'hidemyself',
158 $params = $this->getRequest()->getValues();
159 foreach ( $compatibilityMap as $from => $to ) {
160 if ( isset( $params[$from] ) ) {
161 $params[$to] = $params[$from];
162 unset( $params[$from] );
166 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
167 // methods defined on WebRequest and removing this dependency would cause some code duplication.
168 $request = new DerivativeRequest( $this->getRequest(), $params );
169 $opts->fetchValuesFromRequest( $request );
175 * Return an array of conditions depending of options set in $opts
177 * @param FormOptions $opts
180 public function buildMainQueryConds( FormOptions
$opts ) {
181 $dbr = $this->getDB();
182 $conds = parent
::buildMainQueryConds( $opts );
185 if ( $opts['days'] > 0 ) {
186 $conds[] = 'rc_timestamp > ' .
187 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
196 * @param array $conds
197 * @param FormOptions $opts
198 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
200 public function doMainQuery( $conds, $opts ) {
201 global $wgShowUpdatedMarker;
203 $dbr = $this->getDB();
204 $user = $this->getUser();
206 # Toggle watchlist content (all recent edits or just the latest)
207 if ( $opts['extended'] ) {
208 $limitWatchlist = $user->getIntOption( 'wllimit' );
211 # Top log Ids for a page are not stored
212 $nonRevisionTypes = array( RC_LOG
);
213 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
214 if ( $nonRevisionTypes ) {
215 $conds[] = $dbr->makeList(
217 'rc_this_oldid=page_latest',
218 'rc_type' => $nonRevisionTypes,
227 $tables = array( 'recentchanges', 'watchlist' );
228 $fields = RecentChange
::selectFields();
229 $query_options = array( 'ORDER BY' => 'rc_timestamp DESC' );
231 'watchlist' => array(
234 'wl_user' => $user->getId(),
235 'wl_namespace=rc_namespace',
241 if ( $wgShowUpdatedMarker ) {
242 $fields[] = 'wl_notificationtimestamp';
244 if ( $limitWatchlist ) {
245 $query_options['LIMIT'] = $limitWatchlist;
248 $rollbacker = $user->isAllowed( 'rollback' );
249 if ( $usePage ||
$rollbacker ) {
251 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
253 $fields[] = 'page_latest';
257 // Log entries with DELETED_ACTION must not show up unless the user has
258 // the necessary rights.
259 if ( !$user->isAllowed( 'deletedhistory' ) ) {
260 $bitmask = LogPage
::DELETED_ACTION
;
261 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
262 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
267 $conds[] = $dbr->makeList( array(
268 'rc_type != ' . RC_LOG
,
269 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
273 ChangeTags
::modifyDisplayQuery(
282 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
294 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ) {
295 return parent
::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
297 'SpecialWatchlistQuery',
298 array( &$conds, &$tables, &$join_conds, &$fields, $opts ),
304 * Return a DatabaseBase object for reading
306 * @return DatabaseBase
308 protected function getDB() {
309 return wfGetDB( DB_SLAVE
, 'watchlist' );
315 public function outputFeedLinks() {
316 $user = $this->getUser();
317 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
319 $this->addFeedLinks( array(
320 'action' => 'feedwatchlist',
322 'wlowner' => $user->getName(),
323 'wltoken' => $wlToken,
329 * Build and output the actual changes list.
331 * @param ResultWrapper $rows Database rows
332 * @param FormOptions $opts
334 public function outputChangesList( $rows, $opts ) {
335 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
337 $dbr = $this->getDB();
338 $user = $this->getUser();
339 $output = $this->getOutput();
341 # Show a message about slave lag, if applicable
342 $lag = wfGetLB()->safeGetLag( $dbr );
344 $output->showLagWarning( $lag );
347 # If no rows to display, show message before try to render the list
348 if ( $rows->numRows() == 0 ) {
349 $output->wrapWikiMsg(
350 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
355 $dbr->dataSeek( $rows, 0 );
357 $list = ChangesList
::newFromContext( $this->getContext() );
358 $list->setWatchlistDivs();
359 $list->initChangesListRows( $rows );
360 $dbr->dataSeek( $rows, 0 );
362 $s = $list->beginRecentChangesList();
364 foreach ( $rows as $obj ) {
366 $rc = RecentChange
::newFromRow( $obj );
367 $rc->counter
= $counter++
;
369 if ( $wgShowUpdatedMarker ) {
370 $updated = $obj->wl_notificationtimestamp
;
375 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
376 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
379 'wl_namespace' => $obj->rc_namespace
,
380 'wl_title' => $obj->rc_title
,
384 $rc->numberofWatchingusers
= 0;
387 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
388 if ( $changeLine !== false ) {
392 $s .= $list->endRecentChangesList();
394 $output->addHTML( $s );
398 * Set the text to be displayed above the changes
400 * @param FormOptions $opts
401 * @param int $numRows Number of rows in the result to show after this header
403 public function doHeader( $opts, $numRows ) {
404 $user = $this->getUser();
406 $this->getOutput()->addSubtitle(
407 $this->msg( 'watchlistfor2', $user->getName() )
408 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
411 $this->setTopText( $opts );
413 $lang = $this->getLanguage();
415 if ( $opts['days'] > 0 ) {
416 $timestamp = wfTimestampNow();
417 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $opts['days'] * 24 ) )->params(
418 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
419 )->parse() . "<br />\n";
422 $nondefaults = $opts->getChangedValues();
423 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
425 # Spit out some control panel links
427 'hideminor' => 'rcshowhideminor',
428 'hidebots' => 'rcshowhidebots',
429 'hideanons' => 'rcshowhideanons',
430 'hideliu' => 'rcshowhideliu',
431 'hidemyself' => 'rcshowhidemine',
432 'hidepatrolled' => 'rcshowhidepatr'
434 foreach ( $this->getCustomFilters() as $key => $params ) {
435 $filters[$key] = $params['msg'];
437 // Disable some if needed
438 if ( !$user->useNPPatrol() ) {
439 unset( $filters['hidepatrolled'] );
443 foreach ( $filters as $name => $msg ) {
444 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
447 $hiddenFields = $nondefaults;
448 unset( $hiddenFields['namespace'] );
449 unset( $hiddenFields['invert'] );
450 unset( $hiddenFields['associated'] );
455 # Namespace filter and put the whole form together.
457 $form .= $cutofflinks;
458 $form .= $lang->pipeList( $links ) . "\n";
459 $form .= "<hr />\n<p>";
460 $form .= Html
::namespaceSelector(
462 'selected' => $opts['namespace'],
464 'label' => $this->msg( 'namespace' )->text()
466 'name' => 'namespace',
468 'class' => 'namespaceselector',
471 $form .= Xml
::checkLabel(
472 $this->msg( 'invert' )->text(),
476 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
478 $form .= Xml
::checkLabel(
479 $this->msg( 'namespace_association' )->text(),
483 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
485 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
486 foreach ( $hiddenFields as $key => $value ) {
487 $form .= Html
::hidden( $key, $value ) . "\n";
489 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
490 $form .= Xml
::closeElement( 'form' ) . "\n";
491 $this->getOutput()->addHTML( $form );
493 $this->setBottomText( $opts );
496 function setTopText( FormOptions
$opts ) {
497 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
499 $nondefaults = $opts->getChangedValues();
501 $user = $this->getUser();
503 $dbr = $this->getDB();
504 $numItems = $this->countItems( $dbr );
506 // Show watchlist header
508 if ( $numItems == 0 ) {
509 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
511 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
512 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
513 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
515 if ( $wgShowUpdatedMarker ) {
516 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
521 if ( $numItems > 0 && $wgShowUpdatedMarker ) {
522 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
523 'action' => $this->getPageTitle()->getLocalURL(),
524 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
525 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
526 Html
::hidden( 'reset', 'all' ) . "\n";
527 foreach ( $nondefaults as $key => $value ) {
528 $form .= Html
::hidden( $key, $value ) . "\n";
530 $form .= Xml
::closeElement( 'form' ) . "\n";
533 $form .= Xml
::openElement( 'form', array(
535 'action' => $this->getPageTitle()->getLocalURL(),
536 'id' => 'mw-watchlist-form'
538 $form .= Xml
::fieldset(
539 $this->msg( 'watchlist-options' )->text(),
541 array( 'id' => 'mw-watchlist-options' )
544 $form .= SpecialRecentChanges
::makeLegend( $this->getContext() );
546 $this->getOutput()->addHTML( $form );
549 protected function showHideLink( $options, $message, $name, $value ) {
550 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
551 $options[$name] = 1 - (int)$value;
553 return $this->msg( $message )
554 ->rawParams( Linker
::linkKnown( $this->getPageTitle(), $label, array(), $options ) )
558 protected function hoursLink( $h, $options = array() ) {
559 $options['days'] = ( $h / 24.0 );
561 return Linker
::linkKnown(
562 $this->getPageTitle(),
563 $this->getLanguage()->formatNum( $h ),
569 protected function daysLink( $d, $options = array() ) {
570 $options['days'] = $d;
571 $message = $d ?
$this->getLanguage()->formatNum( $d )
572 : $this->msg( 'watchlistall2' )->escaped();
574 return Linker
::linkKnown(
575 $this->getPageTitle(),
585 * @param int $days This gets overwritten, so is not used
586 * @param array $options Query parameters for URL
589 protected function cutoffLinks( $days, $options = array() ) {
590 $hours = array( 1, 2, 6, 12 );
591 $days = array( 1, 3, 7 );
593 foreach ( $hours as $h ) {
594 $hours[$i++
] = $this->hoursLink( $h, $options );
597 foreach ( $days as $d ) {
598 $days[$i++
] = $this->daysLink( $d, $options );
601 return $this->msg( 'wlshowlast' )->rawParams(
602 $this->getLanguage()->pipeList( $hours ),
603 $this->getLanguage()->pipeList( $days ),
604 $this->daysLink( 0, $options ) )->parse();
608 * Count the number of items on a user's watchlist
610 * @param DatabaseBase $dbr A database connection
613 protected function countItems( $dbr ) {
614 # Fetch the raw count
615 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
616 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
617 $row = $dbr->fetchObject( $rows );
618 $count = $row->count
;
620 return floor( $count / 2 );