b3225475e8ec775e5f384cd0d0b2b1c61333ec1e
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 ) {
79 case SpecialEditWatchlist
::EDIT_CLEAR
:
82 case SpecialEditWatchlist
::EDIT_RAW
:
88 $title = SpecialPage
::getTitleFor( 'EditWatchlist', $mode );
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' ) ), /* 3.0 or 0.5, watch further below */
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'] = $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 if ( is_null( $values['days'] ) ||
!is_numeric( $values['days'] ) ) {
162 $big = 1000; /* The magical big */
163 if ( $nitems > $big ) {
164 # Set default cutoff shorter
165 $values['days'] = $defaults['days'] = ( 12.0 / 24.0 ); # 12 hours...
167 $values['days'] = $defaults['days']; # default cutoff for shortlisters
170 $values['days'] = floatval( $values['days'] );
173 // Dump everything here
174 $nondefaults = array();
175 foreach ( $defaults as $name => $defValue ) {
176 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
179 if ( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
180 $request->wasPosted() )
182 $user->clearAllNotifications();
183 $output->redirect( $this->getTitle()->getFullURL( $nondefaults ) );
187 # Possible where conditions
190 if ( $values['days'] > 0 ) {
191 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $values['days'] * 86400 ) ) );
194 # If the watchlist is relatively short, it's simplest to zip
195 # down its entirety and then sort the results.
197 # If it's relatively long, it may be worth our while to zip
198 # through the time-sorted page list checking for watched items.
200 # Up estimate of watched items by 15% to compensate for talk pages...
203 if ( $values['hideOwn'] ) {
204 $conds[] = 'rc_user != ' . $user->getId();
206 if ( $values['hideBots'] ) {
207 $conds[] = 'rc_bot = 0';
209 if ( $values['hideMinor'] ) {
210 $conds[] = 'rc_minor = 0';
212 if ( $values['hideLiu'] ) {
213 $conds[] = 'rc_user = 0';
215 if ( $values['hideAnons'] ) {
216 $conds[] = 'rc_user != 0';
218 if ( $user->useRCPatrol() && $values['hidePatrolled'] ) {
219 $conds[] = 'rc_patrolled != 1';
221 if ( $nameSpaceClause ) {
222 $conds[] = $nameSpaceClause;
225 # Toggle watchlist content (all recent edits or just the latest)
226 if ( $values['extended'] ) {
227 $limitWatchlist = $user->getIntOption( 'wllimit' );
230 # Top log Ids for a page are not stored
231 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG
;
236 # Show a message about slave lag, if applicable
237 $lag = wfGetLB()->safeGetLag( $dbr );
239 $output->showLagWarning( $lag );
243 $form = Xml
::fieldset(
244 $this->msg( 'watchlist-options' )->text(),
246 array( 'id' => 'mw-watchlist-options' )
249 # Show watchlist header
251 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
252 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
253 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
255 if ( $wgShowUpdatedMarker ) {
256 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
260 if ( $wgShowUpdatedMarker ) {
261 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
262 'action' => $this->getTitle()->getLocalURL(),
263 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
264 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
265 Html
::hidden( 'reset', 'all' ) . "\n";
266 foreach ( $nondefaults as $key => $value ) {
267 $form .= Html
::hidden( $key, $value ) . "\n";
269 $form .= Xml
::closeElement( 'form' ) . "\n";
274 $tables = array( 'recentchanges', 'watchlist' );
275 $fields = RecentChange
::selectFields();
277 'watchlist' => array(
280 'wl_user' => $user->getId(),
281 'wl_namespace=rc_namespace',
286 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
287 if ( $wgShowUpdatedMarker ) {
288 $fields[] = 'wl_notificationtimestamp';
290 if ( $limitWatchlist ) {
291 $options['LIMIT'] = $limitWatchlist;
294 $rollbacker = $user->isAllowed( 'rollback' );
295 if ( $usePage ||
$rollbacker ) {
297 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
299 $fields[] = 'page_latest';
303 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
304 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $values ) );
306 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
307 $numRows = $res->numRows();
309 /* Start bottom header */
311 $lang = $this->getLanguage();
313 if ( $values['days'] > 0 ) {
314 $timestamp = wfTimestampNow();
315 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
316 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
319 $cutofflinks = $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
321 # Spit out some control panel links
323 'hideMinor' => 'rcshowhideminor',
324 'hideBots' => 'rcshowhidebots',
325 'hideAnons' => 'rcshowhideanons',
326 'hideLiu' => 'rcshowhideliu',
327 'hideOwn' => 'rcshowhidemine',
328 'hidePatrolled' => 'rcshowhidepatr'
330 foreach ( $this->customFilters
as $key => $params ) {
331 $filters[$key] = $params['msg'];
333 // Disable some if needed
334 if ( !$user->useNPPatrol() ) {
335 unset( $filters['hidePatrolled'] );
339 foreach ( $filters as $name => $msg ) {
340 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
343 $hiddenFields = $nondefaults;
344 unset( $hiddenFields['namespace'] );
345 unset( $hiddenFields['invert'] );
346 unset( $hiddenFields['associated'] );
348 # Namespace filter and put the whole form together.
350 $form .= $cutofflinks;
351 $form .= $lang->pipeList( $links ) . "\n";
352 $form .= Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'id' => 'mw-watchlist-form-namespaceselector' ) ) . "\n";
353 $form .= "<hr />\n<p>";
354 $form .= Html
::namespaceSelector(
356 'selected' => $nameSpace,
358 'label' => $this->msg( 'namespace' )->text()
360 'name' => 'namespace',
362 'class' => 'namespaceselector',
365 $form .= Xml
::checkLabel(
366 $this->msg( 'invert' )->text(),
370 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
372 $form .= Xml
::checkLabel(
373 $this->msg( 'namespace_association' )->text(),
377 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
379 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
380 foreach ( $hiddenFields as $key => $value ) {
381 $form .= Html
::hidden( $key, $value ) . "\n";
383 $form .= Xml
::closeElement( 'form' ) . "\n";
384 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
385 $output->addHTML( $form );
387 # If there's nothing to show, stop here
388 if ( $numRows == 0 ) {
389 $output->wrapWikiMsg(
390 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
395 /* End bottom header */
397 /* Do link batch query */
398 $linkBatch = new LinkBatch
;
399 foreach ( $res as $row ) {
400 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
401 if ( $row->rc_user
!= 0 ) {
402 $linkBatch->add( NS_USER
, $userNameUnderscored );
404 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
406 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
408 $linkBatch->execute();
409 $dbr->dataSeek( $res, 0 );
411 $list = ChangesList
::newFromContext( $this->getContext() );
412 $list->setWatchlistDivs();
414 $s = $list->beginRecentChangesList();
416 foreach ( $res as $obj ) {
418 $rc = RecentChange
::newFromRow( $obj );
419 $rc->counter
= $counter++
;
421 if ( $wgShowUpdatedMarker ) {
422 $updated = $obj->wl_notificationtimestamp
;
427 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
428 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
431 'wl_namespace' => $obj->rc_namespace
,
432 'wl_title' => $obj->rc_title
,
436 $rc->numberofWatchingusers
= 0;
439 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
440 if ( $changeLine !== false ) {
444 $s .= $list->endRecentChangesList();
446 $output->addHTML( $s );
449 protected function showHideLink( $options, $message, $name, $value ) {
450 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
451 $options[$name] = 1 - (int) $value;
453 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getTitle(), $label, array(), $options ) )->escaped();
456 protected function hoursLink( $h, $options = array() ) {
457 $options['days'] = ( $h / 24.0 );
459 return Linker
::linkKnown(
461 $this->getLanguage()->formatNum( $h ),
467 protected function daysLink( $d, $options = array() ) {
468 $options['days'] = $d;
469 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
471 return Linker
::linkKnown(
482 * @param int $days This gets overwritten, so is not used
483 * @param array $options Query parameters for URL
486 protected function cutoffLinks( $days, $options = array() ) {
487 $hours = array( 1, 2, 6, 12 );
488 $days = array( 1, 3, 7 );
490 foreach ( $hours as $h ) {
491 $hours[$i++
] = $this->hoursLink( $h, $options );
494 foreach ( $days as $d ) {
495 $days[$i++
] = $this->daysLink( $d, $options );
497 return $this->msg( 'wlshowlast' )->rawParams(
498 $this->getLanguage()->pipeList( $hours ),
499 $this->getLanguage()->pipeList( $days ),
500 $this->daysLink( 0, $options ) )->parse();
504 * Count the number of items on a user's watchlist
506 * @param DatabaseBase $dbr A database connection
509 protected function countItems( $dbr ) {
510 # Fetch the raw count
511 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
512 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
513 $row = $dbr->fetchObject( $res );
514 $count = $row->count
;
516 return floor( $count / 2 );
519 protected function getGroupName() {