00001 <?php
00002
00007 class SpecialRecentChanges extends SpecialPage {
00008 public function __construct() {
00009 parent::__construct( 'Recentchanges' );
00010 $this->includable( true );
00011 }
00012
00018 public function getDefaultOptions() {
00019 global $wgUser;
00020 $opts = new FormOptions();
00021
00022 $opts->add( 'days', (int)$wgUser->getOption( 'rcdays' ) );
00023 $opts->add( 'limit', (int)$wgUser->getOption( 'rclimit' ) );
00024 $opts->add( 'from', '' );
00025
00026 $opts->add( 'hideminor', $wgUser->getBoolOption( 'hideminor' ) );
00027 $opts->add( 'hidebots', true );
00028 $opts->add( 'hideanons', false );
00029 $opts->add( 'hideliu', false );
00030 $opts->add( 'hidepatrolled', $wgUser->getBoolOption( 'hidepatrolled' ) );
00031 $opts->add( 'hidemyself', false );
00032
00033 $opts->add( 'namespace', '', FormOptions::INTNULL );
00034 $opts->add( 'invert', false );
00035
00036 $opts->add( 'categories', '' );
00037 $opts->add( 'categories_any', false );
00038 $opts->add( 'tagfilter', '' );
00039 return $opts;
00040 }
00041
00047 public function setup( $parameters ) {
00048 global $wgRequest;
00049
00050 $opts = $this->getDefaultOptions();
00051 $opts->fetchValuesFromRequest( $wgRequest );
00052
00053
00054 if( $parameters !== null ) {
00055 $this->parseParameters( $parameters, $opts );
00056 }
00057
00058 $opts->validateIntBounds( 'limit', 0, 500 );
00059 return $opts;
00060 }
00061
00067 public function feedSetup() {
00068 global $wgFeedLimit, $wgRequest;
00069 $opts = $this->getDefaultOptions();
00070 # Feed is cached on limit,hideminor; other params would randomly not work
00071 $opts->fetchValuesFromRequest( $wgRequest, array( 'limit', 'hideminor' ) );
00072 $opts->validateIntBounds( 'limit', 0, $wgFeedLimit );
00073 return $opts;
00074 }
00075
00081 public function execute( $parameters ) {
00082 global $wgRequest, $wgOut;
00083 $feedFormat = $wgRequest->getVal( 'feed' );
00084
00085 # 10 seconds server-side caching max
00086 $wgOut->setSquidMaxage( 10 );
00087 # Check if the client has a cached version
00088 $lastmod = $this->checkLastModified( $feedFormat );
00089 if( $lastmod === false ) {
00090 return;
00091 }
00092
00093 $opts = $feedFormat ? $this->feedSetup() : $this->setup( $parameters );
00094 $this->setHeaders();
00095 $this->outputHeader();
00096
00097
00098 $rows = array();
00099 $conds = $this->buildMainQueryConds( $opts );
00100 $rows = $this->doMainQuery( $conds, $opts );
00101 if( $rows === false ){
00102 if( !$this->including() ) {
00103 $this->doHeader( $opts );
00104 }
00105 return;
00106 }
00107
00108 if( !$feedFormat ) {
00109 $batch = new LinkBatch;
00110 foreach( $rows as $row ) {
00111 $batch->add( NS_USER, $row->rc_user_text );
00112 $batch->add( NS_USER_TALK, $row->rc_user_text );
00113 $batch->add( $row->rc_namespace, $row->rc_title );
00114 }
00115 $batch->execute();
00116 }
00117 $target = isset($opts['target']) ? $opts['target'] : '';
00118 if( $feedFormat ) {
00119 list( $feed, $feedObj ) = $this->getFeedObject( $feedFormat );
00120 $feed->execute( $feedObj, $rows, $opts['limit'], $opts['hideminor'], $lastmod, $target );
00121 } else {
00122 $this->webOutput( $rows, $opts );
00123 }
00124
00125 $rows->free();
00126 }
00127
00133 public function getFeedObject( $feedFormat ){
00134 $feed = new ChangesFeed( $feedFormat, 'rcfeed' );
00135 $feedObj = $feed->getFeedObject(
00136 wfMsgForContent( 'recentchanges' ),
00137 wfMsgForContent( 'recentchanges-feed-description' )
00138 );
00139 return array( $feed, $feedObj );
00140 }
00141
00149 public function parseParameters( $par, FormOptions $opts ) {
00150 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
00151 foreach( $bits as $bit ) {
00152 if( 'hidebots' === $bit ) $opts['hidebots'] = true;
00153 if( 'bots' === $bit ) $opts['hidebots'] = false;
00154 if( 'hideminor' === $bit ) $opts['hideminor'] = true;
00155 if( 'minor' === $bit ) $opts['hideminor'] = false;
00156 if( 'hideliu' === $bit ) $opts['hideliu'] = true;
00157 if( 'hidepatrolled' === $bit ) $opts['hidepatrolled'] = true;
00158 if( 'hideanons' === $bit ) $opts['hideanons'] = true;
00159 if( 'hidemyself' === $bit ) $opts['hidemyself'] = true;
00160
00161 if( is_numeric( $bit ) ) $opts['limit'] = $bit;
00162
00163 $m = array();
00164 if( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) $opts['limit'] = $m[1];
00165 if( preg_match( '/^days=(\d+)$/', $bit, $m ) ) $opts['days'] = $m[1];
00166 }
00167 }
00168
00177 public function checkLastModified( $feedFormat ) {
00178 global $wgUseRCPatrol, $wgOut;
00179 $dbr = wfGetDB( DB_SLAVE );
00180 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __FUNCTION__ );
00181 if( $feedFormat || !$wgUseRCPatrol ) {
00182 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ) {
00183 # Client cache fresh and headers sent, nothing more to do.
00184 return false;
00185 }
00186 }
00187 return $lastmod;
00188 }
00189
00196 public function buildMainQueryConds( FormOptions $opts ) {
00197 global $wgUser;
00198
00199 $dbr = wfGetDB( DB_SLAVE );
00200 $conds = array();
00201
00202 # It makes no sense to hide both anons and logged-in users
00203 # Where this occurs, force anons to be shown
00204 $forcebot = false;
00205 if( $opts['hideanons'] && $opts['hideliu'] ){
00206 # Check if the user wants to show bots only
00207 if( $opts['hidebots'] ){
00208 $opts['hideanons'] = false;
00209 } else {
00210 $forcebot = true;
00211 $opts['hidebots'] = false;
00212 }
00213 }
00214
00215
00216 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
00217 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime % 86400);
00218 $cutoff = $dbr->timestamp( $cutoff_unixtime );
00219
00220 $fromValid = preg_match('/^[0-9]{14}$/', $opts['from']);
00221 if( $fromValid && $opts['from'] > wfTimestamp(TS_MW,$cutoff) ) {
00222 $cutoff = $dbr->timestamp($opts['from']);
00223 } else {
00224 $opts->reset( 'from' );
00225 }
00226
00227 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
00228
00229
00230 $hidePatrol = $wgUser->useRCPatrol() && $opts['hidepatrolled'];
00231 $hideLoggedInUsers = $opts['hideliu'] && !$forcebot;
00232 $hideAnonymousUsers = $opts['hideanons'] && !$forcebot;
00233
00234 if( $opts['hideminor'] ) $conds['rc_minor'] = 0;
00235 if( $opts['hidebots'] ) $conds['rc_bot'] = 0;
00236 if( $hidePatrol ) $conds['rc_patrolled'] = 0;
00237 if( $forcebot ) $conds['rc_bot'] = 1;
00238 if( $hideLoggedInUsers ) $conds[] = 'rc_user = 0';
00239 if( $hideAnonymousUsers ) $conds[] = 'rc_user != 0';
00240
00241 if( $opts['hidemyself'] ) {
00242 if( $wgUser->getId() ) {
00243 $conds[] = 'rc_user != ' . $dbr->addQuotes( $wgUser->getId() );
00244 } else {
00245 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $wgUser->getName() );
00246 }
00247 }
00248
00249 # Namespace filtering
00250 if( $opts['namespace'] !== '' ) {
00251 if( !$opts['invert'] ) {
00252 $conds[] = 'rc_namespace = ' . $dbr->addQuotes( $opts['namespace'] );
00253 } else {
00254 $conds[] = 'rc_namespace != ' . $dbr->addQuotes( $opts['namespace'] );
00255 }
00256 }
00257
00258 return $conds;
00259 }
00260
00268 public function doMainQuery( $conds, $opts ) {
00269 global $wgUser;
00270
00271 $tables = array( 'recentchanges' );
00272 $join_conds = array();
00273 $query_options = array( 'USE INDEX' => array('recentchanges' => 'rc_timestamp') );
00274
00275 $uid = $wgUser->getId();
00276 $dbr = wfGetDB( DB_SLAVE );
00277 $limit = $opts['limit'];
00278 $namespace = $opts['namespace'];
00279 $invert = $opts['invert'];
00280
00281 $join_conds = array();
00282
00283
00284 if( $uid ) {
00285 $tables[] = 'watchlist';
00286 $join_conds['watchlist'] = array('LEFT JOIN',
00287 "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace");
00288 }
00289 if ($wgUser->isAllowed("rollback")) {
00290 $tables[] = 'page';
00291 $join_conds['page'] = array('LEFT JOIN', 'rc_cur_id=page_id');
00292 }
00293
00294 $fields = array();
00295
00296 ChangeTags::modifyDisplayQuery( $tables,
00297 $fields,
00298 $conds,
00299 $join_conds,
00300 $query_options,
00301 $opts['tagfilter']
00302 );
00303
00304 wfRunHooks('SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts ) );
00305
00306
00307
00308
00309 if( is_null($namespace) || $invert || $namespace == NS_MAIN || $opts['tagfilter'] ) {
00310 $res = $dbr->select( $tables, '*', $conds, __METHOD__,
00311 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ) +
00312 $query_options,
00313 $join_conds );
00314
00315 } else {
00316
00317 $sqlNew = $dbr->selectSQLText( $tables, '*',
00318 array( 'rc_new' => 1 ) + $conds,
00319 __METHOD__,
00320 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
00321 'USE INDEX' => array('recentchanges' => 'new_name_timestamp') ),
00322 $join_conds );
00323
00324 $sqlOld = $dbr->selectSQLText( $tables, '*',
00325 array( 'rc_new' => 0 ) + $conds,
00326 __METHOD__,
00327 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
00328 'USE INDEX' => array('recentchanges' => 'new_name_timestamp') ),
00329 $join_conds );
00330 # Join the two fast queries, and sort the result set
00331 $sql = "($sqlNew) UNION ($sqlOld) ORDER BY rc_timestamp DESC LIMIT $limit";
00332 $res = $dbr->query( $sql, __METHOD__ );
00333 }
00334
00335 return $res;
00336 }
00337
00344 public function webOutput( $rows, $opts ) {
00345 global $wgOut, $wgUser, $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
00346 global $wgAllowCategorizedRecentChanges;
00347
00348 $limit = $opts['limit'];
00349
00350 if( !$this->including() ) {
00351
00352 $this->doHeader( $opts );
00353 }
00354
00355
00356 $wgOut->setSyndicated( true );
00357
00358 if( $wgAllowCategorizedRecentChanges ) {
00359 $this->filterByCategories( $rows, $opts );
00360 }
00361
00362 $showWatcherCount = $wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' );
00363 $watcherCache = array();
00364
00365 $dbr = wfGetDB( DB_SLAVE );
00366
00367 $counter = 1;
00368 $list = ChangesList::newFromUser( $wgUser );
00369
00370 $s = $list->beginRecentChangesList();
00371 foreach( $rows as $obj ) {
00372 if( $limit == 0 ) break;
00373 $rc = RecentChange::newFromRow( $obj );
00374 $rc->counter = $counter++;
00375 # Check if the page has been updated since the last visit
00376 if( $wgShowUpdatedMarker && !empty($obj->wl_notificationtimestamp) ) {
00377 $rc->notificationtimestamp = ($obj->rc_timestamp >= $obj->wl_notificationtimestamp);
00378 } else {
00379 $rc->notificationtimestamp = false;
00380 }
00381 # Check the number of users watching the page
00382 $rc->numberofWatchingusers = 0;
00383 if( $showWatcherCount && $obj->rc_namespace >= 0 ) {
00384 if( !isset($watcherCache[$obj->rc_namespace][$obj->rc_title]) ) {
00385 $watcherCache[$obj->rc_namespace][$obj->rc_title] =
00386 $dbr->selectField( 'watchlist',
00387 'COUNT(*)',
00388 array(
00389 'wl_namespace' => $obj->rc_namespace,
00390 'wl_title' => $obj->rc_title,
00391 ),
00392 __METHOD__ . '-watchers' );
00393 }
00394 $rc->numberofWatchingusers = $watcherCache[$obj->rc_namespace][$obj->rc_title];
00395 }
00396 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user ), $counter );
00397 --$limit;
00398 }
00399 $s .= $list->endRecentChangesList();
00400 $wgOut->addHTML( $s );
00401 }
00402
00409 public function doHeader( $opts ) {
00410 global $wgScript, $wgOut;
00411
00412 $this->setTopText( $wgOut, $opts );
00413
00414 $defaults = $opts->getAllValues();
00415 $nondefaults = $opts->getChangedValues();
00416 $opts->consumeValues( array( 'namespace', 'invert' ) );
00417
00418 $panel = array();
00419 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
00420 $panel[] = '<hr />';
00421
00422 $extraOpts = $this->getExtraOptions( $opts );
00423 $extraOptsCount = count( $extraOpts );
00424 $count = 0;
00425 $submit = ' ' . Xml::submitbutton( wfMsg( 'allpagessubmit' ) );
00426
00427 $out = Xml::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
00428 foreach( $extraOpts as $optionRow ) {
00429 # Add submit button to the last row only
00430 ++$count;
00431 $addSubmit = $count === $extraOptsCount ? $submit : '';
00432
00433 $out .= Xml::openElement( 'tr' );
00434 if( is_array( $optionRow ) ) {
00435 $out .= Xml::tags( 'td', array( 'class' => 'mw-label' ), $optionRow[0] );
00436 $out .= Xml::tags( 'td', array( 'class' => 'mw-input' ), $optionRow[1] . $addSubmit );
00437 } else {
00438 $out .= Xml::tags( 'td', array( 'class' => 'mw-input', 'colspan' => 2 ), $optionRow . $addSubmit );
00439 }
00440 $out .= Xml::closeElement( 'tr' );
00441 }
00442 $out .= Xml::closeElement( 'table' );
00443
00444 $unconsumed = $opts->getUnconsumedValues();
00445 foreach( $unconsumed as $key => $value ) {
00446 $out .= Xml::hidden( $key, $value );
00447 }
00448
00449 $t = $this->getTitle();
00450 $out .= Xml::hidden( 'title', $t->getPrefixedText() );
00451 $form = Xml::tags( 'form', array( 'action' => $wgScript ), $out );
00452 $panel[] = $form;
00453 $panelString = implode( "\n", $panel );
00454
00455 $wgOut->addHTML(
00456 Xml::fieldset( wfMsg( 'recentchanges-legend' ), $panelString, array( 'class' => 'rcoptions' ) )
00457 );
00458
00459 $this->setBottomText( $wgOut, $opts );
00460 }
00461
00468 function getExtraOptions( $opts ){
00469 $extraOpts = array();
00470 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
00471
00472 global $wgAllowCategorizedRecentChanges;
00473 if( $wgAllowCategorizedRecentChanges ) {
00474 $extraOpts['category'] = $this->categoryFilterForm( $opts );
00475 }
00476
00477 $tagFilter = ChangeTags::buildTagFilterSelector( $opts['tagfilter'] );
00478 if ( count($tagFilter) )
00479 $extraOpts['tagfilter'] = $tagFilter;
00480
00481 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
00482 return $extraOpts;
00483 }
00484
00491 function setTopText( OutputPage $out, FormOptions $opts ){
00492 $out->addWikiText( wfMsgForContentNoTrans( 'recentchangestext' ) );
00493 }
00494
00502 function setBottomText( OutputPage $out, FormOptions $opts ){}
00503
00510 protected function namespaceFilterForm( FormOptions $opts ) {
00511 $nsSelect = Xml::namespaceSelector( $opts['namespace'], '' );
00512 $nsLabel = Xml::label( wfMsg('namespace'), 'namespace' );
00513 $invert = Xml::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $opts['invert'] );
00514 return array( $nsLabel, "$nsSelect $invert" );
00515 }
00516
00523 protected function categoryFilterForm( FormOptions $opts ) {
00524 list( $label, $input ) = Xml::inputLabelSep( wfMsg('rc_categories'),
00525 'categories', 'mw-categories', false, $opts['categories'] );
00526
00527 $input .= ' ' . Xml::checkLabel( wfMsg('rc_categories_any'),
00528 'categories_any', 'mw-categories_any', $opts['categories_any'] );
00529
00530 return array( $label, $input );
00531 }
00532
00539 function filterByCategories( &$rows, FormOptions $opts ) {
00540 $categories = array_map( 'trim', explode( "|" , $opts['categories'] ) );
00541
00542 if( empty($categories) ) {
00543 return;
00544 }
00545
00546 # Filter categories
00547 $cats = array();
00548 foreach( $categories as $cat ) {
00549 $cat = trim( $cat );
00550 if( $cat == "" ) continue;
00551 $cats[] = $cat;
00552 }
00553
00554 # Filter articles
00555 $articles = array();
00556 $a2r = array();
00557 foreach( $rows AS $k => $r ) {
00558 $nt = Title::makeTitle( $r->rc_namespace, $r->rc_title );
00559 $id = $nt->getArticleID();
00560 if( $id == 0 ) continue; # Page might have been deleted...
00561 if( !in_array($id, $articles) ) {
00562 $articles[] = $id;
00563 }
00564 if( !isset($a2r[$id]) ) {
00565 $a2r[$id] = array();
00566 }
00567 $a2r[$id][] = $k;
00568 }
00569
00570 # Shortcut?
00571 if( !count($articles) || !count($cats) )
00572 return ;
00573
00574 # Look up
00575 $c = new Categoryfinder ;
00576 $c->seed( $articles, $cats, $opts['categories_any'] ? "OR" : "AND" ) ;
00577 $match = $c->run();
00578
00579 # Filter
00580 $newrows = array();
00581 foreach( $match AS $id ) {
00582 foreach( $a2r[$id] AS $rev ) {
00583 $k = $rev;
00584 $newrows[$k] = $rows[$k];
00585 }
00586 }
00587 $rows = $newrows;
00588 }
00589
00596 function makeOptionsLink( $title, $override, $options, $active = false ) {
00597 global $wgUser;
00598 $sk = $wgUser->getSkin();
00599 $params = $override + $options;
00600 return $sk->link( $this->getTitle(), htmlspecialchars( $title ),
00601 ( $active ? array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
00602 }
00603
00609 function optionsPanel( $defaults, $nondefaults ) {
00610 global $wgLang, $wgUser, $wgRCLinkLimits, $wgRCLinkDays;
00611
00612 $options = $nondefaults + $defaults;
00613
00614 $note = '';
00615 if( !wfEmptyMsg( 'rclegend', wfMsg('rclegend') ) ) {
00616 $note .= '<div class="mw-rclegend">' . wfMsgExt( 'rclegend', array('parseinline') ) . "</div>\n";
00617 }
00618 if( $options['from'] ) {
00619 $note .= wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
00620 $wgLang->formatNum( $options['limit'] ),
00621 $wgLang->timeanddate( $options['from'], true ) ) . '<br />';
00622 }
00623
00624 # Sort data for display and make sure it's unique after we've added user data.
00625 $wgRCLinkLimits[] = $options['limit'];
00626 $wgRCLinkDays[] = $options['days'];
00627 sort( $wgRCLinkLimits );
00628 sort( $wgRCLinkDays );
00629 $wgRCLinkLimits = array_unique( $wgRCLinkLimits );
00630 $wgRCLinkDays = array_unique( $wgRCLinkDays );
00631
00632
00633 foreach( $wgRCLinkLimits as $value ) {
00634 $cl[] = $this->makeOptionsLink( $wgLang->formatNum( $value ),
00635 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] ) ;
00636 }
00637 $cl = $wgLang->pipeList( $cl );
00638
00639
00640 foreach( $wgRCLinkDays as $value ) {
00641 $dl[] = $this->makeOptionsLink( $wgLang->formatNum( $value ),
00642 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] ) ;
00643 }
00644 $dl = $wgLang->pipeList( $dl );
00645
00646
00647
00648 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
00649 $minorLink = $this->makeOptionsLink( $showhide[1-$options['hideminor']],
00650 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
00651 $botLink = $this->makeOptionsLink( $showhide[1-$options['hidebots']],
00652 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
00653 $anonsLink = $this->makeOptionsLink( $showhide[ 1 - $options['hideanons'] ],
00654 array( 'hideanons' => 1 - $options['hideanons'] ), $nondefaults );
00655 $liuLink = $this->makeOptionsLink( $showhide[1-$options['hideliu']],
00656 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
00657 $patrLink = $this->makeOptionsLink( $showhide[1-$options['hidepatrolled']],
00658 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
00659 $myselfLink = $this->makeOptionsLink( $showhide[1-$options['hidemyself']],
00660 array( 'hidemyself' => 1-$options['hidemyself'] ), $nondefaults);
00661
00662 $links[] = wfMsgHtml( 'rcshowhideminor', $minorLink );
00663 $links[] = wfMsgHtml( 'rcshowhidebots', $botLink );
00664 $links[] = wfMsgHtml( 'rcshowhideanons', $anonsLink );
00665 $links[] = wfMsgHtml( 'rcshowhideliu', $liuLink );
00666 if( $wgUser->useRCPatrol() )
00667 $links[] = wfMsgHtml( 'rcshowhidepatr', $patrLink );
00668 $links[] = wfMsgHtml( 'rcshowhidemine', $myselfLink );
00669 $hl = $wgLang->pipeList( $links );
00670
00671
00672 $now = $wgLang->timeanddate( wfTimestampNow(), true );
00673 $tl = $this->makeOptionsLink( $now, array( 'from' => wfTimestampNow() ), $nondefaults );
00674
00675 $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter' ),
00676 $cl, $dl, $hl );
00677 $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter' ), $tl );
00678 return "{$note}$rclinks<br />$rclistfrom";
00679 }
00680 }