* (bug 11115) API: Adding the SHA1 to the imageinfo query
[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 $wgOut->setSyndicated( true );
224
225 $list = ChangesList::newFromUser( $wgUser );
226
227 if ( $wgAllowCategorizedRecentChanges ) {
228 $categories = trim ( $wgRequest->getVal ( 'categories' , "" ) ) ;
229 $categories = str_replace ( "|" , "\n" , $categories ) ;
230 $categories = explode ( "\n" , $categories ) ;
231 rcFilterByCategories ( $rows , $categories , $any ) ;
232 }
233
234 $s = $list->beginRecentChangesList();
235 $counter = 1;
236 foreach( $rows as $obj ){
237 if( $limit == 0) {
238 break;
239 }
240
241 if ( ! ( $hideminor && $obj->rc_minor ) &&
242 ! ( $hidepatrolled && $obj->rc_patrolled ) ) {
243 $rc = RecentChange::newFromRow( $obj );
244 $rc->counter = $counter++;
245
246 if ($wgShowUpdatedMarker
247 && !empty( $obj->wl_notificationtimestamp )
248 && ($obj->rc_timestamp >= $obj->wl_notificationtimestamp)) {
249 $rc->notificationtimestamp = true;
250 } else {
251 $rc->notificationtimestamp = false;
252 }
253
254 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
255 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" . $dbr->strencode($obj->rc_title) ."' AND wl_namespace=$obj->rc_namespace" ;
256 $res3 = $dbr->query( $sql3, 'wfSpecialRecentChanges');
257 $x = $dbr->fetchObject( $res3 );
258 $rc->numberofWatchingusers = $x->n;
259 } else {
260 $rc->numberofWatchingusers = 0;
261 }
262 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user ) );
263 --$limit;
264 }
265 }
266 $s .= $list->endRecentChangesList();
267 $wgOut->addHTML( $s );
268 }
269 }
270
271 function rcFilterByCategories ( &$rows , $categories , $any ) {
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, $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 = wfMemcKey( 'rcfeed', $feedFormat, 'timestamp' );
326 $key = wfMemcKey( 'rcfeed', $feedFormat, 'limit', $limit, 'minor', $hideminor );
327
328 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
329 ' [' . $wgContLanguageCode . ']';
330 $feed = new $wgFeedClasses[$feedFormat](
331 $feedTitle,
332 htmlspecialchars( wfMsgForContent( 'recentchanges-feed-description' ) ),
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 }
396
397 foreach( $sorted as $obj ) {
398 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
399 $talkpage = $title->getTalkPage();
400 $item = new FeedItem(
401 $title->getPrefixedText(),
402 rcFormatDiff( $obj ),
403 $title->getFullURL(),
404 $obj->rc_timestamp,
405 $obj->rc_user_text,
406 $talkpage->getFullURL()
407 );
408 $feed->outItem( $item );
409 }
410 $feed->outFooter();
411 wfProfileOut( $fname );
412 }
413
414 /**
415 *
416 */
417 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
418 global $wgUser, $wgLang, $wgContLang;
419 $sk = $wgUser->getSkin();
420 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
421 ($lim ? $wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
422 ($d ? "days={$d}&" : '') . 'limit='.$lim );
423 return $s;
424 }
425
426 /**
427 *
428 */
429 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
430 global $wgUser, $wgLang, $wgContLang;
431 $sk = $wgUser->getSkin();
432 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
433 ($d ? $wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
434 ($lim ? '&limit='.$lim : '') );
435 return $s;
436 }
437
438 /**
439 * Used by Recentchangeslinked
440 */
441 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
442 $botLink = '', $liuLink = '', $patrLink = '', $myselfLink = '' ) {
443 if ($more != '') $more .= '&';
444 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
445 rcCountLink( 100, $days, $page, $more ) . ' | ' .
446 rcCountLink( 250, $days, $page, $more ) . ' | ' .
447 rcCountLink( 500, $days, $page, $more ) .
448 ( $doall ? ( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
449 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
450 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
451 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
452 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
453 rcDaysLink( $limit, 30, $page, $more ) .
454 ( $doall ? ( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
455
456 $linkParts = array( 'minorLink' => 'minor', 'botLink' => 'bots', 'liuLink' => 'liu', 'patrLink' => 'patr', 'myselfLink' => 'mine' );
457 foreach( $linkParts as $linkVar => $linkMsg ) {
458 if( $$linkVar != '' )
459 $links[] = wfMsgHtml( 'rcshowhide' . $linkMsg, $$linkVar );
460 }
461
462 $shm = implode( ' | ', $links );
463 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
464 return $note;
465 }
466
467
468 /**
469 * Makes change an option link which carries all the other options
470 * @param $title see Title
471 * @param $override
472 * @param $options
473 */
474 function makeOptionsLink( $title, $override, $options ) {
475 global $wgUser, $wgContLang;
476 $sk = $wgUser->getSkin();
477 return $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
478 htmlspecialchars( $title ), wfArrayToCGI( $override, $options ) );
479 }
480
481 /**
482 * Creates the options panel.
483 * @param $defaults
484 * @param $nondefaults
485 */
486 function rcOptionsPanel( $defaults, $nondefaults ) {
487 global $wgLang, $wgUseRCPatrol;
488
489 $options = $nondefaults + $defaults;
490
491 if( $options['from'] )
492 $note = wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
493 $wgLang->formatNum( $options['limit'] ),
494 $wgLang->timeanddate( $options['from'], true ) );
495 else
496 $note = wfMsgExt( 'rcnote', array( 'parseinline' ),
497 $wgLang->formatNum( $options['limit'] ),
498 $wgLang->formatNum( $options['days'] ),
499 $wgLang->timeAndDate( wfTimestampNow(), true ) );
500
501 // limit links
502 $options_limit = array(50, 100, 250, 500);
503 foreach( $options_limit as $value ) {
504 $cl[] = makeOptionsLink( $wgLang->formatNum( $value ),
505 array( 'limit' => $value ), $nondefaults) ;
506 }
507 $cl = implode( ' | ', $cl);
508
509 // day links, reset 'from' to none
510 $options_days = array(1, 3, 7, 14, 30);
511 foreach( $options_days as $value ) {
512 $dl[] = makeOptionsLink( $wgLang->formatNum( $value ),
513 array( 'days' => $value, 'from' => '' ), $nondefaults) ;
514 }
515 $dl = implode( ' | ', $dl);
516
517
518 // show/hide links
519 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
520 $minorLink = makeOptionsLink( $showhide[1-$options['hideminor']],
521 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
522 $botLink = makeOptionsLink( $showhide[1-$options['hidebots']],
523 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
524 $anonsLink = makeOptionsLink( $showhide[ 1 - $options['hideanons'] ],
525 array( 'hideanons' => 1 - $options['hideanons'] ), $nondefaults );
526 $liuLink = makeOptionsLink( $showhide[1-$options['hideliu']],
527 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
528 $patrLink = makeOptionsLink( $showhide[1-$options['hidepatrolled']],
529 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
530 $myselfLink = makeOptionsLink( $showhide[1-$options['hidemyself']],
531 array( 'hidemyself' => 1-$options['hidemyself'] ), $nondefaults);
532
533 $links[] = wfMsgHtml( 'rcshowhideminor', $minorLink );
534 $links[] = wfMsgHtml( 'rcshowhidebots', $botLink );
535 $links[] = wfMsgHtml( 'rcshowhideanons', $anonsLink );
536 $links[] = wfMsgHtml( 'rcshowhideliu', $liuLink );
537 if( $wgUseRCPatrol )
538 $links[] = wfMsgHtml( 'rcshowhidepatr', $patrLink );
539 $links[] = wfMsgHtml( 'rcshowhidemine', $myselfLink );
540 $hl = implode( ' | ', $links );
541
542 // show from this onward link
543 $now = $wgLang->timeanddate( wfTimestampNow(), true );
544 $tl = makeOptionsLink( $now, array( 'from' => wfTimestampNow()), $nondefaults );
545
546 $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter'),
547 $cl, $dl, $hl );
548 $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter'), $tl );
549 return "$note<br />$rclinks<br />$rclistfrom";
550
551 }
552
553 /**
554 * Creates the choose namespace selection
555 *
556 * @private
557 *
558 * @param $namespace Mixed: the key of the currently selected namespace, empty string
559 * if there is none
560 * @param $invert Bool: whether to invert the namespace selection
561 * @param $nondefaults Array: an array of non default options to be remembered
562 * @param $categories_any Bool: Default value for the checkbox
563 *
564 * @return string
565 */
566 function rcNamespaceForm( $namespace, $invert, $nondefaults, $categories_any ) {
567 global $wgScript, $wgAllowCategorizedRecentChanges, $wgRequest;
568 $t = SpecialPage::getTitleFor( 'Recentchanges' );
569
570 $namespaceselect = HTMLnamespaceselector($namespace, '');
571 $submitbutton = '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . "\" />\n";
572 $invertbox = "<input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ? ' checked="checked"' : '' ) . ' />';
573
574 if ( $wgAllowCategorizedRecentChanges ) {
575 $categories = trim ( $wgRequest->getVal ( 'categories' , "" ) ) ;
576 $cb_arr = array( 'type' => 'checkbox', 'name' => 'categories_any', 'value' => "1" ) ;
577 if ( $categories_any ) $cb_arr['checked'] = "checked" ;
578 $catbox = "<br />" ;
579 $catbox .= wfMsgExt('rc_categories', array('parseinline')) . " ";
580 $catbox .= wfElement('input', array( 'type' => 'text', 'name' => 'categories', 'value' => $categories));
581 $catbox .= " &nbsp;" ;
582 $catbox .= wfElement('input', $cb_arr );
583 $catbox .= wfMsgExt('rc_categories_any', array('parseinline'));
584 } else {
585 $catbox = "" ;
586 }
587
588 $out = "<div class='namespacesettings'><form method='get' action='{$wgScript}'>\n";
589
590 foreach ( $nondefaults as $key => $value ) {
591 if ($key != 'namespace' && $key != 'invert')
592 $out .= wfElement('input', array( 'type' => 'hidden', 'name' => $key, 'value' => $value));
593 }
594
595 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
596 $out .= "
597 <div id='nsselect' class='recentchanges'>
598 <label for='namespace'>" . wfMsgHtml('namespace') . "</label>
599 {$namespaceselect}{$submitbutton}{$invertbox} <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>{$catbox}\n</div>";
600 $out .= '</form></div>';
601 return $out;
602 }
603
604
605 /**
606 * Format a diff for the newsfeed
607 */
608 function rcFormatDiff( $row ) {
609 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
610 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
611 return rcFormatDiffRow( $titleObj,
612 $row->rc_last_oldid, $row->rc_this_oldid,
613 $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 $skin = $wgUser->getSkin();
623 $completeText = '<p>' . $skin->formatComment( $comment ) . "</p>\n";
624
625 if( $title->getNamespace() >= 0 && $title->userCan( 'read' ) ) {
626 if( $oldid ) {
627 wfProfileIn( "$fname-dodiff" );
628
629 $de = new DifferenceEngine( $title, $oldid, $newid );
630 #$diffText = $de->getDiff( wfMsg( 'revisionasof',
631 # $wgContLang->timeanddate( $timestamp ) ),
632 # wfMsg( 'currentrev' ) );
633 $diffText = $de->getDiff(
634 wfMsg( 'previousrevision' ), // hack
635 wfMsg( 'revisionasof',
636 $wgContLang->timeanddate( $timestamp ) ) );
637
638
639 if ( strlen( $diffText ) > $wgFeedDiffCutoff ) {
640 // Omit large diffs
641 $diffLink = $title->escapeFullUrl(
642 'diff=' . $newid .
643 '&oldid=' . $oldid );
644 $diffText = '<a href="' .
645 $diffLink .
646 '">' .
647 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
648 '</a>';
649 } elseif ( $diffText === false ) {
650 // Error in diff engine, probably a missing revision
651 $diffText = "<p>Can't load revision $newid</p>";
652 } else {
653 // Diff output fine, clean up any illegal UTF-8
654 $diffText = UtfNormal::cleanUp( $diffText );
655 $diffText = rcApplyDiffStyle( $diffText );
656 }
657 wfProfileOut( "$fname-dodiff" );
658 } else {
659 $rev = Revision::newFromId( $newid );
660 if( is_null( $rev ) ) {
661 $newtext = '';
662 } else {
663 $newtext = $rev->getText();
664 }
665 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
666 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
667 }
668 $completeText .= $diffText;
669 }
670
671 wfProfileOut( $fname );
672 return $completeText;
673 }
674
675 /**
676 * Hacky application of diff styles for the feeds.
677 * Might be 'cleaner' to use DOM or XSLT or something,
678 * but *gack* it's a pain in the ass.
679 *
680 * @param $text String:
681 * @return string
682 * @private
683 */
684 function rcApplyDiffStyle( $text ) {
685 $styles = array(
686 'diff' => 'background-color: white; color:black;',
687 'diff-otitle' => 'background-color: white; color:black;',
688 'diff-ntitle' => 'background-color: white; color:black;',
689 'diff-addedline' => 'background: #cfc; color:black; font-size: smaller;',
690 'diff-deletedline' => 'background: #ffa; color:black; font-size: smaller;',
691 'diff-context' => 'background: #eee; color:black; font-size: smaller;',
692 'diffchange' => 'color: red; font-weight: bold; text-decoration: none;',
693 );
694
695 foreach( $styles as $class => $style ) {
696 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
697 "\\1style=\"$style\"\\3", $text );
698 }
699
700 return $text;
701 }
702
703