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 // Anons don't get a watchlist
42 $this->requireLogin( 'watchlistanontext' );
44 $output = $this->getOutput();
45 $request = $this->getRequest();
46 $this->addHelpLink( 'Help:Watching pages' );
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' );
55 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
58 $output->redirect( $title->getLocalURL() );
63 $this->checkPermissions();
65 $user = $this->getUser();
66 $opts = $this->getOptions();
68 $config = $this->getConfig();
69 if ( ( $config->get( 'EnotifWatchlist' ) ||
$config->get( 'ShowUpdatedMarker' ) )
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 that this special page will accept.
85 * @see also SpecialEditWatchlist::getSubpagesForPrefixSearch
86 * @return string[] subpages
88 public function getSubpagesForPrefixSearch() {
97 * Get a FormOptions object containing the default options
101 public function getDefaultOptions() {
102 $opts = parent
::getDefaultOptions();
103 $user = $this->getUser();
105 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
106 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
107 if ( $this->getRequest()->getVal( 'action' ) == 'submit' ) {
108 // The user has submitted the form, so we dont need the default values
112 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
113 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
114 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
115 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
116 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
117 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
119 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
120 $opts->add( 'hidecategorization', $user->getBoolOption( 'watchlisthidecategorization' ) );
127 * Get custom show/hide filters
129 * @return array Map of filter URL param names to properties (msg/default)
131 protected function getCustomFilters() {
132 if ( $this->customFilters
=== null ) {
133 $this->customFilters
= parent
::getCustomFilters();
134 Hooks
::run( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
), '1.23' );
137 return $this->customFilters
;
141 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
143 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
144 * to the current ones.
146 * @param FormOptions $opts
147 * @return FormOptions
149 protected function fetchOptionsFromRequest( $opts ) {
150 static $compatibilityMap = array(
151 'hideMinor' => 'hideminor',
152 'hideBots' => 'hidebots',
153 'hideAnons' => 'hideanons',
154 'hideLiu' => 'hideliu',
155 'hidePatrolled' => 'hidepatrolled',
156 'hideOwn' => 'hidemyself',
159 $params = $this->getRequest()->getValues();
160 foreach ( $compatibilityMap as $from => $to ) {
161 if ( isset( $params[$from] ) ) {
162 $params[$to] = $params[$from];
163 unset( $params[$from] );
167 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
168 // methods defined on WebRequest and removing this dependency would cause some code duplication.
169 $request = new DerivativeRequest( $this->getRequest(), $params );
170 $opts->fetchValuesFromRequest( $request );
176 * Return an array of conditions depending of options set in $opts
178 * @param FormOptions $opts
181 public function buildMainQueryConds( FormOptions
$opts ) {
182 $dbr = $this->getDB();
183 $conds = parent
::buildMainQueryConds( $opts );
186 if ( $opts['days'] > 0 ) {
187 $conds[] = 'rc_timestamp > ' .
188 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
197 * @param array $conds
198 * @param FormOptions $opts
199 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
201 public function doMainQuery( $conds, $opts ) {
202 $dbr = $this->getDB();
203 $user = $this->getUser();
205 # Toggle watchlist content (all recent edits or just the latest)
206 if ( $opts['extended'] ) {
207 $limitWatchlist = $user->getIntOption( 'wllimit' );
210 # Top log Ids for a page are not stored
211 $nonRevisionTypes = array( RC_LOG
);
212 Hooks
::run( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
213 if ( $nonRevisionTypes ) {
214 $conds[] = $dbr->makeList(
216 'rc_this_oldid=page_latest',
217 'rc_type' => $nonRevisionTypes,
226 $tables = array( 'recentchanges', 'watchlist' );
227 $fields = RecentChange
::selectFields();
228 $query_options = array( 'ORDER BY' => 'rc_timestamp DESC' );
230 'watchlist' => array(
233 'wl_user' => $user->getId(),
234 'wl_namespace=rc_namespace',
240 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
241 $fields[] = 'wl_notificationtimestamp';
243 if ( $limitWatchlist ) {
244 $query_options['LIMIT'] = $limitWatchlist;
247 $rollbacker = $user->isAllowed( 'rollback' );
248 if ( $usePage ||
$rollbacker ) {
250 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
252 $fields[] = 'page_latest';
256 // Log entries with DELETED_ACTION must not show up unless the user has
257 // the necessary rights.
258 if ( !$user->isAllowed( 'deletedhistory' ) ) {
259 $bitmask = LogPage
::DELETED_ACTION
;
260 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
261 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
266 $conds[] = $dbr->makeList( array(
267 'rc_type != ' . RC_LOG
,
268 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
272 ChangeTags
::modifyDisplayQuery(
281 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
293 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
296 return parent
::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
298 'SpecialWatchlistQuery',
299 array( &$conds, &$tables, &$join_conds, &$fields, $opts ),
305 * Return a IDatabase object for reading
309 protected function getDB() {
310 return wfGetDB( DB_SLAVE
, 'watchlist' );
316 public function outputFeedLinks() {
317 $user = $this->getUser();
318 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
320 $this->addFeedLinks( array(
321 'action' => 'feedwatchlist',
323 'wlowner' => $user->getName(),
324 'wltoken' => $wlToken,
330 * Build and output the actual changes list.
332 * @param ResultWrapper $rows Database rows
333 * @param FormOptions $opts
335 public function outputChangesList( $rows, $opts ) {
336 $dbr = $this->getDB();
337 $user = $this->getUser();
338 $output = $this->getOutput();
340 # Show a message about slave lag, if applicable
341 $lag = wfGetLB()->safeGetLag( $dbr );
343 $output->showLagWarning( $lag );
346 # If no rows to display, show message before try to render the list
347 if ( $rows->numRows() == 0 ) {
348 $output->wrapWikiMsg(
349 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
354 $dbr->dataSeek( $rows, 0 );
356 $list = ChangesList
::newFromContext( $this->getContext() );
357 $list->setWatchlistDivs();
358 $list->initChangesListRows( $rows );
359 $dbr->dataSeek( $rows, 0 );
361 $s = $list->beginRecentChangesList();
363 foreach ( $rows as $obj ) {
365 $rc = RecentChange
::newFromRow( $obj );
366 $rc->counter
= $counter++
;
368 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
369 $updated = $obj->wl_notificationtimestamp
;
374 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
375 && $user->getOption( 'shownumberswatching' )
377 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
380 'wl_namespace' => $obj->rc_namespace
,
381 'wl_title' => $obj->rc_title
,
385 $rc->numberofWatchingusers
= 0;
388 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
389 if ( $changeLine !== false ) {
393 $s .= $list->endRecentChangesList();
395 $output->addHTML( $s );
399 * Set the text to be displayed above the changes
401 * @param FormOptions $opts
402 * @param int $numRows Number of rows in the result to show after this header
404 public function doHeader( $opts, $numRows ) {
405 $user = $this->getUser();
407 $this->getOutput()->addSubtitle(
408 $this->msg( 'watchlistfor2', $user->getName() )
409 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
412 $this->setTopText( $opts );
414 $lang = $this->getLanguage();
416 if ( $opts['days'] > 0 ) {
417 $timestamp = wfTimestampNow();
418 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $opts['days'] * 24 ) )->params(
419 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
420 )->parse() . "<br />\n";
423 $nondefaults = $opts->getChangedValues();
424 $cutofflinks = $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts );
426 # Spit out some control panel links
428 'hideminor' => 'wlshowhideminor',
429 'hidebots' => 'wlshowhidebots',
430 'hideanons' => 'wlshowhideanons',
431 'hideliu' => 'wlshowhideliu',
432 'hidemyself' => 'wlshowhidemine',
433 'hidepatrolled' => 'wlshowhidepatr'
436 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
437 $filters['hidecategorization'] = 'rcshowhidecategorization';
440 foreach ( $this->getCustomFilters() as $key => $params ) {
441 $filters[$key] = $params['msg'];
443 // Disable some if needed
444 if ( !$user->useRCPatrol() ) {
445 unset( $filters['hidepatrolled'] );
449 foreach ( $filters as $name => $msg ) {
450 $links[] = $this->showHideCheck( $nondefaults, $msg, $name, $opts[$name] );
453 $hiddenFields = $nondefaults;
454 $hiddenFields['action'] = 'submit';
455 unset( $hiddenFields['namespace'] );
456 unset( $hiddenFields['invert'] );
457 unset( $hiddenFields['associated'] );
458 unset( $hiddenFields['days'] );
459 foreach ( $filters as $key => $value ) {
460 unset( $hiddenFields[$key] );
466 # Namespace filter and put the whole form together.
468 $form .= $cutofflinks;
469 $form .= $this->msg( 'hide' ) .
470 $this->msg( 'colon-separator' )->escaped() .
471 implode( ' ', $links );
472 $form .= "\n<hr />\n<p>";
473 $form .= Html
::namespaceSelector(
475 'selected' => $opts['namespace'],
477 'label' => $this->msg( 'namespace' )->text()
479 'name' => 'namespace',
481 'class' => 'namespaceselector',
484 $form .= Xml
::checkLabel(
485 $this->msg( 'invert' )->text(),
489 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
491 $form .= Xml
::checkLabel(
492 $this->msg( 'namespace_association' )->text(),
496 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
498 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
499 foreach ( $hiddenFields as $key => $value ) {
500 $form .= Html
::hidden( $key, $value ) . "\n";
502 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
503 $form .= Xml
::closeElement( 'form' ) . "\n";
504 $this->getOutput()->addHTML( $form );
506 $this->setBottomText( $opts );
509 function cutoffselector( $options ) {
512 $hours = array( 1, 2, 6, 12 );
513 $days = array( 1, 3, 7 );
514 foreach ( $hours as $h ) {
515 $name = $this->msg( 'hours', $h );
517 $selected = ( $value == $options['days'] ) ?
true : false;
519 $selectOptions .= Xml
::option( $name, $value, $selected );
521 foreach ( $days as $d ) {
522 $name = $this->msg( 'days', $d );
524 $selected = ( $value == $options['days'] ) ?
true : false;
526 $selectOptions .= Xml
::option( $name, $value, $selected );
530 $name = $this->msg( 'watchlistall2' );
532 $selected = ( $value == $options['days'] ) ?
true : false;
533 $selectOptions .= Xml
::option( $name, $value, $selected );
535 $attribs = array( "name" => "days", "id" => "days" );
536 return Xml
::openElement( 'select', $attribs )
540 . Xml
::closeElement( 'select' )
544 function setTopText( FormOptions
$opts ) {
545 $nondefaults = $opts->getChangedValues();
547 $user = $this->getUser();
549 $dbr = $this->getDB();
550 $numItems = $this->countItems( $dbr );
551 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
553 // Show watchlist header
555 if ( $numItems == 0 ) {
556 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
558 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
559 if ( $this->getConfig()->get( 'EnotifWatchlist' )
560 && $user->getOption( 'enotifwatchlistpages' )
562 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
564 if ( $showUpdatedMarker ) {
565 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
570 if ( $numItems > 0 && $showUpdatedMarker ) {
571 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
572 'action' => $this->getPageTitle()->getLocalURL(),
573 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
574 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
575 Html
::hidden( 'reset', 'all' ) . "\n";
576 foreach ( $nondefaults as $key => $value ) {
577 $form .= Html
::hidden( $key, $value ) . "\n";
579 $form .= Xml
::closeElement( 'form' ) . "\n";
582 $form .= Xml
::openElement( 'form', array(
584 'action' => $this->getPageTitle()->getLocalURL(),
585 'id' => 'mw-watchlist-form'
587 $form .= Xml
::fieldset(
588 $this->msg( 'watchlist-options' )->text(),
590 array( 'id' => 'mw-watchlist-options' )
593 $form .= SpecialRecentChanges
::makeLegend( $this->getContext() );
595 $this->getOutput()->addHTML( $form );
598 protected function showHideCheck( $options, $message, $name, $value ) {
599 $options[$name] = 1 - (int)$value;
601 return Xml
::checkLabel(
602 $this->msg( $message, '' )->text(),
610 * Count the number of items on a user's watchlist
612 * @param IDatabase $dbr A database connection
615 protected function countItems( $dbr ) {
616 # Fetch the raw count
617 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
618 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
619 $row = $dbr->fetchObject( $rows );
620 $count = $row->count
;
622 return floor( $count / 2 );