No longer display the number of rows shown on Special:Watchlist
[lhc/web/wiklou.git] / includes / specials / SpecialWatchlist.php
1 <?php
2 /**
3 * Implements Special:Watchlist
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage Watchlist
22 */
23 class SpecialWatchlist extends ChangesListSpecialPage {
24 /**
25 * Constructor
26 */
27 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
28 parent::__construct( $page, $restriction );
29 }
30
31 /**
32 * Execute
33 * @param $par Parameter passed to the page
34 */
35 function execute( $par ) {
36 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
37
38 $user = $this->getUser();
39 $output = $this->getOutput();
40
41 # Anons don't get a watchlist
42 $this->requireLogin( 'watchlistanontext' );
43
44 // Check permissions
45 $this->checkPermissions();
46
47 $request = $this->getRequest();
48 $opts = $this->getOptions();
49
50 $mode = SpecialEditWatchlist::getMode( $request, $par );
51 if ( $mode !== false ) {
52 if ( $mode === SpecialEditWatchlist::EDIT_RAW ) {
53 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'raw' );
54 } else {
55 $title = SpecialPage::getTitleFor( 'EditWatchlist' );
56 }
57
58 $output->redirect( $title->getLocalURL() );
59 return;
60 }
61
62 if ( ( $wgEnotifWatchlist || $wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
63 $request->wasPosted() )
64 {
65 $user->clearAllNotifications();
66 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
67 return;
68 }
69
70 $this->setHeaders();
71 $this->outputHeader();
72 $this->addModules();
73
74 // Fetch results, prepare a batch link existence check query
75 $conds = $this->buildMainQueryConds( $opts );
76 $rows = $this->doMainQuery( $conds, $opts );
77 if ( $rows === false ) {
78 $this->doHeader( $opts );
79
80 return;
81 }
82
83 $feedFormat = $this->getRequest()->getVal( 'feed' );
84 if ( !$feedFormat ) {
85 $batch = new LinkBatch;
86 foreach ( $rows as $row ) {
87 $batch->add( NS_USER, $row->rc_user_text );
88 $batch->add( NS_USER_TALK, $row->rc_user_text );
89 $batch->add( $row->rc_namespace, $row->rc_title );
90 }
91 $batch->execute();
92 }
93 if ( $feedFormat ) {
94 list( $changesFeed, $formatter ) = $this->getFeedObject( $feedFormat );
95 /** @var ChangesFeed $changesFeed */
96 $changesFeed->execute( $formatter, $rows, $lastmod, $opts );
97 } else {
98 $this->webOutput( $rows, $opts );
99 }
100
101 $rows->free();
102 }
103
104 /**
105 * Get a FormOptions object containing the default options
106 *
107 * @return FormOptions
108 */
109 public function getDefaultOptions() {
110 $opts = parent::getDefaultOptions();
111 $user = $this->getUser();
112
113 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions::FLOAT );
114
115 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
116 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
117 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
118 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
119 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
120 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
121
122 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
123
124 return $opts;
125 }
126
127 /**
128 * Get custom show/hide filters
129 *
130 * @return array Map of filter URL param names to properties (msg/default)
131 */
132 protected function getCustomFilters() {
133 if ( $this->customFilters === null ) {
134 $this->customFilters = array();
135 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ) );
136 }
137
138 return $this->customFilters;
139 }
140
141 /**
142 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
143 *
144 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
145 * to the current ones.
146 *
147 * @param FormOptions $parameters
148 * @return FormOptions
149 */
150 protected function fetchOptionsFromRequest( $opts ) {
151 static $compatibilityMap = array(
152 'hideMinor' => 'hideminor',
153 'hideBots' => 'hidebots',
154 'hideAnons' => 'hideanons',
155 'hideLiu' => 'hideliu',
156 'hidePatrolled' => 'hidepatrolled',
157 'hideOwn' => 'hidemyself',
158 );
159
160 $params = $this->getRequest()->getValues();
161 foreach ( $compatibilityMap as $from => $to ) {
162 if ( isset( $params[$from] ) ) {
163 $params[$to] = $params[$from];
164 unset( $params[$from] );
165 }
166 }
167
168 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
169 // methods defined on WebRequest and removing this dependency would cause some code duplication.
170 $request = new DerivativeRequest( $this->getRequest(), $params );
171 $opts->fetchValuesFromRequest( $request );
172 return $opts;
173 }
174
175 /**
176 * Return an array of conditions depending of options set in $opts
177 *
178 * @param FormOptions $opts
179 * @return array
180 */
181 public function buildMainQueryConds( FormOptions $opts ) {
182 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
183 $conds = array();
184 $user = $this->getUser();
185
186 // Calculate cutoff
187 if ( $opts['days'] > 0 ) {
188 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
189 }
190
191 # Toggles
192 if ( $opts['hidemyself'] ) {
193 $conds[] = 'rc_user != ' . $user->getId();
194 }
195 if ( $opts['hidebots'] ) {
196 $conds[] = 'rc_bot = 0';
197 }
198 if ( $opts['hideminor'] ) {
199 $conds[] = 'rc_minor = 0';
200 }
201 if ( $opts['hideliu'] ) {
202 $conds[] = 'rc_user = 0';
203 }
204 if ( $opts['hideanons'] ) {
205 $conds[] = 'rc_user != 0';
206 }
207 if ( $user->useRCPatrol() && $opts['hidepatrolled'] ) {
208 $conds[] = 'rc_patrolled != 1';
209 }
210
211 # Namespace filtering
212 if ( $opts['namespace'] !== '' ) {
213 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
214 $operator = $opts['invert'] ? '!=' : '=';
215 $boolean = $opts['invert'] ? 'AND' : 'OR';
216
217 # namespace association (bug 2429)
218 if ( !$opts['associated'] ) {
219 $condition = "rc_namespace $operator $selectedNS";
220 } else {
221 # Also add the associated namespace
222 $associatedNS = $dbr->addQuotes(
223 MWNamespace::getAssociated( $opts['namespace'] )
224 );
225 $condition = "(rc_namespace $operator $selectedNS "
226 . $boolean
227 . " rc_namespace $operator $associatedNS)";
228 }
229
230 $conds[] = $condition;
231 }
232
233 return $conds;
234 }
235
236 /**
237 * Process the query
238 *
239 * @param array $conds
240 * @param FormOptions $opts
241 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
242 */
243 public function doMainQuery( $conds, $opts ) {
244 global $wgShowUpdatedMarker;
245
246 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
247 $user = $this->getUser();
248 # Toggle watchlist content (all recent edits or just the latest)
249 if ( $opts['extended'] ) {
250 $limitWatchlist = $user->getIntOption( 'wllimit' );
251 $usePage = false;
252 } else {
253 # Top log Ids for a page are not stored
254 $nonRevisionTypes = array( RC_LOG );
255 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
256 if ( $nonRevisionTypes ) {
257 $conds[] = $dbr->makeList(
258 array(
259 'rc_this_oldid=page_latest',
260 'rc_type' => $nonRevisionTypes,
261 ),
262 LIST_OR
263 );
264 }
265 $limitWatchlist = 0;
266 $usePage = true;
267 }
268
269 $tables = array( 'recentchanges', 'watchlist' );
270 $fields = RecentChange::selectFields();
271 $join_conds = array(
272 'watchlist' => array(
273 'INNER JOIN',
274 array(
275 'wl_user' => $user->getId(),
276 'wl_namespace=rc_namespace',
277 'wl_title=rc_title'
278 ),
279 ),
280 );
281 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
282 if ( $wgShowUpdatedMarker ) {
283 $fields[] = 'wl_notificationtimestamp';
284 }
285 if ( $limitWatchlist ) {
286 $options['LIMIT'] = $limitWatchlist;
287 }
288
289 $rollbacker = $user->isAllowed( 'rollback' );
290 if ( $usePage || $rollbacker ) {
291 $tables[] = 'page';
292 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
293 if ( $rollbacker ) {
294 $fields[] = 'page_latest';
295 }
296 }
297
298 // Log entries with DELETED_ACTION must not show up unless the user has
299 // the necessary rights.
300 if ( !$user->isAllowed( 'deletedhistory' ) ) {
301 $bitmask = LogPage::DELETED_ACTION;
302 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
303 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
304 } else {
305 $bitmask = 0;
306 }
307 if ( $bitmask ) {
308 $conds[] = $dbr->makeList( array(
309 'rc_type != ' . RC_LOG,
310 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
311 ), LIST_OR );
312 }
313
314
315 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
316 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $opts ) );
317
318 return $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
319 }
320
321 /**
322 * Send output to the OutputPage object, only called if not used feeds
323 *
324 * @param array $rows Database rows
325 * @param FormOptions $opts
326 */
327 public function webOutput( $rows, $opts ) {
328 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
329
330 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
331 $user = $this->getUser();
332
333 $dbr->dataSeek( $rows, 0 );
334
335 $list = ChangesList::newFromContext( $this->getContext() );
336 $list->setWatchlistDivs();
337
338 $s = $list->beginRecentChangesList();
339 $counter = 1;
340 foreach ( $rows as $obj ) {
341 # Make RC entry
342 $rc = RecentChange::newFromRow( $obj );
343 $rc->counter = $counter++;
344
345 if ( $wgShowUpdatedMarker ) {
346 $updated = $obj->wl_notificationtimestamp;
347 } else {
348 $updated = false;
349 }
350
351 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
352 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
353 'COUNT(*)',
354 array(
355 'wl_namespace' => $obj->rc_namespace,
356 'wl_title' => $obj->rc_title,
357 ),
358 __METHOD__ );
359 } else {
360 $rc->numberofWatchingusers = 0;
361 }
362
363 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
364 if ( $changeLine !== false ) {
365 $s .= $changeLine;
366 }
367 }
368 $s .= $list->endRecentChangesList();
369
370 // Print things out
371
372 $output = $this->getOutput();
373
374 $output->addSubtitle(
375 $this->msg( 'watchlistfor2', $user->getName() )
376 ->rawParams( SpecialEditWatchlist::buildTools( null ) )
377 );
378
379 // Output options box
380 $this->doHeader( $opts );
381
382 // Add feed links
383 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
384 if ( $wlToken ) {
385 $this->addFeedLinks( array(
386 'action' => 'feedwatchlist',
387 'allrev' => 1,
388 'wlowner' => $user->getName(),
389 'wltoken' => $wlToken,
390 ) );
391 }
392
393 # Show a message about slave lag, if applicable
394 $lag = wfGetLB()->safeGetLag( $dbr );
395 if ( $lag > 0 ) {
396 $output->showLagWarning( $lag );
397 }
398
399 if ( $rows->numRows() == 0 ) {
400 $output->wrapWikiMsg(
401 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
402 );
403 } else {
404 $output->addHTML( $s );
405 }
406 }
407
408 /**
409 * Return the text to be displayed above the changes
410 *
411 * @param FormOptions $opts
412 * @return string XHTML
413 */
414 public function doHeader( $opts ) {
415 global $wgScript;
416
417 $user = $this->getUser();
418
419 $this->setTopText( $opts );
420
421 $lang = $this->getLanguage();
422 $wlInfo = '';
423 if ( $opts['days'] > 0 ) {
424 $timestamp = wfTimestampNow();
425 $wlInfo = $this->msg( 'wlnote2' )->numParams( round( $opts['days'] * 24 ) )->params(
426 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
427 }
428
429 $nondefaults = $opts->getChangedValues();
430 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
431
432 # Spit out some control panel links
433 $filters = array(
434 'hideminor' => 'rcshowhideminor',
435 'hidebots' => 'rcshowhidebots',
436 'hideanons' => 'rcshowhideanons',
437 'hideliu' => 'rcshowhideliu',
438 'hidemyself' => 'rcshowhidemine',
439 'hidepatrolled' => 'rcshowhidepatr'
440 );
441 foreach ( $this->getCustomFilters() as $key => $params ) {
442 $filters[$key] = $params['msg'];
443 }
444 // Disable some if needed
445 if ( !$user->useNPPatrol() ) {
446 unset( $filters['hidepatrolled'] );
447 }
448
449 $links = array();
450 foreach ( $filters as $name => $msg ) {
451 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
452 }
453
454 $hiddenFields = $nondefaults;
455 unset( $hiddenFields['namespace'] );
456 unset( $hiddenFields['invert'] );
457 unset( $hiddenFields['associated'] );
458
459 # Create output
460 $form = '';
461
462 # Namespace filter and put the whole form together.
463 $form .= $wlInfo;
464 $form .= $cutofflinks;
465 $form .= $lang->pipeList( $links ) . "\n";
466 $form .= "<hr />\n<p>";
467 $form .= Html::namespaceSelector(
468 array(
469 'selected' => $opts['namespace'],
470 'all' => '',
471 'label' => $this->msg( 'namespace' )->text()
472 ), array(
473 'name' => 'namespace',
474 'id' => 'namespace',
475 'class' => 'namespaceselector',
476 )
477 ) . '&#160;';
478 $form .= Xml::checkLabel(
479 $this->msg( 'invert' )->text(),
480 'invert',
481 'nsinvert',
482 $opts['invert'],
483 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
484 ) . '&#160;';
485 $form .= Xml::checkLabel(
486 $this->msg( 'namespace_association' )->text(),
487 'associated',
488 'nsassociated',
489 $opts['associated'],
490 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
491 ) . '&#160;';
492 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
493 foreach ( $hiddenFields as $key => $value ) {
494 $form .= Html::hidden( $key, $value ) . "\n";
495 }
496 $form .= Xml::closeElement( 'fieldset' ) . "\n";
497 $form .= Xml::closeElement( 'form' ) . "\n";
498 $this->getOutput()->addHTML( $form );
499
500 $this->setBottomText( $opts );
501 }
502
503 function setTopText( FormOptions $opts ) {
504 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
505
506 $nondefaults = $opts->getChangedValues();
507 $form = "";
508 $user = $this->getUser();
509
510 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
511 $numItems = $this->countItems( $dbr );
512
513 // Show watchlist header
514 $form .= "<p>";
515 if ( $numItems == 0 ) {
516 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
517 } else {
518 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
519 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
520 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
521 }
522 if ( $wgShowUpdatedMarker ) {
523 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
524 }
525 }
526 $form .= "</p>";
527
528 if ( $numItems > 0 && $wgShowUpdatedMarker ) {
529 $form .= Xml::openElement( 'form', array( 'method' => 'post',
530 'action' => $this->getPageTitle()->getLocalURL(),
531 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
532 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
533 Html::hidden( 'reset', 'all' ) . "\n";
534 foreach ( $nondefaults as $key => $value ) {
535 $form .= Html::hidden( $key, $value ) . "\n";
536 }
537 $form .= Xml::closeElement( 'form' ) . "\n";
538 }
539
540 $form .= Xml::openElement( 'form', array(
541 'method' => 'post',
542 'action' => $this->getPageTitle()->getLocalURL(),
543 'id' => 'mw-watchlist-form'
544 ) );
545 $form .= Xml::fieldset(
546 $this->msg( 'watchlist-options' )->text(),
547 false,
548 array( 'id' => 'mw-watchlist-options' )
549 );
550
551 $form .= SpecialRecentChanges::makeLegend( $this->getContext() );
552
553 $this->getOutput()->addHTML( $form );
554 }
555
556 protected function showHideLink( $options, $message, $name, $value ) {
557 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
558 $options[$name] = 1 - (int)$value;
559
560 return $this->msg( $message )->rawParams( Linker::linkKnown( $this->getPageTitle(), $label, array(), $options ) )->escaped();
561 }
562
563 protected function hoursLink( $h, $options = array() ) {
564 $options['days'] = ( $h / 24.0 );
565
566 return Linker::linkKnown(
567 $this->getPageTitle(),
568 $this->getLanguage()->formatNum( $h ),
569 array(),
570 $options
571 );
572 }
573
574 protected function daysLink( $d, $options = array() ) {
575 $options['days'] = $d;
576 $message = ( $d ? $this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
577
578 return Linker::linkKnown(
579 $this->getPageTitle(),
580 $message,
581 array(),
582 $options
583 );
584 }
585
586 /**
587 * Returns html
588 *
589 * @param int $days This gets overwritten, so is not used
590 * @param array $options Query parameters for URL
591 * @return string
592 */
593 protected function cutoffLinks( $days, $options = array() ) {
594 $hours = array( 1, 2, 6, 12 );
595 $days = array( 1, 3, 7 );
596 $i = 0;
597 foreach ( $hours as $h ) {
598 $hours[$i++] = $this->hoursLink( $h, $options );
599 }
600 $i = 0;
601 foreach ( $days as $d ) {
602 $days[$i++] = $this->daysLink( $d, $options );
603 }
604 return $this->msg( 'wlshowlast' )->rawParams(
605 $this->getLanguage()->pipeList( $hours ),
606 $this->getLanguage()->pipeList( $days ),
607 $this->daysLink( 0, $options ) )->parse();
608 }
609
610 /**
611 * Count the number of items on a user's watchlist
612 *
613 * @param DatabaseBase $dbr A database connection
614 * @return Integer
615 */
616 protected function countItems( $dbr ) {
617 # Fetch the raw count
618 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
619 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
620 $row = $dbr->fetchObject( $rows );
621 $count = $row->count;
622
623 return floor( $count / 2 );
624 }
625 }