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
{
34 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
35 parent
::__construct( $page, $restriction );
39 * Main execution point
41 * @param string $subpage
43 function execute( $subpage ) {
44 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
46 // Anons don't get a watchlist
47 $this->requireLogin( 'watchlistanontext' );
49 $output = $this->getOutput();
50 $request = $this->getRequest();
52 $mode = SpecialEditWatchlist
::getMode( $request, $subpage );
53 if ( $mode !== false ) {
54 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
55 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
57 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
60 $output->redirect( $title->getLocalURL() );
64 $this->checkPermissions();
66 $user = $this->getUser();
67 $opts = $this->getOptions();
69 if ( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
70 $request->wasPosted() )
72 $user->clearAllNotifications();
73 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
77 parent
::execute( $subpage );
81 * Get a FormOptions object containing the default options
85 public function getDefaultOptions() {
86 $opts = parent
::getDefaultOptions();
87 $user = $this->getUser();
89 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
91 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
92 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
93 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
94 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
95 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
96 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
98 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
104 * Get custom show/hide filters
106 * @return array Map of filter URL param names to properties (msg/default)
108 protected function getCustomFilters() {
109 if ( $this->customFilters
=== null ) {
110 $this->customFilters
= array();
111 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
) );
114 return $this->customFilters
;
118 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
120 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
121 * to the current ones.
123 * @param FormOptions $parameters
124 * @return FormOptions
126 protected function fetchOptionsFromRequest( $opts ) {
127 static $compatibilityMap = array(
128 'hideMinor' => 'hideminor',
129 'hideBots' => 'hidebots',
130 'hideAnons' => 'hideanons',
131 'hideLiu' => 'hideliu',
132 'hidePatrolled' => 'hidepatrolled',
133 'hideOwn' => 'hidemyself',
136 $params = $this->getRequest()->getValues();
137 foreach ( $compatibilityMap as $from => $to ) {
138 if ( isset( $params[$from] ) ) {
139 $params[$to] = $params[$from];
140 unset( $params[$from] );
144 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
145 // methods defined on WebRequest and removing this dependency would cause some code duplication.
146 $request = new DerivativeRequest( $this->getRequest(), $params );
147 $opts->fetchValuesFromRequest( $request );
152 * Return an array of conditions depending of options set in $opts
154 * @param FormOptions $opts
157 public function buildMainQueryConds( FormOptions
$opts ) {
158 $dbr = $this->getDB();
159 $conds = parent
::buildMainQueryConds( $opts );
162 if ( $opts['days'] > 0 ) {
163 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
172 * @param array $conds
173 * @param FormOptions $opts
174 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
176 public function doMainQuery( $conds, $opts ) {
177 global $wgShowUpdatedMarker;
179 $dbr = $this->getDB();
180 $user = $this->getUser();
182 # Toggle watchlist content (all recent edits or just the latest)
183 if ( $opts['extended'] ) {
184 $limitWatchlist = $user->getIntOption( 'wllimit' );
187 # Top log Ids for a page are not stored
188 $nonRevisionTypes = array( RC_LOG
);
189 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
190 if ( $nonRevisionTypes ) {
191 $conds[] = $dbr->makeList(
193 'rc_this_oldid=page_latest',
194 'rc_type' => $nonRevisionTypes,
203 $tables = array( 'recentchanges', 'watchlist' );
204 $fields = RecentChange
::selectFields();
205 $query_options = array( 'ORDER BY' => 'rc_timestamp DESC' );
207 'watchlist' => array(
210 'wl_user' => $user->getId(),
211 'wl_namespace=rc_namespace',
217 if ( $wgShowUpdatedMarker ) {
218 $fields[] = 'wl_notificationtimestamp';
220 if ( $limitWatchlist ) {
221 $query_options['LIMIT'] = $limitWatchlist;
224 $rollbacker = $user->isAllowed( 'rollback' );
225 if ( $usePage ||
$rollbacker ) {
227 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
229 $fields[] = 'page_latest';
233 // Log entries with DELETED_ACTION must not show up unless the user has
234 // the necessary rights.
235 if ( !$user->isAllowed( 'deletedhistory' ) ) {
236 $bitmask = LogPage
::DELETED_ACTION
;
237 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
238 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
243 $conds[] = $dbr->makeList( array(
244 'rc_type != ' . RC_LOG
,
245 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
249 ChangeTags
::modifyDisplayQuery(
258 wfRunHooks( 'SpecialWatchlistQuery',
259 array( &$conds, &$tables, &$join_conds, &$fields, $opts ) );
272 * Return a DatabaseBase object for reading
274 * @return DatabaseBase
276 protected function getDB() {
277 return wfGetDB( DB_SLAVE
, 'watchlist' );
283 public function outputFeedLinks() {
284 $user = $this->getUser();
285 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
287 $this->addFeedLinks( array(
288 'action' => 'feedwatchlist',
290 'wlowner' => $user->getName(),
291 'wltoken' => $wlToken,
297 * Build and output the actual changes list.
299 * @param ResultWrapper $rows Database rows
300 * @param FormOptions $opts
302 public function outputChangesList( $rows, $opts ) {
303 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
305 $dbr = $this->getDB();
306 $user = $this->getUser();
307 $output = $this->getOutput();
309 # Show a message about slave lag, if applicable
310 $lag = wfGetLB()->safeGetLag( $dbr );
312 $output->showLagWarning( $lag );
315 $dbr->dataSeek( $rows, 0 );
317 $list = ChangesList
::newFromContext( $this->getContext() );
318 $list->setWatchlistDivs();
320 $s = $list->beginRecentChangesList();
322 foreach ( $rows as $obj ) {
324 $rc = RecentChange
::newFromRow( $obj );
325 $rc->counter
= $counter++
;
327 if ( $wgShowUpdatedMarker ) {
328 $updated = $obj->wl_notificationtimestamp
;
333 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
334 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
337 'wl_namespace' => $obj->rc_namespace
,
338 'wl_title' => $obj->rc_title
,
342 $rc->numberofWatchingusers
= 0;
345 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
346 if ( $changeLine !== false ) {
350 $s .= $list->endRecentChangesList();
352 if ( $rows->numRows() == 0 ) {
353 $output->wrapWikiMsg(
354 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
357 $output->addHTML( $s );
362 * Return the text to be displayed above the changes
364 * @param FormOptions $opts
365 * @return string XHTML
367 public function doHeader( $opts ) {
368 $user = $this->getUser();
370 $this->getOutput()->addSubtitle(
371 $this->msg( 'watchlistfor2', $user->getName() )
372 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
375 $this->setTopText( $opts );
377 $lang = $this->getLanguage();
379 if ( $opts['days'] > 0 ) {
380 $timestamp = wfTimestampNow();
381 $wlInfo = $this->msg( 'wlnote2' )->numParams( round( $opts['days'] * 24 ) )->params(
382 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
385 $nondefaults = $opts->getChangedValues();
386 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
388 # Spit out some control panel links
390 'hideminor' => 'rcshowhideminor',
391 'hidebots' => 'rcshowhidebots',
392 'hideanons' => 'rcshowhideanons',
393 'hideliu' => 'rcshowhideliu',
394 'hidemyself' => 'rcshowhidemine',
395 'hidepatrolled' => 'rcshowhidepatr'
397 foreach ( $this->getCustomFilters() as $key => $params ) {
398 $filters[$key] = $params['msg'];
400 // Disable some if needed
401 if ( !$user->useNPPatrol() ) {
402 unset( $filters['hidepatrolled'] );
406 foreach ( $filters as $name => $msg ) {
407 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
410 $hiddenFields = $nondefaults;
411 unset( $hiddenFields['namespace'] );
412 unset( $hiddenFields['invert'] );
413 unset( $hiddenFields['associated'] );
418 # Namespace filter and put the whole form together.
420 $form .= $cutofflinks;
421 $form .= $lang->pipeList( $links ) . "\n";
422 $form .= "<hr />\n<p>";
423 $form .= Html
::namespaceSelector(
425 'selected' => $opts['namespace'],
427 'label' => $this->msg( 'namespace' )->text()
429 'name' => 'namespace',
431 'class' => 'namespaceselector',
434 $form .= Xml
::checkLabel(
435 $this->msg( 'invert' )->text(),
439 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
441 $form .= Xml
::checkLabel(
442 $this->msg( 'namespace_association' )->text(),
446 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
448 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
449 foreach ( $hiddenFields as $key => $value ) {
450 $form .= Html
::hidden( $key, $value ) . "\n";
452 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
453 $form .= Xml
::closeElement( 'form' ) . "\n";
454 $this->getOutput()->addHTML( $form );
456 $this->setBottomText( $opts );
459 function setTopText( FormOptions
$opts ) {
460 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
462 $nondefaults = $opts->getChangedValues();
464 $user = $this->getUser();
466 $dbr = $this->getDB();
467 $numItems = $this->countItems( $dbr );
469 // Show watchlist header
471 if ( $numItems == 0 ) {
472 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
474 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
475 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
476 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
478 if ( $wgShowUpdatedMarker ) {
479 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
484 if ( $numItems > 0 && $wgShowUpdatedMarker ) {
485 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
486 'action' => $this->getPageTitle()->getLocalURL(),
487 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
488 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
489 Html
::hidden( 'reset', 'all' ) . "\n";
490 foreach ( $nondefaults as $key => $value ) {
491 $form .= Html
::hidden( $key, $value ) . "\n";
493 $form .= Xml
::closeElement( 'form' ) . "\n";
496 $form .= Xml
::openElement( 'form', array(
498 'action' => $this->getPageTitle()->getLocalURL(),
499 'id' => 'mw-watchlist-form'
501 $form .= Xml
::fieldset(
502 $this->msg( 'watchlist-options' )->text(),
504 array( 'id' => 'mw-watchlist-options' )
507 $form .= SpecialRecentChanges
::makeLegend( $this->getContext() );
509 $this->getOutput()->addHTML( $form );
512 protected function showHideLink( $options, $message, $name, $value ) {
513 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
514 $options[$name] = 1 - (int)$value;
516 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getPageTitle(), $label, array(), $options ) )->escaped();
519 protected function hoursLink( $h, $options = array() ) {
520 $options['days'] = ( $h / 24.0 );
522 return Linker
::linkKnown(
523 $this->getPageTitle(),
524 $this->getLanguage()->formatNum( $h ),
530 protected function daysLink( $d, $options = array() ) {
531 $options['days'] = $d;
532 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
534 return Linker
::linkKnown(
535 $this->getPageTitle(),
545 * @param int $days This gets overwritten, so is not used
546 * @param array $options Query parameters for URL
549 protected function cutoffLinks( $days, $options = array() ) {
550 $hours = array( 1, 2, 6, 12 );
551 $days = array( 1, 3, 7 );
553 foreach ( $hours as $h ) {
554 $hours[$i++
] = $this->hoursLink( $h, $options );
557 foreach ( $days as $d ) {
558 $days[$i++
] = $this->daysLink( $d, $options );
560 return $this->msg( 'wlshowlast' )->rawParams(
561 $this->getLanguage()->pipeList( $hours ),
562 $this->getLanguage()->pipeList( $days ),
563 $this->daysLink( 0, $options ) )->parse();
567 * Count the number of items on a user's watchlist
569 * @param DatabaseBase $dbr A database connection
572 protected function countItems( $dbr ) {
573 # Fetch the raw count
574 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
575 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
576 $row = $dbr->fetchObject( $rows );
577 $count = $row->count
;
579 return floor( $count / 2 );