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->getOption( 'watchlisttoken' );
63 $wlToken = MWCryptRand
::generateHex( 40 );
64 $user->setOption( 'watchlisttoken', $wlToken );
65 $user->saveSettings();
68 $this->addFeedLinks( array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
69 'wlowner' => $user->getName(), 'wltoken' => $wlToken ) );
72 $this->outputHeader();
74 $output->addSubtitle( $this->msg( 'watchlistfor2', $user->getName()
75 )->rawParams( SpecialEditWatchlist
::buildTools( null ) ) );
77 $request = $this->getRequest();
79 $mode = SpecialEditWatchlist
::getMode( $request, $par );
80 if ( $mode !== false ) {
83 case SpecialEditWatchlist
::EDIT_CLEAR
:
86 case SpecialEditWatchlist
::EDIT_RAW
:
92 $title = SpecialPage
::getTitleFor( 'EditWatchlist', $mode );
93 $output->redirect( $title->getLocalURL() );
97 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
99 $nitems = $this->countItems( $dbr );
100 if ( $nitems == 0 ) {
101 $output->addWikiMsg( 'nowatchlist' );
105 // @todo use FormOptions!
107 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
108 /* bool */ 'hideMinor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
109 /* bool */ 'hideBots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
110 /* bool */ 'hideAnons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
111 /* bool */ 'hideLiu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
112 /* bool */ 'hidePatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
113 /* bool */ 'hideOwn' => (int)$user->getBoolOption( 'watchlisthideown' ),
114 /* bool */ 'extended' => (int)$user->getBoolOption( 'extendwatchlist' ),
115 /* ? */ 'namespace' => '', //means all
116 /* ? */ 'invert' => false,
117 /* bool */ 'associated' => false,
119 $this->customFilters
= array();
120 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
) );
121 foreach ( $this->customFilters
as $key => $params ) {
122 $defaults[$key] = $params['default'];
125 # Extract variables from the request, falling back to user preferences or
126 # other default values if these don't exist
128 $values['days'] = $request->getVal( 'days', $defaults['days'] );
129 $values['hideMinor'] = (int)$request->getBool( 'hideMinor', $defaults['hideMinor'] );
130 $values['hideBots'] = (int)$request->getBool( 'hideBots', $defaults['hideBots'] );
131 $values['hideAnons'] = (int)$request->getBool( 'hideAnons', $defaults['hideAnons'] );
132 $values['hideLiu'] = (int)$request->getBool( 'hideLiu', $defaults['hideLiu'] );
133 $values['hideOwn'] = (int)$request->getBool( 'hideOwn', $defaults['hideOwn'] );
134 $values['hidePatrolled'] = (int)$request->getBool( 'hidePatrolled', $defaults['hidePatrolled'] );
135 $values['extended'] = (int)$request->getBool( 'extended', $defaults['extended'] );
136 foreach ( $this->customFilters
as $key => $params ) {
137 $values[$key] = (int)$request->getBool( $key, $defaults[$key] );
140 # Get namespace value, if supplied, and prepare a WHERE fragment
141 $nameSpace = $request->getIntOrNull( 'namespace' );
142 $invert = $request->getBool( 'invert' );
143 $associated = $request->getBool( 'associated' );
144 if ( !is_null( $nameSpace ) ) {
145 $eq_op = $invert ?
'!=' : '=';
146 $bool_op = $invert ?
'AND' : 'OR';
147 $nameSpace = intval( $nameSpace ); // paranioa
148 if ( !$associated ) {
149 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
151 $associatedNS = MWNamespace
::getAssociated( $nameSpace );
153 "rc_namespace $eq_op $nameSpace " .
155 " rc_namespace $eq_op $associatedNS";
159 $nameSpaceClause = '';
161 $values['namespace'] = $nameSpace;
162 $values['invert'] = $invert;
163 $values['associated'] = $associated;
165 if ( is_null( $values['days'] ) ||
!is_numeric( $values['days'] ) ) {
166 $big = 1000; /* The magical big */
167 if ( $nitems > $big ) {
168 # Set default cutoff shorter
169 $values['days'] = $defaults['days'] = ( 12.0 / 24.0 ); # 12 hours...
171 $values['days'] = $defaults['days']; # default cutoff for shortlisters
174 $values['days'] = floatval( $values['days'] );
177 // Dump everything here
178 $nondefaults = array();
179 foreach ( $defaults as $name => $defValue ) {
180 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
183 if ( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
184 $request->wasPosted() )
186 $user->clearAllNotifications();
187 $output->redirect( $this->getTitle()->getFullURL( $nondefaults ) );
191 # Possible where conditions
194 if ( $values['days'] > 0 ) {
195 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $values['days'] * 86400 ) ) );
198 # If the watchlist is relatively short, it's simplest to zip
199 # down its entirety and then sort the results.
201 # If it's relatively long, it may be worth our while to zip
202 # through the time-sorted page list checking for watched items.
204 # Up estimate of watched items by 15% to compensate for talk pages...
207 if ( $values['hideOwn'] ) {
208 $conds[] = 'rc_user != ' . $user->getId();
210 if ( $values['hideBots'] ) {
211 $conds[] = 'rc_bot = 0';
213 if ( $values['hideMinor'] ) {
214 $conds[] = 'rc_minor = 0';
216 if ( $values['hideLiu'] ) {
217 $conds[] = 'rc_user = 0';
219 if ( $values['hideAnons'] ) {
220 $conds[] = 'rc_user != 0';
222 if ( $user->useRCPatrol() && $values['hidePatrolled'] ) {
223 $conds[] = 'rc_patrolled != 1';
225 if ( $nameSpaceClause ) {
226 $conds[] = $nameSpaceClause;
229 # Toggle watchlist content (all recent edits or just the latest)
230 if ( $values['extended'] ) {
231 $limitWatchlist = $user->getIntOption( 'wllimit' );
234 # Top log Ids for a page are not stored
235 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG
;
240 # Show a message about slave lag, if applicable
241 $lag = wfGetLB()->safeGetLag( $dbr );
243 $output->showLagWarning( $lag );
247 $form = Xml
::fieldset(
248 $this->msg( 'watchlist-options' )->text(),
250 array( 'id' => 'mw-watchlist-options' )
253 # Show watchlist header
255 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
256 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
257 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
259 if ( $wgShowUpdatedMarker ) {
260 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
264 if ( $wgShowUpdatedMarker ) {
265 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
266 'action' => $this->getTitle()->getLocalURL(),
267 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
268 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
269 Html
::hidden( 'reset', 'all' ) . "\n";
270 foreach ( $nondefaults as $key => $value ) {
271 $form .= Html
::hidden( $key, $value ) . "\n";
273 $form .= Xml
::closeElement( 'form' ) . "\n";
278 $tables = array( 'recentchanges', 'watchlist' );
279 $fields = RecentChange
::selectFields();
281 'watchlist' => array(
284 'wl_user' => $user->getId(),
285 'wl_namespace=rc_namespace',
290 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
291 if ( $wgShowUpdatedMarker ) {
292 $fields[] = 'wl_notificationtimestamp';
294 if ( $limitWatchlist ) {
295 $options['LIMIT'] = $limitWatchlist;
298 $rollbacker = $user->isAllowed( 'rollback' );
299 if ( $usePage ||
$rollbacker ) {
301 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
303 $fields[] = 'page_latest';
307 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
308 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $values ) );
310 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
311 $numRows = $res->numRows();
313 /* Start bottom header */
315 $lang = $this->getLanguage();
317 if ( $values['days'] > 0 ) {
318 $timestamp = wfTimestampNow();
319 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
320 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
323 $cutofflinks = $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
325 # Spit out some control panel links
327 'hideMinor' => 'rcshowhideminor',
328 'hideBots' => 'rcshowhidebots',
329 'hideAnons' => 'rcshowhideanons',
330 'hideLiu' => 'rcshowhideliu',
331 'hideOwn' => 'rcshowhidemine',
332 'hidePatrolled' => 'rcshowhidepatr'
334 foreach ( $this->customFilters
as $key => $params ) {
335 $filters[$key] = $params['msg'];
337 // Disable some if needed
338 if ( !$user->useNPPatrol() ) {
339 unset( $filters['hidePatrolled'] );
343 foreach ( $filters as $name => $msg ) {
344 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
347 $hiddenFields = $nondefaults;
348 unset( $hiddenFields['namespace'] );
349 unset( $hiddenFields['invert'] );
350 unset( $hiddenFields['associated'] );
352 # Namespace filter and put the whole form together.
354 $form .= $cutofflinks;
355 $form .= $lang->pipeList( $links ) . "\n";
356 $form .= Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'id' => 'mw-watchlist-form-namespaceselector' ) ) . "\n";
357 $form .= "<hr />\n<p>";
358 $form .= Html
::namespaceSelector(
360 'selected' => $nameSpace,
362 'label' => $this->msg( 'namespace' )->text()
364 'name' => 'namespace',
366 'class' => 'namespaceselector',
369 $form .= Xml
::checkLabel(
370 $this->msg( 'invert' )->text(),
374 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
376 $form .= Xml
::checkLabel(
377 $this->msg( 'namespace_association' )->text(),
381 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
383 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
384 foreach ( $hiddenFields as $key => $value ) {
385 $form .= Html
::hidden( $key, $value ) . "\n";
387 $form .= Xml
::closeElement( 'form' ) . "\n";
388 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
389 $output->addHTML( $form );
391 # If there's nothing to show, stop here
392 if ( $numRows == 0 ) {
393 $output->addWikiMsg( 'watchnochange' );
397 /* End bottom header */
399 /* Do link batch query */
400 $linkBatch = new LinkBatch
;
401 foreach ( $res as $row ) {
402 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
403 if ( $row->rc_user
!= 0 ) {
404 $linkBatch->add( NS_USER
, $userNameUnderscored );
406 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
408 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
410 $linkBatch->execute();
411 $dbr->dataSeek( $res, 0 );
413 $list = ChangesList
::newFromContext( $this->getContext() );
414 $list->setWatchlistDivs();
416 $s = $list->beginRecentChangesList();
418 foreach ( $res as $obj ) {
420 $rc = RecentChange
::newFromRow( $obj );
421 $rc->counter
= $counter++
;
423 if ( $wgShowUpdatedMarker ) {
424 $updated = $obj->wl_notificationtimestamp
;
429 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
430 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
433 'wl_namespace' => $obj->rc_namespace
,
434 'wl_title' => $obj->rc_title
,
438 $rc->numberofWatchingusers
= 0;
441 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
442 if ( $changeLine !== false ) {
446 $s .= $list->endRecentChangesList();
448 $output->addHTML( $s );
451 protected function showHideLink( $options, $message, $name, $value ) {
452 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
453 $options[$name] = 1 - (int) $value;
455 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getTitle(), $label, array(), $options ) )->escaped();
458 protected function hoursLink( $h, $options = array() ) {
459 $options['days'] = ( $h / 24.0 );
461 return Linker
::linkKnown(
463 $this->getLanguage()->formatNum( $h ),
469 protected function daysLink( $d, $options = array() ) {
470 $options['days'] = $d;
471 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
473 return Linker
::linkKnown(
484 * @param int $days This gets overwritten, so is not used
485 * @param array $options Query parameters for URL
488 protected function cutoffLinks( $days, $options = array() ) {
489 $hours = array( 1, 2, 6, 12 );
490 $days = array( 1, 3, 7 );
492 foreach ( $hours as $h ) {
493 $hours[$i++
] = $this->hoursLink( $h, $options );
496 foreach ( $days as $d ) {
497 $days[$i++
] = $this->daysLink( $d, $options );
499 return $this->msg( 'wlshowlast' )->rawParams(
500 $this->getLanguage()->pipeList( $hours ),
501 $this->getLanguage()->pipeList( $days ),
502 $this->daysLink( 0, $options ) )->parse();
506 * Count the number of items on a user's watchlist
508 * @param DatabaseBase $dbr A database connection
511 protected function countItems( $dbr ) {
512 # Fetch the raw count
513 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
514 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
515 $row = $dbr->fetchObject( $res );
516 $count = $row->count
;
518 return floor( $count / 2 );
521 protected function getGroupName() {