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(
64 'action' => 'feedwatchlist',
66 'wlowner' => $user->getName(),
67 'wltoken' => $wlToken,
72 $this->outputHeader();
75 $this->msg( 'watchlistfor2', $user->getName() )
76 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
79 $request = $this->getRequest();
81 $mode = SpecialEditWatchlist
::getMode( $request, $par );
82 if ( $mode !== false ) {
83 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
84 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
86 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
89 $output->redirect( $title->getLocalURL() );
93 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
95 $nitems = $this->countItems( $dbr );
97 $output->addWikiMsg( 'nowatchlist' );
101 // @todo use FormOptions!
103 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ),
104 /* bool */ 'hideMinor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
105 /* bool */ 'hideBots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
106 /* bool */ 'hideAnons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
107 /* bool */ 'hideLiu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
108 /* bool */ 'hidePatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
109 /* bool */ 'hideOwn' => (int)$user->getBoolOption( 'watchlisthideown' ),
110 /* bool */ 'extended' => (int)$user->getBoolOption( 'extendwatchlist' ),
111 /* ? */ 'namespace' => '', //means all
112 /* ? */ 'invert' => false,
113 /* bool */ 'associated' => false,
115 $this->customFilters
= array();
116 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
) );
117 foreach ( $this->customFilters
as $key => $params ) {
118 $defaults[$key] = $params['default'];
121 # Extract variables from the request, falling back to user preferences or
122 # other default values if these don't exist
124 $values['days'] = floatval( $request->getVal( 'days', $defaults['days'] ) );
125 $values['hideMinor'] = (int)$request->getBool( 'hideMinor', $defaults['hideMinor'] );
126 $values['hideBots'] = (int)$request->getBool( 'hideBots', $defaults['hideBots'] );
127 $values['hideAnons'] = (int)$request->getBool( 'hideAnons', $defaults['hideAnons'] );
128 $values['hideLiu'] = (int)$request->getBool( 'hideLiu', $defaults['hideLiu'] );
129 $values['hideOwn'] = (int)$request->getBool( 'hideOwn', $defaults['hideOwn'] );
130 $values['hidePatrolled'] = (int)$request->getBool( 'hidePatrolled', $defaults['hidePatrolled'] );
131 $values['extended'] = (int)$request->getBool( 'extended', $defaults['extended'] );
132 foreach ( $this->customFilters
as $key => $params ) {
133 $values[$key] = (int)$request->getBool( $key, $defaults[$key] );
136 # Get namespace value, if supplied, and prepare a WHERE fragment
137 $nameSpace = $request->getIntOrNull( 'namespace' );
138 $invert = $request->getBool( 'invert' );
139 $associated = $request->getBool( 'associated' );
140 if ( !is_null( $nameSpace ) ) {
141 $eq_op = $invert ?
'!=' : '=';
142 $bool_op = $invert ?
'AND' : 'OR';
143 $nameSpace = intval( $nameSpace ); // paranioa
144 if ( !$associated ) {
145 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
147 $associatedNS = MWNamespace
::getAssociated( $nameSpace );
149 "rc_namespace $eq_op $nameSpace " .
151 " rc_namespace $eq_op $associatedNS";
155 $nameSpaceClause = '';
157 $values['namespace'] = $nameSpace;
158 $values['invert'] = $invert;
159 $values['associated'] = $associated;
161 // Dump everything here
162 $nondefaults = array();
163 foreach ( $defaults as $name => $defValue ) {
164 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
167 if ( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
168 $request->wasPosted() )
170 $user->clearAllNotifications();
171 $output->redirect( $this->getTitle()->getFullURL( $nondefaults ) );
175 # Possible where conditions
178 if ( $values['days'] > 0 ) {
179 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $values['days'] * 86400 ) ) );
183 if ( $values['hideOwn'] ) {
184 $conds[] = 'rc_user != ' . $user->getId();
186 if ( $values['hideBots'] ) {
187 $conds[] = 'rc_bot = 0';
189 if ( $values['hideMinor'] ) {
190 $conds[] = 'rc_minor = 0';
192 if ( $values['hideLiu'] ) {
193 $conds[] = 'rc_user = 0';
195 if ( $values['hideAnons'] ) {
196 $conds[] = 'rc_user != 0';
198 if ( $user->useRCPatrol() && $values['hidePatrolled'] ) {
199 $conds[] = 'rc_patrolled != 1';
201 if ( $nameSpaceClause ) {
202 $conds[] = $nameSpaceClause;
205 # Toggle watchlist content (all recent edits or just the latest)
206 if ( $values['extended'] ) {
207 $limitWatchlist = $user->getIntOption( 'wllimit' );
210 # Top log Ids for a page are not stored
211 $nonRevisionTypes = array( RC_LOG
);
212 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
213 if ( $nonRevisionTypes ) {
214 $conds[] = $dbr->makeList(
216 'rc_this_oldid=page_latest',
217 'rc_type' => $nonRevisionTypes,
226 # Show a message about slave lag, if applicable
227 $lag = wfGetLB()->safeGetLag( $dbr );
229 $output->showLagWarning( $lag );
235 # Show watchlist header
237 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
238 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
239 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
241 if ( $wgShowUpdatedMarker ) {
242 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
246 if ( $wgShowUpdatedMarker ) {
247 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
248 'action' => $this->getTitle()->getLocalURL(),
249 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
250 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
251 Html
::hidden( 'reset', 'all' ) . "\n";
252 foreach ( $nondefaults as $key => $value ) {
253 $form .= Html
::hidden( $key, $value ) . "\n";
255 $form .= Xml
::closeElement( 'form' ) . "\n";
258 $form .= Xml
::openElement( 'form', array(
260 'action' => $this->getTitle()->getLocalURL(),
261 'id' => 'mw-watchlist-form'
263 $form .= Xml
::fieldset(
264 $this->msg( 'watchlist-options' )->text(),
266 array( 'id' => 'mw-watchlist-options' )
269 $tables = array( 'recentchanges', 'watchlist' );
270 $fields = RecentChange
::selectFields();
272 'watchlist' => array(
275 'wl_user' => $user->getId(),
276 'wl_namespace=rc_namespace',
281 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
282 if ( $wgShowUpdatedMarker ) {
283 $fields[] = 'wl_notificationtimestamp';
285 if ( $limitWatchlist ) {
286 $options['LIMIT'] = $limitWatchlist;
289 $rollbacker = $user->isAllowed( 'rollback' );
290 if ( $usePage ||
$rollbacker ) {
292 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
294 $fields[] = 'page_latest';
298 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
299 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $values ) );
301 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
302 $numRows = $res->numRows();
304 /* Start bottom header */
306 $lang = $this->getLanguage();
308 if ( $values['days'] > 0 ) {
309 $timestamp = wfTimestampNow();
310 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
311 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
314 $cutofflinks = $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
316 # Spit out some control panel links
318 'hideMinor' => 'rcshowhideminor',
319 'hideBots' => 'rcshowhidebots',
320 'hideAnons' => 'rcshowhideanons',
321 'hideLiu' => 'rcshowhideliu',
322 'hideOwn' => 'rcshowhidemine',
323 'hidePatrolled' => 'rcshowhidepatr'
325 foreach ( $this->customFilters
as $key => $params ) {
326 $filters[$key] = $params['msg'];
328 // Disable some if needed
329 if ( !$user->useNPPatrol() ) {
330 unset( $filters['hidePatrolled'] );
334 foreach ( $filters as $name => $msg ) {
335 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
338 $hiddenFields = $nondefaults;
339 unset( $hiddenFields['namespace'] );
340 unset( $hiddenFields['invert'] );
341 unset( $hiddenFields['associated'] );
343 # Namespace filter and put the whole form together.
345 $form .= $cutofflinks;
346 $form .= $lang->pipeList( $links ) . "\n";
347 $form .= "<hr />\n<p>";
348 $form .= Html
::namespaceSelector(
350 'selected' => $nameSpace,
352 'label' => $this->msg( 'namespace' )->text()
354 'name' => 'namespace',
356 'class' => 'namespaceselector',
359 $form .= Xml
::checkLabel(
360 $this->msg( 'invert' )->text(),
364 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
366 $form .= Xml
::checkLabel(
367 $this->msg( 'namespace_association' )->text(),
371 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
373 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
374 foreach ( $hiddenFields as $key => $value ) {
375 $form .= Html
::hidden( $key, $value ) . "\n";
377 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
378 $form .= Xml
::closeElement( 'form' ) . "\n";
379 $output->addHTML( $form );
381 # If there's nothing to show, stop here
382 if ( $numRows == 0 ) {
383 $output->wrapWikiMsg(
384 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
389 /* End bottom header */
391 /* Do link batch query */
392 $linkBatch = new LinkBatch
;
393 foreach ( $res as $row ) {
394 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
395 if ( $row->rc_user
!= 0 ) {
396 $linkBatch->add( NS_USER
, $userNameUnderscored );
398 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
400 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
402 $linkBatch->execute();
403 $dbr->dataSeek( $res, 0 );
405 $list = ChangesList
::newFromContext( $this->getContext() );
406 $list->setWatchlistDivs();
408 $s = $list->beginRecentChangesList();
410 foreach ( $res as $obj ) {
412 $rc = RecentChange
::newFromRow( $obj );
413 $rc->counter
= $counter++
;
415 if ( $wgShowUpdatedMarker ) {
416 $updated = $obj->wl_notificationtimestamp
;
421 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
422 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
425 'wl_namespace' => $obj->rc_namespace
,
426 'wl_title' => $obj->rc_title
,
430 $rc->numberofWatchingusers
= 0;
433 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
434 if ( $changeLine !== false ) {
438 $s .= $list->endRecentChangesList();
440 $output->addHTML( $s );
443 protected function showHideLink( $options, $message, $name, $value ) {
444 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
445 $options[$name] = 1 - (int)$value;
447 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getTitle(), $label, array(), $options ) )->escaped();
450 protected function hoursLink( $h, $options = array() ) {
451 $options['days'] = ( $h / 24.0 );
453 return Linker
::linkKnown(
455 $this->getLanguage()->formatNum( $h ),
461 protected function daysLink( $d, $options = array() ) {
462 $options['days'] = $d;
463 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
465 return Linker
::linkKnown(
476 * @param int $days This gets overwritten, so is not used
477 * @param array $options Query parameters for URL
480 protected function cutoffLinks( $days, $options = array() ) {
481 $hours = array( 1, 2, 6, 12 );
482 $days = array( 1, 3, 7 );
484 foreach ( $hours as $h ) {
485 $hours[$i++
] = $this->hoursLink( $h, $options );
488 foreach ( $days as $d ) {
489 $days[$i++
] = $this->daysLink( $d, $options );
491 return $this->msg( 'wlshowlast' )->rawParams(
492 $this->getLanguage()->pipeList( $hours ),
493 $this->getLanguage()->pipeList( $days ),
494 $this->daysLink( 0, $options ) )->parse();
498 * Count the number of items on a user's watchlist
500 * @param DatabaseBase $dbr A database connection
503 protected function countItems( $dbr ) {
504 # Fetch the raw count
505 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
506 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
507 $row = $dbr->fetchObject( $res );
508 $count = $row->count
;
510 return floor( $count / 2 );
513 protected function getGroupName() {