95e9ee5e4ae85d19624014b3bf92217bc23d31bf
[lhc/web/wiklou.git] / includes / SpecialRecentchanges.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 *
10 */
11 require_once( 'ChangesList.php' );
12
13 /**
14 * Constructor
15 */
16 function wfSpecialRecentchanges( $par, $specialPage ) {
17 global $wgUser, $wgOut, $wgRequest, $wgUseRCPatrol, $wgDBtype;
18 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
19 global $wgAllowCategorizedRecentChanges ;
20 $fname = 'wfSpecialRecentchanges';
21
22 # Get query parameters
23 $feedFormat = $wgRequest->getVal( 'feed' );
24
25 /* Checkbox values can't be true be default, because
26 * we cannot differentiate between unset and not set at all
27 */
28 $defaults = array(
29 /* int */ 'days' => $wgUser->getDefaultOption('rcdays'),
30 /* int */ 'limit' => $wgUser->getDefaultOption('rclimit'),
31 /* bool */ 'hideminor' => false,
32 /* bool */ 'hidebots' => true,
33 /* bool */ 'hideanons' => false,
34 /* bool */ 'hideliu' => false,
35 /* bool */ 'hidepatrolled' => false,
36 /* bool */ 'hidemyself' => false,
37 /* text */ 'from' => '',
38 /* text */ 'namespace' => null,
39 /* bool */ 'invert' => false,
40 /* bool */ 'categories_any' => false,
41 );
42
43 extract($defaults);
44
45
46 $days = $wgUser->getOption( 'rcdays' );
47 if ( !$days ) { $days = $defaults['days']; }
48 $days = $wgRequest->getInt( 'days', $days );
49
50 $limit = $wgUser->getOption( 'rclimit' );
51 if ( !$limit ) { $limit = $defaults['limit']; }
52
53 # list( $limit, $offset ) = wfCheckLimits( 100, 'rclimit' );
54 $limit = $wgRequest->getInt( 'limit', $limit );
55
56 /* order of selection: url > preferences > default */
57 $hideminor = $wgRequest->getBool( 'hideminor', $wgUser->getOption( 'hideminor') ? true : $defaults['hideminor'] );
58
59 # As a feed, use limited settings only
60 if( $feedFormat ) {
61 global $wgFeedLimit;
62 if( $limit > $wgFeedLimit ) {
63 $options['limit'] = $wgFeedLimit;
64 }
65
66 } else {
67
68 $namespace = $wgRequest->getIntOrNull( 'namespace' );
69 $invert = $wgRequest->getBool( 'invert', $defaults['invert'] );
70 $hidebots = $wgRequest->getBool( 'hidebots', $defaults['hidebots'] );
71 $hideanons = $wgRequest->getBool( 'hideanons', $defaults['hideanons'] );
72 $hideliu = $wgRequest->getBool( 'hideliu', $defaults['hideliu'] );
73 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled', $defaults['hidepatrolled'] );
74 $hidemyself = $wgRequest->getBool ( 'hidemyself', $defaults['hidemyself'] );
75 $from = $wgRequest->getVal( 'from', $defaults['from'] );
76
77 # Get query parameters from path
78 if( $par ) {
79 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
80 foreach ( $bits as $bit ) {
81 if ( 'hidebots' == $bit ) $hidebots = 1;
82 if ( 'bots' == $bit ) $hidebots = 0;
83 if ( 'hideminor' == $bit ) $hideminor = 1;
84 if ( 'minor' == $bit ) $hideminor = 0;
85 if ( 'hideliu' == $bit ) $hideliu = 1;
86 if ( 'hidepatrolled' == $bit ) $hidepatrolled = 1;
87 if ( 'hideanons' == $bit ) $hideanons = 1;
88 if ( 'hidemyself' == $bit ) $hidemyself = 1;
89
90 if ( is_numeric( $bit ) ) {
91 $limit = $bit;
92 }
93
94 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
95 $limit = $m[1];
96 }
97
98 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
99 $days = $m[1];
100 }
101 }
102 }
103 }
104
105 if ( $limit < 0 || $limit > 5000 ) $limit = $defaults['limit'];
106
107
108 # Database connection and caching
109 $dbr =& wfGetDB( DB_SLAVE );
110 extract( $dbr->tableNames( 'recentchanges', 'watchlist' ) );
111
112
113 $cutoff_unixtime = time() - ( $days * 86400 );
114 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime % 86400);
115 $cutoff = $dbr->timestamp( $cutoff_unixtime );
116 if(preg_match('/^[0-9]{14}$/', $from) and $from > wfTimestamp(TS_MW,$cutoff)) {
117 $cutoff = $dbr->timestamp($from);
118 } else {
119 $from = $defaults['from'];
120 }
121
122 # 10 seconds server-side caching max
123 $wgOut->setSquidMaxage( 10 );
124
125 # Get last modified date, for client caching
126 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
127 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, $fname );
128 if ( $feedFormat || !$wgUseRCPatrol ) {
129 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
130 # Client cache fresh and headers sent, nothing more to do.
131 return;
132 }
133 }
134
135 # It makes no sense to hide both anons and logged-in users
136 # Where this occurs, force anons to be shown
137 if( $hideanons && $hideliu )
138 $hideanons = false;
139
140 # Form WHERE fragments for all the options
141 $hidem = $hideminor ? 'AND rc_minor = 0' : '';
142 $hidem .= $hidebots ? ' AND rc_bot = 0' : '';
143 $hidem .= $hideliu ? ' AND rc_user = 0' : '';
144 $hidem .= ( $wgUseRCPatrol && $hidepatrolled ) ? ' AND rc_patrolled = 0' : '';
145 $hidem .= $hideanons ? ' AND rc_user != 0' : '';
146
147 if( $hidemyself ) {
148 if( $wgUser->getID() ) {
149 $hidem .= ' AND rc_user != ' . $wgUser->getID();
150 } else {
151 $hidem .= ' AND rc_user_text != ' . $dbr->addQuotes( $wgUser->getName() );
152 }
153 }
154
155 # Namespace filtering
156 $hidem .= is_null( $namespace ) ? '' : ' AND rc_namespace' . ($invert ? '!=' : '=') . $namespace;
157
158 // This is the big thing!
159
160 $uid = $wgUser->getID();
161
162 // Perform query
163 $forceclause = $dbr->useIndexClause("rc_timestamp");
164 $sql2 = "SELECT * FROM $recentchanges $forceclause".
165 ($uid ? "LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "") .
166 "WHERE rc_timestamp >= '{$cutoff}' {$hidem} " .
167 "ORDER BY rc_timestamp DESC";
168 $sql2 = $dbr->limitResult($sql2, $limit, 0);
169 $res = $dbr->query( $sql2, $fname );
170
171 // Fetch results, prepare a batch link existence check query
172 $rows = array();
173 $batch = new LinkBatch;
174 while( $row = $dbr->fetchObject( $res ) ){
175 $rows[] = $row;
176 if ( !$feedFormat ) {
177 // User page link
178 $title = Title::makeTitleSafe( NS_USER, $row->rc_user_text );
179 $batch->addObj( $title );
180
181 // User talk
182 $title = Title::makeTitleSafe( NS_USER_TALK, $row->rc_user_text );
183 $batch->addObj( $title );
184 }
185
186 }
187 $dbr->freeResult( $res );
188
189 if( $feedFormat ) {
190 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
191 } else {
192
193 # Web output...
194
195 // Run existence checks
196 $batch->execute();
197 $any = $wgRequest->getBool( 'categories_any', $defaults['categories_any']);
198
199 // Output header
200 if ( !$specialPage->including() ) {
201 $wgOut->addWikiText( wfMsgForContent( "recentchangestext" ) );
202
203 // Dump everything here
204 $nondefaults = array();
205
206 wfAppendToArrayIfNotDefault( 'days', $days, $defaults, $nondefaults);
207 wfAppendToArrayIfNotDefault( 'limit', $limit , $defaults, $nondefaults);
208 wfAppendToArrayIfNotDefault( 'hideminor', $hideminor, $defaults, $nondefaults);
209 wfAppendToArrayIfNotDefault( 'hidebots', $hidebots, $defaults, $nondefaults);
210 wfAppendToArrayIfNotDefault( 'hideanons', $hideanons, $defaults, $nondefaults );
211 wfAppendToArrayIfNotDefault( 'hideliu', $hideliu, $defaults, $nondefaults);
212 wfAppendToArrayIfNotDefault( 'hidepatrolled', $hidepatrolled, $defaults, $nondefaults);
213 wfAppendToArrayIfNotDefault( 'hidemyself', $hidemyself, $defaults, $nondefaults);
214 wfAppendToArrayIfNotDefault( 'from', $from, $defaults, $nondefaults);
215 wfAppendToArrayIfNotDefault( 'namespace', $namespace, $defaults, $nondefaults);
216 wfAppendToArrayIfNotDefault( 'invert', $invert, $defaults, $nondefaults);
217 wfAppendToArrayIfNotDefault( 'categories_any', $any, $defaults, $nondefaults);
218
219 // Add end of the texts
220 $wgOut->addHTML( '<div class="rcoptions">' . rcOptionsPanel( $defaults, $nondefaults ) . "\n" );
221 $wgOut->addHTML( rcNamespaceForm( $namespace, $invert, $nondefaults, $any ) . '</div>'."\n");
222 }
223
224 // And now for the content
225 $wgOut->setSyndicated( true );
226
227 $list = ChangesList::newFromUser( $wgUser );
228
229 if ( $wgAllowCategorizedRecentChanges ) {
230 $categories = trim ( $wgRequest->getVal ( 'categories' , "" ) ) ;
231 $categories = str_replace ( "|" , "\n" , $categories ) ;
232 $categories = explode ( "\n" , $categories ) ;
233 rcFilterByCategories ( $rows , $categories , $any ) ;
234 }
235
236 $s = $list->beginRecentChangesList();
237 $counter = 1;
238 foreach( $rows as $obj ){
239 if( $limit == 0) {
240 break;
241 }
242
243 if ( ! ( $hideminor && $obj->rc_minor ) &&
244 ! ( $hidepatrolled && $obj->rc_patrolled ) ) {
245 $rc = RecentChange::newFromRow( $obj );
246 $rc->counter = $counter++;
247
248 if ($wgShowUpdatedMarker
249 && !empty( $obj->wl_notificationtimestamp )
250 && ($obj->rc_timestamp >= $obj->wl_notificationtimestamp)) {
251 $rc->notificationtimestamp = true;
252 } else {
253 $rc->notificationtimestamp = false;
254 }
255
256 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
257 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" . $dbr->strencode($obj->rc_title) ."' AND wl_namespace=$obj->rc_namespace" ;
258 $res3 = $dbr->query( $sql3, 'wfSpecialRecentChanges');
259 $x = $dbr->fetchObject( $res3 );
260 $rc->numberofWatchingusers = $x->n;
261 } else {
262 $rc->numberofWatchingusers = 0;
263 }
264 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user ) );
265 --$limit;
266 }
267 }
268 $s .= $list->endRecentChangesList();
269 $wgOut->addHTML( $s );
270 }
271 }
272
273 function rcFilterByCategories ( &$rows , $categories , $any ) {
274 require_once ( 'Categoryfinder.php' ) ;
275
276 # Filter categories
277 $cats = array () ;
278 foreach ( $categories AS $cat ) {
279 $cat = trim ( $cat ) ;
280 if ( $cat == "" ) continue ;
281 $cats[] = $cat ;
282 }
283
284 # Filter articles
285 $articles = array () ;
286 $a2r = array () ;
287 foreach ( $rows AS $k => $r ) {
288 $nt = Title::newFromText ( $r->rc_title , $r->rc_namespace ) ;
289 $id = $nt->getArticleID() ;
290 if ( $id == 0 ) continue ; # Page might have been deleted...
291 if ( !in_array ( $id , $articles ) ) {
292 $articles[] = $id ;
293 }
294 if ( !isset ( $a2r[$id] ) ) {
295 $a2r[$id] = array() ;
296 }
297 $a2r[$id][] = $k ;
298 }
299
300 # Shortcut?
301 if ( count ( $articles ) == 0 OR count ( $cats ) == 0 )
302 return ;
303
304 # Look up
305 $c = new Categoryfinder ;
306 $c->seed ( $articles , $cats , $any ? "OR" : "AND" ) ;
307 $match = $c->run () ;
308
309 # Filter
310 $newrows = array () ;
311 foreach ( $match AS $id ) {
312 foreach ( $a2r[$id] AS $rev ) {
313 $k = $rev ;
314 $newrows[$k] = $rows[$k] ;
315 }
316 }
317 $rows = $newrows ;
318 }
319
320 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
321 global $messageMemc, $wgFeedCacheTimeout;
322 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
323
324 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
325 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
326 return false;
327 }
328
329 $timekey = wfMemcKey( 'rcfeed', $feedFormat, 'timestamp' );
330 $key = wfMemcKey( 'rcfeed', $feedFormat, 'limit', $limit, 'minor', $hideminor );
331
332 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
333 ' [' . $wgContLanguageCode . ']';
334 $feed = new $wgFeedClasses[$feedFormat](
335 $feedTitle,
336 htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
337 $wgTitle->getFullUrl() );
338
339 /**
340 * Bumping around loading up diffs can be pretty slow, so where
341 * possible we want to cache the feed output so the next visitor
342 * gets it quick too.
343 */
344 $cachedFeed = false;
345 if( ( $wgFeedCacheTimeout > 0 ) && ( $feedLastmod = $messageMemc->get( $timekey ) ) ) {
346 /**
347 * If the cached feed was rendered very recently, we may
348 * go ahead and use it even if there have been edits made
349 * since it was rendered. This keeps a swarm of requests
350 * from being too bad on a super-frequently edited wiki.
351 */
352 if( time() - wfTimestamp( TS_UNIX, $feedLastmod )
353 < $wgFeedCacheTimeout
354 || wfTimestamp( TS_UNIX, $feedLastmod )
355 > wfTimestamp( TS_UNIX, $lastmod ) ) {
356 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
357 $cachedFeed = $messageMemc->get( $key );
358 } else {
359 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
360 }
361 }
362 if( is_string( $cachedFeed ) ) {
363 wfDebug( "RC: Outputting cached feed\n" );
364 $feed->httpHeaders();
365 echo $cachedFeed;
366 } else {
367 wfDebug( "RC: rendering new feed and caching it\n" );
368 ob_start();
369 rcDoOutputFeed( $rows, $feed );
370 $cachedFeed = ob_get_contents();
371 ob_end_flush();
372
373 $expire = 3600 * 24; # One day
374 $messageMemc->set( $key, $cachedFeed );
375 $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
376 }
377 return true;
378 }
379
380 function rcDoOutputFeed( $rows, &$feed ) {
381 $fname = 'rcDoOutputFeed';
382 wfProfileIn( $fname );
383
384 $feed->outHeader();
385
386 # Merge adjacent edits by one user
387 $sorted = array();
388 $n = 0;
389 foreach( $rows as $obj ) {
390 if( $n > 0 &&
391 $obj->rc_namespace >= 0 &&
392 $obj->rc_cur_id == $sorted[$n-1]->rc_cur_id &&
393 $obj->rc_user_text == $sorted[$n-1]->rc_user_text ) {
394 $sorted[$n-1]->rc_last_oldid = $obj->rc_last_oldid;
395 } else {
396 $sorted[$n] = $obj;
397 $n++;
398 }
399 $first = false;
400 }
401
402 foreach( $sorted as $obj ) {
403 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
404 $talkpage = $title->getTalkPage();
405 $item = new FeedItem(
406 $title->getPrefixedText(),
407 rcFormatDiff( $obj ),
408 $title->getFullURL(),
409 $obj->rc_timestamp,
410 $obj->rc_user_text,
411 $talkpage->getFullURL()
412 );
413 $feed->outItem( $item );
414 }
415 $feed->outFooter();
416 wfProfileOut( $fname );
417 }
418
419 /**
420 *
421 */
422 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
423 global $wgLang, $wgContLang;
424 $s = Linker::makeKnownLink( $wgContLang->specialPage( $page ),
425 ($lim ? $wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
426 ($d ? "days={$d}&" : '') . 'limit='.$lim );
427 return $s;
428 }
429
430 /**
431 *
432 */
433 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
434 global $wgLang, $wgContLang;
435 $s = Linker::makeKnownLink( $wgContLang->specialPage( $page ),
436 ($d ? $wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
437 ($lim ? '&limit='.$lim : '') );
438 return $s;
439 }
440
441 /**
442 * Used by Recentchangeslinked
443 */
444 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
445 $botLink = '', $liuLink = '', $patrLink = '', $myselfLink = '' ) {
446 if ($more != '') $more .= '&';
447 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
448 rcCountLink( 100, $days, $page, $more ) . ' | ' .
449 rcCountLink( 250, $days, $page, $more ) . ' | ' .
450 rcCountLink( 500, $days, $page, $more ) .
451 ( $doall ? ( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
452 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
453 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
454 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
455 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
456 rcDaysLink( $limit, 30, $page, $more ) .
457 ( $doall ? ( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
458
459 $linkParts = array( 'minorLink' => 'minor', 'botLink' => 'bots', 'liuLink' => 'liu', 'patrLink' => 'patr', 'myselfLink' => 'mine' );
460 foreach( $linkParts as $linkVar => $linkMsg ) {
461 if( $$linkVar != '' )
462 $links[] = wfMsgHtml( 'rcshowhide' . $linkMsg, $$linkVar );
463 }
464
465 $shm = implode( ' | ', $links );
466 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
467 return $note;
468 }
469
470
471 /**
472 * Makes change an option link which carries all the other options
473 * @param $title @see Title
474 * @param $override
475 * @param $options
476 */
477 function makeOptionsLink( $title, $override, $options ) {
478 global $wgContLang;
479 return Linker::makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
480 htmlspecialchars( $title ), wfArrayToCGI( $override, $options ) );
481 }
482
483 /**
484 * Creates the options panel.
485 * @param $defaults
486 * @param $nondefaults
487 */
488 function rcOptionsPanel( $defaults, $nondefaults ) {
489 global $wgLang, $wgUseRCPatrol;
490
491 $options = $nondefaults + $defaults;
492
493 if( $options['from'] )
494 $note = wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
495 $wgLang->formatNum( $options['limit'] ),
496 $wgLang->timeanddate( $options['from'], true ) );
497 else
498 $note = wfMsgExt( 'rcnote', array( 'parseinline' ),
499 $wgLang->formatNum( $options['limit'] ),
500 $wgLang->formatNum( $options['days'] ),
501 $wgLang->timeAndDate( wfTimestampNow(), true ) );
502
503 // limit links
504 $options_limit = array(50, 100, 250, 500);
505 foreach( $options_limit as $value ) {
506 $cl[] = makeOptionsLink( $wgLang->formatNum( $value ),
507 array( 'limit' => $value ), $nondefaults) ;
508 }
509 $cl = implode( ' | ', $cl);
510
511 // day links, reset 'from' to none
512 $options_days = array(1, 3, 7, 14, 30);
513 foreach( $options_days as $value ) {
514 $dl[] = makeOptionsLink( $wgLang->formatNum( $value ),
515 array( 'days' => $value, 'from' => '' ), $nondefaults) ;
516 }
517 $dl = implode( ' | ', $dl);
518
519
520 // show/hide links
521 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
522 $minorLink = makeOptionsLink( $showhide[1-$options['hideminor']],
523 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
524 $botLink = makeOptionsLink( $showhide[1-$options['hidebots']],
525 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
526 $anonsLink = makeOptionsLink( $showhide[ 1 - $options['hideanons'] ],
527 array( 'hideanons' => 1 - $options['hideanons'] ), $nondefaults );
528 $liuLink = makeOptionsLink( $showhide[1-$options['hideliu']],
529 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
530 $patrLink = makeOptionsLink( $showhide[1-$options['hidepatrolled']],
531 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
532 $myselfLink = makeOptionsLink( $showhide[1-$options['hidemyself']],
533 array( 'hidemyself' => 1-$options['hidemyself'] ), $nondefaults);
534
535 $links[] = wfMsgHtml( 'rcshowhideminor', $minorLink );
536 $links[] = wfMsgHtml( 'rcshowhidebots', $botLink );
537 $links[] = wfMsgHtml( 'rcshowhideanons', $anonsLink );
538 $links[] = wfMsgHtml( 'rcshowhideliu', $liuLink );
539 if( $wgUseRCPatrol )
540 $links[] = wfMsgHtml( 'rcshowhidepatr', $patrLink );
541 $links[] = wfMsgHtml( 'rcshowhidemine', $myselfLink );
542 $hl = implode( ' | ', $links );
543
544 // show from this onward link
545 $now = $wgLang->timeanddate( wfTimestampNow(), true );
546 $tl = makeOptionsLink( $now, array( 'from' => wfTimestampNow()), $nondefaults );
547
548 $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter'),
549 $cl, $dl, $hl );
550 $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter'), $tl );
551 return "$note<br />$rclinks<br />$rclistfrom";
552
553 }
554
555 /**
556 * Creates the choose namespace selection
557 *
558 * @private
559 *
560 * @param $namespace Mixed: the key of the currently selected namespace, empty string
561 * if there is none
562 * @param $invert Bool: whether to invert the namespace selection
563 * @param $nondefaults Array: an array of non default options to be remembered
564 * @param $categories_any Bool: Default value for the checkbox
565 *
566 * @return string
567 */
568 function rcNamespaceForm( $namespace, $invert, $nondefaults, $categories_any ) {
569 global $wgScript, $wgAllowCategorizedRecentChanges, $wgRequest;
570 $t = SpecialPage::getTitleFor( 'Recentchanges' );
571
572 $namespaceselect = HTMLnamespaceselector($namespace, '');
573 $submitbutton = '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . "\" />\n";
574 $invertbox = "<input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ? ' checked="checked"' : '' ) . ' />';
575
576 if ( $wgAllowCategorizedRecentChanges ) {
577 $categories = trim ( $wgRequest->getVal ( 'categories' , "" ) ) ;
578 $cb_arr = array( 'type' => 'checkbox', 'name' => 'categories_any', 'value' => "1" ) ;
579 if ( $categories_any ) $cb_arr['checked'] = "checked" ;
580 $catbox = "<br />" ;
581 $catbox .= wfMsgExt('rc_categories', array('parseinline')) . " ";
582 $catbox .= wfElement('input', array( 'type' => 'text', 'name' => 'categories', 'value' => $categories));
583 $catbox .= " &nbsp;" ;
584 $catbox .= wfElement('input', $cb_arr );
585 $catbox .= wfMsgExt('rc_categories_any', array('parseinline'));
586 } else {
587 $catbox = "" ;
588 }
589
590 $out = "<div class='namespacesettings'><form method='get' action='{$wgScript}'>\n";
591
592 foreach ( $nondefaults as $key => $value ) {
593 if ($key != 'namespace' && $key != 'invert')
594 $out .= wfElement('input', array( 'type' => 'hidden', 'name' => $key, 'value' => $value));
595 }
596
597 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
598 $out .= "
599 <div id='nsselect' class='recentchanges'>
600 <label for='namespace'>" . wfMsgHtml('namespace') . "</label>
601 {$namespaceselect}{$submitbutton}{$invertbox} <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>{$catbox}\n</div>";
602 $out .= '</form></div>';
603 return $out;
604 }
605
606
607 /**
608 * Format a diff for the newsfeed
609 */
610 function rcFormatDiff( $row ) {
611 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
612 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
613 return rcFormatDiffRow( $titleObj,
614 $row->rc_last_oldid, $row->rc_this_oldid,
615 $timestamp,
616 $row->rc_comment );
617 }
618
619 function rcFormatDiffRow( $title, $oldid, $newid, $timestamp, $comment ) {
620 global $wgFeedDiffCutoff, $wgContLang;
621 $fname = 'rcFormatDiff';
622 wfProfileIn( $fname );
623
624 $completeText = '<p>' . Linker::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 ?>