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
{
34 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
35 parent
::__construct( $page, $restriction );
39 * Main execution point
41 * @param string $subpage
43 function execute( $subpage ) {
44 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
46 // Anons don't get a watchlist
47 $this->requireLogin( 'watchlistanontext' );
49 $output = $this->getOutput();
50 $request = $this->getRequest();
52 $mode = SpecialEditWatchlist
::getMode( $request, $subpage );
53 if ( $mode !== false ) {
54 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
55 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
57 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
60 $output->redirect( $title->getLocalURL() );
64 $this->checkPermissions();
66 $user = $this->getUser();
67 $opts = $this->getOptions();
69 if ( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
70 $request->wasPosted() )
72 $user->clearAllNotifications();
73 $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
= array();
111 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
) );
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 $parameters
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 );
152 * Return an array of conditions depending of options set in $opts
154 * @param FormOptions $opts
157 public function buildMainQueryConds( FormOptions
$opts ) {
158 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
160 $user = $this->getUser();
163 if ( $opts['days'] > 0 ) {
164 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
168 if ( $opts['hidemyself'] ) {
169 $conds[] = 'rc_user != ' . $user->getId();
171 if ( $opts['hidebots'] ) {
172 $conds[] = 'rc_bot = 0';
174 if ( $opts['hideminor'] ) {
175 $conds[] = 'rc_minor = 0';
177 if ( $opts['hideliu'] ) {
178 $conds[] = 'rc_user = 0';
180 if ( $opts['hideanons'] ) {
181 $conds[] = 'rc_user != 0';
183 if ( $user->useRCPatrol() && $opts['hidepatrolled'] ) {
184 $conds[] = 'rc_patrolled != 1';
187 # Namespace filtering
188 if ( $opts['namespace'] !== '' ) {
189 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
190 $operator = $opts['invert'] ?
'!=' : '=';
191 $boolean = $opts['invert'] ?
'AND' : 'OR';
193 # namespace association (bug 2429)
194 if ( !$opts['associated'] ) {
195 $condition = "rc_namespace $operator $selectedNS";
197 # Also add the associated namespace
198 $associatedNS = $dbr->addQuotes(
199 MWNamespace
::getAssociated( $opts['namespace'] )
201 $condition = "(rc_namespace $operator $selectedNS "
203 . " rc_namespace $operator $associatedNS)";
206 $conds[] = $condition;
215 * @param array $conds
216 * @param FormOptions $opts
217 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
219 public function doMainQuery( $conds, $opts ) {
220 global $wgShowUpdatedMarker;
222 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
223 $user = $this->getUser();
224 # Toggle watchlist content (all recent edits or just the latest)
225 if ( $opts['extended'] ) {
226 $limitWatchlist = $user->getIntOption( 'wllimit' );
229 # Top log Ids for a page are not stored
230 $nonRevisionTypes = array( RC_LOG
);
231 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
232 if ( $nonRevisionTypes ) {
233 $conds[] = $dbr->makeList(
235 'rc_this_oldid=page_latest',
236 'rc_type' => $nonRevisionTypes,
245 $tables = array( 'recentchanges', 'watchlist' );
246 $fields = RecentChange
::selectFields();
248 'watchlist' => array(
251 'wl_user' => $user->getId(),
252 'wl_namespace=rc_namespace',
257 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
258 if ( $wgShowUpdatedMarker ) {
259 $fields[] = 'wl_notificationtimestamp';
261 if ( $limitWatchlist ) {
262 $options['LIMIT'] = $limitWatchlist;
265 $rollbacker = $user->isAllowed( 'rollback' );
266 if ( $usePage ||
$rollbacker ) {
268 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
270 $fields[] = 'page_latest';
274 // Log entries with DELETED_ACTION must not show up unless the user has
275 // the necessary rights.
276 if ( !$user->isAllowed( 'deletedhistory' ) ) {
277 $bitmask = LogPage
::DELETED_ACTION
;
278 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
279 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
284 $conds[] = $dbr->makeList( array(
285 'rc_type != ' . RC_LOG
,
286 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
291 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
292 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $opts ) );
294 return $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
298 * Send output to the OutputPage object, only called if not used feeds
300 * @param ResultWrapper $rows Database rows
301 * @param FormOptions $opts
303 public function webOutput( $rows, $opts ) {
304 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
306 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
307 $user = $this->getUser();
309 $dbr->dataSeek( $rows, 0 );
311 $list = ChangesList
::newFromContext( $this->getContext() );
312 $list->setWatchlistDivs();
314 $s = $list->beginRecentChangesList();
316 foreach ( $rows as $obj ) {
318 $rc = RecentChange
::newFromRow( $obj );
319 $rc->counter
= $counter++
;
321 if ( $wgShowUpdatedMarker ) {
322 $updated = $obj->wl_notificationtimestamp
;
327 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
328 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
331 'wl_namespace' => $obj->rc_namespace
,
332 'wl_title' => $obj->rc_title
,
336 $rc->numberofWatchingusers
= 0;
339 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
340 if ( $changeLine !== false ) {
344 $s .= $list->endRecentChangesList();
348 $output = $this->getOutput();
350 $output->addSubtitle(
351 $this->msg( 'watchlistfor2', $user->getName() )
352 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
355 // Output options box
356 $this->doHeader( $opts );
359 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
361 $this->addFeedLinks( array(
362 'action' => 'feedwatchlist',
364 'wlowner' => $user->getName(),
365 'wltoken' => $wlToken,
369 # Show a message about slave lag, if applicable
370 $lag = wfGetLB()->safeGetLag( $dbr );
372 $output->showLagWarning( $lag );
375 if ( $rows->numRows() == 0 ) {
376 $output->wrapWikiMsg(
377 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
380 $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->setTopText( $opts );
395 $lang = $this->getLanguage();
397 if ( $opts['days'] > 0 ) {
398 $timestamp = wfTimestampNow();
399 $wlInfo = $this->msg( 'wlnote2' )->numParams( round( $opts['days'] * 24 ) )->params(
400 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
403 $nondefaults = $opts->getChangedValues();
404 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
406 # Spit out some control panel links
408 'hideminor' => 'rcshowhideminor',
409 'hidebots' => 'rcshowhidebots',
410 'hideanons' => 'rcshowhideanons',
411 'hideliu' => 'rcshowhideliu',
412 'hidemyself' => 'rcshowhidemine',
413 'hidepatrolled' => 'rcshowhidepatr'
415 foreach ( $this->getCustomFilters() as $key => $params ) {
416 $filters[$key] = $params['msg'];
418 // Disable some if needed
419 if ( !$user->useNPPatrol() ) {
420 unset( $filters['hidepatrolled'] );
424 foreach ( $filters as $name => $msg ) {
425 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
428 $hiddenFields = $nondefaults;
429 unset( $hiddenFields['namespace'] );
430 unset( $hiddenFields['invert'] );
431 unset( $hiddenFields['associated'] );
436 # Namespace filter and put the whole form together.
438 $form .= $cutofflinks;
439 $form .= $lang->pipeList( $links ) . "\n";
440 $form .= "<hr />\n<p>";
441 $form .= Html
::namespaceSelector(
443 'selected' => $opts['namespace'],
445 'label' => $this->msg( 'namespace' )->text()
447 'name' => 'namespace',
449 'class' => 'namespaceselector',
452 $form .= Xml
::checkLabel(
453 $this->msg( 'invert' )->text(),
457 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
459 $form .= Xml
::checkLabel(
460 $this->msg( 'namespace_association' )->text(),
464 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
466 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
467 foreach ( $hiddenFields as $key => $value ) {
468 $form .= Html
::hidden( $key, $value ) . "\n";
470 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
471 $form .= Xml
::closeElement( 'form' ) . "\n";
472 $this->getOutput()->addHTML( $form );
474 $this->setBottomText( $opts );
477 function setTopText( FormOptions
$opts ) {
478 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
480 $nondefaults = $opts->getChangedValues();
482 $user = $this->getUser();
484 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
485 $numItems = $this->countItems( $dbr );
487 // Show watchlist header
489 if ( $numItems == 0 ) {
490 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
492 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
493 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
494 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
496 if ( $wgShowUpdatedMarker ) {
497 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
502 if ( $numItems > 0 && $wgShowUpdatedMarker ) {
503 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
504 'action' => $this->getPageTitle()->getLocalURL(),
505 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
506 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
507 Html
::hidden( 'reset', 'all' ) . "\n";
508 foreach ( $nondefaults as $key => $value ) {
509 $form .= Html
::hidden( $key, $value ) . "\n";
511 $form .= Xml
::closeElement( 'form' ) . "\n";
514 $form .= Xml
::openElement( 'form', array(
516 'action' => $this->getPageTitle()->getLocalURL(),
517 'id' => 'mw-watchlist-form'
519 $form .= Xml
::fieldset(
520 $this->msg( 'watchlist-options' )->text(),
522 array( 'id' => 'mw-watchlist-options' )
525 $form .= SpecialRecentChanges
::makeLegend( $this->getContext() );
527 $this->getOutput()->addHTML( $form );
530 protected function showHideLink( $options, $message, $name, $value ) {
531 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
532 $options[$name] = 1 - (int)$value;
534 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getPageTitle(), $label, array(), $options ) )->escaped();
537 protected function hoursLink( $h, $options = array() ) {
538 $options['days'] = ( $h / 24.0 );
540 return Linker
::linkKnown(
541 $this->getPageTitle(),
542 $this->getLanguage()->formatNum( $h ),
548 protected function daysLink( $d, $options = array() ) {
549 $options['days'] = $d;
550 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
552 return Linker
::linkKnown(
553 $this->getPageTitle(),
563 * @param int $days This gets overwritten, so is not used
564 * @param array $options Query parameters for URL
567 protected function cutoffLinks( $days, $options = array() ) {
568 $hours = array( 1, 2, 6, 12 );
569 $days = array( 1, 3, 7 );
571 foreach ( $hours as $h ) {
572 $hours[$i++
] = $this->hoursLink( $h, $options );
575 foreach ( $days as $d ) {
576 $days[$i++
] = $this->daysLink( $d, $options );
578 return $this->msg( 'wlshowlast' )->rawParams(
579 $this->getLanguage()->pipeList( $hours ),
580 $this->getLanguage()->pipeList( $days ),
581 $this->daysLink( 0, $options ) )->parse();
585 * Count the number of items on a user's watchlist
587 * @param DatabaseBase $dbr A database connection
590 protected function countItems( $dbr ) {
591 # Fetch the raw count
592 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
593 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
594 $row = $dbr->fetchObject( $rows );
595 $count = $row->count
;
597 return floor( $count / 2 );