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