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 SpecialPage
{
24 protected $customFilters;
29 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
30 parent
::__construct( $page, $restriction );
35 * @param $par Parameter passed to the page
37 function execute( $par ) {
38 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
40 $user = $this->getUser();
41 $output = $this->getOutput();
43 # Anons don't get a watchlist
44 if ( $user->isAnon() ) {
45 $output->setPageTitle( $this->msg( 'watchnologin' ) );
46 $output->setRobotPolicy( 'noindex,nofollow' );
47 $llink = Linker
::linkKnown(
48 SpecialPage
::getTitleFor( 'Userlogin' ),
49 $this->msg( 'loginreqlink' )->escaped(),
51 array( 'returnto' => $this->getTitle()->getPrefixedText() )
53 $output->addHTML( $this->msg( 'watchlistanontext' )->rawParams( $llink )->parse() );
58 $this->checkPermissions();
61 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
63 $this->addFeedLinks( array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
64 'wlowner' => $user->getName(), 'wltoken' => $wlToken ) );
68 $this->outputHeader();
70 $output->addSubtitle( $this->msg( 'watchlistfor2', $user->getName()
71 )->rawParams( SpecialEditWatchlist
::buildTools( null ) ) );
73 $request = $this->getRequest();
75 $mode = SpecialEditWatchlist
::getMode( $request, $par );
76 if ( $mode !== false ) {
77 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
78 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
80 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
83 $output->redirect( $title->getLocalURL() );
87 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
89 $nitems = $this->countItems( $dbr );
91 $output->addWikiMsg( 'nowatchlist' );
95 // @todo use FormOptions!
97 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ),
98 /* bool */ 'hideMinor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
99 /* bool */ 'hideBots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
100 /* bool */ 'hideAnons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
101 /* bool */ 'hideLiu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
102 /* bool */ 'hidePatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
103 /* bool */ 'hideOwn' => (int)$user->getBoolOption( 'watchlisthideown' ),
104 /* bool */ 'extended' => (int)$user->getBoolOption( 'extendwatchlist' ),
105 /* ? */ 'namespace' => '', //means all
106 /* ? */ 'invert' => false,
107 /* bool */ 'associated' => false,
109 $this->customFilters
= array();
110 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
) );
111 foreach ( $this->customFilters
as $key => $params ) {
112 $defaults[$key] = $params['default'];
115 # Extract variables from the request, falling back to user preferences or
116 # other default values if these don't exist
118 $values['days'] = floatval( $request->getVal( 'days', $defaults['days'] ) );
119 $values['hideMinor'] = (int)$request->getBool( 'hideMinor', $defaults['hideMinor'] );
120 $values['hideBots'] = (int)$request->getBool( 'hideBots', $defaults['hideBots'] );
121 $values['hideAnons'] = (int)$request->getBool( 'hideAnons', $defaults['hideAnons'] );
122 $values['hideLiu'] = (int)$request->getBool( 'hideLiu', $defaults['hideLiu'] );
123 $values['hideOwn'] = (int)$request->getBool( 'hideOwn', $defaults['hideOwn'] );
124 $values['hidePatrolled'] = (int)$request->getBool( 'hidePatrolled', $defaults['hidePatrolled'] );
125 $values['extended'] = (int)$request->getBool( 'extended', $defaults['extended'] );
126 foreach ( $this->customFilters
as $key => $params ) {
127 $values[$key] = (int)$request->getBool( $key, $defaults[$key] );
130 # Get namespace value, if supplied, and prepare a WHERE fragment
131 $nameSpace = $request->getIntOrNull( 'namespace' );
132 $invert = $request->getBool( 'invert' );
133 $associated = $request->getBool( 'associated' );
134 if ( !is_null( $nameSpace ) ) {
135 $eq_op = $invert ?
'!=' : '=';
136 $bool_op = $invert ?
'AND' : 'OR';
137 $nameSpace = intval( $nameSpace ); // paranioa
138 if ( !$associated ) {
139 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
141 $associatedNS = MWNamespace
::getAssociated( $nameSpace );
143 "rc_namespace $eq_op $nameSpace " .
145 " rc_namespace $eq_op $associatedNS";
149 $nameSpaceClause = '';
151 $values['namespace'] = $nameSpace;
152 $values['invert'] = $invert;
153 $values['associated'] = $associated;
155 // Dump everything here
156 $nondefaults = array();
157 foreach ( $defaults as $name => $defValue ) {
158 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
161 if ( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
162 $request->wasPosted() )
164 $user->clearAllNotifications();
165 $output->redirect( $this->getTitle()->getFullURL( $nondefaults ) );
169 # Possible where conditions
172 if ( $values['days'] > 0 ) {
173 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $values['days'] * 86400 ) ) );
177 if ( $values['hideOwn'] ) {
178 $conds[] = 'rc_user != ' . $user->getId();
180 if ( $values['hideBots'] ) {
181 $conds[] = 'rc_bot = 0';
183 if ( $values['hideMinor'] ) {
184 $conds[] = 'rc_minor = 0';
186 if ( $values['hideLiu'] ) {
187 $conds[] = 'rc_user = 0';
189 if ( $values['hideAnons'] ) {
190 $conds[] = 'rc_user != 0';
192 if ( $user->useRCPatrol() && $values['hidePatrolled'] ) {
193 $conds[] = 'rc_patrolled != 1';
195 if ( $nameSpaceClause ) {
196 $conds[] = $nameSpaceClause;
199 # Toggle watchlist content (all recent edits or just the latest)
200 if ( $values['extended'] ) {
201 $limitWatchlist = $user->getIntOption( 'wllimit' );
204 # Top log Ids for a page are not stored
205 $nonRevisionTypes = array( RC_LOG
);
206 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
207 if ( $nonRevisionTypes ) {
208 if ( count( $nonRevisionTypes ) === 1 ) {
209 // if only one use an equality instead of IN condition
210 $nonRevisionTypes = reset( $nonRevisionTypes );
212 $conds[] = $dbr->makeList(
214 'rc_this_oldid=page_latest',
215 'rc_type' => $nonRevisionTypes,
224 # Show a message about slave lag, if applicable
225 $lag = wfGetLB()->safeGetLag( $dbr );
227 $output->showLagWarning( $lag );
233 # Show watchlist header
235 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
236 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
237 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
239 if ( $wgShowUpdatedMarker ) {
240 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
244 if ( $wgShowUpdatedMarker ) {
245 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
246 'action' => $this->getTitle()->getLocalURL(),
247 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
248 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
249 Html
::hidden( 'reset', 'all' ) . "\n";
250 foreach ( $nondefaults as $key => $value ) {
251 $form .= Html
::hidden( $key, $value ) . "\n";
253 $form .= Xml
::closeElement( 'form' ) . "\n";
256 $form .= Xml
::openElement( 'form', array(
258 'action' => $this->getTitle()->getLocalURL(),
259 'id' => 'mw-watchlist-form'
261 $form .= Xml
::fieldset(
262 $this->msg( 'watchlist-options' )->text(),
264 array( 'id' => 'mw-watchlist-options' )
267 $tables = array( 'recentchanges', 'watchlist' );
268 $fields = RecentChange
::selectFields();
270 'watchlist' => array(
273 'wl_user' => $user->getId(),
274 'wl_namespace=rc_namespace',
279 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
280 if ( $wgShowUpdatedMarker ) {
281 $fields[] = 'wl_notificationtimestamp';
283 if ( $limitWatchlist ) {
284 $options['LIMIT'] = $limitWatchlist;
287 $rollbacker = $user->isAllowed( 'rollback' );
288 if ( $usePage ||
$rollbacker ) {
290 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
292 $fields[] = 'page_latest';
296 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
297 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $values ) );
299 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
300 $numRows = $res->numRows();
302 /* Start bottom header */
304 $lang = $this->getLanguage();
306 if ( $values['days'] > 0 ) {
307 $timestamp = wfTimestampNow();
308 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
309 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
312 $cutofflinks = $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
314 # Spit out some control panel links
316 'hideMinor' => 'rcshowhideminor',
317 'hideBots' => 'rcshowhidebots',
318 'hideAnons' => 'rcshowhideanons',
319 'hideLiu' => 'rcshowhideliu',
320 'hideOwn' => 'rcshowhidemine',
321 'hidePatrolled' => 'rcshowhidepatr'
323 foreach ( $this->customFilters
as $key => $params ) {
324 $filters[$key] = $params['msg'];
326 // Disable some if needed
327 if ( !$user->useNPPatrol() ) {
328 unset( $filters['hidePatrolled'] );
332 foreach ( $filters as $name => $msg ) {
333 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
336 $hiddenFields = $nondefaults;
337 unset( $hiddenFields['namespace'] );
338 unset( $hiddenFields['invert'] );
339 unset( $hiddenFields['associated'] );
341 # Namespace filter and put the whole form together.
343 $form .= $cutofflinks;
344 $form .= $lang->pipeList( $links ) . "\n";
345 $form .= "<hr />\n<p>";
346 $form .= Html
::namespaceSelector(
348 'selected' => $nameSpace,
350 'label' => $this->msg( 'namespace' )->text()
352 'name' => 'namespace',
354 'class' => 'namespaceselector',
357 $form .= Xml
::checkLabel(
358 $this->msg( 'invert' )->text(),
362 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
364 $form .= Xml
::checkLabel(
365 $this->msg( 'namespace_association' )->text(),
369 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
371 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
372 foreach ( $hiddenFields as $key => $value ) {
373 $form .= Html
::hidden( $key, $value ) . "\n";
375 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
376 $form .= Xml
::closeElement( 'form' ) . "\n";
377 $output->addHTML( $form );
379 # If there's nothing to show, stop here
380 if ( $numRows == 0 ) {
381 $output->wrapWikiMsg(
382 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
387 /* End bottom header */
389 /* Do link batch query */
390 $linkBatch = new LinkBatch
;
391 foreach ( $res as $row ) {
392 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
393 if ( $row->rc_user
!= 0 ) {
394 $linkBatch->add( NS_USER
, $userNameUnderscored );
396 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
398 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
400 $linkBatch->execute();
401 $dbr->dataSeek( $res, 0 );
403 $list = ChangesList
::newFromContext( $this->getContext() );
404 $list->setWatchlistDivs();
406 $s = $list->beginRecentChangesList();
408 foreach ( $res as $obj ) {
410 $rc = RecentChange
::newFromRow( $obj );
411 $rc->counter
= $counter++
;
413 if ( $wgShowUpdatedMarker ) {
414 $updated = $obj->wl_notificationtimestamp
;
419 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
420 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
423 'wl_namespace' => $obj->rc_namespace
,
424 'wl_title' => $obj->rc_title
,
428 $rc->numberofWatchingusers
= 0;
431 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
432 if ( $changeLine !== false ) {
436 $s .= $list->endRecentChangesList();
438 $output->addHTML( $s );
441 protected function showHideLink( $options, $message, $name, $value ) {
442 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
443 $options[$name] = 1 - (int)$value;
445 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getTitle(), $label, array(), $options ) )->escaped();
448 protected function hoursLink( $h, $options = array() ) {
449 $options['days'] = ( $h / 24.0 );
451 return Linker
::linkKnown(
453 $this->getLanguage()->formatNum( $h ),
459 protected function daysLink( $d, $options = array() ) {
460 $options['days'] = $d;
461 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
463 return Linker
::linkKnown(
474 * @param int $days This gets overwritten, so is not used
475 * @param array $options Query parameters for URL
478 protected function cutoffLinks( $days, $options = array() ) {
479 $hours = array( 1, 2, 6, 12 );
480 $days = array( 1, 3, 7 );
482 foreach ( $hours as $h ) {
483 $hours[$i++
] = $this->hoursLink( $h, $options );
486 foreach ( $days as $d ) {
487 $days[$i++
] = $this->daysLink( $d, $options );
489 return $this->msg( 'wlshowlast' )->rawParams(
490 $this->getLanguage()->pipeList( $hours ),
491 $this->getLanguage()->pipeList( $days ),
492 $this->daysLink( 0, $options ) )->parse();
496 * Count the number of items on a user's watchlist
498 * @param DatabaseBase $dbr A database connection
501 protected function countItems( $dbr ) {
502 # Fetch the raw count
503 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
504 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
505 $row = $dbr->fetchObject( $res );
506 $count = $row->count
;
508 return floor( $count / 2 );
511 protected function getGroupName() {