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