16904b9f470d622359f4bdd6d268c9c9ae06a31e
7 class SpecialRecentChanges
extends SpecialPage
{
8 public function __construct() {
9 SpecialPage
::SpecialPage( 'Recentchanges' );
10 $this->includable( true );
13 public function getDefaultOptions() {
14 $opts = new FormOptions();
16 $opts->add( 'days', (int)User
::getDefaultOption( 'rcdays' ) );
17 $opts->add( 'limit', (int)User
::getDefaultOption( 'rclimit' ) );
18 $opts->add( 'from', '' );
20 $opts->add( 'hideminor', false );
21 $opts->add( 'hidebots', true );
22 $opts->add( 'hideanons', false );
23 $opts->add( 'hideliu', false );
24 $opts->add( 'hidepatrolled', false );
25 $opts->add( 'hidemyself', false );
27 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
28 $opts->add( 'invert', false );
30 $opts->add( 'categories', '' );
31 $opts->add( 'categories_any', false );
36 public function setup( $parameters ) {
37 global $wgUser, $wgRequest;
39 $opts = $this->getDefaultOptions();
40 $opts['days'] = (int)$wgUser->getOption( 'rcdays', $opts['days'] );
41 $opts['limit'] = (int)$wgUser->getOption( 'rclimit', $opts['limit'] );
42 $opts['hideminor'] = $wgUser->getOption( 'hideminor', $opts['hideminor'] );
43 $opts->fetchValuesFromRequest( $wgRequest );
45 // Give precedence to subpage syntax
46 if ( $parameters !== null ) {
47 $this->parseParameters( $parameters, $opts );
50 $opts->validateIntBounds( 'limit', 0, 5000 );
54 public function feedSetup() {
55 global $wgFeedLimit, $wgRequest;
56 $opts = $this->getDefaultOptions();
57 $opts->fetchValuesFromRequest( $wgRequest, array( 'days', 'limit', 'hideminor' ) );
58 $opts->validateIntBounds( 'limit', 0, $wgFeedLimit );
62 public function execute( $parameters ) {
63 global $wgRequest, $wgOut;
64 $feedFormat = $wgRequest->getVal( 'feed' );
66 # 10 seconds server-side caching max
67 $wgOut->setSquidMaxage( 10 );
69 $lastmod = $this->checkLastModified( $feedFormat );
70 if( $lastmod === false ){
74 $opts = $feedFormat ?
$this->feedSetup() : $this->setup( $parameters );
77 // Fetch results, prepare a batch link existence check query
79 $batch = new LinkBatch
;
80 $conds = $this->buildMainQueryConds( $opts );
81 $res = $this->doMainQuery( $conds, $opts );
82 $dbr = wfGetDB( DB_SLAVE
);
83 while( $row = $dbr->fetchObject( $res ) ){
86 // User page and talk links
87 $batch->add( NS_USER
, $row->rc_user_text
);
88 $batch->add( NS_USER_TALK
, $row->rc_user_text
);
92 $dbr->freeResult( $res );
95 $feed = new ChangesFeed( $feedFormat, 'rcfeed' );
96 $feedObj = $feed->getFeedObject(
97 wfMsgForContent( 'recentchanges' ),
98 wfMsgForContent( 'recentchanges-feed-description' )
100 $feed->execute( $feedObj, $rows, $opts['limit'], $opts['hideminor'], $lastmod );
103 $this->webOutput( $rows, $opts );
108 public function parseParameters( $par, FormOptions
$opts ) {
109 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
110 foreach ( $bits as $bit ) {
111 if ( 'hidebots' === $bit ) $opts['hidebots'] = true;
112 if ( 'bots' === $bit ) $opts['hidebots'] = false;
113 if ( 'hideminor' === $bit ) $opts['hideminor'] = true;
114 if ( 'minor' === $bit ) $opts['hideminor'] = false;
115 if ( 'hideliu' === $bit ) $opts['hideliu'] = true;
116 if ( 'hidepatrolled' === $bit ) $opts['hidepatrolled'] = true;
117 if ( 'hideanons' === $bit ) $opts['hideanons'] = true;
118 if ( 'hidemyself' === $bit ) $opts['hidemyself'] = true;
120 if ( is_numeric( $bit ) ) $opts['limit'] = $bit;
123 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) $opts['limit'] = $m[1];
124 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) $opts['days'] = $m[1];
128 # Get last modified date, for client caching
129 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
130 public function checkLastModified( $feedFormat ) {
131 global $wgUseRCPatrol, $wgOut;
132 $dbr = wfGetDB( DB_SLAVE
);
133 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __FUNCTION__
);
134 if ( $feedFormat ||
!$wgUseRCPatrol ) {
135 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
136 # Client cache fresh and headers sent, nothing more to do.
143 public function buildMainQueryConds( FormOptions
$opts ) {
146 $dbr = wfGetDB( DB_SLAVE
);
149 # It makes no sense to hide both anons and logged-in users
150 # Where this occurs, force anons to be shown
152 if( $opts['hideanons'] && $opts['hideliu'] ){
153 # Check if the user wants to show bots only
154 if( $opts['hidebots'] ){
155 $opts['hideanons'] = false;
158 $opts['hidebots'] = false;
163 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
164 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime %
86400);
165 $cutoff = $dbr->timestamp( $cutoff_unixtime );
167 $fromValid = preg_match('/^[0-9]{14}$/', $opts['from']);
168 if( $fromValid && $opts['from'] > wfTimestamp(TS_MW
,$cutoff) ) {
169 $cutoff = $dbr->timestamp($opts['from']);
171 $opts->reset( 'from' );
174 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
177 $hidePatrol = $wgUser->useRCPatrol() && $opts['hidepatrolled'];
178 $hideLoggedInUsers = $opts['hideliu'] && !$forcebot;
179 $hideAnonymousUsers = $opts['hideanons'] && !$forcebot;
181 if ( $opts['hideminor'] ) $conds['rc_minor'] = 0;
182 if ( $opts['hidebots'] ) $conds['rc_bot'] = 0;
183 if ( $hidePatrol ) $conds['rc_patrolled'] = 0;
184 if ( $forcebot ) $conds['rc_bot'] = 1;
185 if ( $hideLoggedInUsers ) $conds[] = 'rc_user = 0';
186 if ( $hideAnonymousUsers ) $conds[] = 'rc_user != 0';
188 if( $opts['hidemyself'] ) {
189 if( $wgUser->getId() ) {
190 $conds[] = 'rc_user != ' . $dbr->addQuotes( $wgUser->getId() );
192 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $wgUser->getName() );
196 # Namespace filtering
197 if ( $opts['namespace'] !== '' ) {
198 if ( !$opts['invert'] ) {
199 $conds[] = 'rc_namespace = ' . $dbr->addQuotes( $opts['namespace'] );
201 $conds[] = 'rc_namespace != ' . $dbr->addQuotes( $opts['namespace'] );
208 public function doMainQuery( $conds, $opts ) {
211 $tables = array( 'recentchanges' );
212 $join_conds = array();
214 $uid = $wgUser->getId();
215 $dbr = wfGetDB( DB_SLAVE
);
216 $limit = $opts['limit'];
217 $namespace = $opts['namespace'];
218 $invert = $opts['invert'];
220 // JOIN on watchlist for users
221 if( $wgUser->getId() ) {
222 $tables[] = 'watchlist';
223 $join_conds = array( 'watchlist' => array('LEFT JOIN',"wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace") );
226 wfRunHooks('SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts ) );
228 // Is there either one namespace selected or excluded?
229 // Also, if this is "all" or main namespace, just use timestamp index.
230 if( is_null($namespace) ||
$invert ||
$namespace == NS_MAIN
) {
231 $res = $dbr->select( $tables, '*', $conds, __METHOD__
,
232 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
233 'USE INDEX' => array('recentchanges' => 'rc_timestamp') ),
235 // We have a new_namespace_time index! UNION over new=(0,1) and sort result set!
238 $sqlNew = $dbr->selectSQLText( $tables, '*',
239 array( 'rc_new' => 1 ) +
$conds,
241 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
242 'USE INDEX' => array('recentchanges' => 'new_name_timestamp') ),
245 $sqlOld = $dbr->selectSQLText( $tables, '*',
246 array( 'rc_new' => 0 ) +
$conds,
248 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
249 'USE INDEX' => array('recentchanges' => 'new_name_timestamp') ),
251 # Join the two fast queries, and sort the result set
252 $sql = "($sqlNew) UNION ($sqlOld) ORDER BY rc_timestamp DESC LIMIT $limit";
253 $res = $dbr->query( $sql, __METHOD__
);
259 public function webOutput( $rows, $opts ) {
260 global $wgOut, $wgUser, $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
261 global $wgAllowCategorizedRecentChanges;
263 $limit = $opts['limit'];
265 if ( !$this->including() ) {
266 // Output options box
267 $this->doHeader( $opts );
270 // And now for the content
271 $wgOut->setSyndicated( true );
273 $list = ChangesList
::newFromUser( $wgUser );
275 if ( $wgAllowCategorizedRecentChanges ) {
276 rcFilterByCategories( $rows, $opts );
279 $s = $list->beginRecentChangesList();
282 $showWatcherCount = $wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' );
283 $watcherCache = array();
285 $dbr = wfGetDB( DB_SLAVE
);
287 foreach( $rows as $obj ){
292 if ( ! ( $opts['hideminor'] && $obj->rc_minor
) &&
293 ! ( $opts['hidepatrolled'] && $obj->rc_patrolled
) ) {
294 $rc = RecentChange
::newFromRow( $obj );
295 $rc->counter
= $counter++
;
297 if ($wgShowUpdatedMarker
298 && !empty( $obj->wl_notificationtimestamp
)
299 && ($obj->rc_timestamp
>= $obj->wl_notificationtimestamp
)) {
300 $rc->notificationtimestamp
= true;
302 $rc->notificationtimestamp
= false;
305 $rc->numberofWatchingusers
= 0; // Default
306 if ($showWatcherCount && $obj->rc_namespace
>= 0) {
307 if (!isset($watcherCache[$obj->rc_namespace
][$obj->rc_title
])) {
308 $watcherCache[$obj->rc_namespace
][$obj->rc_title
] =
309 $dbr->selectField( 'watchlist',
312 'wl_namespace' => $obj->rc_namespace
,
313 'wl_title' => $obj->rc_title
,
315 __METHOD__
. '-watchers' );
317 $rc->numberofWatchingusers
= $watcherCache[$obj->rc_namespace
][$obj->rc_title
];
319 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user
) );
323 $s .= $list->endRecentChangesList();
324 $wgOut->addHTML( $s );
327 public function doHeader( $opts ) {
328 global $wgScript, $wgOut;
329 $wgOut->addWikiText( wfMsgForContentNoTrans( 'recentchangestext' ) );
331 $defaults = $opts->getAllValues();
332 $nondefaults = $opts->getChangedValues();
333 $opts->consumeValues( array( 'namespace', 'invert' ) );
336 $panel[] = rcOptionsPanel( $defaults, $nondefaults );
339 $extraOpts = array();
340 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
342 global $wgAllowCategorizedRecentChanges;
343 if ( $wgAllowCategorizedRecentChanges ) {
344 $extraOpts['category'] = $this->categoryFilterForm( $opts );
347 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
348 $extraOpts['submit'] = Xml
::submitbutton( wfMsg('allpagessubmit') );
350 $out = Xml
::openElement( 'table' );
351 foreach ( $extraOpts as $optionRow ) {
352 $out .= Xml
::openElement( 'tr' );
353 if ( is_array($optionRow) ) {
354 $out .= Xml
::tags( 'td', null, $optionRow[0] );
355 $out .= Xml
::tags( 'td', null, $optionRow[1] );
357 $out .= Xml
::tags( 'td', array( 'colspan' => 2 ), $optionRow );
359 $out .= Xml
::closeElement( 'tr' );
361 $out .= Xml
::closeElement( 'table' );
363 $unconsumed = $opts->getUnconsumedValues();
364 foreach ( $unconsumed as $key => $value ) {
365 $out .= Xml
::hidden( $key, $value );
368 $t = $this->getTitle();
369 $out .= Xml
::hidden( 'title', $t->getPrefixedText() );
370 $form = Xml
::tags( 'form', array( 'action' => $wgScript ), $out );
372 $panelString = implode( "\n", $panel );
375 Xml
::fieldset( wfMsg( 'recentchanges' ), $panelString, array( 'class' => 'rcoptions' ) )
380 * Creates the choose namespace selection
384 protected function namespaceFilterForm( FormOptions
$opts ) {
385 $nsSelect = HTMLnamespaceselector( $opts['namespace'], '' );
386 $nsLabel = Xml
::label( wfMsg('namespace'), 'namespace' );
387 $invert = Xml
::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $opts['invert'] );
388 return array( $nsLabel, "$nsSelect $invert" );
391 protected function categoryFilterForm( FormOptions
$opts ) {
392 list( $label, $input ) = Xml
::inputLabelSep( wfMsg('rc_categories'),
393 'categories', 'mw-categories', false, $opts['categories'] );
395 $input .= ' ' . Xml
::checkLabel( wfMsg('rc_categories_any'),
396 'categories_any', 'mw-categories_any', $opts['categories_any'] );
398 return array( $label, $input );
403 function rcFilterByCategories ( &$rows, FormOptions
$opts ) {
404 $categories = array_map( 'trim', explode( "|" , $categories ) );
406 if( empty($categories) ) {
412 foreach ( $opts['categories'] AS $cat ) {
414 if ( $cat == "" ) continue;
421 foreach ( $rows AS $k => $r ) {
422 $nt = Title
::makeTitle( $r->rc_namespace
, $r->rc_title
);
423 $id = $nt->getArticleID();
424 if ( $id == 0 ) continue; # Page might have been deleted...
425 if ( !in_array($id, $articles) ) {
428 if ( !isset($a2r[$id]) ) {
435 if ( !count($articles) ||
!count($cats) )
439 $c = new Categoryfinder
;
440 $c->seed( $articles, $cats, $opts['categories_any'] ?
"OR" : "AND" ) ;
445 foreach ( $match AS $id ) {
446 foreach ( $a2r[$id] AS $rev ) {
448 $newrows[$k] = $rows[$k];
457 function rcCountLink( $lim, $d, $page='Recentchanges', $more='', $active = false ) {
458 global $wgUser, $wgLang, $wgContLang;
459 $sk = $wgUser->getSkin();
460 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
461 ($lim ?
$wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
462 ($d ?
"days={$d}&" : '') . 'limit='.$lim, '', '',
463 $active ?
'style="font-weight: bold;"' : '' );
470 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='', $active = false ) {
471 global $wgUser, $wgLang, $wgContLang;
472 $sk = $wgUser->getSkin();
473 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
474 ($d ?
$wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
475 ($lim ?
'&limit='.$lim : ''), '', '',
476 $active ?
'style="font-weight: bold;"' : '' );
481 * Used by Recentchangeslinked
483 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
484 $botLink = '', $liuLink = '', $patrLink = '', $myselfLink = '' ) {
485 global $wgRCLinkLimits, $wgRCLinkDays;
486 if ($more != '') $more .= '&';
488 # Sort data for display and make sure it's unique after we've added user data.
489 # FIXME: why does this piss around with globals like this? Why is $limit added on globally?
490 $wgRCLinkLimits[] = $limit;
491 $wgRCLinkDays[] = $days;
492 sort($wgRCLinkLimits);
494 $wgRCLinkLimits = array_unique($wgRCLinkLimits);
495 $wgRCLinkDays = array_unique($wgRCLinkDays);
498 foreach( $wgRCLinkLimits as $countLink ) {
499 $cl[] = rcCountLink( $countLink, $days, $page, $more, $countLink == $limit );
501 if( $doall ) $cl[] = rcCountLink( 0, $days, $page, $more );
502 $cl = implode( ' | ', $cl);
505 foreach( $wgRCLinkDays as $daysLink ) {
506 $dl[] = rcDaysLink( $limit, $daysLink, $page, $more, $daysLink == $days );
508 if( $doall ) $dl[] = rcDaysLink( $limit, 0, $page, $more );
509 $dl = implode( ' | ', $dl);
511 $linkParts = array( 'minorLink' => 'minor', 'botLink' => 'bots', 'liuLink' => 'liu', 'patrLink' => 'patr', 'myselfLink' => 'mine' );
512 foreach( $linkParts as $linkVar => $linkMsg ) {
513 if( $
$linkVar != '' )
514 $links[] = wfMsgHtml( 'rcshowhide' . $linkMsg, $
$linkVar );
517 $shm = implode( ' | ', $links );
518 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
524 * Makes change an option link which carries all the other options
525 * @param $title see Title
529 function makeOptionsLink( $title, $override, $options, $active = false ) {
530 global $wgUser, $wgContLang;
531 $sk = $wgUser->getSkin();
532 return $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
533 htmlspecialchars( $title ), wfArrayToCGI( $override, $options ), '', '',
534 $active ?
'style="font-weight: bold;"' : '' );
538 * Creates the options panel.
540 * @param $nondefaults
542 function rcOptionsPanel( $defaults, $nondefaults ) {
543 global $wgLang, $wgUser, $wgRCLinkLimits, $wgRCLinkDays;
545 $options = $nondefaults +
$defaults;
547 if( $options['from'] )
548 $note = wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
549 $wgLang->formatNum( $options['limit'] ),
550 $wgLang->timeanddate( $options['from'], true ) );
552 $note = wfMsgExt( 'rcnote', array( 'parseinline' ),
553 $wgLang->formatNum( $options['limit'] ),
554 $wgLang->formatNum( $options['days'] ),
555 $wgLang->timeAndDate( wfTimestampNow(), true ) );
557 # Sort data for display and make sure it's unique after we've added user data.
558 $wgRCLinkLimits[] = $options['limit'];
559 $wgRCLinkDays[] = $options['days'];
560 sort($wgRCLinkLimits);
562 $wgRCLinkLimits = array_unique($wgRCLinkLimits);
563 $wgRCLinkDays = array_unique($wgRCLinkDays);
566 foreach( $wgRCLinkLimits as $value ) {
567 $cl[] = makeOptionsLink( $wgLang->formatNum( $value ),
568 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] ) ;
570 $cl = implode( ' | ', $cl);
572 // day links, reset 'from' to none
573 foreach( $wgRCLinkDays as $value ) {
574 $dl[] = makeOptionsLink( $wgLang->formatNum( $value ),
575 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] ) ;
577 $dl = implode( ' | ', $dl);
581 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
582 $minorLink = makeOptionsLink( $showhide[1-$options['hideminor']],
583 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
584 $botLink = makeOptionsLink( $showhide[1-$options['hidebots']],
585 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
586 $anonsLink = makeOptionsLink( $showhide[ 1 - $options['hideanons'] ],
587 array( 'hideanons' => 1 - $options['hideanons'] ), $nondefaults );
588 $liuLink = makeOptionsLink( $showhide[1-$options['hideliu']],
589 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
590 $patrLink = makeOptionsLink( $showhide[1-$options['hidepatrolled']],
591 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
592 $myselfLink = makeOptionsLink( $showhide[1-$options['hidemyself']],
593 array( 'hidemyself' => 1-$options['hidemyself'] ), $nondefaults);
595 $links[] = wfMsgHtml( 'rcshowhideminor', $minorLink );
596 $links[] = wfMsgHtml( 'rcshowhidebots', $botLink );
597 $links[] = wfMsgHtml( 'rcshowhideanons', $anonsLink );
598 $links[] = wfMsgHtml( 'rcshowhideliu', $liuLink );
599 if( $wgUser->useRCPatrol() )
600 $links[] = wfMsgHtml( 'rcshowhidepatr', $patrLink );
601 $links[] = wfMsgHtml( 'rcshowhidemine', $myselfLink );
602 $hl = implode( ' | ', $links );
604 // show from this onward link
605 $now = $wgLang->timeanddate( wfTimestampNow(), true );
606 $tl = makeOptionsLink( $now, array( 'from' => wfTimestampNow()), $nondefaults );
608 $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter'),
610 $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter'), $tl );
611 return "$note<br />$rclinks<br />$rclistfrom";