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