24761930a333bcf513a240ece95ee12aee78ef70
7 class SpecialRecentChanges
extends SpecialPage
{
8 public function __construct() {
9 SpecialPage
::SpecialPage( 'Recentchanges' );
10 $this->includable( true );
13 public function getDefaultOptions() {
16 $opts = new FormOptions();
18 $opts->add( 'days', (int)$wgUser->getDefaultOption('rcdays') );
19 $opts->add( 'limit', (int)$wgUser->getDefaultOption('rclimit') );
20 $opts->add( 'from', '' );
22 $opts->add( 'hideminor', false );
23 $opts->add( 'hidebots', true );
24 $opts->add( 'hideanons', false );
25 $opts->add( 'hideliu', false );
26 $opts->add( 'hidepatrolled', false );
27 $opts->add( 'hidemyself', false );
29 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
30 $opts->add( 'invert', false );
32 $opts->add( 'categories', '' );
33 $opts->add( 'categories_any', false );
38 public function setup( $parameters ) {
39 global $wgUser, $wgRequest;
41 $opts = $this->getDefaultOptions();
42 $opts['days'] = (int)$wgUser->getOption( 'rcdays', $opts['days'] );
43 $opts['limit'] = (int)$wgUser->getOption( 'rclimit', $opts['limit'] );
44 $opts['hideminor'] = $wgUser->getOption( 'hideminor', $opts['hideminor'] );
45 $opts->fetchValuesFromRequest( $wgRequest );
47 // Give precedence to subpage syntax
48 if ( $parameters !== null ) {
49 $this->parseParameters( $this->par
, $opts );
52 $opts->validateIntBounds( 'limit', 0, 5000 );
56 public function feedSetup() {
57 global $wgFeedLimit, $wgRequest;
58 $opts = $this->getDefaultOptions();
59 $opts->fetchValuesFromRequest( $wgRequest, array( 'days', 'limit', 'hideminor' ) );
60 $opts->validateIntBounds( 'limit', 0, $wgFeedLimit );
64 public function execute( $parameters ) {
65 global $wgRequest, $wgOut;
66 $feedFormat = $wgRequest->getVal( 'feed' );
68 # 10 seconds server-side caching max
69 $wgOut->setSquidMaxage( 10 );
71 $lastmod = $this->checkLastModified( $feedFormat );
76 $opts = $feedFormat ?
$this->feedSetup() : $this->setup( $parameters );
79 // Fetch results, prepare a batch link existence check query
81 $batch = new LinkBatch
;
82 $conds = $this->buildMainQueryConds( $opts );
83 $res = $this->doMainQuery( $conds, $opts );
84 $dbr = wfGetDB( DB_SLAVE
);
85 while( $row = $dbr->fetchObject( $res ) ){
88 // User page and talk links
89 $batch->add( NS_USER
, $row->rc_user_text
);
90 $batch->add( NS_USER_TALK
, $row->rc_user_text
);
94 $dbr->freeResult( $res );
97 $feed = new ChangesFeed( $feedFormat, 'rcfeed' );
98 $feedObj = $feed->getFeedObject(
99 wfMsgForContent( 'recentchanges' ),
100 wfMsgForContent( 'recentchanges-feed-description' )
102 $feed->execute( $feedObj, $rows, $opts['limit'], $opts['hideminor'], $lastmod );
105 $this->webOutput( $rows, $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 ) $opts['hidebots'] = true;
114 if ( 'bots' === $bit ) $opts['hidebots'] = false;
115 if ( 'hideminor' === $bit ) $opts['hideminor'] = true;
116 if ( 'minor' === $bit ) $opts['hideminor'] = false;
117 if ( 'hideliu' === $bit ) $opts['hideliu'] = true;
118 if ( 'hidepatrolled' === $bit ) $opts['hidepatrolled'] = true;
119 if ( 'hideanons' === $bit ) $opts['hideanons'] = true;
120 if ( 'hidemyself' === $bit ) $opts['hidemyself'] = true;
122 if ( is_numeric( $bit ) ) $opts['limit'] = $bit;
125 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) $opts['limit'] = $m[1];
126 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) $opts['days'] = $m[1];
130 # Get last modified date, for client caching
131 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
132 public function checkLastModified( $feedFormat ) {
133 global $wgUseRCPatrol, $wgOut;
134 $dbr = wfGetDB( DB_SLAVE
);
135 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __FUNCTION__
);
136 if ( $feedFormat ||
!$wgUseRCPatrol ) {
137 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
138 # Client cache fresh and headers sent, nothing more to do.
145 public function buildMainQueryConds( FormOptions
$opts ) {
148 $dbr = wfGetDB( DB_SLAVE
);
151 # It makes no sense to hide both anons and logged-in users
152 # Where this occurs, force anons to be shown
154 if( $opts['hideanons'] && $opts['hideliu'] ){
155 # Check if the user wants to show bots only
156 if( $opts['hidebots'] ){
157 $opts['hideanons'] = false;
160 $opts['hidebots'] = false;
165 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
166 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime %
86400);
167 $cutoff = $dbr->timestamp( $cutoff_unixtime );
169 $fromValid = preg_match('/^[0-9]{14}$/', $opts['from']);
170 if( $fromValid && $opts['from'] > wfTimestamp(TS_MW
,$cutoff) ) {
171 $cutoff = $dbr->timestamp($opts['from']);
173 $opts->reset( 'from' );
176 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
179 $hidePatrol = $wgUser->useRCPatrol() && $opts['hidepatrolled'];
180 $hideLoggedInUsers = $opts['hideliu'] && !$forcebot;
181 $hideAnonymousUsers = $opts['hideanons'] && !$forcebot;
183 if ( $opts['hideminor'] ) $conds['rc_minor'] = 0;
184 if ( $opts['hidebots'] ) $conds['rc_bot'] = 0;
185 if ( $hidePatrol ) $conds['rc_patrolled'] = 0;
186 if ( $forcebot ) $conds['rc_bot'] = 1;
187 if ( $hideLoggedInUsers ) $conds[] = 'rc_user = 0';
188 if ( $hideAnonymousUsers ) $conds[] = 'rc_user != 0';
190 if( $opts['hidemyself'] ) {
191 if( $wgUser->getId() ) {
192 $conds[] = 'rc_user != ' . $dbr->addQuotes( $wgUser->getId() );
194 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $wgUser->getName() );
198 # Namespace filtering
199 if ( $opts['namespace'] !== '' ) {
200 if ( !$opts['invert'] ) {
201 $conds[] = 'rc_namespace = ' . $dbr->addQuotes( $opts['namespace'] );
203 $conds[] = 'rc_namespace != ' . $dbr->addQuotes( $opts['namespace'] );
210 public function doMainQuery( $conds, $opts ) {
213 $tables = array( 'recentchanges' );
214 $join_conds = array();
216 $uid = $wgUser->getId();
217 $dbr = wfGetDB( DB_SLAVE
);
218 $limit = $opts['limit'];
219 $namespace = $opts['namespace'];
220 $invert = $opts['invert'];
222 // JOIN on watchlist for users
223 if( $wgUser->getId() ) {
224 $tables[] = 'watchlist';
225 $join_conds = array( 'watchlist' => array('LEFT JOIN',"wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace") );
228 wfRunHooks('SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts ) );
230 // Is there either one namespace selected or excluded?
231 // Also, if this is "all" or main namespace, just use timestamp index.
232 if( is_null($namespace) ||
$invert ||
$namespace == NS_MAIN
) {
233 $res = $dbr->select( $tables, '*', $conds, __METHOD__
,
234 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
235 'USE INDEX' => array('recentchanges' => 'rc_timestamp') ),
237 // We have a new_namespace_time index! UNION over new=(0,1) and sort result set!
240 $sqlNew = $dbr->selectSQLText( $tables, '*',
241 array( 'rc_new' => 1 ) +
$conds,
243 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
244 'USE INDEX' => array('recentchanges' => 'new_name_timestamp') ),
247 $sqlOld = $dbr->selectSQLText( $tables, '*',
248 array( 'rc_new' => 0 ) +
$conds,
250 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
251 'USE INDEX' => array('recentchanges' => 'new_name_timestamp') ),
253 # Join the two fast queries, and sort the result set
254 $sql = "($sqlNew) UNION ($sqlOld) ORDER BY rc_timestamp DESC LIMIT $limit";
255 $res = $dbr->query( $sql, __METHOD__
);
261 public function webOutput( $rows, $opts ) {
262 global $wgOut, $wgUser, $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
263 global $wgAllowCategorizedRecentChanges;
265 $limit = $opts['limit'];
267 if ( !$this->including() ) {
268 // Output options box
269 $this->doHeader( $opts );
272 // And now for the content
273 $wgOut->setSyndicated( true );
275 $list = ChangesList
::newFromUser( $wgUser );
277 if ( $wgAllowCategorizedRecentChanges ) {
278 rcFilterByCategories( $rows, $opts );
281 $s = $list->beginRecentChangesList();
284 $showWatcherCount = $wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' );
285 $watcherCache = array();
287 $dbr = wfGetDB( DB_SLAVE
);
289 foreach( $rows as $obj ){
294 if ( ! ( $opts['hideminor'] && $obj->rc_minor
) &&
295 ! ( $opts['hidepatrolled'] && $obj->rc_patrolled
) ) {
296 $rc = RecentChange
::newFromRow( $obj );
297 $rc->counter
= $counter++
;
299 if ($wgShowUpdatedMarker
300 && !empty( $obj->wl_notificationtimestamp
)
301 && ($obj->rc_timestamp
>= $obj->wl_notificationtimestamp
)) {
302 $rc->notificationtimestamp
= true;
304 $rc->notificationtimestamp
= false;
307 $rc->numberofWatchingusers
= 0; // Default
308 if ($showWatcherCount && $obj->rc_namespace
>= 0) {
309 if (!isset($watcherCache[$obj->rc_namespace
][$obj->rc_title
])) {
310 $watcherCache[$obj->rc_namespace
][$obj->rc_title
] =
311 $dbr->selectField( 'watchlist',
314 'wl_namespace' => $obj->rc_namespace
,
315 'wl_title' => $obj->rc_title
,
317 __METHOD__
. '-watchers' );
319 $rc->numberofWatchingusers
= $watcherCache[$obj->rc_namespace
][$obj->rc_title
];
321 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user
) );
325 $s .= $list->endRecentChangesList();
326 $wgOut->addHTML( $s );
329 public function doHeader( $opts ) {
330 global $wgScript, $wgOut;
331 $wgOut->addWikiText( wfMsgForContentNoTrans( 'recentchangestext' ) );
333 $defaults = $opts->getAllValues();
334 $nondefaults = $opts->getChangedValues();
335 $opts->consumeValues( array( 'namespace', 'invert' ) );
338 $panel[] = rcOptionsPanel( $defaults, $nondefaults );
341 $extraOpts = array();
342 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
344 global $wgAllowCategorizedRecentChanges;
345 if ( $wgAllowCategorizedRecentChanges ) {
346 $extraOpts['category'] = $this->categoryFilterForm( $opts );
349 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
350 $extraOpts['submit'] = Xml
::submitbutton( wfMsg('allpagessubmit') );
352 $out = Xml
::openElement( 'table' );
353 foreach ( $extraOpts as $optionRow ) {
354 $out .= Xml
::openElement( 'tr' );
355 if ( is_array($optionRow) ) {
356 $out .= Xml
::tags( 'td', null, $optionRow[0] );
357 $out .= Xml
::tags( 'td', null, $optionRow[1] );
359 $out .= Xml
::tags( 'td', array( 'colspan' => 2 ), $optionRow );
361 $out .= Xml
::closeElement( 'tr' );
363 $out .= Xml
::closeElement( 'table' );
365 $unconsumed = $opts->getUnconsumedValues();
366 foreach ( $unconsumed as $key => $value ) {
367 $out .= Xml
::hidden( $key, $value );
370 $t = $this->getTitle();
371 $out .= Xml
::hidden( 'title', $t->getPrefixedText() );
372 $form = Xml
::tags( 'form', array( 'action' => $wgScript ), $out );
374 $panelString = implode( "\n", $panel );
377 Xml
::fieldset( wfMsg( 'recentchanges' ), $panelString, array( 'class' => 'rcoptions' ) )
382 * Creates the choose namespace selection
386 protected function namespaceFilterForm( FormOptions
$opts ) {
387 $nsSelect = HTMLnamespaceselector( $opts['namespace'], '' );
388 $nsLabel = Xml
::label( wfMsg('namespace'), 'namespace' );
389 $invert = Xml
::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $opts['invert'] );
390 return array( $nsLabel, "$nsSelect $invert" );
393 protected function categoryFilterForm( FormOptions
$opts ) {
394 list( $label, $input ) = Xml
::inputLabelSep( wfMsg('rc_categories'),
395 'categories', 'mw-categories', false, $opts['categories'] );
397 $input .= ' ' . Xml
::checkLabel( wfMsg('rc_categories_any'),
398 'categories_any', 'mw-categories_any', $opts['categories_any'] );
400 return array( $label, $input );
405 function rcFilterByCategories ( &$rows, FormOptions
$opts ) {
406 $categories = array_map( 'trim', explode( "|" , $categories ) );
408 if( empty($categories) ) {
414 foreach ( $opts['categories'] AS $cat ) {
416 if ( $cat == "" ) continue;
423 foreach ( $rows AS $k => $r ) {
424 $nt = Title
::makeTitle( $r->rc_namespace
, $r->rc_title
);
425 $id = $nt->getArticleID();
426 if ( $id == 0 ) continue; # Page might have been deleted...
427 if ( !in_array($id, $articles) ) {
430 if ( !isset($a2r[$id]) ) {
437 if ( !count($articles) ||
!count($cats) )
441 $c = new Categoryfinder
;
442 $c->seed( $articles, $cats, $opts['categories_any'] ?
"OR" : "AND" ) ;
447 foreach ( $match AS $id ) {
448 foreach ( $a2r[$id] AS $rev ) {
450 $newrows[$k] = $rows[$k];
459 function rcCountLink( $lim, $d, $page='Recentchanges', $more='', $active = false ) {
460 global $wgUser, $wgLang, $wgContLang;
461 $sk = $wgUser->getSkin();
462 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
463 ($lim ?
$wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
464 ($d ?
"days={$d}&" : '') . 'limit='.$lim, '', '',
465 $active ?
'style="font-weight: bold;"' : '' );
472 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='', $active = false ) {
473 global $wgUser, $wgLang, $wgContLang;
474 $sk = $wgUser->getSkin();
475 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
476 ($d ?
$wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
477 ($lim ?
'&limit='.$lim : ''), '', '',
478 $active ?
'style="font-weight: bold;"' : '' );
483 * Used by Recentchangeslinked
485 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
486 $botLink = '', $liuLink = '', $patrLink = '', $myselfLink = '' ) {
487 global $wgRCLinkLimits, $wgRCLinkDays;
488 if ($more != '') $more .= '&';
490 # Sort data for display and make sure it's unique after we've added user data.
491 # FIXME: why does this piss around with globals like this? Why is $limit added on globally?
492 $wgRCLinkLimits[] = $limit;
493 $wgRCLinkDays[] = $days;
494 sort($wgRCLinkLimits);
496 $wgRCLinkLimits = array_unique($wgRCLinkLimits);
497 $wgRCLinkDays = array_unique($wgRCLinkDays);
500 foreach( $wgRCLinkLimits as $countLink ) {
501 $cl[] = rcCountLink( $countLink, $days, $page, $more, $countLink == $limit );
503 if( $doall ) $cl[] = rcCountLink( 0, $days, $page, $more );
504 $cl = implode( ' | ', $cl);
507 foreach( $wgRCLinkDays as $daysLink ) {
508 $dl[] = rcDaysLink( $limit, $daysLink, $page, $more, $daysLink == $days );
510 if( $doall ) $dl[] = rcDaysLink( $limit, 0, $page, $more );
511 $dl = implode( ' | ', $dl);
513 $linkParts = array( 'minorLink' => 'minor', 'botLink' => 'bots', 'liuLink' => 'liu', 'patrLink' => 'patr', 'myselfLink' => 'mine' );
514 foreach( $linkParts as $linkVar => $linkMsg ) {
515 if( $
$linkVar != '' )
516 $links[] = wfMsgHtml( 'rcshowhide' . $linkMsg, $
$linkVar );
519 $shm = implode( ' | ', $links );
520 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
526 * Makes change an option link which carries all the other options
527 * @param $title see Title
531 function makeOptionsLink( $title, $override, $options, $active = false ) {
532 global $wgUser, $wgContLang;
533 $sk = $wgUser->getSkin();
534 return $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
535 htmlspecialchars( $title ), wfArrayToCGI( $override, $options ), '', '',
536 $active ?
'style="font-weight: bold;"' : '' );
540 * Creates the options panel.
542 * @param $nondefaults
544 function rcOptionsPanel( $defaults, $nondefaults ) {
545 global $wgLang, $wgUser, $wgRCLinkLimits, $wgRCLinkDays;
547 $options = $nondefaults +
$defaults;
549 if( $options['from'] )
550 $note = wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
551 $wgLang->formatNum( $options['limit'] ),
552 $wgLang->timeanddate( $options['from'], true ) );
554 $note = wfMsgExt( 'rcnote', array( 'parseinline' ),
555 $wgLang->formatNum( $options['limit'] ),
556 $wgLang->formatNum( $options['days'] ),
557 $wgLang->timeAndDate( wfTimestampNow(), true ) );
559 # Sort data for display and make sure it's unique after we've added user data.
560 $wgRCLinkLimits[] = $options['limit'];
561 $wgRCLinkDays[] = $options['days'];
562 sort($wgRCLinkLimits);
564 $wgRCLinkLimits = array_unique($wgRCLinkLimits);
565 $wgRCLinkDays = array_unique($wgRCLinkDays);
568 foreach( $wgRCLinkLimits as $value ) {
569 $cl[] = makeOptionsLink( $wgLang->formatNum( $value ),
570 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] ) ;
572 $cl = implode( ' | ', $cl);
574 // day links, reset 'from' to none
575 foreach( $wgRCLinkDays as $value ) {
576 $dl[] = makeOptionsLink( $wgLang->formatNum( $value ),
577 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] ) ;
579 $dl = implode( ' | ', $dl);
583 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
584 $minorLink = makeOptionsLink( $showhide[1-$options['hideminor']],
585 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
586 $botLink = makeOptionsLink( $showhide[1-$options['hidebots']],
587 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
588 $anonsLink = makeOptionsLink( $showhide[ 1 - $options['hideanons'] ],
589 array( 'hideanons' => 1 - $options['hideanons'] ), $nondefaults );
590 $liuLink = makeOptionsLink( $showhide[1-$options['hideliu']],
591 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
592 $patrLink = makeOptionsLink( $showhide[1-$options['hidepatrolled']],
593 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
594 $myselfLink = makeOptionsLink( $showhide[1-$options['hidemyself']],
595 array( 'hidemyself' => 1-$options['hidemyself'] ), $nondefaults);
597 $links[] = wfMsgHtml( 'rcshowhideminor', $minorLink );
598 $links[] = wfMsgHtml( 'rcshowhidebots', $botLink );
599 $links[] = wfMsgHtml( 'rcshowhideanons', $anonsLink );
600 $links[] = wfMsgHtml( 'rcshowhideliu', $liuLink );
601 if( $wgUser->useRCPatrol() )
602 $links[] = wfMsgHtml( 'rcshowhidepatr', $patrLink );
603 $links[] = wfMsgHtml( 'rcshowhidemine', $myselfLink );
604 $hl = implode( ' | ', $links );
606 // show from this onward link
607 $now = $wgLang->timeanddate( wfTimestampNow(), true );
608 $tl = makeOptionsLink( $now, array( 'from' => wfTimestampNow()), $nondefaults );
610 $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter'),
612 $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter'), $tl );
613 return "$note<br />$rclinks<br />$rclistfrom";