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 $this->numRows
= $rows->numRows(); // TODO kill me
78 if ( $rows === false ) {
79 $this->doHeader( $opts );
84 $feedFormat = $this->getRequest()->getVal( 'feed' );
86 $batch = new LinkBatch
;
87 foreach ( $rows as $row ) {
88 $batch->add( NS_USER
, $row->rc_user_text
);
89 $batch->add( NS_USER_TALK
, $row->rc_user_text
);
90 $batch->add( $row->rc_namespace
, $row->rc_title
);
95 list( $changesFeed, $formatter ) = $this->getFeedObject( $feedFormat );
96 /** @var ChangesFeed $changesFeed */
97 $changesFeed->execute( $formatter, $rows, $lastmod, $opts );
99 $this->webOutput( $rows, $opts );
106 * Get a FormOptions object containing the default options
108 * @return FormOptions
110 public function getDefaultOptions() {
111 $opts = parent
::getDefaultOptions();
112 $user = $this->getUser();
114 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
116 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
117 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
118 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
119 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
120 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
121 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
123 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
129 * Get custom show/hide filters
131 * @return array Map of filter URL param names to properties (msg/default)
133 protected function getCustomFilters() {
134 if ( $this->customFilters
=== null ) {
135 $this->customFilters
= array();
136 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
) );
139 return $this->customFilters
;
143 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
145 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
146 * to the current ones.
148 * @param FormOptions $parameters
149 * @return FormOptions
151 protected function fetchOptionsFromRequest( $opts ) {
152 static $compatibilityMap = array(
153 'hideMinor' => 'hideminor',
154 'hideBots' => 'hidebots',
155 'hideAnons' => 'hideanons',
156 'hideLiu' => 'hideliu',
157 'hidePatrolled' => 'hidepatrolled',
158 'hideOwn' => 'hidemyself',
161 $params = $this->getRequest()->getValues();
162 foreach ( $compatibilityMap as $from => $to ) {
163 if ( isset( $params[$from] ) ) {
164 $params[$to] = $params[$from];
165 unset( $params[$from] );
169 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
170 // methods defined on WebRequest and removing this dependency would cause some code duplication.
171 $request = new DerivativeRequest( $this->getRequest(), $params );
172 $opts->fetchValuesFromRequest( $request );
177 * Return an array of conditions depending of options set in $opts
179 * @param FormOptions $opts
182 public function buildMainQueryConds( FormOptions
$opts ) {
183 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
185 $user = $this->getUser();
188 if ( $opts['days'] > 0 ) {
189 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
193 if ( $opts['hidemyself'] ) {
194 $conds[] = 'rc_user != ' . $user->getId();
196 if ( $opts['hidebots'] ) {
197 $conds[] = 'rc_bot = 0';
199 if ( $opts['hideminor'] ) {
200 $conds[] = 'rc_minor = 0';
202 if ( $opts['hideliu'] ) {
203 $conds[] = 'rc_user = 0';
205 if ( $opts['hideanons'] ) {
206 $conds[] = 'rc_user != 0';
208 if ( $user->useRCPatrol() && $opts['hidepatrolled'] ) {
209 $conds[] = 'rc_patrolled != 1';
212 # Namespace filtering
213 if ( $opts['namespace'] !== '' ) {
214 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
215 $operator = $opts['invert'] ?
'!=' : '=';
216 $boolean = $opts['invert'] ?
'AND' : 'OR';
218 # namespace association (bug 2429)
219 if ( !$opts['associated'] ) {
220 $condition = "rc_namespace $operator $selectedNS";
222 # Also add the associated namespace
223 $associatedNS = $dbr->addQuotes(
224 MWNamespace
::getAssociated( $opts['namespace'] )
226 $condition = "(rc_namespace $operator $selectedNS "
228 . " rc_namespace $operator $associatedNS)";
231 $conds[] = $condition;
240 * @param array $conds
241 * @param FormOptions $opts
242 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
244 public function doMainQuery( $conds, $opts ) {
245 global $wgShowUpdatedMarker;
247 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
248 $user = $this->getUser();
249 # Toggle watchlist content (all recent edits or just the latest)
250 if ( $opts['extended'] ) {
251 $limitWatchlist = $user->getIntOption( 'wllimit' );
254 # Top log Ids for a page are not stored
255 $nonRevisionTypes = array( RC_LOG
);
256 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
257 if ( $nonRevisionTypes ) {
258 $conds[] = $dbr->makeList(
260 'rc_this_oldid=page_latest',
261 'rc_type' => $nonRevisionTypes,
270 $tables = array( 'recentchanges', 'watchlist' );
271 $fields = RecentChange
::selectFields();
273 'watchlist' => array(
276 'wl_user' => $user->getId(),
277 'wl_namespace=rc_namespace',
282 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
283 if ( $wgShowUpdatedMarker ) {
284 $fields[] = 'wl_notificationtimestamp';
286 if ( $limitWatchlist ) {
287 $options['LIMIT'] = $limitWatchlist;
290 $rollbacker = $user->isAllowed( 'rollback' );
291 if ( $usePage ||
$rollbacker ) {
293 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
295 $fields[] = 'page_latest';
299 // Log entries with DELETED_ACTION must not show up unless the user has
300 // the necessary rights.
301 if ( !$user->isAllowed( 'deletedhistory' ) ) {
302 $bitmask = LogPage
::DELETED_ACTION
;
303 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
304 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
309 $conds[] = $dbr->makeList( array(
310 'rc_type != ' . RC_LOG
,
311 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
316 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
317 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $opts ) );
319 return $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
323 * Send output to the OutputPage object, only called if not used feeds
325 * @param array $rows Database rows
326 * @param FormOptions $opts
328 public function webOutput( $rows, $opts ) {
329 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
331 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
332 $user = $this->getUser();
334 $dbr->dataSeek( $rows, 0 );
336 $list = ChangesList
::newFromContext( $this->getContext() );
337 $list->setWatchlistDivs();
339 $s = $list->beginRecentChangesList();
341 foreach ( $rows as $obj ) {
343 $rc = RecentChange
::newFromRow( $obj );
344 $rc->counter
= $counter++
;
346 if ( $wgShowUpdatedMarker ) {
347 $updated = $obj->wl_notificationtimestamp
;
352 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
353 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
356 'wl_namespace' => $obj->rc_namespace
,
357 'wl_title' => $obj->rc_title
,
361 $rc->numberofWatchingusers
= 0;
364 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
365 if ( $changeLine !== false ) {
369 $s .= $list->endRecentChangesList();
373 $output = $this->getOutput();
375 $output->addSubtitle(
376 $this->msg( 'watchlistfor2', $user->getName() )
377 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
380 // Output options box
381 $this->doHeader( $opts );
384 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
386 $this->addFeedLinks( array(
387 'action' => 'feedwatchlist',
389 'wlowner' => $user->getName(),
390 'wltoken' => $wlToken,
394 # Show a message about slave lag, if applicable
395 $lag = wfGetLB()->safeGetLag( $dbr );
397 $output->showLagWarning( $lag );
400 if ( $rows->numRows() == 0 ) {
401 $output->wrapWikiMsg(
402 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
405 $output->addHTML( $s );
410 * Return the text to be displayed above the changes
412 * @param FormOptions $opts
413 * @return string XHTML
415 public function doHeader( $opts ) {
418 $user = $this->getUser();
420 $this->setTopText( $opts );
422 $lang = $this->getLanguage();
424 if ( $opts['days'] > 0 ) {
425 $timestamp = wfTimestampNow();
426 $wlInfo = $this->msg( 'wlnote' )->numParams( $this->numRows
, round( $opts['days'] * 24 ) )->params(
427 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
430 $nondefaults = $opts->getChangedValues();
431 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
433 # Spit out some control panel links
435 'hideminor' => 'rcshowhideminor',
436 'hidebots' => 'rcshowhidebots',
437 'hideanons' => 'rcshowhideanons',
438 'hideliu' => 'rcshowhideliu',
439 'hidemyself' => 'rcshowhidemine',
440 'hidepatrolled' => 'rcshowhidepatr'
442 foreach ( $this->getCustomFilters() as $key => $params ) {
443 $filters[$key] = $params['msg'];
445 // Disable some if needed
446 if ( !$user->useNPPatrol() ) {
447 unset( $filters['hidepatrolled'] );
451 foreach ( $filters as $name => $msg ) {
452 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
455 $hiddenFields = $nondefaults;
456 unset( $hiddenFields['namespace'] );
457 unset( $hiddenFields['invert'] );
458 unset( $hiddenFields['associated'] );
463 # Namespace filter and put the whole form together.
465 $form .= $cutofflinks;
466 $form .= $lang->pipeList( $links ) . "\n";
467 $form .= "<hr />\n<p>";
468 $form .= Html
::namespaceSelector(
470 'selected' => $opts['namespace'],
472 'label' => $this->msg( 'namespace' )->text()
474 'name' => 'namespace',
476 'class' => 'namespaceselector',
479 $form .= Xml
::checkLabel(
480 $this->msg( 'invert' )->text(),
484 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
486 $form .= Xml
::checkLabel(
487 $this->msg( 'namespace_association' )->text(),
491 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
493 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
494 foreach ( $hiddenFields as $key => $value ) {
495 $form .= Html
::hidden( $key, $value ) . "\n";
497 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
498 $form .= Xml
::closeElement( 'form' ) . "\n";
499 $this->getOutput()->addHTML( $form );
501 $this->setBottomText( $opts );
504 function setTopText( FormOptions
$opts ) {
505 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
507 $nondefaults = $opts->getChangedValues();
509 $user = $this->getUser();
511 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
512 $numItems = $this->countItems( $dbr );
514 // Show watchlist header
516 if ( $numItems == 0 ) {
517 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
519 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
520 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
521 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
523 if ( $wgShowUpdatedMarker ) {
524 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
529 if ( $numItems > 0 && $wgShowUpdatedMarker ) {
530 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
531 'action' => $this->getPageTitle()->getLocalURL(),
532 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
533 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
534 Html
::hidden( 'reset', 'all' ) . "\n";
535 foreach ( $nondefaults as $key => $value ) {
536 $form .= Html
::hidden( $key, $value ) . "\n";
538 $form .= Xml
::closeElement( 'form' ) . "\n";
541 $form .= Xml
::openElement( 'form', array(
543 'action' => $this->getPageTitle()->getLocalURL(),
544 'id' => 'mw-watchlist-form'
546 $form .= Xml
::fieldset(
547 $this->msg( 'watchlist-options' )->text(),
549 array( 'id' => 'mw-watchlist-options' )
552 $form .= SpecialRecentChanges
::makeLegend( $this->getContext() );
554 $this->getOutput()->addHTML( $form );
557 protected function showHideLink( $options, $message, $name, $value ) {
558 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
559 $options[$name] = 1 - (int)$value;
561 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getPageTitle(), $label, array(), $options ) )->escaped();
564 protected function hoursLink( $h, $options = array() ) {
565 $options['days'] = ( $h / 24.0 );
567 return Linker
::linkKnown(
568 $this->getPageTitle(),
569 $this->getLanguage()->formatNum( $h ),
575 protected function daysLink( $d, $options = array() ) {
576 $options['days'] = $d;
577 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
579 return Linker
::linkKnown(
580 $this->getPageTitle(),
590 * @param int $days This gets overwritten, so is not used
591 * @param array $options Query parameters for URL
594 protected function cutoffLinks( $days, $options = array() ) {
595 $hours = array( 1, 2, 6, 12 );
596 $days = array( 1, 3, 7 );
598 foreach ( $hours as $h ) {
599 $hours[$i++
] = $this->hoursLink( $h, $options );
602 foreach ( $days as $d ) {
603 $days[$i++
] = $this->daysLink( $d, $options );
605 return $this->msg( 'wlshowlast' )->rawParams(
606 $this->getLanguage()->pipeList( $hours ),
607 $this->getLanguage()->pipeList( $days ),
608 $this->daysLink( 0, $options ) )->parse();
612 * Count the number of items on a user's watchlist
614 * @param DatabaseBase $dbr A database connection
617 protected function countItems( $dbr ) {
618 # Fetch the raw count
619 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
620 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
621 $row = $dbr->fetchObject( $rows );
622 $count = $row->count
;
624 return floor( $count / 2 );