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 * Map old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
39 * to the current ones.
41 * This creates derivative context and request, pokes with request's parameters, and sets them as
42 * the context for this class instance, mapping old keys to new ones completely transparently (as
43 * long as nothing tries to access the globals instead of current context).
45 private function mapCompatibilityRequestParameters() {
47 'hideMinor' => 'hideminor',
48 'hideBots' => 'hidebots',
49 'hideAnons' => 'hideanons',
50 'hideLiu' => 'hideliu',
51 'hidePatrolled' => 'hidepatrolled',
52 'hideOwn' => 'hidemyself',
55 $params = $this->getRequest()->getValues();
56 foreach ( $map as $from => $to ) {
57 if ( isset( $params[$from] ) ) {
58 $params[$to] = $params[$from];
59 unset( $params[$from] );
63 $context = new DerivativeContext( $this->getContext() );
64 $request = new DerivativeRequest( $context->getRequest(), $params );
65 $context->setRequest( $request );
66 $this->setContext( $context );
71 * @param $par Parameter passed to the page
73 function execute( $par ) {
74 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
76 $this->mapCompatibilityRequestParameters();
78 $user = $this->getUser();
79 $output = $this->getOutput();
80 $output->addModuleStyles( 'mediawiki.special.changeslist' );
81 $output->addModules( 'mediawiki.special.changeslist.js' );
83 # Anons don't get a watchlist
84 $this->requireLogin( 'watchlistanontext' );
87 $this->checkPermissions();
90 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
92 $this->addFeedLinks( array(
93 'action' => 'feedwatchlist',
95 'wlowner' => $user->getName(),
96 'wltoken' => $wlToken,
101 $this->outputHeader();
103 $output->addSubtitle(
104 $this->msg( 'watchlistfor2', $user->getName() )
105 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
108 $request = $this->getRequest();
110 $mode = SpecialEditWatchlist
::getMode( $request, $par );
111 if ( $mode !== false ) {
112 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
113 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
115 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
118 $output->redirect( $title->getLocalURL() );
122 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
124 $nitems = $this->countItems( $dbr );
125 if ( $nitems == 0 ) {
126 $output->addWikiMsg( 'nowatchlist' );
130 // @todo use FormOptions!
132 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ),
133 /* bool */ 'hideminor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
134 /* bool */ 'hidebots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
135 /* bool */ 'hideanons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
136 /* bool */ 'hideliu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
137 /* bool */ 'hidepatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
138 /* bool */ 'hidemyself' => (int)$user->getBoolOption( 'watchlisthideown' ),
139 /* bool */ 'extended' => (int)$user->getBoolOption( 'extendwatchlist' ),
140 /* ? */ 'namespace' => '', //means all
141 /* ? */ 'invert' => false,
142 /* bool */ 'associated' => false,
144 $this->customFilters
= array();
145 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
) );
146 foreach ( $this->customFilters
as $key => $params ) {
147 $defaults[$key] = $params['default'];
150 # Extract variables from the request, falling back to user preferences or
151 # other default values if these don't exist
153 $values['days'] = floatval( $request->getVal( 'days', $defaults['days'] ) );
154 $values['hideminor'] = (int)$request->getBool( 'hideminor', $defaults['hideminor'] );
155 $values['hidebots'] = (int)$request->getBool( 'hidebots', $defaults['hidebots'] );
156 $values['hideanons'] = (int)$request->getBool( 'hideanons', $defaults['hideanons'] );
157 $values['hideliu'] = (int)$request->getBool( 'hideliu', $defaults['hideliu'] );
158 $values['hidemyself'] = (int)$request->getBool( 'hidemyself', $defaults['hidemyself'] );
159 $values['hidepatrolled'] = (int)$request->getBool( 'hidepatrolled', $defaults['hidepatrolled'] );
160 $values['extended'] = (int)$request->getBool( 'extended', $defaults['extended'] );
161 foreach ( $this->customFilters
as $key => $params ) {
162 $values[$key] = (int)$request->getBool( $key, $defaults[$key] );
165 # Get namespace value, if supplied, and prepare a WHERE fragment
166 $nameSpace = $request->getIntOrNull( 'namespace' );
167 $invert = $request->getBool( 'invert' );
168 $associated = $request->getBool( 'associated' );
169 if ( !is_null( $nameSpace ) ) {
170 $eq_op = $invert ?
'!=' : '=';
171 $bool_op = $invert ?
'AND' : 'OR';
172 $nameSpace = intval( $nameSpace ); // paranioa
173 if ( !$associated ) {
174 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
176 $associatedNS = MWNamespace
::getAssociated( $nameSpace );
178 "rc_namespace $eq_op $nameSpace " .
180 " rc_namespace $eq_op $associatedNS";
184 $nameSpaceClause = '';
186 $values['namespace'] = $nameSpace;
187 $values['invert'] = $invert;
188 $values['associated'] = $associated;
190 // Dump everything here
191 $nondefaults = array();
192 foreach ( $defaults as $name => $defValue ) {
193 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
196 if ( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' )
197 && $request->wasPosted()
199 $user->clearAllNotifications();
200 $output->redirect( $this->getPageTitle()->getFullURL( $nondefaults ) );
204 # Possible where conditions
207 if ( $values['days'] > 0 ) {
208 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $values['days'] * 86400 ) ) );
212 if ( $values['hidemyself'] ) {
213 $conds[] = 'rc_user != ' . $user->getId();
215 if ( $values['hidebots'] ) {
216 $conds[] = 'rc_bot = 0';
218 if ( $values['hideminor'] ) {
219 $conds[] = 'rc_minor = 0';
221 if ( $values['hideliu'] ) {
222 $conds[] = 'rc_user = 0';
224 if ( $values['hideanons'] ) {
225 $conds[] = 'rc_user != 0';
227 if ( $user->useRCPatrol() && $values['hidepatrolled'] ) {
228 $conds[] = 'rc_patrolled != 1';
230 if ( $nameSpaceClause ) {
231 $conds[] = $nameSpaceClause;
234 # Toggle watchlist content (all recent edits or just the latest)
235 if ( $values['extended'] ) {
236 $limitWatchlist = $user->getIntOption( 'wllimit' );
239 # Top log Ids for a page are not stored
240 $nonRevisionTypes = array( RC_LOG
);
241 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
242 if ( $nonRevisionTypes ) {
243 $conds[] = $dbr->makeList(
245 'rc_this_oldid=page_latest',
246 'rc_type' => $nonRevisionTypes,
255 # Show a message about slave lag, if applicable
256 $lag = wfGetLB()->safeGetLag( $dbr );
258 $output->showLagWarning( $lag );
264 # Show watchlist header
266 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
267 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
268 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
270 if ( $wgShowUpdatedMarker ) {
271 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
275 if ( $wgShowUpdatedMarker ) {
276 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
277 'action' => $this->getPageTitle()->getLocalURL(),
278 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
279 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
280 Html
::hidden( 'reset', 'all' ) . "\n";
281 foreach ( $nondefaults as $key => $value ) {
282 $form .= Html
::hidden( $key, $value ) . "\n";
284 $form .= Xml
::closeElement( 'form' ) . "\n";
287 $form .= Xml
::openElement( 'form', array(
289 'action' => $this->getPageTitle()->getLocalURL(),
290 'id' => 'mw-watchlist-form'
292 $form .= Xml
::fieldset(
293 $this->msg( 'watchlist-options' )->text(),
295 array( 'id' => 'mw-watchlist-options' )
298 $form .= SpecialRecentChanges
::makeLegend( $this->getContext() );
300 $tables = array( 'recentchanges', 'watchlist' );
301 $fields = RecentChange
::selectFields();
303 'watchlist' => array(
306 'wl_user' => $user->getId(),
307 'wl_namespace=rc_namespace',
312 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
313 if ( $wgShowUpdatedMarker ) {
314 $fields[] = 'wl_notificationtimestamp';
316 if ( $limitWatchlist ) {
317 $options['LIMIT'] = $limitWatchlist;
320 $rollbacker = $user->isAllowed( 'rollback' );
321 if ( $usePage ||
$rollbacker ) {
323 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
325 $fields[] = 'page_latest';
329 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
330 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $values ) );
332 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
333 $numRows = $res->numRows();
335 /* Start bottom header */
337 $lang = $this->getLanguage();
339 if ( $values['days'] > 0 ) {
340 $timestamp = wfTimestampNow();
341 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
342 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
345 $cutofflinks = $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
347 # Spit out some control panel links
349 'hideminor' => 'rcshowhideminor',
350 'hidebots' => 'rcshowhidebots',
351 'hideanons' => 'rcshowhideanons',
352 'hideliu' => 'rcshowhideliu',
353 'hidemyself' => 'rcshowhidemine',
354 'hidepatrolled' => 'rcshowhidepatr'
356 foreach ( $this->customFilters
as $key => $params ) {
357 $filters[$key] = $params['msg'];
359 // Disable some if needed
360 if ( !$user->useNPPatrol() ) {
361 unset( $filters['hidepatrolled'] );
365 foreach ( $filters as $name => $msg ) {
366 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
369 $hiddenFields = $nondefaults;
370 unset( $hiddenFields['namespace'] );
371 unset( $hiddenFields['invert'] );
372 unset( $hiddenFields['associated'] );
374 # Namespace filter and put the whole form together.
376 $form .= $cutofflinks;
377 $form .= $lang->pipeList( $links ) . "\n";
378 $form .= "<hr />\n<p>";
379 $form .= Html
::namespaceSelector(
381 'selected' => $nameSpace,
383 'label' => $this->msg( 'namespace' )->text()
385 'name' => 'namespace',
387 'class' => 'namespaceselector',
390 $form .= Xml
::checkLabel(
391 $this->msg( 'invert' )->text(),
395 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
397 $form .= Xml
::checkLabel(
398 $this->msg( 'namespace_association' )->text(),
402 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
404 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
405 foreach ( $hiddenFields as $key => $value ) {
406 $form .= Html
::hidden( $key, $value ) . "\n";
408 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
409 $form .= Xml
::closeElement( 'form' ) . "\n";
410 $output->addHTML( $form );
412 # If there's nothing to show, stop here
413 if ( $numRows == 0 ) {
414 $output->wrapWikiMsg(
415 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
420 /* End bottom header */
422 /* Do link batch query */
423 $linkBatch = new LinkBatch
;
424 foreach ( $res as $row ) {
425 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
426 if ( $row->rc_user
!= 0 ) {
427 $linkBatch->add( NS_USER
, $userNameUnderscored );
429 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
431 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
433 $linkBatch->execute();
434 $dbr->dataSeek( $res, 0 );
436 $list = ChangesList
::newFromContext( $this->getContext() );
437 $list->setWatchlistDivs();
439 $s = $list->beginRecentChangesList();
441 foreach ( $res as $obj ) {
443 $rc = RecentChange
::newFromRow( $obj );
444 $rc->counter
= $counter++
;
446 if ( $wgShowUpdatedMarker ) {
447 $updated = $obj->wl_notificationtimestamp
;
452 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
453 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
456 'wl_namespace' => $obj->rc_namespace
,
457 'wl_title' => $obj->rc_title
,
461 $rc->numberofWatchingusers
= 0;
464 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
465 if ( $changeLine !== false ) {
469 $s .= $list->endRecentChangesList();
471 $output->addHTML( $s );
474 protected function showHideLink( $options, $message, $name, $value ) {
475 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
476 $options[$name] = 1 - (int)$value;
478 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getPageTitle(), $label, array(), $options ) )->escaped();
481 protected function hoursLink( $h, $options = array() ) {
482 $options['days'] = ( $h / 24.0 );
484 return Linker
::linkKnown(
485 $this->getPageTitle(),
486 $this->getLanguage()->formatNum( $h ),
492 protected function daysLink( $d, $options = array() ) {
493 $options['days'] = $d;
494 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
496 return Linker
::linkKnown(
497 $this->getPageTitle(),
507 * @param int $days This gets overwritten, so is not used
508 * @param array $options Query parameters for URL
511 protected function cutoffLinks( $days, $options = array() ) {
512 $hours = array( 1, 2, 6, 12 );
513 $days = array( 1, 3, 7 );
515 foreach ( $hours as $h ) {
516 $hours[$i++
] = $this->hoursLink( $h, $options );
519 foreach ( $days as $d ) {
520 $days[$i++
] = $this->daysLink( $d, $options );
522 return $this->msg( 'wlshowlast' )->rawParams(
523 $this->getLanguage()->pipeList( $hours ),
524 $this->getLanguage()->pipeList( $days ),
525 $this->daysLink( 0, $options ) )->parse();
529 * Count the number of items on a user's watchlist
531 * @param DatabaseBase $dbr A database connection
534 protected function countItems( $dbr ) {
535 # Fetch the raw count
536 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
537 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
538 $row = $dbr->fetchObject( $res );
539 $count = $row->count
;
541 return floor( $count / 2 );