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