Use AutoLoader to load classes:
[lhc/web/wiklou.git] / includes / SpecialRecentchanges.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 * Constructor
10 */
11 function wfSpecialRecentchanges( $par, $specialPage ) {
12 global $wgUser, $wgOut, $wgRequest, $wgUseRCPatrol, $wgDBtype;
13 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
14 global $wgAllowCategorizedRecentChanges ;
15 $fname = 'wfSpecialRecentchanges';
16
17 # Get query parameters
18 $feedFormat = $wgRequest->getVal( 'feed' );
19
20 /* Checkbox values can't be true be default, because
21 * we cannot differentiate between unset and not set at all
22 */
23 $defaults = array(
24 /* int */ 'days' => $wgUser->getDefaultOption('rcdays'),
25 /* int */ 'limit' => $wgUser->getDefaultOption('rclimit'),
26 /* bool */ 'hideminor' => false,
27 /* bool */ 'hidebots' => true,
28 /* bool */ 'hideanons' => false,
29 /* bool */ 'hideliu' => false,
30 /* bool */ 'hidepatrolled' => false,
31 /* bool */ 'hidemyself' => false,
32 /* text */ 'from' => '',
33 /* text */ 'namespace' => null,
34 /* bool */ 'invert' => false,
35 /* bool */ 'categories_any' => false,
36 );
37
38 extract($defaults);
39
40
41 $days = $wgUser->getOption( 'rcdays' );
42 if ( !$days ) { $days = $defaults['days']; }
43 $days = $wgRequest->getInt( 'days', $days );
44
45 $limit = $wgUser->getOption( 'rclimit' );
46 if ( !$limit ) { $limit = $defaults['limit']; }
47
48 # list( $limit, $offset ) = wfCheckLimits( 100, 'rclimit' );
49 $limit = $wgRequest->getInt( 'limit', $limit );
50
51 /* order of selection: url > preferences > default */
52 $hideminor = $wgRequest->getBool( 'hideminor', $wgUser->getOption( 'hideminor') ? true : $defaults['hideminor'] );
53
54 # As a feed, use limited settings only
55 if( $feedFormat ) {
56 global $wgFeedLimit;
57 if( $limit > $wgFeedLimit ) {
58 $options['limit'] = $wgFeedLimit;
59 }
60
61 } else {
62
63 $namespace = $wgRequest->getIntOrNull( 'namespace' );
64 $invert = $wgRequest->getBool( 'invert', $defaults['invert'] );
65 $hidebots = $wgRequest->getBool( 'hidebots', $defaults['hidebots'] );
66 $hideanons = $wgRequest->getBool( 'hideanons', $defaults['hideanons'] );
67 $hideliu = $wgRequest->getBool( 'hideliu', $defaults['hideliu'] );
68 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled', $defaults['hidepatrolled'] );
69 $hidemyself = $wgRequest->getBool ( 'hidemyself', $defaults['hidemyself'] );
70 $from = $wgRequest->getVal( 'from', $defaults['from'] );
71
72 # Get query parameters from path
73 if( $par ) {
74 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
75 foreach ( $bits as $bit ) {
76 if ( 'hidebots' == $bit ) $hidebots = 1;
77 if ( 'bots' == $bit ) $hidebots = 0;
78 if ( 'hideminor' == $bit ) $hideminor = 1;
79 if ( 'minor' == $bit ) $hideminor = 0;
80 if ( 'hideliu' == $bit ) $hideliu = 1;
81 if ( 'hidepatrolled' == $bit ) $hidepatrolled = 1;
82 if ( 'hideanons' == $bit ) $hideanons = 1;
83 if ( 'hidemyself' == $bit ) $hidemyself = 1;
84
85 if ( is_numeric( $bit ) ) {
86 $limit = $bit;
87 }
88
89 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
90 $limit = $m[1];
91 }
92
93 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
94 $days = $m[1];
95 }
96 }
97 }
98 }
99
100 if ( $limit < 0 || $limit > 5000 ) $limit = $defaults['limit'];
101
102
103 # Database connection and caching
104 $dbr =& wfGetDB( DB_SLAVE );
105 extract( $dbr->tableNames( 'recentchanges', 'watchlist' ) );
106
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 if( $hideanons && $hideliu )
133 $hideanons = false;
134
135 # Form WHERE fragments for all the options
136 $hidem = $hideminor ? 'AND rc_minor = 0' : '';
137 $hidem .= $hidebots ? ' AND rc_bot = 0' : '';
138 $hidem .= $hideliu ? ' AND rc_user = 0' : '';
139 $hidem .= ( $wgUseRCPatrol && $hidepatrolled ) ? ' AND rc_patrolled = 0' : '';
140 $hidem .= $hideanons ? ' AND rc_user != 0' : '';
141
142 if( $hidemyself ) {
143 if( $wgUser->getID() ) {
144 $hidem .= ' AND rc_user != ' . $wgUser->getID();
145 } else {
146 $hidem .= ' AND rc_user_text != ' . $dbr->addQuotes( $wgUser->getName() );
147 }
148 }
149
150 # Namespace filtering
151 $hidem .= is_null( $namespace ) ? '' : ' AND rc_namespace' . ($invert ? '!=' : '=') . $namespace;
152
153 // This is the big thing!
154
155 $uid = $wgUser->getID();
156
157 // Perform query
158 $forceclause = $dbr->useIndexClause("rc_timestamp");
159 $sql2 = "SELECT * FROM $recentchanges $forceclause".
160 ($uid ? "LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "") .
161 "WHERE rc_timestamp >= '{$cutoff}' {$hidem} " .
162 "ORDER BY rc_timestamp DESC";
163 $sql2 = $dbr->limitResult($sql2, $limit, 0);
164 $res = $dbr->query( $sql2, $fname );
165
166 // Fetch results, prepare a batch link existence check query
167 $rows = array();
168 $batch = new LinkBatch;
169 while( $row = $dbr->fetchObject( $res ) ){
170 $rows[] = $row;
171 if ( !$feedFormat ) {
172 // User page link
173 $title = Title::makeTitleSafe( NS_USER, $row->rc_user_text );
174 $batch->addObj( $title );
175
176 // User talk
177 $title = Title::makeTitleSafe( NS_USER_TALK, $row->rc_user_text );
178 $batch->addObj( $title );
179 }
180
181 }
182 $dbr->freeResult( $res );
183
184 if( $feedFormat ) {
185 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
186 } else {
187
188 # Web output...
189
190 // Run existence checks
191 $batch->execute();
192 $any = $wgRequest->getBool( 'categories_any', $defaults['categories_any']);
193
194 // Output header
195 if ( !$specialPage->including() ) {
196 $wgOut->addWikiText( wfMsgForContent( "recentchangestext" ) );
197
198 // Dump everything here
199 $nondefaults = array();
200
201 wfAppendToArrayIfNotDefault( 'days', $days, $defaults, $nondefaults);
202 wfAppendToArrayIfNotDefault( 'limit', $limit , $defaults, $nondefaults);
203 wfAppendToArrayIfNotDefault( 'hideminor', $hideminor, $defaults, $nondefaults);
204 wfAppendToArrayIfNotDefault( 'hidebots', $hidebots, $defaults, $nondefaults);
205 wfAppendToArrayIfNotDefault( 'hideanons', $hideanons, $defaults, $nondefaults );
206 wfAppendToArrayIfNotDefault( 'hideliu', $hideliu, $defaults, $nondefaults);
207 wfAppendToArrayIfNotDefault( 'hidepatrolled', $hidepatrolled, $defaults, $nondefaults);
208 wfAppendToArrayIfNotDefault( 'hidemyself', $hidemyself, $defaults, $nondefaults);
209 wfAppendToArrayIfNotDefault( 'from', $from, $defaults, $nondefaults);
210 wfAppendToArrayIfNotDefault( 'namespace', $namespace, $defaults, $nondefaults);
211 wfAppendToArrayIfNotDefault( 'invert', $invert, $defaults, $nondefaults);
212 wfAppendToArrayIfNotDefault( 'categories_any', $any, $defaults, $nondefaults);
213
214 // Add end of the texts
215 $wgOut->addHTML( '<div class="rcoptions">' . rcOptionsPanel( $defaults, $nondefaults ) . "\n" );
216 $wgOut->addHTML( rcNamespaceForm( $namespace, $invert, $nondefaults, $any ) . '</div>'."\n");
217 }
218
219 // And now for the content
220 $sk = $wgUser->getSkin();
221 $wgOut->setSyndicated( true );
222
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 require_once ( 'Categoryfinder.php' ) ;
271
272 # Filter categories
273 $cats = array () ;
274 foreach ( $categories AS $cat ) {
275 $cat = trim ( $cat ) ;
276 if ( $cat == "" ) continue ;
277 $cats[] = $cat ;
278 }
279
280 # Filter articles
281 $articles = array () ;
282 $a2r = array () ;
283 foreach ( $rows AS $k => $r ) {
284 $nt = Title::newFromText ( $r->rc_title , $r->rc_namespace ) ;
285 $id = $nt->getArticleID() ;
286 if ( $id == 0 ) continue ; # Page might have been deleted...
287 if ( !in_array ( $id , $articles ) ) {
288 $articles[] = $id ;
289 }
290 if ( !isset ( $a2r[$id] ) ) {
291 $a2r[$id] = array() ;
292 }
293 $a2r[$id][] = $k ;
294 }
295
296 # Shortcut?
297 if ( count ( $articles ) == 0 OR count ( $cats ) == 0 )
298 return ;
299
300 # Look up
301 $c = new Categoryfinder ;
302 $c->seed ( $articles , $cats , $any ? "OR" : "AND" ) ;
303 $match = $c->run () ;
304
305 # Filter
306 $newrows = array () ;
307 foreach ( $match AS $id ) {
308 foreach ( $a2r[$id] AS $rev ) {
309 $k = $rev ;
310 $newrows[$k] = $rows[$k] ;
311 }
312 }
313 $rows = $newrows ;
314 }
315
316 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
317 global $messageMemc, $wgDBname, $wgFeedCacheTimeout;
318 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
319
320 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
321 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
322 return false;
323 }
324
325 $timekey = "$wgDBname:rcfeed:$feedFormat:timestamp";
326 $key = "$wgDBname:rcfeed:$feedFormat:limit:$limit:minor:$hideminor";
327
328 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
329 ' [' . $wgContLanguageCode . ']';
330 $feed = new $wgFeedClasses[$feedFormat](
331 $feedTitle,
332 htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
333 $wgTitle->getFullUrl() );
334
335 /**
336 * Bumping around loading up diffs can be pretty slow, so where
337 * possible we want to cache the feed output so the next visitor
338 * gets it quick too.
339 */
340 $cachedFeed = false;
341 if( ( $wgFeedCacheTimeout > 0 ) && ( $feedLastmod = $messageMemc->get( $timekey ) ) ) {
342 /**
343 * If the cached feed was rendered very recently, we may
344 * go ahead and use it even if there have been edits made
345 * since it was rendered. This keeps a swarm of requests
346 * from being too bad on a super-frequently edited wiki.
347 */
348 if( time() - wfTimestamp( TS_UNIX, $feedLastmod )
349 < $wgFeedCacheTimeout
350 || wfTimestamp( TS_UNIX, $feedLastmod )
351 > wfTimestamp( TS_UNIX, $lastmod ) ) {
352 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
353 $cachedFeed = $messageMemc->get( $key );
354 } else {
355 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
356 }
357 }
358 if( is_string( $cachedFeed ) ) {
359 wfDebug( "RC: Outputting cached feed\n" );
360 $feed->httpHeaders();
361 echo $cachedFeed;
362 } else {
363 wfDebug( "RC: rendering new feed and caching it\n" );
364 ob_start();
365 rcDoOutputFeed( $rows, $feed );
366 $cachedFeed = ob_get_contents();
367 ob_end_flush();
368
369 $expire = 3600 * 24; # One day
370 $messageMemc->set( $key, $cachedFeed );
371 $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
372 }
373 return true;
374 }
375
376 function rcDoOutputFeed( $rows, &$feed ) {
377 $fname = 'rcDoOutputFeed';
378 wfProfileIn( $fname );
379
380 $feed->outHeader();
381
382 # Merge adjacent edits by one user
383 $sorted = array();
384 $n = 0;
385 foreach( $rows as $obj ) {
386 if( $n > 0 &&
387 $obj->rc_namespace >= 0 &&
388 $obj->rc_cur_id == $sorted[$n-1]->rc_cur_id &&
389 $obj->rc_user_text == $sorted[$n-1]->rc_user_text ) {
390 $sorted[$n-1]->rc_last_oldid = $obj->rc_last_oldid;
391 } else {
392 $sorted[$n] = $obj;
393 $n++;
394 }
395 $first = false;
396 }
397
398 foreach( $sorted as $obj ) {
399 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
400 $talkpage = $title->getTalkPage();
401 $item = new FeedItem(
402 $title->getPrefixedText(),
403 rcFormatDiff( $obj ),
404 $title->getFullURL(),
405 $obj->rc_timestamp,
406 $obj->rc_user_text,
407 $talkpage->getFullURL()
408 );
409 $feed->outItem( $item );
410 }
411 $feed->outFooter();
412 wfProfileOut( $fname );
413 }
414
415 /**
416 *
417 */
418 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
419 global $wgUser, $wgLang, $wgContLang;
420 $sk = $wgUser->getSkin();
421 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
422 ($lim ? $wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
423 ($d ? "days={$d}&" : '') . 'limit='.$lim );
424 return $s;
425 }
426
427 /**
428 *
429 */
430 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
431 global $wgUser, $wgLang, $wgContLang;
432 $sk = $wgUser->getSkin();
433 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
434 ($d ? $wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
435 ($lim ? '&limit='.$lim : '') );
436 return $s;
437 }
438
439 /**
440 * Used by Recentchangeslinked
441 */
442 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
443 $botLink = '', $liuLink = '', $patrLink = '', $myselfLink = '' ) {
444 if ($more != '') $more .= '&';
445 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
446 rcCountLink( 100, $days, $page, $more ) . ' | ' .
447 rcCountLink( 250, $days, $page, $more ) . ' | ' .
448 rcCountLink( 500, $days, $page, $more ) .
449 ( $doall ? ( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
450 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
451 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
452 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
453 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
454 rcDaysLink( $limit, 30, $page, $more ) .
455 ( $doall ? ( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
456
457 $linkParts = array( 'minorLink' => 'minor', 'botLink' => 'bots', 'liuLink' => 'liu', 'patrLink' => 'patr', 'myselfLink' => 'mine' );
458 foreach( $linkParts as $linkVar => $linkMsg ) {
459 if( $$linkVar != '' )
460 $links[] = wfMsgHtml( 'rcshowhide' . $linkMsg, $$linkVar );
461 }
462
463 $shm = implode( ' | ', $links );
464 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
465 return $note;
466 }
467
468
469 /**
470 * Makes change an option link which carries all the other options
471 * @param $title @see Title
472 * @param $override
473 * @param $options
474 */
475 function makeOptionsLink( $title, $override, $options ) {
476 global $wgUser, $wgContLang;
477 $sk = $wgUser->getSkin();
478 return $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
479 $title, wfArrayToCGI( $override, $options ) );
480 }
481
482 /**
483 * Creates the options panel.
484 * @param $defaults
485 * @param $nondefaults
486 */
487 function rcOptionsPanel( $defaults, $nondefaults ) {
488 global $wgLang, $wgUseRCPatrol;
489
490 $options = $nondefaults + $defaults;
491
492 if( $options['from'] )
493 $note = wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
494 $wgLang->formatNum( $options['limit'] ),
495 $wgLang->timeanddate( $options['from'], true ) );
496 else
497 $note = wfMsgExt( 'rcnote', array( 'parseinline' ),
498 $wgLang->formatNum( $options['limit'] ),
499 $wgLang->formatNum( $options['days'] ),
500 $wgLang->timeAndDate( wfTimestampNow(), true ) );
501
502 // limit links
503 $options_limit = array(50, 100, 250, 500);
504 foreach( $options_limit as $value ) {
505 $cl[] = makeOptionsLink( $wgLang->formatNum( $value ),
506 array( 'limit' => $value ), $nondefaults) ;
507 }
508 $cl = implode( ' | ', $cl);
509
510 // day links, reset 'from' to none
511 $options_days = array(1, 3, 7, 14, 30);
512 foreach( $options_days as $value ) {
513 $dl[] = makeOptionsLink( $wgLang->formatNum( $value ),
514 array( 'days' => $value, 'from' => '' ), $nondefaults) ;
515 }
516 $dl = implode( ' | ', $dl);
517
518
519 // show/hide links
520 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
521 $minorLink = makeOptionsLink( $showhide[1-$options['hideminor']],
522 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
523 $botLink = makeOptionsLink( $showhide[1-$options['hidebots']],
524 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
525 $anonsLink = makeOptionsLink( $showhide[ 1 - $options['hideanons'] ],
526 array( 'hideanons' => 1 - $options['hideanons'] ), $nondefaults );
527 $liuLink = makeOptionsLink( $showhide[1-$options['hideliu']],
528 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
529 $patrLink = makeOptionsLink( $showhide[1-$options['hidepatrolled']],
530 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
531 $myselfLink = makeOptionsLink( $showhide[1-$options['hidemyself']],
532 array( 'hidemyself' => 1-$options['hidemyself'] ), $nondefaults);
533
534 $links[] = wfMsgHtml( 'rcshowhideminor', $minorLink );
535 $links[] = wfMsgHtml( 'rcshowhidebots', $botLink );
536 $links[] = wfMsgHtml( 'rcshowhideanons', $anonsLink );
537 $links[] = wfMsgHtml( 'rcshowhideliu', $liuLink );
538 if( $wgUseRCPatrol )
539 $links[] = wfMsgHtml( 'rcshowhidepatr', $patrLink );
540 $links[] = wfMsgHtml( 'rcshowhidemine', $myselfLink );
541 $hl = implode( ' | ', $links );
542
543 // show from this onward link
544 $now = $wgLang->timeanddate( wfTimestampNow(), true );
545 $tl = makeOptionsLink( $now, array( 'from' => wfTimestampNow()), $nondefaults );
546
547 $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter'),
548 $cl, $dl, $hl );
549 $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter'), $tl );
550 return "$note<br />$rclinks<br />$rclistfrom";
551
552 }
553
554 /**
555 * Creates the choose namespace selection
556 *
557 * @private
558 *
559 * @param $namespace Mixed: the key of the currently selected namespace, empty string
560 * if there is none
561 * @param $invert Bool: whether to invert the namespace selection
562 * @param $nondefaults Array: an array of non default options to be remembered
563 * @param $categories_any Bool: Default value for the checkbox
564 *
565 * @return string
566 */
567 function rcNamespaceForm( $namespace, $invert, $nondefaults, $categories_any ) {
568 global $wgScript, $wgAllowCategorizedRecentChanges, $wgRequest;
569 $t = Title::makeTitle( NS_SPECIAL, 'Recentchanges' );
570
571 $namespaceselect = HTMLnamespaceselector($namespace, '');
572 $submitbutton = '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . "\" />\n";
573 $invertbox = "<input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ? ' checked="checked"' : '' ) . ' />';
574
575 if ( $wgAllowCategorizedRecentChanges ) {
576 $categories = trim ( $wgRequest->getVal ( 'categories' , "" ) ) ;
577 $cb_arr = array( 'type' => 'checkbox', 'name' => 'categories_any', 'value' => "1" ) ;
578 if ( $categories_any ) $cb_arr['checked'] = "checked" ;
579 $catbox = "<br/>" ;
580 $catbox .= wfMsgExt('rc_categories', array('parseinline')) . " ";
581 $catbox .= wfElement('input', array( 'type' => 'text', 'name' => 'categories', 'value' => $categories));
582 $catbox .= " &nbsp;" ;
583 $catbox .= wfElement('input', $cb_arr );
584 $catbox .= wfMsgExt('rc_categories_any', array('parseinline'));
585 } else {
586 $catbox = "" ;
587 }
588
589 $out = "<div class='namespacesettings'><form method='get' action='{$wgScript}'>\n";
590
591 foreach ( $nondefaults as $key => $value ) {
592 if ($key != 'namespace' && $key != 'invert')
593 $out .= wfElement('input', array( 'type' => 'hidden', 'name' => $key, 'value' => $value));
594 }
595
596 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
597 $out .= "
598 <div id='nsselect' class='recentchanges'>
599 <label for='namespace'>" . wfMsgHtml('namespace') . "</label>
600 {$namespaceselect}{$submitbutton}{$invertbox} <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>{$catbox}\n</div>";
601 $out .= '</form></div>';
602 return $out;
603 }
604
605
606 /**
607 * Format a diff for the newsfeed
608 */
609 function rcFormatDiff( $row ) {
610 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
611 return rcFormatDiffRow( $titleObj,
612 $row->rc_last_oldid, $row->rc_this_oldid,
613 $row->rc_timestamp,
614 $row->rc_comment );
615 }
616
617 function rcFormatDiffRow( $title, $oldid, $newid, $timestamp, $comment ) {
618 global $wgFeedDiffCutoff, $wgContLang, $wgUser;
619 $fname = 'rcFormatDiff';
620 wfProfileIn( $fname );
621
622 require_once( 'DifferenceEngine.php' );
623 $skin = $wgUser->getSkin();
624 $completeText = '<p>' . $skin->formatComment( $comment ) . "</p>\n";
625
626 if( $title->getNamespace() >= 0 ) {
627 if( $oldid ) {
628 wfProfileIn( "$fname-dodiff" );
629
630 $de = new DifferenceEngine( $title, $oldid, $newid );
631 #$diffText = $de->getDiff( wfMsg( 'revisionasof',
632 # $wgContLang->timeanddate( $timestamp ) ),
633 # wfMsg( 'currentrev' ) );
634 $diffText = $de->getDiff(
635 wfMsg( 'previousrevision' ), // hack
636 wfMsg( 'revisionasof',
637 $wgContLang->timeanddate( $timestamp ) ) );
638
639
640 if ( strlen( $diffText ) > $wgFeedDiffCutoff ) {
641 // Omit large diffs
642 $diffLink = $title->escapeFullUrl(
643 'diff=' . $newid .
644 '&oldid=' . $oldid );
645 $diffText = '<a href="' .
646 $diffLink .
647 '">' .
648 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
649 '</a>';
650 } elseif ( $diffText === false ) {
651 // Error in diff engine, probably a missing revision
652 $diffText = "<p>Can't load revision $newid</p>";
653 } else {
654 // Diff output fine, clean up any illegal UTF-8
655 $diffText = UtfNormal::cleanUp( $diffText );
656 $diffText = rcApplyDiffStyle( $diffText );
657 }
658 wfProfileOut( "$fname-dodiff" );
659 } else {
660 $rev = Revision::newFromId( $newid );
661 if( is_null( $rev ) ) {
662 $newtext = '';
663 } else {
664 $newtext = $rev->getText();
665 }
666 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
667 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
668 }
669 $completeText .= $diffText;
670 }
671
672 wfProfileOut( $fname );
673 return $completeText;
674 }
675
676 /**
677 * Hacky application of diff styles for the feeds.
678 * Might be 'cleaner' to use DOM or XSLT or something,
679 * but *gack* it's a pain in the ass.
680 *
681 * @param $text String:
682 * @return string
683 * @private
684 */
685 function rcApplyDiffStyle( $text ) {
686 $styles = array(
687 'diff' => 'background-color: white;',
688 'diff-otitle' => 'background-color: white;',
689 'diff-ntitle' => 'background-color: white;',
690 'diff-addedline' => 'background: #cfc; font-size: smaller;',
691 'diff-deletedline' => 'background: #ffa; font-size: smaller;',
692 'diff-context' => 'background: #eee; font-size: smaller;',
693 'diffchange' => 'color: red; font-weight: bold;',
694 );
695
696 foreach( $styles as $class => $style ) {
697 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
698 "\\1style=\"$style\"\\3", $text );
699 }
700
701 return $text;
702 }
703
704 ?>