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 integer $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 $subpages = array( 'clear', 'edit', 'raw' );
92 $escaped = preg_quote( $search );
93 return array_slice( preg_grep( "/^$escaped/i", $subpages ), 0, $limit );
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 );
107 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
108 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
109 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
110 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
111 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
112 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
114 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
120 * Get custom show/hide filters
122 * @return array Map of filter URL param names to properties (msg/default)
124 protected function getCustomFilters() {
125 if ( $this->customFilters
=== null ) {
126 $this->customFilters
= parent
::getCustomFilters();
127 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
), '1.23' );
130 return $this->customFilters
;
134 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
136 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
137 * to the current ones.
139 * @param FormOptions $opts
140 * @return FormOptions
142 protected function fetchOptionsFromRequest( $opts ) {
143 static $compatibilityMap = array(
144 'hideMinor' => 'hideminor',
145 'hideBots' => 'hidebots',
146 'hideAnons' => 'hideanons',
147 'hideLiu' => 'hideliu',
148 'hidePatrolled' => 'hidepatrolled',
149 'hideOwn' => 'hidemyself',
152 $params = $this->getRequest()->getValues();
153 foreach ( $compatibilityMap as $from => $to ) {
154 if ( isset( $params[$from] ) ) {
155 $params[$to] = $params[$from];
156 unset( $params[$from] );
160 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
161 // methods defined on WebRequest and removing this dependency would cause some code duplication.
162 $request = new DerivativeRequest( $this->getRequest(), $params );
163 $opts->fetchValuesFromRequest( $request );
169 * Return an array of conditions depending of options set in $opts
171 * @param FormOptions $opts
174 public function buildMainQueryConds( FormOptions
$opts ) {
175 $dbr = $this->getDB();
176 $conds = parent
::buildMainQueryConds( $opts );
179 if ( $opts['days'] > 0 ) {
180 $conds[] = 'rc_timestamp > ' .
181 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
190 * @param array $conds
191 * @param FormOptions $opts
192 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
194 public function doMainQuery( $conds, $opts ) {
195 global $wgShowUpdatedMarker;
197 $dbr = $this->getDB();
198 $user = $this->getUser();
200 # Toggle watchlist content (all recent edits or just the latest)
201 if ( $opts['extended'] ) {
202 $limitWatchlist = $user->getIntOption( 'wllimit' );
205 # Top log Ids for a page are not stored
206 $nonRevisionTypes = array( RC_LOG
);
207 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
208 if ( $nonRevisionTypes ) {
209 $conds[] = $dbr->makeList(
211 'rc_this_oldid=page_latest',
212 'rc_type' => $nonRevisionTypes,
221 $tables = array( 'recentchanges', 'watchlist' );
222 $fields = RecentChange
::selectFields();
223 $query_options = array( 'ORDER BY' => 'rc_timestamp DESC' );
225 'watchlist' => array(
228 'wl_user' => $user->getId(),
229 'wl_namespace=rc_namespace',
235 if ( $wgShowUpdatedMarker ) {
236 $fields[] = 'wl_notificationtimestamp';
238 if ( $limitWatchlist ) {
239 $query_options['LIMIT'] = $limitWatchlist;
242 $rollbacker = $user->isAllowed( 'rollback' );
243 if ( $usePage ||
$rollbacker ) {
245 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
247 $fields[] = 'page_latest';
251 // Log entries with DELETED_ACTION must not show up unless the user has
252 // the necessary rights.
253 if ( !$user->isAllowed( 'deletedhistory' ) ) {
254 $bitmask = LogPage
::DELETED_ACTION
;
255 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
256 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
261 $conds[] = $dbr->makeList( array(
262 'rc_type != ' . RC_LOG
,
263 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
267 ChangeTags
::modifyDisplayQuery(
276 wfRunHooks( 'SpecialWatchlistQuery',
277 array( &$conds, &$tables, &$join_conds, &$fields, $opts ),
291 * Return a DatabaseBase object for reading
293 * @return DatabaseBase
295 protected function getDB() {
296 return wfGetDB( DB_SLAVE
, 'watchlist' );
302 public function outputFeedLinks() {
303 $user = $this->getUser();
304 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
306 $this->addFeedLinks( array(
307 'action' => 'feedwatchlist',
309 'wlowner' => $user->getName(),
310 'wltoken' => $wlToken,
316 * Build and output the actual changes list.
318 * @param ResultWrapper $rows Database rows
319 * @param FormOptions $opts
321 public function outputChangesList( $rows, $opts ) {
322 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
324 $dbr = $this->getDB();
325 $user = $this->getUser();
326 $output = $this->getOutput();
328 # Show a message about slave lag, if applicable
329 $lag = wfGetLB()->safeGetLag( $dbr );
331 $output->showLagWarning( $lag );
334 # If no rows to display, show message before try to render the list
335 if ( $rows->numRows() == 0 ) {
336 $output->wrapWikiMsg(
337 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
342 $dbr->dataSeek( $rows, 0 );
344 $list = ChangesList
::newFromContext( $this->getContext() );
345 $list->setWatchlistDivs();
346 $list->initChangesListRows( $rows );
347 $dbr->dataSeek( $rows, 0 );
349 $s = $list->beginRecentChangesList();
351 foreach ( $rows as $obj ) {
353 $rc = RecentChange
::newFromRow( $obj );
354 $rc->counter
= $counter++
;
356 if ( $wgShowUpdatedMarker ) {
357 $updated = $obj->wl_notificationtimestamp
;
362 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
363 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
366 'wl_namespace' => $obj->rc_namespace
,
367 'wl_title' => $obj->rc_title
,
371 $rc->numberofWatchingusers
= 0;
374 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
375 if ( $changeLine !== false ) {
379 $s .= $list->endRecentChangesList();
381 $output->addHTML( $s );
385 * Return the text to be displayed above the changes
387 * @param FormOptions $opts
388 * @return string XHTML
390 public function doHeader( $opts ) {
391 $user = $this->getUser();
393 $this->getOutput()->addSubtitle(
394 $this->msg( 'watchlistfor2', $user->getName() )
395 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
398 $this->setTopText( $opts );
400 $lang = $this->getLanguage();
402 if ( $opts['days'] > 0 ) {
403 $timestamp = wfTimestampNow();
404 $wlInfo = $this->msg( 'wlnote2' )->numParams( round( $opts['days'] * 24 ) )->params(
405 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
406 )->parse() . "<br />\n";
409 $nondefaults = $opts->getChangedValues();
410 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
412 # Spit out some control panel links
414 'hideminor' => 'rcshowhideminor',
415 'hidebots' => 'rcshowhidebots',
416 'hideanons' => 'rcshowhideanons',
417 'hideliu' => 'rcshowhideliu',
418 'hidemyself' => 'rcshowhidemine',
419 'hidepatrolled' => 'rcshowhidepatr'
421 foreach ( $this->getCustomFilters() as $key => $params ) {
422 $filters[$key] = $params['msg'];
424 // Disable some if needed
425 if ( !$user->useNPPatrol() ) {
426 unset( $filters['hidepatrolled'] );
430 foreach ( $filters as $name => $msg ) {
431 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
434 $hiddenFields = $nondefaults;
435 unset( $hiddenFields['namespace'] );
436 unset( $hiddenFields['invert'] );
437 unset( $hiddenFields['associated'] );
442 # Namespace filter and put the whole form together.
444 $form .= $cutofflinks;
445 $form .= $lang->pipeList( $links ) . "\n";
446 $form .= "<hr />\n<p>";
447 $form .= Html
::namespaceSelector(
449 'selected' => $opts['namespace'],
451 'label' => $this->msg( 'namespace' )->text()
453 'name' => 'namespace',
455 'class' => 'namespaceselector',
458 $form .= Xml
::checkLabel(
459 $this->msg( 'invert' )->text(),
463 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
465 $form .= Xml
::checkLabel(
466 $this->msg( 'namespace_association' )->text(),
470 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
472 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
473 foreach ( $hiddenFields as $key => $value ) {
474 $form .= Html
::hidden( $key, $value ) . "\n";
476 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
477 $form .= Xml
::closeElement( 'form' ) . "\n";
478 $this->getOutput()->addHTML( $form );
480 $this->setBottomText( $opts );
483 function setTopText( FormOptions
$opts ) {
484 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
486 $nondefaults = $opts->getChangedValues();
488 $user = $this->getUser();
490 $dbr = $this->getDB();
491 $numItems = $this->countItems( $dbr );
493 // Show watchlist header
495 if ( $numItems == 0 ) {
496 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
498 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
499 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
500 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
502 if ( $wgShowUpdatedMarker ) {
503 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
508 if ( $numItems > 0 && $wgShowUpdatedMarker ) {
509 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
510 'action' => $this->getPageTitle()->getLocalURL(),
511 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
512 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
513 Html
::hidden( 'reset', 'all' ) . "\n";
514 foreach ( $nondefaults as $key => $value ) {
515 $form .= Html
::hidden( $key, $value ) . "\n";
517 $form .= Xml
::closeElement( 'form' ) . "\n";
520 $form .= Xml
::openElement( 'form', array(
522 'action' => $this->getPageTitle()->getLocalURL(),
523 'id' => 'mw-watchlist-form'
525 $form .= Xml
::fieldset(
526 $this->msg( 'watchlist-options' )->text(),
528 array( 'id' => 'mw-watchlist-options' )
531 $form .= SpecialRecentChanges
::makeLegend( $this->getContext() );
533 $this->getOutput()->addHTML( $form );
536 protected function showHideLink( $options, $message, $name, $value ) {
537 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
538 $options[$name] = 1 - (int)$value;
540 return $this->msg( $message )
541 ->rawParams( Linker
::linkKnown( $this->getPageTitle(), $label, array(), $options ) )
545 protected function hoursLink( $h, $options = array() ) {
546 $options['days'] = ( $h / 24.0 );
548 return Linker
::linkKnown(
549 $this->getPageTitle(),
550 $this->getLanguage()->formatNum( $h ),
556 protected function daysLink( $d, $options = array() ) {
557 $options['days'] = $d;
558 $message = $d ?
$this->getLanguage()->formatNum( $d )
559 : $this->msg( 'watchlistall2' )->escaped();
561 return Linker
::linkKnown(
562 $this->getPageTitle(),
572 * @param int $days This gets overwritten, so is not used
573 * @param array $options Query parameters for URL
576 protected function cutoffLinks( $days, $options = array() ) {
577 $hours = array( 1, 2, 6, 12 );
578 $days = array( 1, 3, 7 );
580 foreach ( $hours as $h ) {
581 $hours[$i++
] = $this->hoursLink( $h, $options );
584 foreach ( $days as $d ) {
585 $days[$i++
] = $this->daysLink( $d, $options );
588 return $this->msg( 'wlshowlast' )->rawParams(
589 $this->getLanguage()->pipeList( $hours ),
590 $this->getLanguage()->pipeList( $days ),
591 $this->daysLink( 0, $options ) )->parse();
595 * Count the number of items on a user's watchlist
597 * @param DatabaseBase $dbr A database connection
600 protected function countItems( $dbr ) {
601 # Fetch the raw count
602 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
603 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
604 $row = $dbr->fetchObject( $rows );
605 $count = $row->count
;
607 return floor( $count / 2 );