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' );
54 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
57 $output->redirect( $title->getLocalURL() );
62 $this->checkPermissions();
64 $user = $this->getUser();
65 $opts = $this->getOptions();
67 if ( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker )
68 && $request->getVal( 'reset' )
69 && $request->wasPosted()
71 $user->clearAllNotifications();
72 $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
= parent
::getCustomFilters();
111 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
), '1.23' );
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 $opts
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 );
153 * Return an array of conditions depending of options set in $opts
155 * @param FormOptions $opts
158 public function buildMainQueryConds( FormOptions
$opts ) {
159 $dbr = $this->getDB();
160 $conds = parent
::buildMainQueryConds( $opts );
163 if ( $opts['days'] > 0 ) {
164 $conds[] = 'rc_timestamp > ' .
165 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
174 * @param array $conds
175 * @param FormOptions $opts
176 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
178 public function doMainQuery( $conds, $opts ) {
179 global $wgShowUpdatedMarker;
181 $dbr = $this->getDB();
182 $user = $this->getUser();
184 # Toggle watchlist content (all recent edits or just the latest)
185 if ( $opts['extended'] ) {
186 $limitWatchlist = $user->getIntOption( 'wllimit' );
189 # Top log Ids for a page are not stored
190 $nonRevisionTypes = array( RC_LOG
);
191 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
192 if ( $nonRevisionTypes ) {
193 $conds[] = $dbr->makeList(
195 'rc_this_oldid=page_latest',
196 'rc_type' => $nonRevisionTypes,
205 $tables = array( 'recentchanges', 'watchlist' );
206 $fields = RecentChange
::selectFields();
207 $query_options = array( 'ORDER BY' => 'rc_timestamp DESC' );
209 'watchlist' => array(
212 'wl_user' => $user->getId(),
213 'wl_namespace=rc_namespace',
219 if ( $wgShowUpdatedMarker ) {
220 $fields[] = 'wl_notificationtimestamp';
222 if ( $limitWatchlist ) {
223 $query_options['LIMIT'] = $limitWatchlist;
226 $rollbacker = $user->isAllowed( 'rollback' );
227 if ( $usePage ||
$rollbacker ) {
229 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
231 $fields[] = 'page_latest';
235 // Log entries with DELETED_ACTION must not show up unless the user has
236 // the necessary rights.
237 if ( !$user->isAllowed( 'deletedhistory' ) ) {
238 $bitmask = LogPage
::DELETED_ACTION
;
239 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
240 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
245 $conds[] = $dbr->makeList( array(
246 'rc_type != ' . RC_LOG
,
247 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
251 ChangeTags
::modifyDisplayQuery(
260 wfRunHooks( 'SpecialWatchlistQuery',
261 array( &$conds, &$tables, &$join_conds, &$fields, $opts ),
275 * Return a DatabaseBase object for reading
277 * @return DatabaseBase
279 protected function getDB() {
280 return wfGetDB( DB_SLAVE
, 'watchlist' );
286 public function outputFeedLinks() {
287 $user = $this->getUser();
288 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
290 $this->addFeedLinks( array(
291 'action' => 'feedwatchlist',
293 'wlowner' => $user->getName(),
294 'wltoken' => $wlToken,
300 * Build and output the actual changes list.
302 * @param ResultWrapper $rows Database rows
303 * @param FormOptions $opts
305 public function outputChangesList( $rows, $opts ) {
306 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
308 $dbr = $this->getDB();
309 $user = $this->getUser();
310 $output = $this->getOutput();
312 # Show a message about slave lag, if applicable
313 $lag = wfGetLB()->safeGetLag( $dbr );
315 $output->showLagWarning( $lag );
318 $dbr->dataSeek( $rows, 0 );
320 $list = ChangesList
::newFromContext( $this->getContext() );
321 $list->setWatchlistDivs();
322 $list->initChangesListRows( $rows );
323 $dbr->dataSeek( $rows, 0 );
325 $s = $list->beginRecentChangesList();
327 foreach ( $rows as $obj ) {
329 $rc = RecentChange
::newFromRow( $obj );
330 $rc->counter
= $counter++
;
332 if ( $wgShowUpdatedMarker ) {
333 $updated = $obj->wl_notificationtimestamp
;
338 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
339 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
342 'wl_namespace' => $obj->rc_namespace
,
343 'wl_title' => $obj->rc_title
,
347 $rc->numberofWatchingusers
= 0;
350 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
351 if ( $changeLine !== false ) {
355 $s .= $list->endRecentChangesList();
357 if ( $rows->numRows() == 0 ) {
358 $output->wrapWikiMsg(
359 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
362 $output->addHTML( $s );
367 * Return the text to be displayed above the changes
369 * @param FormOptions $opts
370 * @return string XHTML
372 public function doHeader( $opts ) {
373 $user = $this->getUser();
375 $this->getOutput()->addSubtitle(
376 $this->msg( 'watchlistfor2', $user->getName() )
377 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
380 $this->setTopText( $opts );
382 $lang = $this->getLanguage();
384 if ( $opts['days'] > 0 ) {
385 $timestamp = wfTimestampNow();
386 $wlInfo = $this->msg( 'wlnote2' )->numParams( round( $opts['days'] * 24 ) )->params(
387 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
388 )->parse() . "<br />\n";
391 $nondefaults = $opts->getChangedValues();
392 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
394 # Spit out some control panel links
396 'hideminor' => 'rcshowhideminor',
397 'hidebots' => 'rcshowhidebots',
398 'hideanons' => 'rcshowhideanons',
399 'hideliu' => 'rcshowhideliu',
400 'hidemyself' => 'rcshowhidemine',
401 'hidepatrolled' => 'rcshowhidepatr'
403 foreach ( $this->getCustomFilters() as $key => $params ) {
404 $filters[$key] = $params['msg'];
406 // Disable some if needed
407 if ( !$user->useNPPatrol() ) {
408 unset( $filters['hidepatrolled'] );
412 foreach ( $filters as $name => $msg ) {
413 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
416 $hiddenFields = $nondefaults;
417 unset( $hiddenFields['namespace'] );
418 unset( $hiddenFields['invert'] );
419 unset( $hiddenFields['associated'] );
424 # Namespace filter and put the whole form together.
426 $form .= $cutofflinks;
427 $form .= $lang->pipeList( $links ) . "\n";
428 $form .= "<hr />\n<p>";
429 $form .= Html
::namespaceSelector(
431 'selected' => $opts['namespace'],
433 'label' => $this->msg( 'namespace' )->text()
435 'name' => 'namespace',
437 'class' => 'namespaceselector',
440 $form .= Xml
::checkLabel(
441 $this->msg( 'invert' )->text(),
445 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
447 $form .= Xml
::checkLabel(
448 $this->msg( 'namespace_association' )->text(),
452 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
454 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
455 foreach ( $hiddenFields as $key => $value ) {
456 $form .= Html
::hidden( $key, $value ) . "\n";
458 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
459 $form .= Xml
::closeElement( 'form' ) . "\n";
460 $this->getOutput()->addHTML( $form );
462 $this->setBottomText( $opts );
465 function setTopText( FormOptions
$opts ) {
466 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
468 $nondefaults = $opts->getChangedValues();
470 $user = $this->getUser();
472 $dbr = $this->getDB();
473 $numItems = $this->countItems( $dbr );
475 // Show watchlist header
477 if ( $numItems == 0 ) {
478 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
480 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
481 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
482 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
484 if ( $wgShowUpdatedMarker ) {
485 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
490 if ( $numItems > 0 && $wgShowUpdatedMarker ) {
491 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
492 'action' => $this->getPageTitle()->getLocalURL(),
493 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
494 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
495 Html
::hidden( 'reset', 'all' ) . "\n";
496 foreach ( $nondefaults as $key => $value ) {
497 $form .= Html
::hidden( $key, $value ) . "\n";
499 $form .= Xml
::closeElement( 'form' ) . "\n";
502 $form .= Xml
::openElement( 'form', array(
504 'action' => $this->getPageTitle()->getLocalURL(),
505 'id' => 'mw-watchlist-form'
507 $form .= Xml
::fieldset(
508 $this->msg( 'watchlist-options' )->text(),
510 array( 'id' => 'mw-watchlist-options' )
513 $form .= SpecialRecentChanges
::makeLegend( $this->getContext() );
515 $this->getOutput()->addHTML( $form );
518 protected function showHideLink( $options, $message, $name, $value ) {
519 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
520 $options[$name] = 1 - (int)$value;
522 return $this->msg( $message )
523 ->rawParams( Linker
::linkKnown( $this->getPageTitle(), $label, array(), $options ) )
527 protected function hoursLink( $h, $options = array() ) {
528 $options['days'] = ( $h / 24.0 );
530 return Linker
::linkKnown(
531 $this->getPageTitle(),
532 $this->getLanguage()->formatNum( $h ),
538 protected function daysLink( $d, $options = array() ) {
539 $options['days'] = $d;
540 $message = $d ?
$this->getLanguage()->formatNum( $d )
541 : $this->msg( 'watchlistall2' )->escaped();
543 return Linker
::linkKnown(
544 $this->getPageTitle(),
554 * @param int $days This gets overwritten, so is not used
555 * @param array $options Query parameters for URL
558 protected function cutoffLinks( $days, $options = array() ) {
559 $hours = array( 1, 2, 6, 12 );
560 $days = array( 1, 3, 7 );
562 foreach ( $hours as $h ) {
563 $hours[$i++
] = $this->hoursLink( $h, $options );
566 foreach ( $days as $d ) {
567 $days[$i++
] = $this->daysLink( $d, $options );
570 return $this->msg( 'wlshowlast' )->rawParams(
571 $this->getLanguage()->pipeList( $hours ),
572 $this->getLanguage()->pipeList( $days ),
573 $this->daysLink( 0, $options ) )->parse();
577 * Count the number of items on a user's watchlist
579 * @param DatabaseBase $dbr A database connection
582 protected function countItems( $dbr ) {
583 # Fetch the raw count
584 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
585 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
586 $row = $dbr->fetchObject( $rows );
587 $count = $row->count
;
589 return floor( $count / 2 );