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