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