Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[lhc/web/wiklou.git] / includes / SpecialRecentchanges.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 *
9 */
10 require_once( '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 be 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 $options['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 require_once ( 'Categoryfinder.php' ) ;
273
274 # Filter categories
275 $cats = array () ;
276 foreach ( $categories AS $cat ) {
277 $cat = trim ( $cat ) ;
278 if ( $cat == "" ) continue ;
279 $cats[] = $cat ;
280 }
281
282 # Filter articles
283 $articles = array () ;
284 $a2r = array () ;
285 foreach ( $rows AS $k => $r ) {
286 $nt = Title::newFromText ( $r->rc_title , $r->rc_namespace ) ;
287 $id = $nt->getArticleID() ;
288 if ( $id == 0 ) continue ; # Page might have been deleted...
289 if ( !in_array ( $id , $articles ) ) {
290 $articles[] = $id ;
291 }
292 if ( !isset ( $a2r[$id] ) ) {
293 $a2r[$id] = array() ;
294 }
295 $a2r[$id][] = $k ;
296 }
297
298 # Shortcut?
299 if ( count ( $articles ) == 0 OR count ( $cats ) == 0 )
300 return ;
301
302 # Look up
303 $c = new Categoryfinder ;
304 $c->seed ( $articles , $cats , $any ? "OR" : "AND" ) ;
305 $match = $c->run () ;
306
307 # Filter
308 $newrows = array () ;
309 foreach ( $match AS $id ) {
310 foreach ( $a2r[$id] AS $rev ) {
311 $k = $rev ;
312 $newrows[$k] = $rows[$k] ;
313 }
314 }
315 $rows = $newrows ;
316 }
317
318 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
319 global $messageMemc, $wgFeedCacheTimeout;
320 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
321
322 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
323 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
324 return false;
325 }
326
327 $timekey = wfMemcKey( 'rcfeed', $feedFormat, 'timestamp' );
328 $key = wfMemcKey( 'rcfeed', $feedFormat, 'limit', $limit, 'minor', $hideminor );
329
330 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
331 ' [' . $wgContLanguageCode . ']';
332 $feed = new $wgFeedClasses[$feedFormat](
333 $feedTitle,
334 htmlspecialchars( wfMsgForContent( 'recentchanges-feed-description' ) ),
335 $wgTitle->getFullUrl() );
336
337 /**
338 * Bumping around loading up diffs can be pretty slow, so where
339 * possible we want to cache the feed output so the next visitor
340 * gets it quick too.
341 */
342 $cachedFeed = false;
343 if( ( $wgFeedCacheTimeout > 0 ) && ( $feedLastmod = $messageMemc->get( $timekey ) ) ) {
344 /**
345 * If the cached feed was rendered very recently, we may
346 * go ahead and use it even if there have been edits made
347 * since it was rendered. This keeps a swarm of requests
348 * from being too bad on a super-frequently edited wiki.
349 */
350 if( time() - wfTimestamp( TS_UNIX, $feedLastmod )
351 < $wgFeedCacheTimeout
352 || wfTimestamp( TS_UNIX, $feedLastmod )
353 > wfTimestamp( TS_UNIX, $lastmod ) ) {
354 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
355 $cachedFeed = $messageMemc->get( $key );
356 } else {
357 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
358 }
359 }
360 if( is_string( $cachedFeed ) ) {
361 wfDebug( "RC: Outputting cached feed\n" );
362 $feed->httpHeaders();
363 echo $cachedFeed;
364 } else {
365 wfDebug( "RC: rendering new feed and caching it\n" );
366 ob_start();
367 rcDoOutputFeed( $rows, $feed );
368 $cachedFeed = ob_get_contents();
369 ob_end_flush();
370
371 $expire = 3600 * 24; # One day
372 $messageMemc->set( $key, $cachedFeed );
373 $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
374 }
375 return true;
376 }
377
378 function rcDoOutputFeed( $rows, &$feed ) {
379 $fname = 'rcDoOutputFeed';
380 wfProfileIn( $fname );
381
382 $feed->outHeader();
383
384 # Merge adjacent edits by one user
385 $sorted = array();
386 $n = 0;
387 foreach( $rows as $obj ) {
388 if( $n > 0 &&
389 $obj->rc_namespace >= 0 &&
390 $obj->rc_cur_id == $sorted[$n-1]->rc_cur_id &&
391 $obj->rc_user_text == $sorted[$n-1]->rc_user_text ) {
392 $sorted[$n-1]->rc_last_oldid = $obj->rc_last_oldid;
393 } else {
394 $sorted[$n] = $obj;
395 $n++;
396 }
397 }
398
399 foreach( $sorted as $obj ) {
400 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
401 $talkpage = $title->getTalkPage();
402 $item = new FeedItem(
403 $title->getPrefixedText(),
404 rcFormatDiff( $obj ),
405 $title->getFullURL(),
406 $obj->rc_timestamp,
407 $obj->rc_user_text,
408 $talkpage->getFullURL()
409 );
410 $feed->outItem( $item );
411 }
412 $feed->outFooter();
413 wfProfileOut( $fname );
414 }
415
416 /**
417 *
418 */
419 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
420 global $wgUser, $wgLang, $wgContLang;
421 $sk = $wgUser->getSkin();
422 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
423 ($lim ? $wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
424 ($d ? "days={$d}&" : '') . 'limit='.$lim );
425 return $s;
426 }
427
428 /**
429 *
430 */
431 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
432 global $wgUser, $wgLang, $wgContLang;
433 $sk = $wgUser->getSkin();
434 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
435 ($d ? $wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
436 ($lim ? '&limit='.$lim : '') );
437 return $s;
438 }
439
440 /**
441 * Used by Recentchangeslinked
442 */
443 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
444 $botLink = '', $liuLink = '', $patrLink = '', $myselfLink = '' ) {
445 if ($more != '') $more .= '&';
446 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
447 rcCountLink( 100, $days, $page, $more ) . ' | ' .
448 rcCountLink( 250, $days, $page, $more ) . ' | ' .
449 rcCountLink( 500, $days, $page, $more ) .
450 ( $doall ? ( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
451 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
452 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
453 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
454 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
455 rcDaysLink( $limit, 30, $page, $more ) .
456 ( $doall ? ( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
457
458 $linkParts = array( 'minorLink' => 'minor', 'botLink' => 'bots', 'liuLink' => 'liu', 'patrLink' => 'patr', 'myselfLink' => 'mine' );
459 foreach( $linkParts as $linkVar => $linkMsg ) {
460 if( $$linkVar != '' )
461 $links[] = wfMsgHtml( 'rcshowhide' . $linkMsg, $$linkVar );
462 }
463
464 $shm = implode( ' | ', $links );
465 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
466 return $note;
467 }
468
469
470 /**
471 * Makes change an option link which carries all the other options
472 * @param $title @see Title
473 * @param $override
474 * @param $options
475 */
476 function makeOptionsLink( $title, $override, $options ) {
477 global $wgUser, $wgContLang;
478 $sk = $wgUser->getSkin();
479 return $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
480 htmlspecialchars( $title ), wfArrayToCGI( $override, $options ) );
481 }
482
483 /**
484 * Creates the options panel.
485 * @param $defaults
486 * @param $nondefaults
487 */
488 function rcOptionsPanel( $defaults, $nondefaults ) {
489 global $wgLang, $wgUseRCPatrol;
490
491 $options = $nondefaults + $defaults;
492
493 if( $options['from'] )
494 $note = wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
495 $wgLang->formatNum( $options['limit'] ),
496 $wgLang->timeanddate( $options['from'], true ) );
497 else
498 $note = wfMsgExt( 'rcnote', array( 'parseinline' ),
499 $wgLang->formatNum( $options['limit'] ),
500 $wgLang->formatNum( $options['days'] ),
501 $wgLang->timeAndDate( wfTimestampNow(), true ) );
502
503 // limit links
504 $options_limit = array(50, 100, 250, 500);
505 foreach( $options_limit as $value ) {
506 $cl[] = makeOptionsLink( $wgLang->formatNum( $value ),
507 array( 'limit' => $value ), $nondefaults) ;
508 }
509 $cl = implode( ' | ', $cl);
510
511 // day links, reset 'from' to none
512 $options_days = array(1, 3, 7, 14, 30);
513 foreach( $options_days as $value ) {
514 $dl[] = makeOptionsLink( $wgLang->formatNum( $value ),
515 array( 'days' => $value, 'from' => '' ), $nondefaults) ;
516 }
517 $dl = implode( ' | ', $dl);
518
519
520 // show/hide links
521 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
522 $minorLink = makeOptionsLink( $showhide[1-$options['hideminor']],
523 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
524 $botLink = makeOptionsLink( $showhide[1-$options['hidebots']],
525 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
526 $anonsLink = makeOptionsLink( $showhide[ 1 - $options['hideanons'] ],
527 array( 'hideanons' => 1 - $options['hideanons'] ), $nondefaults );
528 $liuLink = makeOptionsLink( $showhide[1-$options['hideliu']],
529 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
530 $patrLink = makeOptionsLink( $showhide[1-$options['hidepatrolled']],
531 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
532 $myselfLink = makeOptionsLink( $showhide[1-$options['hidemyself']],
533 array( 'hidemyself' => 1-$options['hidemyself'] ), $nondefaults);
534
535 $links[] = wfMsgHtml( 'rcshowhideminor', $minorLink );
536 $links[] = wfMsgHtml( 'rcshowhidebots', $botLink );
537 $links[] = wfMsgHtml( 'rcshowhideanons', $anonsLink );
538 $links[] = wfMsgHtml( 'rcshowhideliu', $liuLink );
539 if( $wgUseRCPatrol )
540 $links[] = wfMsgHtml( 'rcshowhidepatr', $patrLink );
541 $links[] = wfMsgHtml( 'rcshowhidemine', $myselfLink );
542 $hl = implode( ' | ', $links );
543
544 // show from this onward link
545 $now = $wgLang->timeanddate( wfTimestampNow(), true );
546 $tl = makeOptionsLink( $now, array( 'from' => wfTimestampNow()), $nondefaults );
547
548 $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter'),
549 $cl, $dl, $hl );
550 $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter'), $tl );
551 return "$note<br />$rclinks<br />$rclistfrom";
552
553 }
554
555 /**
556 * Creates the choose namespace selection
557 *
558 * @private
559 *
560 * @param $namespace Mixed: the key of the currently selected namespace, empty string
561 * if there is none
562 * @param $invert Bool: whether to invert the namespace selection
563 * @param $nondefaults Array: an array of non default options to be remembered
564 * @param $categories_any Bool: Default value for the checkbox
565 *
566 * @return string
567 */
568 function rcNamespaceForm( $namespace, $invert, $nondefaults, $categories_any ) {
569 global $wgScript, $wgAllowCategorizedRecentChanges, $wgRequest;
570 $t = SpecialPage::getTitleFor( 'Recentchanges' );
571
572 $namespaceselect = HTMLnamespaceselector($namespace, '');
573 $submitbutton = '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . "\" />\n";
574 $invertbox = "<input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ? ' checked="checked"' : '' ) . ' />';
575
576 if ( $wgAllowCategorizedRecentChanges ) {
577 $categories = trim ( $wgRequest->getVal ( 'categories' , "" ) ) ;
578 $cb_arr = array( 'type' => 'checkbox', 'name' => 'categories_any', 'value' => "1" ) ;
579 if ( $categories_any ) $cb_arr['checked'] = "checked" ;
580 $catbox = "<br />" ;
581 $catbox .= wfMsgExt('rc_categories', array('parseinline')) . " ";
582 $catbox .= wfElement('input', array( 'type' => 'text', 'name' => 'categories', 'value' => $categories));
583 $catbox .= " &nbsp;" ;
584 $catbox .= wfElement('input', $cb_arr );
585 $catbox .= wfMsgExt('rc_categories_any', array('parseinline'));
586 } else {
587 $catbox = "" ;
588 }
589
590 $out = "<div class='namespacesettings'><form method='get' action='{$wgScript}'>\n";
591
592 foreach ( $nondefaults as $key => $value ) {
593 if ($key != 'namespace' && $key != 'invert')
594 $out .= wfElement('input', array( 'type' => 'hidden', 'name' => $key, 'value' => $value));
595 }
596
597 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
598 $out .= "
599 <div id='nsselect' class='recentchanges'>
600 <label for='namespace'>" . wfMsgHtml('namespace') . "</label>
601 {$namespaceselect}{$submitbutton}{$invertbox} <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>{$catbox}\n</div>";
602 $out .= '</form></div>';
603 return $out;
604 }
605
606
607 /**
608 * Format a diff for the newsfeed
609 */
610 function rcFormatDiff( $row ) {
611 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
612 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
613 return rcFormatDiffRow( $titleObj,
614 $row->rc_last_oldid, $row->rc_this_oldid,
615 $timestamp,
616 $row->rc_comment );
617 }
618
619 function rcFormatDiffRow( $title, $oldid, $newid, $timestamp, $comment ) {
620 global $wgFeedDiffCutoff, $wgContLang, $wgUser;
621 $fname = 'rcFormatDiff';
622 wfProfileIn( $fname );
623
624 $skin = $wgUser->getSkin();
625 $completeText = '<p>' . $skin->formatComment( $comment ) . "</p>\n";
626
627 if( $title->getNamespace() >= 0 ) {
628 if( $oldid ) {
629 wfProfileIn( "$fname-dodiff" );
630
631 $de = new DifferenceEngine( $title, $oldid, $newid );
632 #$diffText = $de->getDiff( wfMsg( 'revisionasof',
633 # $wgContLang->timeanddate( $timestamp ) ),
634 # wfMsg( 'currentrev' ) );
635 $diffText = $de->getDiff(
636 wfMsg( 'previousrevision' ), // hack
637 wfMsg( 'revisionasof',
638 $wgContLang->timeanddate( $timestamp ) ) );
639
640
641 if ( strlen( $diffText ) > $wgFeedDiffCutoff ) {
642 // Omit large diffs
643 $diffLink = $title->escapeFullUrl(
644 'diff=' . $newid .
645 '&oldid=' . $oldid );
646 $diffText = '<a href="' .
647 $diffLink .
648 '">' .
649 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
650 '</a>';
651 } elseif ( $diffText === false ) {
652 // Error in diff engine, probably a missing revision
653 $diffText = "<p>Can't load revision $newid</p>";
654 } else {
655 // Diff output fine, clean up any illegal UTF-8
656 $diffText = UtfNormal::cleanUp( $diffText );
657 $diffText = rcApplyDiffStyle( $diffText );
658 }
659 wfProfileOut( "$fname-dodiff" );
660 } else {
661 $rev = Revision::newFromId( $newid );
662 if( is_null( $rev ) ) {
663 $newtext = '';
664 } else {
665 $newtext = $rev->getText();
666 }
667 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
668 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
669 }
670 $completeText .= $diffText;
671 }
672
673 wfProfileOut( $fname );
674 return $completeText;
675 }
676
677 /**
678 * Hacky application of diff styles for the feeds.
679 * Might be 'cleaner' to use DOM or XSLT or something,
680 * but *gack* it's a pain in the ass.
681 *
682 * @param $text String:
683 * @return string
684 * @private
685 */
686 function rcApplyDiffStyle( $text ) {
687 $styles = array(
688 'diff' => 'background-color: white;',
689 'diff-otitle' => 'background-color: white;',
690 'diff-ntitle' => 'background-color: white;',
691 'diff-addedline' => 'background: #cfc; font-size: smaller;',
692 'diff-deletedline' => 'background: #ffa; font-size: smaller;',
693 'diff-context' => 'background: #eee; font-size: smaller;',
694 'diffchange' => 'color: red; font-weight: bold;',
695 );
696
697 foreach( $styles as $class => $style ) {
698 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
699 "\\1style=\"$style\"\\3", $text );
700 }
701
702 return $text;
703 }
704
705 ?>