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 SpecialRecentChanges
{
24 protected $customFilters;
29 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
30 parent
::__construct( $page, $restriction );
33 public function isIncludable() {
38 * Get a FormOptions object containing the default options
42 public function getDefaultOptions() {
43 $opts = parent
::getDefaultOptions();
44 $user = $this->getUser();
46 // Overwrite RC options with Watchlist options
47 // (calling #add() again is okay)
48 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
49 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
50 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
51 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
52 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
53 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
54 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
57 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
63 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
65 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
66 * to the current ones.
68 * @param FormOptions $parameters
71 protected function fetchOptionsFromRequest( $opts ) {
72 static $compatibilityMap = array(
73 'hideMinor' => 'hideminor',
74 'hideBots' => 'hidebots',
75 'hideAnons' => 'hideanons',
76 'hideLiu' => 'hideliu',
77 'hidePatrolled' => 'hidepatrolled',
78 'hideOwn' => 'hidemyself',
81 $params = $this->getRequest()->getValues();
82 foreach ( $compatibilityMap as $from => $to ) {
83 if ( isset( $params[$from] ) ) {
84 $params[$to] = $params[$from];
85 unset( $params[$from] );
89 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
90 // methods defined on WebRequest and removing this dependency would cause some code duplication.
91 $request = new DerivativeRequest( $this->getRequest(), $params );
92 $opts->fetchValuesFromRequest( $request );
97 * Get custom show/hide filters
99 * @return array Map of filter URL param names to properties (msg/default)
101 protected function getCustomFilters() {
102 if ( $this->customFilters
=== null ) {
103 $this->customFilters
= array();
104 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
) );
107 return $this->customFilters
;
111 * Process $par and put options found if $opts. Not used for Watchlist.
114 * @param FormOptions $opts
116 public function parseParameters( $par, FormOptions
$opts ) {
120 * Get the current FormOptions for this request
122 public function getOptions() {
123 if ( $this->rcOptions
=== null ) {
124 $this->rcOptions
= $this->setup( null );
127 return $this->rcOptions
;
132 * @param $par Parameter passed to the page
134 function execute( $par ) {
135 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
137 $user = $this->getUser();
138 $output = $this->getOutput();
140 # Anons don't get a watchlist
141 $this->requireLogin( 'watchlistanontext' );
144 $this->checkPermissions();
146 $request = $this->getRequest();
147 $opts = $this->getOptions();
149 $mode = SpecialEditWatchlist
::getMode( $request, $par );
150 if ( $mode !== false ) {
151 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
152 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
154 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
157 $output->redirect( $title->getLocalURL() );
161 if ( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
162 $request->wasPosted() )
164 $user->clearAllNotifications();
165 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
170 $this->outputHeader();
173 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
175 $this->numItems
= $this->countItems( $dbr ); // TODO kill me
176 if ( $this->numItems
== 0 ) {
177 $output->addWikiMsg( 'nowatchlist' );
181 // Fetch results, prepare a batch link existence check query
182 $conds = $this->buildMainQueryConds( $opts );
183 $rows = $this->doMainQuery( $conds, $opts );
184 $this->numRows
= $rows->numRows(); // TODO kill me
185 if ( $rows === false ) {
186 $this->doHeader( $opts );
191 $feedFormat = $this->getRequest()->getVal( 'feed' );
192 if ( !$feedFormat ) {
193 $batch = new LinkBatch
;
194 foreach ( $rows as $row ) {
195 $batch->add( NS_USER
, $row->rc_user_text
);
196 $batch->add( NS_USER_TALK
, $row->rc_user_text
);
197 $batch->add( $row->rc_namespace
, $row->rc_title
);
202 list( $changesFeed, $formatter ) = $this->getFeedObject( $feedFormat );
203 /** @var ChangesFeed $changesFeed */
204 $changesFeed->execute( $formatter, $rows, $lastmod, $opts );
206 $this->webOutput( $rows, $opts );
213 * Return an array of conditions depending of options set in $opts
215 * @param FormOptions $opts
218 public function buildMainQueryConds( FormOptions
$opts ) {
219 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
221 $user = $this->getUser();
224 if ( $opts['days'] > 0 ) {
225 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
229 if ( $opts['hidemyself'] ) {
230 $conds[] = 'rc_user != ' . $user->getId();
232 if ( $opts['hidebots'] ) {
233 $conds[] = 'rc_bot = 0';
235 if ( $opts['hideminor'] ) {
236 $conds[] = 'rc_minor = 0';
238 if ( $opts['hideliu'] ) {
239 $conds[] = 'rc_user = 0';
241 if ( $opts['hideanons'] ) {
242 $conds[] = 'rc_user != 0';
244 if ( $user->useRCPatrol() && $opts['hidepatrolled'] ) {
245 $conds[] = 'rc_patrolled != 1';
248 # Namespace filtering
249 if ( $opts['namespace'] !== '' ) {
250 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
251 $operator = $opts['invert'] ?
'!=' : '=';
252 $boolean = $opts['invert'] ?
'AND' : 'OR';
254 # namespace association (bug 2429)
255 if ( !$opts['associated'] ) {
256 $condition = "rc_namespace $operator $selectedNS";
258 # Also add the associated namespace
259 $associatedNS = $dbr->addQuotes(
260 MWNamespace
::getAssociated( $opts['namespace'] )
262 $condition = "(rc_namespace $operator $selectedNS "
264 . " rc_namespace $operator $associatedNS)";
267 $conds[] = $condition;
276 * @param array $conds
277 * @param FormOptions $opts
278 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
280 public function doMainQuery( $conds, $opts ) {
281 global $wgShowUpdatedMarker;
283 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
284 $user = $this->getUser();
285 # Toggle watchlist content (all recent edits or just the latest)
286 if ( $opts['extended'] ) {
287 $limitWatchlist = $user->getIntOption( 'wllimit' );
290 # Top log Ids for a page are not stored
291 $nonRevisionTypes = array( RC_LOG
);
292 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
293 if ( $nonRevisionTypes ) {
294 $conds[] = $dbr->makeList(
296 'rc_this_oldid=page_latest',
297 'rc_type' => $nonRevisionTypes,
306 $tables = array( 'recentchanges', 'watchlist' );
307 $fields = RecentChange
::selectFields();
309 'watchlist' => array(
312 'wl_user' => $user->getId(),
313 'wl_namespace=rc_namespace',
318 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
319 if ( $wgShowUpdatedMarker ) {
320 $fields[] = 'wl_notificationtimestamp';
322 if ( $limitWatchlist ) {
323 $options['LIMIT'] = $limitWatchlist;
326 $rollbacker = $user->isAllowed( 'rollback' );
327 if ( $usePage ||
$rollbacker ) {
329 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
331 $fields[] = 'page_latest';
335 // Log entries with DELETED_ACTION must not show up unless the user has
336 // the necessary rights.
337 if ( !$user->isAllowed( 'deletedhistory' ) ) {
338 $bitmask = LogPage
::DELETED_ACTION
;
339 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
340 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
345 $conds[] = $dbr->makeList( array(
346 'rc_type != ' . RC_LOG
,
347 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
352 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
353 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $opts ) );
355 return $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
359 * Send output to the OutputPage object, only called if not used feeds
361 * @param array $rows Database rows
362 * @param FormOptions $opts
364 public function webOutput( $rows, $opts ) {
365 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
367 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
368 $user = $this->getUser();
370 $dbr->dataSeek( $rows, 0 );
372 $list = ChangesList
::newFromContext( $this->getContext() );
373 $list->setWatchlistDivs();
375 $s = $list->beginRecentChangesList();
377 foreach ( $rows as $obj ) {
379 $rc = RecentChange
::newFromRow( $obj );
380 $rc->counter
= $counter++
;
382 if ( $wgShowUpdatedMarker ) {
383 $updated = $obj->wl_notificationtimestamp
;
388 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
389 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
392 'wl_namespace' => $obj->rc_namespace
,
393 'wl_title' => $obj->rc_title
,
397 $rc->numberofWatchingusers
= 0;
400 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
401 if ( $changeLine !== false ) {
405 $s .= $list->endRecentChangesList();
409 $output = $this->getOutput();
411 $output->addSubtitle(
412 $this->msg( 'watchlistfor2', $user->getName() )
413 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
416 // Output options box
417 $this->doHeader( $opts );
420 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
422 $this->addFeedLinks( array(
423 'action' => 'feedwatchlist',
425 'wlowner' => $user->getName(),
426 'wltoken' => $wlToken,
430 # Show a message about slave lag, if applicable
431 $lag = wfGetLB()->safeGetLag( $dbr );
433 $output->showLagWarning( $lag );
436 if ( $rows->numRows() == 0 ) {
437 $output->wrapWikiMsg(
438 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
441 $output->addHTML( $s );
445 function setTopText( FormOptions
$opts ) {
446 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
448 $nondefaults = $opts->getChangedValues();
450 $user = $this->getUser();
452 # Show watchlist header
454 $form .= $this->msg( 'watchlist-details' )->numParams( $this->numItems
)->parse() . "\n";
455 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
456 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
458 if ( $wgShowUpdatedMarker ) {
459 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
463 if ( $wgShowUpdatedMarker ) {
464 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
465 'action' => $this->getPageTitle()->getLocalURL(),
466 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
467 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
468 Html
::hidden( 'reset', 'all' ) . "\n";
469 foreach ( $nondefaults as $key => $value ) {
470 $form .= Html
::hidden( $key, $value ) . "\n";
472 $form .= Xml
::closeElement( 'form' ) . "\n";
475 $form .= Xml
::openElement( 'form', array(
477 'action' => $this->getPageTitle()->getLocalURL(),
478 'id' => 'mw-watchlist-form'
480 $form .= Xml
::fieldset(
481 $this->msg( 'watchlist-options' )->text(),
483 array( 'id' => 'mw-watchlist-options' )
486 $form .= SpecialRecentChanges
::makeLegend( $this->getContext() );
488 $this->getOutput()->addHTML( $form );
492 * Return the text to be displayed above the changes
494 * @param FormOptions $opts
495 * @return string XHTML
497 public function doHeader( $opts ) {
500 $user = $this->getUser();
502 $this->setTopText( $opts );
504 $lang = $this->getLanguage();
506 if ( $opts['days'] > 0 ) {
507 $timestamp = wfTimestampNow();
508 $wlInfo = $this->msg( 'wlnote' )->numParams( $this->numRows
, round( $opts['days'] * 24 ) )->params(
509 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
512 $nondefaults = $opts->getChangedValues();
513 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
515 # Spit out some control panel links
517 'hideminor' => 'rcshowhideminor',
518 'hidebots' => 'rcshowhidebots',
519 'hideanons' => 'rcshowhideanons',
520 'hideliu' => 'rcshowhideliu',
521 'hidemyself' => 'rcshowhidemine',
522 'hidepatrolled' => 'rcshowhidepatr'
524 foreach ( $this->getCustomFilters() as $key => $params ) {
525 $filters[$key] = $params['msg'];
527 // Disable some if needed
528 if ( !$user->useNPPatrol() ) {
529 unset( $filters['hidepatrolled'] );
533 foreach ( $filters as $name => $msg ) {
534 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
537 $hiddenFields = $nondefaults;
538 unset( $hiddenFields['namespace'] );
539 unset( $hiddenFields['invert'] );
540 unset( $hiddenFields['associated'] );
545 # Namespace filter and put the whole form together.
547 $form .= $cutofflinks;
548 $form .= $lang->pipeList( $links ) . "\n";
549 $form .= "<hr />\n<p>";
550 $form .= Html
::namespaceSelector(
552 'selected' => $opts['namespace'],
554 'label' => $this->msg( 'namespace' )->text()
556 'name' => 'namespace',
558 'class' => 'namespaceselector',
561 $form .= Xml
::checkLabel(
562 $this->msg( 'invert' )->text(),
566 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
568 $form .= Xml
::checkLabel(
569 $this->msg( 'namespace_association' )->text(),
573 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
575 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
576 foreach ( $hiddenFields as $key => $value ) {
577 $form .= Html
::hidden( $key, $value ) . "\n";
579 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
580 $form .= Xml
::closeElement( 'form' ) . "\n";
581 $this->getOutput()->addHTML( $form );
583 $this->setBottomText( $opts );
586 protected function showHideLink( $options, $message, $name, $value ) {
587 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
588 $options[$name] = 1 - (int)$value;
590 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getPageTitle(), $label, array(), $options ) )->escaped();
593 protected function hoursLink( $h, $options = array() ) {
594 $options['days'] = ( $h / 24.0 );
596 return Linker
::linkKnown(
597 $this->getPageTitle(),
598 $this->getLanguage()->formatNum( $h ),
604 protected function daysLink( $d, $options = array() ) {
605 $options['days'] = $d;
606 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
608 return Linker
::linkKnown(
609 $this->getPageTitle(),
619 * @param int $days This gets overwritten, so is not used
620 * @param array $options Query parameters for URL
623 protected function cutoffLinks( $days, $options = array() ) {
624 $hours = array( 1, 2, 6, 12 );
625 $days = array( 1, 3, 7 );
627 foreach ( $hours as $h ) {
628 $hours[$i++
] = $this->hoursLink( $h, $options );
631 foreach ( $days as $d ) {
632 $days[$i++
] = $this->daysLink( $d, $options );
634 return $this->msg( 'wlshowlast' )->rawParams(
635 $this->getLanguage()->pipeList( $hours ),
636 $this->getLanguage()->pipeList( $days ),
637 $this->daysLink( 0, $options ) )->parse();
641 * Count the number of items on a user's watchlist
643 * @param DatabaseBase $dbr A database connection
646 protected function countItems( $dbr ) {
647 # Fetch the raw count
648 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
649 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
650 $row = $dbr->fetchObject( $rows );
651 $count = $row->count
;
653 return floor( $count / 2 );