Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / specials / SpecialWatchlist.php
blobff4313f2e2fa3780f68a43b7c19a0f4e15da838d
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage Watchlist
23 class SpecialWatchlist extends SpecialPage {
24 protected $customFilters;
26 /**
27 * Constructor
29 public function __construct( $page = 'Watchlist' ) {
30 parent::__construct( $page );
33 /**
34 * Execute
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(),
50 array(),
51 array( 'returnto' => $this->getTitle()->getPrefixedText() )
53 $output->addHTML( $this->msg( 'watchlistanontext' )->rawParams( $llink )->parse() );
54 return;
57 // Add feed links
58 $wlToken = $user->getOption( 'watchlisttoken' );
59 if ( !$wlToken ) {
60 $wlToken = MWCryptRand::generateHex( 40 );
61 $user->setOption( 'watchlisttoken', $wlToken );
62 $user->saveSettings();
65 $this->addFeedLinks( array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
66 'wlowner' => $user->getName(), 'wltoken' => $wlToken ) );
68 $this->setHeaders();
69 $this->outputHeader();
71 $output->addSubtitle( $this->msg( 'watchlistfor2', $user->getName()
72 )->rawParams( SpecialEditWatchlist::buildTools( null ) ) );
74 $request = $this->getRequest();
76 $mode = SpecialEditWatchlist::getMode( $request, $par );
77 if ( $mode !== false ) {
78 # TODO: localise?
79 switch( $mode ) {
80 case SpecialEditWatchlist::EDIT_CLEAR:
81 $mode = 'clear';
82 break;
83 case SpecialEditWatchlist::EDIT_RAW:
84 $mode = 'raw';
85 break;
86 default:
87 $mode = null;
89 $title = SpecialPage::getTitleFor( 'EditWatchlist', $mode );
90 $output->redirect( $title->getLocalURL() );
91 return;
94 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
96 $nitems = $this->countItems( $dbr );
97 if ( $nitems == 0 ) {
98 $output->addWikiMsg( 'nowatchlist' );
99 return;
102 // @TODO: use FormOptions!
103 $defaults = array(
104 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
105 /* bool */ 'hideMinor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
106 /* bool */ 'hideBots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
107 /* bool */ 'hideAnons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
108 /* bool */ 'hideLiu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
109 /* bool */ 'hidePatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
110 /* bool */ 'hideOwn' => (int)$user->getBoolOption( 'watchlisthideown' ),
111 /* bool */ 'extended' => (int)$user->getBoolOption( 'extendwatchlist' ),
112 /* ? */ 'namespace' => '', //means all
113 /* ? */ 'invert' => false,
114 /* bool */ 'associated' => false,
116 $this->customFilters = array();
117 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ) );
118 foreach ( $this->customFilters as $key => $params ) {
119 $defaults[$key] = $params['default'];
122 # Extract variables from the request, falling back to user preferences or
123 # other default values if these don't exist
124 $values = array();
125 $values['days'] = $request->getVal( 'days', $defaults['days'] );
126 $values['hideMinor'] = (int)$request->getBool( 'hideMinor', $defaults['hideMinor'] );
127 $values['hideBots'] = (int)$request->getBool( 'hideBots', $defaults['hideBots'] );
128 $values['hideAnons'] = (int)$request->getBool( 'hideAnons', $defaults['hideAnons'] );
129 $values['hideLiu'] = (int)$request->getBool( 'hideLiu', $defaults['hideLiu'] );
130 $values['hideOwn'] = (int)$request->getBool( 'hideOwn', $defaults['hideOwn'] );
131 $values['hidePatrolled'] = (int)$request->getBool( 'hidePatrolled', $defaults['hidePatrolled'] );
132 $values['extended'] = (int)$request->getBool( 'extended', $defaults['extended'] );
133 foreach ( $this->customFilters as $key => $params ) {
134 $values[$key] = (int)$request->getBool( $key, $defaults[$key] );
137 # Get namespace value, if supplied, and prepare a WHERE fragment
138 $nameSpace = $request->getIntOrNull( 'namespace' );
139 $invert = $request->getBool( 'invert' );
140 $associated = $request->getBool( 'associated' );
141 if ( !is_null( $nameSpace ) ) {
142 $eq_op = $invert ? '!=' : '=';
143 $bool_op = $invert ? 'AND' : 'OR';
144 $nameSpace = intval( $nameSpace ); // paranioa
145 if ( !$associated ) {
146 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
147 } else {
148 $associatedNS = MWNamespace::getAssociated( $nameSpace );
149 $nameSpaceClause =
150 "rc_namespace $eq_op $nameSpace " .
151 $bool_op .
152 " rc_namespace $eq_op $associatedNS";
154 } else {
155 $nameSpace = '';
156 $nameSpaceClause = '';
158 $values['namespace'] = $nameSpace;
159 $values['invert'] = $invert;
160 $values['associated'] = $associated;
162 if ( is_null( $values['days'] ) || !is_numeric( $values['days'] ) ) {
163 $big = 1000; /* The magical big */
164 if ( $nitems > $big ) {
165 # Set default cutoff shorter
166 $values['days'] = $defaults['days'] = (12.0 / 24.0); # 12 hours...
167 } else {
168 $values['days'] = $defaults['days']; # default cutoff for shortlisters
170 } else {
171 $values['days'] = floatval( $values['days'] );
174 // Dump everything here
175 $nondefaults = array();
176 foreach ( $defaults as $name => $defValue ) {
177 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
180 if ( ( $wgEnotifWatchlist || $wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
181 $request->wasPosted() )
183 $user->clearAllNotifications();
184 $output->redirect( $this->getTitle()->getFullURL( $nondefaults ) );
185 return;
188 # Possible where conditions
189 $conds = array();
191 if ( $values['days'] > 0 ) {
192 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $values['days'] * 86400 ) ) );
195 # If the watchlist is relatively short, it's simplest to zip
196 # down its entirety and then sort the results.
198 # If it's relatively long, it may be worth our while to zip
199 # through the time-sorted page list checking for watched items.
201 # Up estimate of watched items by 15% to compensate for talk pages...
203 # Toggles
204 if ( $values['hideOwn'] ) {
205 $conds[] = 'rc_user != ' . $user->getId();
207 if ( $values['hideBots'] ) {
208 $conds[] = 'rc_bot = 0';
210 if ( $values['hideMinor'] ) {
211 $conds[] = 'rc_minor = 0';
213 if ( $values['hideLiu'] ) {
214 $conds[] = 'rc_user = 0';
216 if ( $values['hideAnons'] ) {
217 $conds[] = 'rc_user != 0';
219 if ( $user->useRCPatrol() && $values['hidePatrolled'] ) {
220 $conds[] = 'rc_patrolled != 1';
222 if ( $nameSpaceClause ) {
223 $conds[] = $nameSpaceClause;
226 # Toggle watchlist content (all recent edits or just the latest)
227 if ( $values['extended'] ) {
228 $limitWatchlist = $user->getIntOption( 'wllimit' );
229 $usePage = false;
230 } else {
231 # Top log Ids for a page are not stored
232 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG;
233 $limitWatchlist = 0;
234 $usePage = true;
237 # Show a message about slave lag, if applicable
238 $lag = wfGetLB()->safeGetLag( $dbr );
239 if ( $lag > 0 ) {
240 $output->showLagWarning( $lag );
243 # Create output form
244 $form = Xml::fieldset( $this->msg( 'watchlist-options' )->text(), false, array( 'id' => 'mw-watchlist-options' ) );
246 # Show watchlist header
247 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
249 if ( $user->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist ) {
250 $form .= $this->msg( 'wlheader-enotif' )->parseAsBlock() . "\n";
252 if ( $wgShowUpdatedMarker ) {
253 $form .= Xml::openElement( 'form', array( 'method' => 'post',
254 'action' => $this->getTitle()->getLocalURL(),
255 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
256 $this->msg( 'wlheader-showupdated' )->parse() .
257 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
258 Html::hidden( 'reset', 'all' ) . "\n";
259 foreach ( $nondefaults as $key => $value ) {
260 $form .= Html::hidden( $key, $value ) . "\n";
262 $form .= Xml::closeElement( 'form' ) . "\n";
264 $form .= "<hr />\n";
266 $tables = array( 'recentchanges', 'watchlist' );
267 $fields = RecentChange::selectFields();
268 $join_conds = array(
269 'watchlist' => array(
270 'INNER JOIN',
271 array(
272 'wl_user' => $user->getId(),
273 'wl_namespace=rc_namespace',
274 'wl_title=rc_title'
278 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
279 if ( $wgShowUpdatedMarker ) {
280 $fields[] = 'wl_notificationtimestamp';
282 if ( $limitWatchlist ) {
283 $options['LIMIT'] = $limitWatchlist;
286 $rollbacker = $user->isAllowed( 'rollback' );
287 if ( $usePage || $rollbacker ) {
288 $tables[] = 'page';
289 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
290 if ( $rollbacker ) {
291 $fields[] = 'page_latest';
295 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
296 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $values ) );
298 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
299 $numRows = $res->numRows();
301 /* Start bottom header */
303 $lang = $this->getLanguage();
304 $wlInfo = '';
305 if ( $values['days'] > 0 ) {
306 $timestamp = wfTimestampNow();
307 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
308 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
311 $cutofflinks = $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
313 # Spit out some control panel links
314 $filters = array(
315 'hideMinor' => 'rcshowhideminor',
316 'hideBots' => 'rcshowhidebots',
317 'hideAnons' => 'rcshowhideanons',
318 'hideLiu' => 'rcshowhideliu',
319 'hideOwn' => 'rcshowhidemine',
320 'hidePatrolled' => 'rcshowhidepatr'
322 foreach ( $this->customFilters as $key => $params ) {
323 $filters[$key] = $params['msg'];
325 // Disable some if needed
326 if ( !$user->useNPPatrol() ) {
327 unset( $filters['hidePatrolled'] );
330 $links = array();
331 foreach ( $filters as $name => $msg ) {
332 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
335 $hiddenFields = $nondefaults;
336 unset( $hiddenFields['namespace'] );
337 unset( $hiddenFields['invert'] );
338 unset( $hiddenFields['associated'] );
340 # Namespace filter and put the whole form together.
341 $form .= $wlInfo;
342 $form .= $cutofflinks;
343 $form .= $lang->pipeList( $links ) . "\n";
344 $form .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'id' => 'mw-watchlist-form-namespaceselector' ) ) . "\n";
345 $form .= "<hr />\n<p>";
346 $form .= Html::namespaceSelector(
347 array(
348 'selected' => $nameSpace,
349 'all' => '',
350 'label' => $this->msg( 'namespace' )->text()
351 ), array(
352 'name' => 'namespace',
353 'id' => 'namespace',
354 'class' => 'namespaceselector',
356 ) . '&#160;';
357 $form .= Xml::checkLabel(
358 $this->msg( 'invert' )->text(),
359 'invert',
360 'nsinvert',
361 $invert,
362 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
363 ) . '&#160;';
364 $form .= Xml::checkLabel(
365 $this->msg( 'namespace_association' )->text(),
366 'associated',
367 'associated',
368 $associated,
369 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
370 ) . '&#160;';
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( 'form' ) . "\n";
376 $form .= Xml::closeElement( 'fieldset' ) . "\n";
377 $output->addHTML( $form );
379 # If there's nothing to show, stop here
380 if ( $numRows == 0 ) {
381 $output->addWikiMsg( 'watchnochange' );
382 return;
385 /* End bottom header */
387 /* Do link batch query */
388 $linkBatch = new LinkBatch;
389 foreach ( $res as $row ) {
390 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
391 if ( $row->rc_user != 0 ) {
392 $linkBatch->add( NS_USER, $userNameUnderscored );
394 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
396 $linkBatch->add( $row->rc_namespace, $row->rc_title );
398 $linkBatch->execute();
399 $dbr->dataSeek( $res, 0 );
401 $list = ChangesList::newFromContext( $this->getContext() );
402 $list->setWatchlistDivs();
404 $s = $list->beginRecentChangesList();
405 $counter = 1;
406 foreach ( $res as $obj ) {
407 # Make RC entry
408 $rc = RecentChange::newFromRow( $obj );
409 $rc->counter = $counter++;
411 if ( $wgShowUpdatedMarker ) {
412 $updated = $obj->wl_notificationtimestamp;
413 } else {
414 $updated = false;
417 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
418 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
419 'COUNT(*)',
420 array(
421 'wl_namespace' => $obj->rc_namespace,
422 'wl_title' => $obj->rc_title,
424 __METHOD__ );
425 } else {
426 $rc->numberofWatchingusers = 0;
429 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
430 if ( $changeLine !== false ) {
431 $s .= $changeLine;
434 $s .= $list->endRecentChangesList();
436 $output->addHTML( $s );
439 protected function showHideLink( $options, $message, $name, $value ) {
440 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
441 $options[$name] = 1 - (int) $value;
443 return $this->msg( $message )->rawParams( Linker::linkKnown( $this->getTitle(), $label, array(), $options ) )->escaped();
446 protected function hoursLink( $h, $options = array() ) {
447 $options['days'] = ( $h / 24.0 );
449 return Linker::linkKnown(
450 $this->getTitle(),
451 $this->getLanguage()->formatNum( $h ),
452 array(),
453 $options
457 protected function daysLink( $d, $options = array() ) {
458 $options['days'] = $d;
459 $message = ( $d ? $this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
461 return Linker::linkKnown(
462 $this->getTitle(),
463 $message,
464 array(),
465 $options
470 * Returns html
472 * @param int $days This gets overwritten, so is not used
473 * @param array $options Query parameters for URL
474 * @return string
476 protected function cutoffLinks( $days, $options = array() ) {
477 $hours = array( 1, 2, 6, 12 );
478 $days = array( 1, 3, 7 );
479 $i = 0;
480 foreach ( $hours as $h ) {
481 $hours[$i++] = $this->hoursLink( $h, $options );
483 $i = 0;
484 foreach ( $days as $d ) {
485 $days[$i++] = $this->daysLink( $d, $options );
487 return $this->msg( 'wlshowlast' )->rawParams(
488 $this->getLanguage()->pipeList( $hours ),
489 $this->getLanguage()->pipeList( $days ),
490 $this->daysLink( 0, $options ) )->parse();
494 * Count the number of items on a user's watchlist
496 * @param DatabaseBase $dbr A database connection
497 * @return Integer
499 protected function countItems( $dbr ) {
500 # Fetch the raw count
501 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
502 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
503 $row = $dbr->fetchObject( $res );
504 $count = $row->count;
506 return floor( $count / 2 );
509 protected function getGroupName() {
510 return 'changes';