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",
250 ChangeTags
::modifyDisplayQuery(
259 wfRunHooks( 'SpecialWatchlistQuery',
260 array( &$conds, &$tables, &$join_conds, &$fields, $opts ) );
273 * Return a DatabaseBase object for reading
275 * @return DatabaseBase
277 protected function getDB() {
278 return wfGetDB( DB_SLAVE
, 'watchlist' );
284 public function outputFeedLinks() {
285 $user = $this->getUser();
286 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
288 $this->addFeedLinks( array(
289 'action' => 'feedwatchlist',
291 'wlowner' => $user->getName(),
292 'wltoken' => $wlToken,
298 * Build and output the actual changes list.
300 * @param ResultWrapper $rows Database rows
301 * @param FormOptions $opts
303 public function outputChangesList( $rows, $opts ) {
304 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
306 $dbr = $this->getDB();
307 $user = $this->getUser();
308 $output = $this->getOutput();
310 # Show a message about slave lag, if applicable
311 $lag = wfGetLB()->safeGetLag( $dbr );
313 $output->showLagWarning( $lag );
316 $dbr->dataSeek( $rows, 0 );
318 $list = ChangesList
::newFromContext( $this->getContext() );
319 $list->setWatchlistDivs();
321 $s = $list->beginRecentChangesList();
323 foreach ( $rows as $obj ) {
325 $rc = RecentChange
::newFromRow( $obj );
326 $rc->counter
= $counter++
;
328 if ( $wgShowUpdatedMarker ) {
329 $updated = $obj->wl_notificationtimestamp
;
334 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
335 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
338 'wl_namespace' => $obj->rc_namespace
,
339 'wl_title' => $obj->rc_title
,
343 $rc->numberofWatchingusers
= 0;
346 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
347 if ( $changeLine !== false ) {
351 $s .= $list->endRecentChangesList();
353 if ( $rows->numRows() == 0 ) {
354 $output->wrapWikiMsg(
355 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
358 $output->addHTML( $s );
363 * Return the text to be displayed above the changes
365 * @param FormOptions $opts
366 * @return string XHTML
368 public function doHeader( $opts ) {
369 $user = $this->getUser();
371 $this->getOutput()->addSubtitle(
372 $this->msg( 'watchlistfor2', $user->getName() )
373 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
376 $this->setTopText( $opts );
378 $lang = $this->getLanguage();
380 if ( $opts['days'] > 0 ) {
381 $timestamp = wfTimestampNow();
382 $wlInfo = $this->msg( 'wlnote2' )->numParams( round( $opts['days'] * 24 ) )->params(
383 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
386 $nondefaults = $opts->getChangedValues();
387 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
389 # Spit out some control panel links
391 'hideminor' => 'rcshowhideminor',
392 'hidebots' => 'rcshowhidebots',
393 'hideanons' => 'rcshowhideanons',
394 'hideliu' => 'rcshowhideliu',
395 'hidemyself' => 'rcshowhidemine',
396 'hidepatrolled' => 'rcshowhidepatr'
398 foreach ( $this->getCustomFilters() as $key => $params ) {
399 $filters[$key] = $params['msg'];
401 // Disable some if needed
402 if ( !$user->useNPPatrol() ) {
403 unset( $filters['hidepatrolled'] );
407 foreach ( $filters as $name => $msg ) {
408 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
411 $hiddenFields = $nondefaults;
412 unset( $hiddenFields['namespace'] );
413 unset( $hiddenFields['invert'] );
414 unset( $hiddenFields['associated'] );
419 # Namespace filter and put the whole form together.
421 $form .= $cutofflinks;
422 $form .= $lang->pipeList( $links ) . "\n";
423 $form .= "<hr />\n<p>";
424 $form .= Html
::namespaceSelector(
426 'selected' => $opts['namespace'],
428 'label' => $this->msg( 'namespace' )->text()
430 'name' => 'namespace',
432 'class' => 'namespaceselector',
435 $form .= Xml
::checkLabel(
436 $this->msg( 'invert' )->text(),
440 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
442 $form .= Xml
::checkLabel(
443 $this->msg( 'namespace_association' )->text(),
447 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
449 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
450 foreach ( $hiddenFields as $key => $value ) {
451 $form .= Html
::hidden( $key, $value ) . "\n";
453 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
454 $form .= Xml
::closeElement( 'form' ) . "\n";
455 $this->getOutput()->addHTML( $form );
457 $this->setBottomText( $opts );
460 function setTopText( FormOptions
$opts ) {
461 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
463 $nondefaults = $opts->getChangedValues();
465 $user = $this->getUser();
467 $dbr = $this->getDB();
468 $numItems = $this->countItems( $dbr );
470 // Show watchlist header
472 if ( $numItems == 0 ) {
473 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
475 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
476 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
477 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
479 if ( $wgShowUpdatedMarker ) {
480 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
485 if ( $numItems > 0 && $wgShowUpdatedMarker ) {
486 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
487 'action' => $this->getPageTitle()->getLocalURL(),
488 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
489 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
490 Html
::hidden( 'reset', 'all' ) . "\n";
491 foreach ( $nondefaults as $key => $value ) {
492 $form .= Html
::hidden( $key, $value ) . "\n";
494 $form .= Xml
::closeElement( 'form' ) . "\n";
497 $form .= Xml
::openElement( 'form', array(
499 'action' => $this->getPageTitle()->getLocalURL(),
500 'id' => 'mw-watchlist-form'
502 $form .= Xml
::fieldset(
503 $this->msg( 'watchlist-options' )->text(),
505 array( 'id' => 'mw-watchlist-options' )
508 $form .= SpecialRecentChanges
::makeLegend( $this->getContext() );
510 $this->getOutput()->addHTML( $form );
513 protected function showHideLink( $options, $message, $name, $value ) {
514 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
515 $options[$name] = 1 - (int)$value;
517 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getPageTitle(), $label, array(), $options ) )->escaped();
520 protected function hoursLink( $h, $options = array() ) {
521 $options['days'] = ( $h / 24.0 );
523 return Linker
::linkKnown(
524 $this->getPageTitle(),
525 $this->getLanguage()->formatNum( $h ),
531 protected function daysLink( $d, $options = array() ) {
532 $options['days'] = $d;
533 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
535 return Linker
::linkKnown(
536 $this->getPageTitle(),
546 * @param int $days This gets overwritten, so is not used
547 * @param array $options Query parameters for URL
550 protected function cutoffLinks( $days, $options = array() ) {
551 $hours = array( 1, 2, 6, 12 );
552 $days = array( 1, 3, 7 );
554 foreach ( $hours as $h ) {
555 $hours[$i++
] = $this->hoursLink( $h, $options );
558 foreach ( $days as $d ) {
559 $days[$i++
] = $this->daysLink( $d, $options );
561 return $this->msg( 'wlshowlast' )->rawParams(
562 $this->getLanguage()->pipeList( $hours ),
563 $this->getLanguage()->pipeList( $days ),
564 $this->daysLink( 0, $options ) )->parse();
568 * Count the number of items on a user's watchlist
570 * @param DatabaseBase $dbr A database connection
573 protected function countItems( $dbr ) {
574 # Fetch the raw count
575 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
576 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
577 $row = $dbr->fetchObject( $rows );
578 $count = $row->count
;
580 return floor( $count / 2 );