* Reorganised the includes directory, creating subdirectories db, parser and specials
[lhc/web/wiklou.git] / includes / specials / Recentchanges.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
6
7 /**
8 *
9 */
10 require_once( dirname(__FILE__) . '/ChangesList.php' );
11
12 /**
13 * Constructor
14 */
15 function wfSpecialRecentchanges( $par, $specialPage ) {
16 global $wgUser, $wgOut, $wgRequest, $wgUseRCPatrol;
17 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
18 global $wgAllowCategorizedRecentChanges ;
19
20 # Get query parameters
21 $feedFormat = $wgRequest->getVal( 'feed' );
22
23 /* Checkbox values can't be true by default, because
24 * we cannot differentiate between unset and not set at all
25 */
26 $defaults = array(
27 /* int */ 'days' => $wgUser->getDefaultOption('rcdays'),
28 /* int */ 'limit' => $wgUser->getDefaultOption('rclimit'),
29 /* bool */ 'hideminor' => false,
30 /* bool */ 'hidebots' => true,
31 /* bool */ 'hideanons' => false,
32 /* bool */ 'hideliu' => false,
33 /* bool */ 'hidepatrolled' => false,
34 /* bool */ 'hidemyself' => false,
35 /* text */ 'from' => '',
36 /* text */ 'namespace' => null,
37 /* bool */ 'invert' => false,
38 /* bool */ 'categories_any' => false,
39 );
40
41 extract($defaults);
42
43
44 $days = $wgUser->getOption( 'rcdays', $defaults['days']);
45 $days = $wgRequest->getInt( 'days', $days );
46
47 $limit = $wgUser->getOption( 'rclimit', $defaults['limit'] );
48
49 # list( $limit, $offset ) = wfCheckLimits( 100, 'rclimit' );
50 $limit = $wgRequest->getInt( 'limit', $limit );
51
52 /* order of selection: url > preferences > default */
53 $hideminor = $wgRequest->getBool( 'hideminor', $wgUser->getOption( 'hideminor') ? true : $defaults['hideminor'] );
54
55 # As a feed, use limited settings only
56 if( $feedFormat ) {
57 global $wgFeedLimit;
58 $limit = min( $wgFeedLimit, $limit );
59 } else {
60
61 $namespace = $wgRequest->getIntOrNull( 'namespace' );
62 $invert = $wgRequest->getBool( 'invert', $defaults['invert'] );
63 $hidebots = $wgRequest->getBool( 'hidebots', $defaults['hidebots'] );
64 $hideanons = $wgRequest->getBool( 'hideanons', $defaults['hideanons'] );
65 $hideliu = $wgRequest->getBool( 'hideliu', $defaults['hideliu'] );
66 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled', $defaults['hidepatrolled'] );
67 $hidemyself = $wgRequest->getBool ( 'hidemyself', $defaults['hidemyself'] );
68 $from = $wgRequest->getVal( 'from', $defaults['from'] );
69
70 # Get query parameters from path
71 if( $par ) {
72 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
73 foreach ( $bits as $bit ) {
74 if ( 'hidebots' == $bit ) $hidebots = 1;
75 if ( 'bots' == $bit ) $hidebots = 0;
76 if ( 'hideminor' == $bit ) $hideminor = 1;
77 if ( 'minor' == $bit ) $hideminor = 0;
78 if ( 'hideliu' == $bit ) $hideliu = 1;
79 if ( 'hidepatrolled' == $bit ) $hidepatrolled = 1;
80 if ( 'hideanons' == $bit ) $hideanons = 1;
81 if ( 'hidemyself' == $bit ) $hidemyself = 1;
82
83 if ( is_numeric( $bit ) ) {
84 $limit = $bit;
85 }
86
87 $m = array();
88 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
89 $limit = $m[1];
90 }
91
92 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
93 $days = $m[1];
94 }
95 }
96 }
97 }
98
99 if ( $limit < 0 || $limit > 5000 ) $limit = $defaults['limit'];
100
101 # Database connection and caching
102 $dbr = wfGetDB( DB_SLAVE );
103
104 $cutoff_unixtime = time() - ( $days * 86400 );
105 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime % 86400);
106 $cutoff = $dbr->timestamp( $cutoff_unixtime );
107 if(preg_match('/^[0-9]{14}$/', $from) and $from > wfTimestamp(TS_MW,$cutoff)) {
108 $cutoff = $dbr->timestamp($from);
109 } else {
110 $from = $defaults['from'];
111 }
112
113 # 10 seconds server-side caching max
114 $wgOut->setSquidMaxage( 10 );
115
116 # Get last modified date, for client caching
117 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
118 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __FUNCTION__ );
119 if ( $feedFormat || !$wgUseRCPatrol ) {
120 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
121 # Client cache fresh and headers sent, nothing more to do.
122 return;
123 }
124 }
125
126 # It makes no sense to hide both anons and logged-in users
127 # Where this occurs, force anons to be shown
128 $forcebot = false;
129 if( $hideanons && $hideliu ){
130 # Check if the user wants to show bots only
131 if( $hidebots ){
132 $hideanons = 0;
133 } else {
134 $forcebot = true;
135 $hidebots = 0;
136 }
137 }
138
139 # Form WHERE fragments for all the options
140 $hidem = $hideminor ? 'AND rc_minor = 0' : '';
141 $hidem .= $hidebots ? ' AND rc_bot = 0' : '';
142 $hidem .= $hideliu && !$forcebot ? ' AND rc_user = 0' : '';
143 $hidem .= ($wgUser->useRCPatrol() && $hidepatrolled ) ? ' AND rc_patrolled = 0' : '';
144 $hidem .= $hideanons && !$forcebot ? ' AND rc_user != 0' : '';
145 $hidem .= $forcebot ? ' AND rc_bot = 1' : '';
146
147 if( $hidemyself ) {
148 if( $wgUser->getId() ) {
149 $hidem .= ' AND rc_user != ' . $wgUser->getId();
150 } else {
151 $hidem .= ' AND rc_user_text != ' . $dbr->addQuotes( $wgUser->getName() );
152 }
153 }
154
155 // JOIN on watchlist for users
156 $uid = $wgUser->getId();
157 if( $uid ) {
158 $tables = array( 'recentchanges', 'watchlist' );
159 $join_conds = array( 'watchlist' => array('LEFT JOIN',"wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace") );
160 } else {
161 $tables = array( 'recentchanges' );
162 $join_conds = array();
163 }
164
165 # Namespace filtering
166 $hidem .= is_null($namespace) ? '' : ' AND rc_namespace' . ($invert ? '!=' : '=') . $namespace;
167
168 // Is there either one namespace selected or excluded?
169 // Also, if this is "all" or main namespace, just use timestamp index.
170 if( is_null($namespace) || $invert || $namespace == NS_MAIN ) {
171 $res = $dbr->select( $tables, '*',
172 array( "rc_timestamp >= '{$cutoff}' {$hidem}" ),
173 __METHOD__,
174 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
175 'USE INDEX' => array('recentchanges' => 'rc_timestamp') ),
176 $join_conds );
177 // We have a new_namespace_time index! UNION over new=(0,1) and sort result set!
178 } else {
179 // New pages
180 $sqlNew = $dbr->selectSQLText( $tables, '*',
181 array( 'rc_new' => 1,
182 "rc_timestamp >= '{$cutoff}' {$hidem}" ),
183 __METHOD__,
184 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
185 'USE INDEX' => array('recentchanges' => 'new_name_timestamp') ),
186 $join_conds );
187 // Old pages
188 $sqlOld = $dbr->selectSQLText( $tables, '*',
189 array( 'rc_new' => 0,
190 "rc_timestamp >= '{$cutoff}' {$hidem}" ),
191 __METHOD__,
192 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
193 'USE INDEX' => array('recentchanges' => 'new_name_timestamp') ),
194 $join_conds );
195 # Join the two fast queries, and sort the result set
196 $sql = "($sqlNew) UNION ($sqlOld) ORDER BY rc_timestamp DESC LIMIT $limit";
197 $res = $dbr->query( $sql, __METHOD__ );
198 }
199
200 // Fetch results, prepare a batch link existence check query
201 $rows = array();
202 $batch = new LinkBatch;
203 while( $row = $dbr->fetchObject( $res ) ){
204 $rows[] = $row;
205 if ( !$feedFormat ) {
206 // User page and talk links
207 $batch->add( NS_USER, $row->rc_user_text );
208 $batch->add( NS_USER_TALK, $row->rc_user_text );
209 }
210
211 }
212 $dbr->freeResult( $res );
213
214 if( $feedFormat ) {
215 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
216 } else {
217
218 # Web output...
219
220 // Run existence checks
221 $batch->execute();
222 $any = $wgRequest->getBool( 'categories_any', $defaults['categories_any']);
223
224 // Output header
225 if ( !$specialPage->including() ) {
226 $wgOut->addWikiText( wfMsgForContentNoTrans( "recentchangestext" ) );
227
228 // Dump everything here
229 $nondefaults = array();
230
231 wfAppendToArrayIfNotDefault( 'days', $days, $defaults, $nondefaults);
232 wfAppendToArrayIfNotDefault( 'limit', $limit , $defaults, $nondefaults);
233 wfAppendToArrayIfNotDefault( 'hideminor', $hideminor, $defaults, $nondefaults);
234 wfAppendToArrayIfNotDefault( 'hidebots', $hidebots, $defaults, $nondefaults);
235 wfAppendToArrayIfNotDefault( 'hideanons', $hideanons, $defaults, $nondefaults );
236 wfAppendToArrayIfNotDefault( 'hideliu', $hideliu, $defaults, $nondefaults);
237 wfAppendToArrayIfNotDefault( 'hidepatrolled', $hidepatrolled, $defaults, $nondefaults);
238 wfAppendToArrayIfNotDefault( 'hidemyself', $hidemyself, $defaults, $nondefaults);
239 wfAppendToArrayIfNotDefault( 'from', $from, $defaults, $nondefaults);
240 wfAppendToArrayIfNotDefault( 'namespace', $namespace, $defaults, $nondefaults);
241 wfAppendToArrayIfNotDefault( 'invert', $invert, $defaults, $nondefaults);
242 wfAppendToArrayIfNotDefault( 'categories_any', $any, $defaults, $nondefaults);
243
244 // Add end of the texts
245 $wgOut->addHTML( '<div class="rcoptions">' . rcOptionsPanel( $defaults, $nondefaults ) . "\n" );
246 $wgOut->addHTML( rcNamespaceForm( $namespace, $invert, $nondefaults, $any ) . '</div>'."\n");
247 }
248
249 // And now for the content
250 $wgOut->setSyndicated( true );
251
252 $list = ChangesList::newFromUser( $wgUser );
253
254 if ( $wgAllowCategorizedRecentChanges ) {
255 $categories = trim ( $wgRequest->getVal ( 'categories' , "" ) ) ;
256 $categories = str_replace ( "|" , "\n" , $categories ) ;
257 $categories = explode ( "\n" , $categories ) ;
258 rcFilterByCategories ( $rows , $categories , $any ) ;
259 }
260
261 $s = $list->beginRecentChangesList();
262 $counter = 1;
263
264 $showWatcherCount = $wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' );
265 $watcherCache = array();
266
267 foreach( $rows as $obj ){
268 if( $limit == 0) {
269 break;
270 }
271
272 if ( ! ( $hideminor && $obj->rc_minor ) &&
273 ! ( $hidepatrolled && $obj->rc_patrolled ) ) {
274 $rc = RecentChange::newFromRow( $obj );
275 $rc->counter = $counter++;
276
277 if ($wgShowUpdatedMarker
278 && !empty( $obj->wl_notificationtimestamp )
279 && ($obj->rc_timestamp >= $obj->wl_notificationtimestamp)) {
280 $rc->notificationtimestamp = true;
281 } else {
282 $rc->notificationtimestamp = false;
283 }
284
285 $rc->numberofWatchingusers = 0; // Default
286 if ($showWatcherCount && $obj->rc_namespace >= 0) {
287 if (!isset($watcherCache[$obj->rc_namespace][$obj->rc_title])) {
288 $watcherCache[$obj->rc_namespace][$obj->rc_title] =
289 $dbr->selectField( 'watchlist',
290 'COUNT(*)',
291 array(
292 'wl_namespace' => $obj->rc_namespace,
293 'wl_title' => $obj->rc_title,
294 ),
295 __METHOD__ . '-watchers' );
296 }
297 $rc->numberofWatchingusers = $watcherCache[$obj->rc_namespace][$obj->rc_title];
298 }
299 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user ) );
300 --$limit;
301 }
302 }
303 $s .= $list->endRecentChangesList();
304 $wgOut->addHTML( $s );
305 }
306 }
307
308 function rcFilterByCategories ( &$rows , $categories , $any ) {
309 if( empty( $categories ) ) {
310 return;
311 }
312
313 # Filter categories
314 $cats = array () ;
315 foreach ( $categories AS $cat ) {
316 $cat = trim ( $cat ) ;
317 if ( $cat == "" ) continue ;
318 $cats[] = $cat ;
319 }
320
321 # Filter articles
322 $articles = array () ;
323 $a2r = array () ;
324 foreach ( $rows AS $k => $r ) {
325 $nt = Title::makeTitle( $r->rc_namespace, $r->rc_title );
326 $id = $nt->getArticleID() ;
327 if ( $id == 0 ) continue ; # Page might have been deleted...
328 if ( !in_array ( $id , $articles ) ) {
329 $articles[] = $id ;
330 }
331 if ( !isset ( $a2r[$id] ) ) {
332 $a2r[$id] = array() ;
333 }
334 $a2r[$id][] = $k ;
335 }
336
337 # Shortcut?
338 if ( count ( $articles ) == 0 OR count ( $cats ) == 0 )
339 return ;
340
341 # Look up
342 $c = new Categoryfinder ;
343 $c->seed ( $articles , $cats , $any ? "OR" : "AND" ) ;
344 $match = $c->run () ;
345
346 # Filter
347 $newrows = array () ;
348 foreach ( $match AS $id ) {
349 foreach ( $a2r[$id] AS $rev ) {
350 $k = $rev ;
351 $newrows[$k] = $rows[$k] ;
352 }
353 }
354 $rows = $newrows ;
355 }
356
357 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
358 global $messageMemc, $wgFeedCacheTimeout;
359 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
360 global $wgFeed;
361
362 if ( !$wgFeed ) {
363 global $wgOut;
364 $wgOut->addWikiMsg( 'feed-unavailable' );
365 return;
366 }
367
368 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
369 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
370 return false;
371 }
372
373 $timekey = wfMemcKey( 'rcfeed', $feedFormat, 'timestamp' );
374 $key = wfMemcKey( 'rcfeed', $feedFormat, 'limit', $limit, 'minor', $hideminor );
375
376 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
377 ' [' . $wgContLanguageCode . ']';
378 $feed = new $wgFeedClasses[$feedFormat](
379 $feedTitle,
380 htmlspecialchars( wfMsgForContent( 'recentchanges-feed-description' ) ),
381 $wgTitle->getFullUrl() );
382
383 //purge cache if requested
384 global $wgRequest, $wgUser;
385 $purge = $wgRequest->getVal( 'action' ) == 'purge';
386 if ( $purge && $wgUser->isAllowed('purge') ) {
387 $messageMemc->delete( $timekey );
388 $messageMemc->delete( $key );
389 }
390
391 /**
392 * Bumping around loading up diffs can be pretty slow, so where
393 * possible we want to cache the feed output so the next visitor
394 * gets it quick too.
395 */
396 $cachedFeed = false;
397 if( ( $wgFeedCacheTimeout > 0 ) && ( $feedLastmod = $messageMemc->get( $timekey ) ) ) {
398 /**
399 * If the cached feed was rendered very recently, we may
400 * go ahead and use it even if there have been edits made
401 * since it was rendered. This keeps a swarm of requests
402 * from being too bad on a super-frequently edited wiki.
403 */
404 if( time() - wfTimestamp( TS_UNIX, $feedLastmod )
405 < $wgFeedCacheTimeout
406 || wfTimestamp( TS_UNIX, $feedLastmod )
407 > wfTimestamp( TS_UNIX, $lastmod ) ) {
408 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
409 $cachedFeed = $messageMemc->get( $key );
410 } else {
411 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
412 }
413 }
414 if( is_string( $cachedFeed ) ) {
415 wfDebug( "RC: Outputting cached feed\n" );
416 $feed->httpHeaders();
417 echo $cachedFeed;
418 } else {
419 wfDebug( "RC: rendering new feed and caching it\n" );
420 ob_start();
421 rcDoOutputFeed( $rows, $feed );
422 $cachedFeed = ob_get_contents();
423 ob_end_flush();
424
425 $expire = 3600 * 24; # One day
426 $messageMemc->set( $key, $cachedFeed );
427 $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
428 }
429 return true;
430 }
431
432 /**
433 * @todo document
434 * @param $rows Database resource with recentchanges rows
435 */
436 function rcDoOutputFeed( $rows, &$feed ) {
437 wfProfileIn( __METHOD__ );
438
439 $feed->outHeader();
440
441 # Merge adjacent edits by one user
442 $sorted = array();
443 $n = 0;
444 foreach( $rows as $obj ) {
445 if( $n > 0 &&
446 $obj->rc_namespace >= 0 &&
447 $obj->rc_cur_id == $sorted[$n-1]->rc_cur_id &&
448 $obj->rc_user_text == $sorted[$n-1]->rc_user_text ) {
449 $sorted[$n-1]->rc_last_oldid = $obj->rc_last_oldid;
450 } else {
451 $sorted[$n] = $obj;
452 $n++;
453 }
454 }
455
456 foreach( $sorted as $obj ) {
457 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
458 $talkpage = $title->getTalkPage();
459 $item = new FeedItem(
460 $title->getPrefixedText(),
461 rcFormatDiff( $obj ),
462 $title->getFullURL( 'diff=' . $obj->rc_this_oldid . '&oldid=prev' ),
463 $obj->rc_timestamp,
464 ($obj->rc_deleted & Revision::DELETED_USER) ? wfMsgHtml('rev-deleted-user') : $obj->rc_user_text,
465 $talkpage->getFullURL()
466 );
467 $feed->outItem( $item );
468 }
469 $feed->outFooter();
470 wfProfileOut( __METHOD__ );
471 }
472
473 /**
474 *
475 */
476 function rcCountLink( $lim, $d, $page='Recentchanges', $more='', $active = false ) {
477 global $wgUser, $wgLang, $wgContLang;
478 $sk = $wgUser->getSkin();
479 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
480 ($lim ? $wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
481 ($d ? "days={$d}&" : '') . 'limit='.$lim, '', '',
482 $active ? 'style="font-weight: bold;"' : '' );
483 return $s;
484 }
485
486 /**
487 *
488 */
489 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='', $active = false ) {
490 global $wgUser, $wgLang, $wgContLang;
491 $sk = $wgUser->getSkin();
492 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
493 ($d ? $wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
494 ($lim ? '&limit='.$lim : ''), '', '',
495 $active ? 'style="font-weight: bold;"' : '' );
496 return $s;
497 }
498
499 /**
500 * Used by Recentchangeslinked
501 */
502 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
503 $botLink = '', $liuLink = '', $patrLink = '', $myselfLink = '' ) {
504 global $wgRCLinkLimits, $wgRCLinkDays;
505 if ($more != '') $more .= '&';
506
507 # Sort data for display and make sure it's unique after we've added user data.
508 # FIXME: why does this piss around with globals like this? Why is $limit added on globally?
509 $wgRCLinkLimits[] = $limit;
510 $wgRCLinkDays[] = $days;
511 sort($wgRCLinkLimits);
512 sort($wgRCLinkDays);
513 $wgRCLinkLimits = array_unique($wgRCLinkLimits);
514 $wgRCLinkDays = array_unique($wgRCLinkDays);
515
516 $cl = array();
517 foreach( $wgRCLinkLimits as $countLink ) {
518 $cl[] = rcCountLink( $countLink, $days, $page, $more, $countLink == $limit );
519 }
520 if( $doall ) $cl[] = rcCountLink( 0, $days, $page, $more );
521 $cl = implode( ' | ', $cl);
522
523 $dl = array();
524 foreach( $wgRCLinkDays as $daysLink ) {
525 $dl[] = rcDaysLink( $limit, $daysLink, $page, $more, $daysLink == $days );
526 }
527 if( $doall ) $dl[] = rcDaysLink( $limit, 0, $page, $more );
528 $dl = implode( ' | ', $dl);
529
530 $linkParts = array( 'minorLink' => 'minor', 'botLink' => 'bots', 'liuLink' => 'liu', 'patrLink' => 'patr', 'myselfLink' => 'mine' );
531 foreach( $linkParts as $linkVar => $linkMsg ) {
532 if( $$linkVar != '' )
533 $links[] = wfMsgHtml( 'rcshowhide' . $linkMsg, $$linkVar );
534 }
535
536 $shm = implode( ' | ', $links );
537 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
538 return $note;
539 }
540
541
542 /**
543 * Makes change an option link which carries all the other options
544 * @param $title see Title
545 * @param $override
546 * @param $options
547 */
548 function makeOptionsLink( $title, $override, $options, $active = false ) {
549 global $wgUser, $wgContLang;
550 $sk = $wgUser->getSkin();
551 return $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
552 htmlspecialchars( $title ), wfArrayToCGI( $override, $options ), '', '',
553 $active ? 'style="font-weight: bold;"' : '' );
554 }
555
556 /**
557 * Creates the options panel.
558 * @param $defaults
559 * @param $nondefaults
560 */
561 function rcOptionsPanel( $defaults, $nondefaults ) {
562 global $wgLang, $wgUser, $wgRCLinkLimits, $wgRCLinkDays;
563
564 $options = $nondefaults + $defaults;
565
566 if( $options['from'] )
567 $note = wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
568 $wgLang->formatNum( $options['limit'] ),
569 $wgLang->timeanddate( $options['from'], true ) );
570 else
571 $note = wfMsgExt( 'rcnote', array( 'parseinline' ),
572 $wgLang->formatNum( $options['limit'] ),
573 $wgLang->formatNum( $options['days'] ),
574 $wgLang->timeAndDate( wfTimestampNow(), true ) );
575
576 # Sort data for display and make sure it's unique after we've added user data.
577 $wgRCLinkLimits[] = $options['limit'];
578 $wgRCLinkDays[] = $options['days'];
579 sort($wgRCLinkLimits);
580 sort($wgRCLinkDays);
581 $wgRCLinkLimits = array_unique($wgRCLinkLimits);
582 $wgRCLinkDays = array_unique($wgRCLinkDays);
583
584 // limit links
585 foreach( $wgRCLinkLimits as $value ) {
586 $cl[] = makeOptionsLink( $wgLang->formatNum( $value ),
587 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] ) ;
588 }
589 $cl = implode( ' | ', $cl);
590
591 // day links, reset 'from' to none
592 foreach( $wgRCLinkDays as $value ) {
593 $dl[] = makeOptionsLink( $wgLang->formatNum( $value ),
594 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] ) ;
595 }
596 $dl = implode( ' | ', $dl);
597
598
599 // show/hide links
600 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
601 $minorLink = makeOptionsLink( $showhide[1-$options['hideminor']],
602 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
603 $botLink = makeOptionsLink( $showhide[1-$options['hidebots']],
604 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
605 $anonsLink = makeOptionsLink( $showhide[ 1 - $options['hideanons'] ],
606 array( 'hideanons' => 1 - $options['hideanons'] ), $nondefaults );
607 $liuLink = makeOptionsLink( $showhide[1-$options['hideliu']],
608 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
609 $patrLink = makeOptionsLink( $showhide[1-$options['hidepatrolled']],
610 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
611 $myselfLink = makeOptionsLink( $showhide[1-$options['hidemyself']],
612 array( 'hidemyself' => 1-$options['hidemyself'] ), $nondefaults);
613
614 $links[] = wfMsgHtml( 'rcshowhideminor', $minorLink );
615 $links[] = wfMsgHtml( 'rcshowhidebots', $botLink );
616 $links[] = wfMsgHtml( 'rcshowhideanons', $anonsLink );
617 $links[] = wfMsgHtml( 'rcshowhideliu', $liuLink );
618 if( $wgUser->useRCPatrol() )
619 $links[] = wfMsgHtml( 'rcshowhidepatr', $patrLink );
620 $links[] = wfMsgHtml( 'rcshowhidemine', $myselfLink );
621 $hl = implode( ' | ', $links );
622
623 // show from this onward link
624 $now = $wgLang->timeanddate( wfTimestampNow(), true );
625 $tl = makeOptionsLink( $now, array( 'from' => wfTimestampNow()), $nondefaults );
626
627 $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter'),
628 $cl, $dl, $hl );
629 $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter'), $tl );
630 return "$note<br />$rclinks<br />$rclistfrom";
631
632 }
633
634 /**
635 * Creates the choose namespace selection
636 *
637 * @private
638 *
639 * @param $namespace Mixed: the key of the currently selected namespace, empty string
640 * if there is none
641 * @param $invert Bool: whether to invert the namespace selection
642 * @param $nondefaults Array: an array of non default options to be remembered
643 * @param $categories_any Bool: Default value for the checkbox
644 *
645 * @return string
646 */
647 function rcNamespaceForm( $namespace, $invert, $nondefaults, $categories_any ) {
648 global $wgScript, $wgAllowCategorizedRecentChanges, $wgRequest;
649 $t = SpecialPage::getTitleFor( 'Recentchanges' );
650
651 $namespaceselect = HTMLnamespaceselector($namespace, '');
652 $submitbutton = '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . "\" />\n";
653 $invertbox = "<input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ? ' checked="checked"' : '' ) . ' />';
654
655 if ( $wgAllowCategorizedRecentChanges ) {
656 $categories = trim ( $wgRequest->getVal ( 'categories' , "" ) ) ;
657 $cb_arr = array( 'type' => 'checkbox', 'name' => 'categories_any', 'value' => "1" ) ;
658 if ( $categories_any ) $cb_arr['checked'] = "checked" ;
659 $catbox = "<br />" ;
660 $catbox .= wfMsgExt('rc_categories', array('parseinline')) . " ";
661 $catbox .= wfElement('input', array( 'type' => 'text', 'name' => 'categories', 'value' => $categories));
662 $catbox .= " &nbsp;" ;
663 $catbox .= wfElement('input', $cb_arr );
664 $catbox .= wfMsgExt('rc_categories_any', array('parseinline'));
665 } else {
666 $catbox = "" ;
667 }
668
669 $out = "<div class='namespacesettings'><form method='get' action='{$wgScript}'>\n";
670
671 foreach ( $nondefaults as $key => $value ) {
672 if ($key != 'namespace' && $key != 'invert')
673 $out .= wfElement('input', array( 'type' => 'hidden', 'name' => $key, 'value' => $value));
674 }
675
676 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
677 $out .= "
678 <div id='nsselect' class='recentchanges'>
679 <label for='namespace'>" . wfMsgHtml('namespace') . "</label>
680 {$namespaceselect}{$submitbutton}{$invertbox} <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>{$catbox}\n</div>";
681 $out .= '</form></div>';
682 return $out;
683 }
684
685
686 /**
687 * Format a diff for the newsfeed
688 */
689 function rcFormatDiff( $row ) {
690 global $wgUser;
691
692 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
693 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
694 $actiontext = '';
695 if( $row->rc_type == RC_LOG ) {
696 if( $row->rc_deleted & LogPage::DELETED_ACTION ) {
697 $actiontext = wfMsgHtml('rev-deleted-event');
698 } else {
699 $actiontext = LogPage::actionText( $row->rc_log_type, $row->rc_log_action,
700 $titleObj, $wgUser->getSkin(), LogPage::extractParams($row->rc_params,true,true) );
701 }
702 }
703 return rcFormatDiffRow( $titleObj,
704 $row->rc_last_oldid, $row->rc_this_oldid,
705 $timestamp,
706 ($row->rc_deleted & Revision::DELETED_COMMENT) ? wfMsgHtml('rev-deleted-comment') : $row->rc_comment,
707 $actiontext );
708 }
709
710 function rcFormatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='' ) {
711 global $wgFeedDiffCutoff, $wgContLang, $wgUser;
712 wfProfileIn( __FUNCTION__ );
713
714 $skin = $wgUser->getSkin();
715 # log enties
716 if( $actiontext ) {
717 $comment = "$actiontext $comment";
718 }
719 $completeText = '<p>' . $skin->formatComment( $comment ) . "</p>\n";
720
721 //NOTE: Check permissions for anonymous users, not current user.
722 // No "privileged" version should end up in the cache.
723 // Most feed readers will not log in anway.
724 $anon = new User();
725 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
726
727 if( $title->getNamespace() >= 0 && !$accErrors ) {
728 if( $oldid ) {
729 wfProfileIn( __FUNCTION__."-dodiff" );
730
731 $de = new DifferenceEngine( $title, $oldid, $newid );
732 #$diffText = $de->getDiff( wfMsg( 'revisionasof',
733 # $wgContLang->timeanddate( $timestamp ) ),
734 # wfMsg( 'currentrev' ) );
735 $diffText = $de->getDiff(
736 wfMsg( 'previousrevision' ), // hack
737 wfMsg( 'revisionasof',
738 $wgContLang->timeanddate( $timestamp ) ) );
739
740
741 if ( strlen( $diffText ) > $wgFeedDiffCutoff ) {
742 // Omit large diffs
743 $diffLink = $title->escapeFullUrl(
744 'diff=' . $newid .
745 '&oldid=' . $oldid );
746 $diffText = '<a href="' .
747 $diffLink .
748 '">' .
749 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
750 '</a>';
751 } elseif ( $diffText === false ) {
752 // Error in diff engine, probably a missing revision
753 $diffText = "<p>Can't load revision $newid</p>";
754 } else {
755 // Diff output fine, clean up any illegal UTF-8
756 $diffText = UtfNormal::cleanUp( $diffText );
757 $diffText = rcApplyDiffStyle( $diffText );
758 }
759 wfProfileOut( __FUNCTION__."-dodiff" );
760 } else {
761 $rev = Revision::newFromId( $newid );
762 if( is_null( $rev ) ) {
763 $newtext = '';
764 } else {
765 $newtext = $rev->getText();
766 }
767 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
768 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
769 }
770 $completeText .= $diffText;
771 }
772
773 wfProfileOut( __FUNCTION__ );
774 return $completeText;
775 }
776
777 /**
778 * Hacky application of diff styles for the feeds.
779 * Might be 'cleaner' to use DOM or XSLT or something,
780 * but *gack* it's a pain in the ass.
781 *
782 * @param $text String:
783 * @return string
784 * @private
785 */
786 function rcApplyDiffStyle( $text ) {
787 $styles = array(
788 'diff' => 'background-color: white; color:black;',
789 'diff-otitle' => 'background-color: white; color:black;',
790 'diff-ntitle' => 'background-color: white; color:black;',
791 'diff-addedline' => 'background: #cfc; color:black; font-size: smaller;',
792 'diff-deletedline' => 'background: #ffa; color:black; font-size: smaller;',
793 'diff-context' => 'background: #eee; color:black; font-size: smaller;',
794 'diffchange' => 'color: red; font-weight: bold; text-decoration: none;',
795 );
796
797 foreach( $styles as $class => $style ) {
798 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
799 "\\1style=\"$style\"\\3", $text );
800 }
801
802 return $text;
803 }