Remove useless comments from search output
[mediawiki.git] / includes / specials / SpecialRecentchanges.php
blobf1a31a59257d213cbff448ddfbc85bc2db42cfb4
1 <?php
2 /**
3 * Implements Special:Recentchanges
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
24 /**
25 * A special page that lists last changes made to the wiki
27 * @ingroup SpecialPage
29 class SpecialRecentChanges extends ChangesListSpecialPage {
30 // @codingStandardsIgnoreStart Needed "useless" override to change parameters.
31 public function __construct( $name = 'Recentchanges', $restriction = '' ) {
32 parent::__construct( $name, $restriction );
34 // @codingStandardsIgnoreEnd
36 /**
37 * Main execution point
39 * @param string $subpage
41 public function execute( $subpage ) {
42 // Backwards-compatibility: redirect to new feed URLs
43 $feedFormat = $this->getRequest()->getVal( 'feed' );
44 if ( !$this->including() && $feedFormat ) {
45 $query = $this->getFeedQuery();
46 $query['feedformat'] = $feedFormat === 'atom' ? 'atom' : 'rss';
47 $this->getOutput()->redirect( wfAppendQuery( wfScript( 'api' ), $query ) );
49 return;
52 // 10 seconds server-side caching max
53 $this->getOutput()->setSquidMaxage( 10 );
54 // Check if the client has a cached version
55 $lastmod = $this->checkLastModified();
56 if ( $lastmod === false ) {
57 return;
60 parent::execute( $subpage );
63 /**
64 * Get a FormOptions object containing the default options
66 * @return FormOptions
68 public function getDefaultOptions() {
69 $opts = parent::getDefaultOptions();
70 $user = $this->getUser();
72 $opts->add( 'days', $user->getIntOption( 'rcdays' ) );
73 $opts->add( 'limit', $user->getIntOption( 'rclimit' ) );
74 $opts->add( 'from', '' );
76 $opts->add( 'hideminor', $user->getBoolOption( 'hideminor' ) );
77 $opts->add( 'hidebots', true );
78 $opts->add( 'hideanons', false );
79 $opts->add( 'hideliu', false );
80 $opts->add( 'hidepatrolled', $user->getBoolOption( 'hidepatrolled' ) );
81 $opts->add( 'hidemyself', false );
83 $opts->add( 'categories', '' );
84 $opts->add( 'categories_any', false );
85 $opts->add( 'tagfilter', '' );
87 return $opts;
90 /**
91 * Get custom show/hide filters
93 * @return array Map of filter URL param names to properties (msg/default)
95 protected function getCustomFilters() {
96 if ( $this->customFilters === null ) {
97 $this->customFilters = parent::getCustomFilters();
98 wfRunHooks( 'SpecialRecentChangesFilters', array( $this, &$this->customFilters ), '1.23' );
101 return $this->customFilters;
105 * Process $par and put options found in $opts. Used when including the page.
107 * @param string $par
108 * @param FormOptions $opts
110 public function parseParameters( $par, FormOptions $opts ) {
111 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
112 foreach ( $bits as $bit ) {
113 if ( 'hidebots' === $bit ) {
114 $opts['hidebots'] = true;
116 if ( 'bots' === $bit ) {
117 $opts['hidebots'] = false;
119 if ( 'hideminor' === $bit ) {
120 $opts['hideminor'] = true;
122 if ( 'minor' === $bit ) {
123 $opts['hideminor'] = false;
125 if ( 'hideliu' === $bit ) {
126 $opts['hideliu'] = true;
128 if ( 'hidepatrolled' === $bit ) {
129 $opts['hidepatrolled'] = true;
131 if ( 'hideanons' === $bit ) {
132 $opts['hideanons'] = true;
134 if ( 'hidemyself' === $bit ) {
135 $opts['hidemyself'] = true;
138 if ( is_numeric( $bit ) ) {
139 $opts['limit'] = $bit;
142 $m = array();
143 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
144 $opts['limit'] = $m[1];
146 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
147 $opts['days'] = $m[1];
149 if ( preg_match( '/^namespace=(\d+)$/', $bit, $m ) ) {
150 $opts['namespace'] = $m[1];
155 public function validateOptions( FormOptions $opts ) {
156 $opts->validateIntBounds( 'limit', 0, 5000 );
157 parent::validateOptions( $opts );
161 * Return an array of conditions depending of options set in $opts
163 * @param FormOptions $opts
164 * @return array
166 public function buildMainQueryConds( FormOptions $opts ) {
167 $dbr = $this->getDB();
168 $conds = parent::buildMainQueryConds( $opts );
170 // Calculate cutoff
171 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
172 $cutoff_unixtime = $cutoff_unixtime - ( $cutoff_unixtime % 86400 );
173 $cutoff = $dbr->timestamp( $cutoff_unixtime );
175 $fromValid = preg_match( '/^[0-9]{14}$/', $opts['from'] );
176 if ( $fromValid && $opts['from'] > wfTimestamp( TS_MW, $cutoff ) ) {
177 $cutoff = $dbr->timestamp( $opts['from'] );
178 } else {
179 $opts->reset( 'from' );
182 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
184 return $conds;
188 * Process the query
190 * @param array $conds
191 * @param FormOptions $opts
192 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
194 public function doMainQuery( $conds, $opts ) {
195 global $wgAllowCategorizedRecentChanges;
197 $dbr = $this->getDB();
198 $user = $this->getUser();
200 $tables = array( 'recentchanges' );
201 $fields = RecentChange::selectFields();
202 $query_options = array();
203 $join_conds = array();
205 // JOIN on watchlist for users
206 if ( $user->getId() && $user->isAllowed( 'viewmywatchlist' ) ) {
207 $tables[] = 'watchlist';
208 $fields[] = 'wl_user';
209 $fields[] = 'wl_notificationtimestamp';
210 $join_conds['watchlist'] = array( 'LEFT JOIN', array(
211 'wl_user' => $user->getId(),
212 'wl_title=rc_title',
213 'wl_namespace=rc_namespace'
214 ) );
217 if ( $user->isAllowed( 'rollback' ) ) {
218 $tables[] = 'page';
219 $fields[] = 'page_latest';
220 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
223 ChangeTags::modifyDisplayQuery(
224 $tables,
225 $fields,
226 $conds,
227 $join_conds,
228 $query_options,
229 $opts['tagfilter']
232 if ( !wfRunHooks( 'SpecialRecentChangesQuery',
233 array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$fields ),
234 '1.23' )
236 return false;
239 // rc_new is not an ENUM, but adding a redundant rc_new IN (0,1) gives mysql enough
240 // knowledge to use an index merge if it wants (it may use some other index though).
241 $rows = $dbr->select(
242 $tables,
243 $fields,
244 $conds + array( 'rc_new' => array( 0, 1 ) ),
245 __METHOD__,
246 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $opts['limit'] ) + $query_options,
247 $join_conds
250 // Build the final data
251 if ( $wgAllowCategorizedRecentChanges ) {
252 $this->filterByCategories( $rows, $opts );
255 return $rows;
258 public function outputFeedLinks() {
259 $this->addFeedLinks( $this->getFeedQuery() );
263 * Get URL query parameters for action=feedrecentchanges API feed of current recent changes view.
265 * @return array
267 private function getFeedQuery() {
268 global $wgFeedLimit;
269 $query = array_filter( $this->getOptions()->getAllValues(), function ( $value ) {
270 // API handles empty parameters in a different way
271 return $value !== '';
272 } );
273 $query['action'] = 'feedrecentchanges';
274 if ( $query['limit'] > $wgFeedLimit ) {
275 $query['limit'] = $wgFeedLimit;
278 return $query;
282 * Build and output the actual changes list.
284 * @param array $rows Database rows
285 * @param FormOptions $opts
287 public function outputChangesList( $rows, $opts ) {
288 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
290 $limit = $opts['limit'];
292 $showWatcherCount = $wgRCShowWatchingUsers
293 && $this->getUser()->getOption( 'shownumberswatching' );
294 $watcherCache = array();
296 $dbr = $this->getDB();
298 $counter = 1;
299 $list = ChangesList::newFromContext( $this->getContext() );
300 $list->initChangesListRows( $rows );
302 $rclistOutput = $list->beginRecentChangesList();
303 foreach ( $rows as $obj ) {
304 if ( $limit == 0 ) {
305 break;
307 $rc = RecentChange::newFromRow( $obj );
308 $rc->counter = $counter++;
309 # Check if the page has been updated since the last visit
310 if ( $wgShowUpdatedMarker && !empty( $obj->wl_notificationtimestamp ) ) {
311 $rc->notificationtimestamp = ( $obj->rc_timestamp >= $obj->wl_notificationtimestamp );
312 } else {
313 $rc->notificationtimestamp = false; // Default
315 # Check the number of users watching the page
316 $rc->numberofWatchingusers = 0; // Default
317 if ( $showWatcherCount && $obj->rc_namespace >= 0 ) {
318 if ( !isset( $watcherCache[$obj->rc_namespace][$obj->rc_title] ) ) {
319 $watcherCache[$obj->rc_namespace][$obj->rc_title] =
320 $dbr->selectField(
321 'watchlist',
322 'COUNT(*)',
323 array(
324 'wl_namespace' => $obj->rc_namespace,
325 'wl_title' => $obj->rc_title,
327 __METHOD__ . '-watchers'
330 $rc->numberofWatchingusers = $watcherCache[$obj->rc_namespace][$obj->rc_title];
333 $changeLine = $list->recentChangesLine( $rc, !empty( $obj->wl_user ), $counter );
334 if ( $changeLine !== false ) {
335 $rclistOutput .= $changeLine;
336 --$limit;
339 $rclistOutput .= $list->endRecentChangesList();
341 if ( $rows->numRows() === 0 ) {
342 $this->getOutput()->addHtml(
343 '<div class="mw-changeslist-empty">' .
344 $this->msg( 'recentchanges-noresult' )->parse() .
345 '</div>'
347 } else {
348 $this->getOutput()->addHTML( $rclistOutput );
353 * Return the text to be displayed above the changes
355 * @param FormOptions $opts
356 * @return string XHTML
358 public function doHeader( $opts ) {
359 global $wgScript;
361 $this->setTopText( $opts );
363 $defaults = $opts->getAllValues();
364 $nondefaults = $opts->getChangedValues();
366 $panel = array();
367 $panel[] = self::makeLegend( $this->getContext() );
368 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
369 $panel[] = '<hr />';
371 $extraOpts = $this->getExtraOptions( $opts );
372 $extraOptsCount = count( $extraOpts );
373 $count = 0;
374 $submit = ' ' . Xml::submitbutton( $this->msg( 'allpagessubmit' )->text() );
376 $out = Xml::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
377 foreach ( $extraOpts as $name => $optionRow ) {
378 # Add submit button to the last row only
379 ++$count;
380 $addSubmit = ( $count === $extraOptsCount ) ? $submit : '';
382 $out .= Xml::openElement( 'tr' );
383 if ( is_array( $optionRow ) ) {
384 $out .= Xml::tags(
385 'td',
386 array( 'class' => 'mw-label mw-' . $name . '-label' ),
387 $optionRow[0]
389 $out .= Xml::tags(
390 'td',
391 array( 'class' => 'mw-input' ),
392 $optionRow[1] . $addSubmit
394 } else {
395 $out .= Xml::tags(
396 'td',
397 array( 'class' => 'mw-input', 'colspan' => 2 ),
398 $optionRow . $addSubmit
401 $out .= Xml::closeElement( 'tr' );
403 $out .= Xml::closeElement( 'table' );
405 $unconsumed = $opts->getUnconsumedValues();
406 foreach ( $unconsumed as $key => $value ) {
407 $out .= Html::hidden( $key, $value );
410 $t = $this->getPageTitle();
411 $out .= Html::hidden( 'title', $t->getPrefixedText() );
412 $form = Xml::tags( 'form', array( 'action' => $wgScript ), $out );
413 $panel[] = $form;
414 $panelString = implode( "\n", $panel );
416 $this->getOutput()->addHTML(
417 Xml::fieldset(
418 $this->msg( 'recentchanges-legend' )->text(),
419 $panelString,
420 array( 'class' => 'rcoptions' )
424 $this->setBottomText( $opts );
428 * Send the text to be displayed above the options
430 * @param FormOptions $opts Unused
432 function setTopText( FormOptions $opts ) {
433 global $wgContLang;
435 $message = $this->msg( 'recentchangestext' )->inContentLanguage();
436 if ( !$message->isDisabled() ) {
437 $this->getOutput()->addWikiText(
438 Html::rawElement( 'p',
439 array( 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ),
440 "\n" . $message->plain() . "\n"
442 /* $lineStart */ false,
443 /* $interface */ false
449 * Get options to be displayed in a form
451 * @param FormOptions $opts
452 * @return array
454 function getExtraOptions( $opts ) {
455 $opts->consumeValues( array(
456 'namespace', 'invert', 'associated', 'tagfilter', 'categories', 'categories_any'
457 ) );
459 $extraOpts = array();
460 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
462 global $wgAllowCategorizedRecentChanges;
463 if ( $wgAllowCategorizedRecentChanges ) {
464 $extraOpts['category'] = $this->categoryFilterForm( $opts );
467 $tagFilter = ChangeTags::buildTagFilterSelector( $opts['tagfilter'] );
468 if ( count( $tagFilter ) ) {
469 $extraOpts['tagfilter'] = $tagFilter;
472 // Don't fire the hook for subclasses. (Or should we?)
473 if ( $this->getName() === 'Recentchanges' ) {
474 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
477 return $extraOpts;
481 * Add page-specific modules.
483 protected function addModules() {
484 parent::addModules();
485 $out = $this->getOutput();
486 $out->addModules( 'mediawiki.special.recentchanges' );
490 * Get last modified date, for client caching
491 * Don't use this if we are using the patrol feature, patrol changes don't
492 * update the timestamp
494 * @return string|bool
496 public function checkLastModified() {
497 $dbr = $this->getDB();
498 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__ );
500 return $lastmod;
504 * Creates the choose namespace selection
506 * @param FormOptions $opts
507 * @return string
509 protected function namespaceFilterForm( FormOptions $opts ) {
510 $nsSelect = Html::namespaceSelector(
511 array( 'selected' => $opts['namespace'], 'all' => '' ),
512 array( 'name' => 'namespace', 'id' => 'namespace' )
514 $nsLabel = Xml::label( $this->msg( 'namespace' )->text(), 'namespace' );
515 $invert = Xml::checkLabel(
516 $this->msg( 'invert' )->text(), 'invert', 'nsinvert',
517 $opts['invert'],
518 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
520 $associated = Xml::checkLabel(
521 $this->msg( 'namespace_association' )->text(), 'associated', 'nsassociated',
522 $opts['associated'],
523 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
526 return array( $nsLabel, "$nsSelect $invert $associated" );
530 * Create a input to filter changes by categories
532 * @param FormOptions $opts
533 * @return array
535 protected function categoryFilterForm( FormOptions $opts ) {
536 list( $label, $input ) = Xml::inputLabelSep( $this->msg( 'rc_categories' )->text(),
537 'categories', 'mw-categories', false, $opts['categories'] );
539 $input .= ' ' . Xml::checkLabel( $this->msg( 'rc_categories_any' )->text(),
540 'categories_any', 'mw-categories_any', $opts['categories_any'] );
542 return array( $label, $input );
546 * Filter $rows by categories set in $opts
548 * @param ResultWrapper $rows Database rows
549 * @param FormOptions $opts
551 function filterByCategories( &$rows, FormOptions $opts ) {
552 $categories = array_map( 'trim', explode( '|', $opts['categories'] ) );
554 if ( !count( $categories ) ) {
555 return;
558 # Filter categories
559 $cats = array();
560 foreach ( $categories as $cat ) {
561 $cat = trim( $cat );
562 if ( $cat == '' ) {
563 continue;
565 $cats[] = $cat;
568 # Filter articles
569 $articles = array();
570 $a2r = array();
571 $rowsarr = array();
572 foreach ( $rows as $k => $r ) {
573 $nt = Title::makeTitle( $r->rc_namespace, $r->rc_title );
574 $id = $nt->getArticleID();
575 if ( $id == 0 ) {
576 continue; # Page might have been deleted...
578 if ( !in_array( $id, $articles ) ) {
579 $articles[] = $id;
581 if ( !isset( $a2r[$id] ) ) {
582 $a2r[$id] = array();
584 $a2r[$id][] = $k;
585 $rowsarr[$k] = $r;
588 # Shortcut?
589 if ( !count( $articles ) || !count( $cats ) ) {
590 return;
593 # Look up
594 $c = new Categoryfinder;
595 $c->seed( $articles, $cats, $opts['categories_any'] ? 'OR' : 'AND' );
596 $match = $c->run();
598 # Filter
599 $newrows = array();
600 foreach ( $match as $id ) {
601 foreach ( $a2r[$id] as $rev ) {
602 $k = $rev;
603 $newrows[$k] = $rowsarr[$k];
606 $rows = $newrows;
610 * Makes change an option link which carries all the other options
612 * @param string $title Title
613 * @param array $override Options to override
614 * @param array $options Current options
615 * @param bool $active Whether to show the link in bold
616 * @return string
618 function makeOptionsLink( $title, $override, $options, $active = false ) {
619 $params = $override + $options;
621 // Bug 36524: false values have be converted to "0" otherwise
622 // wfArrayToCgi() will omit it them.
623 foreach ( $params as &$value ) {
624 if ( $value === false ) {
625 $value = '0';
628 unset( $value );
630 $text = htmlspecialchars( $title );
631 if ( $active ) {
632 $text = '<strong>' . $text . '</strong>';
635 return Linker::linkKnown( $this->getPageTitle(), $text, array(), $params );
639 * Creates the options panel.
641 * @param array $defaults
642 * @param array $nondefaults
643 * @return string
645 function optionsPanel( $defaults, $nondefaults ) {
646 global $wgRCLinkLimits, $wgRCLinkDays;
648 $options = $nondefaults + $defaults;
650 $note = '';
651 $msg = $this->msg( 'rclegend' );
652 if ( !$msg->isDisabled() ) {
653 $note .= '<div class="mw-rclegend">' . $msg->parse() . "</div>\n";
656 $lang = $this->getLanguage();
657 $user = $this->getUser();
658 if ( $options['from'] ) {
659 $note .= $this->msg( 'rcnotefrom' )->numParams( $options['limit'] )->params(
660 $lang->userTimeAndDate( $options['from'], $user ),
661 $lang->userDate( $options['from'], $user ),
662 $lang->userTime( $options['from'], $user ) )->parse() . '<br />';
665 # Sort data for display and make sure it's unique after we've added user data.
666 $linkLimits = $wgRCLinkLimits;
667 $linkLimits[] = $options['limit'];
668 sort( $linkLimits );
669 $linkLimits = array_unique( $linkLimits );
671 $linkDays = $wgRCLinkDays;
672 $linkDays[] = $options['days'];
673 sort( $linkDays );
674 $linkDays = array_unique( $linkDays );
676 // limit links
677 $cl = array();
678 foreach ( $linkLimits as $value ) {
679 $cl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
680 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] );
682 $cl = $lang->pipeList( $cl );
684 // day links, reset 'from' to none
685 $dl = array();
686 foreach ( $linkDays as $value ) {
687 $dl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
688 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] );
690 $dl = $lang->pipeList( $dl );
692 // show/hide links
693 $filters = array(
694 'hideminor' => 'rcshowhideminor',
695 'hidebots' => 'rcshowhidebots',
696 'hideanons' => 'rcshowhideanons',
697 'hideliu' => 'rcshowhideliu',
698 'hidepatrolled' => 'rcshowhidepatr',
699 'hidemyself' => 'rcshowhidemine'
702 $showhide = array( 'show', 'hide' );
704 foreach ( $this->getCustomFilters() as $key => $params ) {
705 $filters[$key] = $params['msg'];
707 // Disable some if needed
708 if ( !$user->useRCPatrol() ) {
709 unset( $filters['hidepatrolled'] );
712 $links = array();
713 foreach ( $filters as $key => $msg ) {
714 // The following messages are used here:
715 // rcshowhideminor-show, rcshowhideminor-hide, rcshowhidebots-show, rcshowhidebots-hide,
716 // rcshowhideanons-show, rcshowhideanons-hide, rcshowhideliu-show, rcshowhideliu-hide,
717 // rcshowhidepatr-show, rcshowhidepatr-hide, rcshowhidemine-show, rcshowhidemine-hide.
718 $linkMessage = $this->msg( $msg . '-' . $showhide[1 - $options[$key]] );
719 // Extensions can define additional filters, but don't need to define the corresponding
720 // messages. If they don't exist, just fall back to 'show' and 'hide'.
721 if ( !$linkMessage->exists() ) {
722 $linkMessage = $this->msg( $showhide[1 - $options[$key]] );
725 $link = $this->makeOptionsLink( $linkMessage->text(),
726 array( $key => 1 - $options[$key] ), $nondefaults );
727 $links[] = $this->msg( $msg )->rawParams( $link )->escaped();
730 // show from this onward link
731 $timestamp = wfTimestampNow();
732 $now = $lang->userTimeAndDate( $timestamp, $user );
733 $timenow = $lang->userTime( $timestamp, $user );
734 $datenow = $lang->userDate( $timestamp, $user );
735 $rclinks = $this->msg( 'rclinks' )->rawParams( $cl, $dl, $lang->pipeList( $links ) )
736 ->parse();
737 $rclistfrom = $this->makeOptionsLink(
738 $this->msg( 'rclistfrom' )->rawParams( $now, $timenow, $datenow )->parse(),
739 array( 'from' => $timestamp ),
740 $nondefaults
743 return "{$note}$rclinks<br />$rclistfrom";
746 public function isIncludable() {
747 return true;