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 Watchlist
23 class SpecialWatchlist
extends ChangesListSpecialPage
{
27 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
28 parent
::__construct( $page, $restriction );
33 * @param $par Parameter passed to the page
35 function execute( $par ) {
36 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
38 $user = $this->getUser();
39 $output = $this->getOutput();
41 # Anons don't get a watchlist
42 $this->requireLogin( 'watchlistanontext' );
45 $this->checkPermissions();
47 $request = $this->getRequest();
48 $opts = $this->getOptions();
50 $mode = SpecialEditWatchlist
::getMode( $request, $par );
51 if ( $mode !== false ) {
52 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
53 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
55 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
58 $output->redirect( $title->getLocalURL() );
62 if ( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
63 $request->wasPosted() )
65 $user->clearAllNotifications();
66 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
71 $this->outputHeader();
74 // Fetch results, prepare a batch link existence check query
75 $conds = $this->buildMainQueryConds( $opts );
76 $rows = $this->doMainQuery( $conds, $opts );
77 if ( $rows === false ) {
78 $this->doHeader( $opts );
83 $feedFormat = $this->getRequest()->getVal( 'feed' );
85 $batch = new LinkBatch
;
86 foreach ( $rows as $row ) {
87 $batch->add( NS_USER
, $row->rc_user_text
);
88 $batch->add( NS_USER_TALK
, $row->rc_user_text
);
89 $batch->add( $row->rc_namespace
, $row->rc_title
);
94 list( $changesFeed, $formatter ) = $this->getFeedObject( $feedFormat );
95 /** @var ChangesFeed $changesFeed */
96 $changesFeed->execute( $formatter, $rows, $lastmod, $opts );
98 $this->webOutput( $rows, $opts );
105 * Get a FormOptions object containing the default options
107 * @return FormOptions
109 public function getDefaultOptions() {
110 $opts = parent
::getDefaultOptions();
111 $user = $this->getUser();
113 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
115 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
116 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
117 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
118 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
119 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
120 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
122 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
128 * Get custom show/hide filters
130 * @return array Map of filter URL param names to properties (msg/default)
132 protected function getCustomFilters() {
133 if ( $this->customFilters
=== null ) {
134 $this->customFilters
= array();
135 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
) );
138 return $this->customFilters
;
142 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
144 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
145 * to the current ones.
147 * @param FormOptions $parameters
148 * @return FormOptions
150 protected function fetchOptionsFromRequest( $opts ) {
151 static $compatibilityMap = array(
152 'hideMinor' => 'hideminor',
153 'hideBots' => 'hidebots',
154 'hideAnons' => 'hideanons',
155 'hideLiu' => 'hideliu',
156 'hidePatrolled' => 'hidepatrolled',
157 'hideOwn' => 'hidemyself',
160 $params = $this->getRequest()->getValues();
161 foreach ( $compatibilityMap as $from => $to ) {
162 if ( isset( $params[$from] ) ) {
163 $params[$to] = $params[$from];
164 unset( $params[$from] );
168 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
169 // methods defined on WebRequest and removing this dependency would cause some code duplication.
170 $request = new DerivativeRequest( $this->getRequest(), $params );
171 $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 = wfGetDB( DB_SLAVE
, 'watchlist' );
184 $user = $this->getUser();
187 if ( $opts['days'] > 0 ) {
188 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
192 if ( $opts['hidemyself'] ) {
193 $conds[] = 'rc_user != ' . $user->getId();
195 if ( $opts['hidebots'] ) {
196 $conds[] = 'rc_bot = 0';
198 if ( $opts['hideminor'] ) {
199 $conds[] = 'rc_minor = 0';
201 if ( $opts['hideliu'] ) {
202 $conds[] = 'rc_user = 0';
204 if ( $opts['hideanons'] ) {
205 $conds[] = 'rc_user != 0';
207 if ( $user->useRCPatrol() && $opts['hidepatrolled'] ) {
208 $conds[] = 'rc_patrolled != 1';
211 # Namespace filtering
212 if ( $opts['namespace'] !== '' ) {
213 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
214 $operator = $opts['invert'] ?
'!=' : '=';
215 $boolean = $opts['invert'] ?
'AND' : 'OR';
217 # namespace association (bug 2429)
218 if ( !$opts['associated'] ) {
219 $condition = "rc_namespace $operator $selectedNS";
221 # Also add the associated namespace
222 $associatedNS = $dbr->addQuotes(
223 MWNamespace
::getAssociated( $opts['namespace'] )
225 $condition = "(rc_namespace $operator $selectedNS "
227 . " rc_namespace $operator $associatedNS)";
230 $conds[] = $condition;
239 * @param array $conds
240 * @param FormOptions $opts
241 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
243 public function doMainQuery( $conds, $opts ) {
244 global $wgShowUpdatedMarker;
246 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
247 $user = $this->getUser();
248 # Toggle watchlist content (all recent edits or just the latest)
249 if ( $opts['extended'] ) {
250 $limitWatchlist = $user->getIntOption( 'wllimit' );
253 # Top log Ids for a page are not stored
254 $nonRevisionTypes = array( RC_LOG
);
255 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
256 if ( $nonRevisionTypes ) {
257 $conds[] = $dbr->makeList(
259 'rc_this_oldid=page_latest',
260 'rc_type' => $nonRevisionTypes,
269 $tables = array( 'recentchanges', 'watchlist' );
270 $fields = RecentChange
::selectFields();
272 'watchlist' => array(
275 'wl_user' => $user->getId(),
276 'wl_namespace=rc_namespace',
281 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
282 if ( $wgShowUpdatedMarker ) {
283 $fields[] = 'wl_notificationtimestamp';
285 if ( $limitWatchlist ) {
286 $options['LIMIT'] = $limitWatchlist;
289 $rollbacker = $user->isAllowed( 'rollback' );
290 if ( $usePage ||
$rollbacker ) {
292 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
294 $fields[] = 'page_latest';
298 // Log entries with DELETED_ACTION must not show up unless the user has
299 // the necessary rights.
300 if ( !$user->isAllowed( 'deletedhistory' ) ) {
301 $bitmask = LogPage
::DELETED_ACTION
;
302 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
303 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
308 $conds[] = $dbr->makeList( array(
309 'rc_type != ' . RC_LOG
,
310 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
315 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
316 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $opts ) );
318 return $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
322 * Send output to the OutputPage object, only called if not used feeds
324 * @param array $rows Database rows
325 * @param FormOptions $opts
327 public function webOutput( $rows, $opts ) {
328 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
330 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
331 $user = $this->getUser();
333 $dbr->dataSeek( $rows, 0 );
335 $list = ChangesList
::newFromContext( $this->getContext() );
336 $list->setWatchlistDivs();
338 $s = $list->beginRecentChangesList();
340 foreach ( $rows as $obj ) {
342 $rc = RecentChange
::newFromRow( $obj );
343 $rc->counter
= $counter++
;
345 if ( $wgShowUpdatedMarker ) {
346 $updated = $obj->wl_notificationtimestamp
;
351 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
352 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
355 'wl_namespace' => $obj->rc_namespace
,
356 'wl_title' => $obj->rc_title
,
360 $rc->numberofWatchingusers
= 0;
363 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
364 if ( $changeLine !== false ) {
368 $s .= $list->endRecentChangesList();
372 $output = $this->getOutput();
374 $output->addSubtitle(
375 $this->msg( 'watchlistfor2', $user->getName() )
376 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
379 // Output options box
380 $this->doHeader( $opts );
383 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
385 $this->addFeedLinks( array(
386 'action' => 'feedwatchlist',
388 'wlowner' => $user->getName(),
389 'wltoken' => $wlToken,
393 # Show a message about slave lag, if applicable
394 $lag = wfGetLB()->safeGetLag( $dbr );
396 $output->showLagWarning( $lag );
399 if ( $rows->numRows() == 0 ) {
400 $output->wrapWikiMsg(
401 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
404 $output->addHTML( $s );
409 * Return the text to be displayed above the changes
411 * @param FormOptions $opts
412 * @return string XHTML
414 public function doHeader( $opts ) {
417 $user = $this->getUser();
419 $this->setTopText( $opts );
421 $lang = $this->getLanguage();
423 if ( $opts['days'] > 0 ) {
424 $timestamp = wfTimestampNow();
425 $wlInfo = $this->msg( 'wlnote2' )->numParams( round( $opts['days'] * 24 ) )->params(
426 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
429 $nondefaults = $opts->getChangedValues();
430 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
432 # Spit out some control panel links
434 'hideminor' => 'rcshowhideminor',
435 'hidebots' => 'rcshowhidebots',
436 'hideanons' => 'rcshowhideanons',
437 'hideliu' => 'rcshowhideliu',
438 'hidemyself' => 'rcshowhidemine',
439 'hidepatrolled' => 'rcshowhidepatr'
441 foreach ( $this->getCustomFilters() as $key => $params ) {
442 $filters[$key] = $params['msg'];
444 // Disable some if needed
445 if ( !$user->useNPPatrol() ) {
446 unset( $filters['hidepatrolled'] );
450 foreach ( $filters as $name => $msg ) {
451 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
454 $hiddenFields = $nondefaults;
455 unset( $hiddenFields['namespace'] );
456 unset( $hiddenFields['invert'] );
457 unset( $hiddenFields['associated'] );
462 # Namespace filter and put the whole form together.
464 $form .= $cutofflinks;
465 $form .= $lang->pipeList( $links ) . "\n";
466 $form .= "<hr />\n<p>";
467 $form .= Html
::namespaceSelector(
469 'selected' => $opts['namespace'],
471 'label' => $this->msg( 'namespace' )->text()
473 'name' => 'namespace',
475 'class' => 'namespaceselector',
478 $form .= Xml
::checkLabel(
479 $this->msg( 'invert' )->text(),
483 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
485 $form .= Xml
::checkLabel(
486 $this->msg( 'namespace_association' )->text(),
490 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
492 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
493 foreach ( $hiddenFields as $key => $value ) {
494 $form .= Html
::hidden( $key, $value ) . "\n";
496 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
497 $form .= Xml
::closeElement( 'form' ) . "\n";
498 $this->getOutput()->addHTML( $form );
500 $this->setBottomText( $opts );
503 function setTopText( FormOptions
$opts ) {
504 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
506 $nondefaults = $opts->getChangedValues();
508 $user = $this->getUser();
510 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
511 $numItems = $this->countItems( $dbr );
513 // Show watchlist header
515 if ( $numItems == 0 ) {
516 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
518 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
519 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
520 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
522 if ( $wgShowUpdatedMarker ) {
523 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
528 if ( $numItems > 0 && $wgShowUpdatedMarker ) {
529 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
530 'action' => $this->getPageTitle()->getLocalURL(),
531 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
532 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
533 Html
::hidden( 'reset', 'all' ) . "\n";
534 foreach ( $nondefaults as $key => $value ) {
535 $form .= Html
::hidden( $key, $value ) . "\n";
537 $form .= Xml
::closeElement( 'form' ) . "\n";
540 $form .= Xml
::openElement( 'form', array(
542 'action' => $this->getPageTitle()->getLocalURL(),
543 'id' => 'mw-watchlist-form'
545 $form .= Xml
::fieldset(
546 $this->msg( 'watchlist-options' )->text(),
548 array( 'id' => 'mw-watchlist-options' )
551 $form .= SpecialRecentChanges
::makeLegend( $this->getContext() );
553 $this->getOutput()->addHTML( $form );
556 protected function showHideLink( $options, $message, $name, $value ) {
557 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
558 $options[$name] = 1 - (int)$value;
560 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getPageTitle(), $label, array(), $options ) )->escaped();
563 protected function hoursLink( $h, $options = array() ) {
564 $options['days'] = ( $h / 24.0 );
566 return Linker
::linkKnown(
567 $this->getPageTitle(),
568 $this->getLanguage()->formatNum( $h ),
574 protected function daysLink( $d, $options = array() ) {
575 $options['days'] = $d;
576 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
578 return Linker
::linkKnown(
579 $this->getPageTitle(),
589 * @param int $days This gets overwritten, so is not used
590 * @param array $options Query parameters for URL
593 protected function cutoffLinks( $days, $options = array() ) {
594 $hours = array( 1, 2, 6, 12 );
595 $days = array( 1, 3, 7 );
597 foreach ( $hours as $h ) {
598 $hours[$i++
] = $this->hoursLink( $h, $options );
601 foreach ( $days as $d ) {
602 $days[$i++
] = $this->daysLink( $d, $options );
604 return $this->msg( 'wlshowlast' )->rawParams(
605 $this->getLanguage()->pipeList( $hours ),
606 $this->getLanguage()->pipeList( $days ),
607 $this->daysLink( 0, $options ) )->parse();
611 * Count the number of items on a user's watchlist
613 * @param DatabaseBase $dbr A database connection
616 protected function countItems( $dbr ) {
617 # Fetch the raw count
618 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
619 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
620 $row = $dbr->fetchObject( $rows );
621 $count = $row->count
;
623 return floor( $count / 2 );