#3354 : LanguagePt update by Get_it
[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( 'Feed.php' );
12 require_once( 'ChangesList.php' );
13 require_once( 'Revision.php' );
14
15 /**
16 * Constructor
17 */
18 function wfSpecialRecentchanges( $par, $specialPage ) {
19 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgTitle, $wgMemc, $wgDBname;
20 global $wgRequest, $wgSitename, $wgLanguageCode, $wgContLanguageCode;
21 global $wgFeedClasses, $wgUseRCPatrol;
22 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
23 global $wgLinkCache;
24 $fname = 'wfSpecialRecentchanges';
25
26 # Get query parameters
27 $feedFormat = $wgRequest->getVal( 'feed' );
28
29 $defaults = array(
30 /* int */ 'days' => $wgUser->getDefaultOption('rcdays'),
31 /* int */ 'limit' => $wgUser->getDefaultOption('rclimit'),
32 /* bool */ 'hideminor' => false,
33 /* bool */ 'hidebots' => true,
34 /* bool */ 'hideliu' => false,
35 /* bool */ 'hidepatrolled' => false,
36 /* text */ 'from' => '',
37 /* text */ 'namespace' => null,
38 /* bool */ 'invert' => false,
39 );
40
41 extract($defaults);
42
43
44 $days = $wgUser->getOption( 'rcdays' );
45 if ( !$days ) { $days = $defaults['days']; }
46 $days = $wgRequest->getInt( 'days', $days );
47
48 $limit = $wgUser->getOption( 'rclimit' );
49 if ( !$limit ) { $limit = $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
58 # As a feed, use limited settings only
59 if( $feedFormat ) {
60 global $wgFeedLimit;
61 if( $limit > $wgFeedLimit ) {
62 $options['limit'] = $wgFeedLimit;
63 }
64
65 } else {
66
67 $namespace = $wgRequest->getIntOrNull( 'namespace' );
68 $invert = $wgRequest->getBool( 'invert', $defaults['invert'] );
69 $hidebots = $wgRequest->getBool( 'hidebots', $defaults['hidebots'] );
70 $hideliu = $wgRequest->getBool( 'hideliu', $defaults['hideliu'] );
71 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled', $defaults['hidepatrolled'] );
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
85 if ( is_numeric( $bit ) ) {
86 $limit = $bit;
87 }
88
89 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
90 $limit = $m[1];
91 }
92
93 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
94 $days = $m[1];
95 }
96 }
97 }
98 }
99
100 if ( $limit < 0 || $limit > 5000 ) $limit = $defaults['limit'];
101
102
103 # Database connection and caching
104 $dbr =& wfGetDB( DB_SLAVE );
105 extract( $dbr->tableNames( 'recentchanges', 'watchlist' ) );
106
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 $hidem = $hideminor ? 'AND rc_minor=0' : '';
131 $hidem .= $hidebots ? ' AND rc_bot=0' : '';
132 $hidem .= $hideliu ? ' AND rc_user=0' : '';
133 $hidem .= $hidepatrolled ? ' AND rc_patrolled=0' : '';
134 $hidem .= is_null( $namespace ) ? '' : ' AND rc_namespace' . ($invert ? '!=' : '=') . $namespace;
135
136 // This is the big thing!
137
138 $uid = $wgUser->getID();
139
140 // Perform query
141 $sql2 = "SELECT * FROM $recentchanges " .
142 ($uid ? "LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "") .
143 "WHERE rc_timestamp > '{$cutoff}' {$hidem} " .
144 "ORDER BY rc_timestamp DESC";
145 $sql2 = $dbr->limitResult($sql2, $limit, 0);
146 $res = $dbr->query( $sql2, $fname );
147
148 // Fetch results, prepare a batch link existence check query
149 $rows = array();
150 $batch = new LinkBatch;
151 while( $row = $dbr->fetchObject( $res ) ){
152 $rows[] = $row;
153 // User page link
154 $title = Title::makeTitleSafe( NS_USER, $row->rc_user_text );
155 $batch->addObj( $title );
156
157 // User talk
158 $title = Title::makeTitleSafe( NS_USER_TALK, $row->rc_user_text );
159 $batch->addObj( $title );
160
161 }
162 $dbr->freeResult( $res );
163
164 // Run existence checks
165 $batch->execute( $wgLinkCache );
166
167 if( $feedFormat ) {
168 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
169 } else {
170
171 # Web output...
172
173 // Output header
174 if ( !$specialPage->including() ) {
175 $wgOut->addWikiText( wfMsgForContent( "recentchangestext" ) );
176
177 // Dump everything here
178 $nondefaults = array();
179
180 wfAppendToArrayIfNotDefault( 'days', $days, $defaults, $nondefaults);
181 wfAppendToArrayIfNotDefault( 'limit', $limit , $defaults, $nondefaults);
182 wfAppendToArrayIfNotDefault( 'hideminor', $hideminor, $defaults, $nondefaults);
183 wfAppendToArrayIfNotDefault( 'hidebots', $hidebots, $defaults, $nondefaults);
184 wfAppendToArrayIfNotDefault( 'hideliu', $hideliu, $defaults, $nondefaults);
185 wfAppendToArrayIfNotDefault( 'hidepatrolled', $hidepatrolled, $defaults, $nondefaults);
186 wfAppendToArrayIfNotDefault( 'from', $from, $defaults, $nondefaults);
187 wfAppendToArrayIfNotDefault( 'namespace', $namespace, $defaults, $nondefaults);
188 wfAppendToArrayIfNotDefault( 'invert', $invert, $defaults, $nondefaults);
189
190 // Add end of the texts
191 $wgOut->addHTML( '<div class="rcoptions">' . rcOptionsPanel( $defaults, $nondefaults ) . "\n" );
192 $wgOut->addHTML( rcNamespaceForm( $namespace, $invert, $nondefaults) . '</div>'."\n");
193 }
194
195 // And now for the content
196 $sk = $wgUser->getSkin();
197 $wgOut->setSyndicated( true );
198
199 if ( $wgUser->getOption('usenewrc') ) {
200 $list =& new EnhancedChangesList( $sk );
201 } else {
202 $list =& new OldChangesList( $sk );
203 }
204
205 $s = $list->beginRecentChangesList();
206 $counter = 1;
207 foreach( $rows as $obj ){
208 if( $limit == 0) {
209 break;
210 }
211
212 if ( ! ( $hideminor && $obj->rc_minor ) &&
213 ! ( $hidepatrolled && $obj->rc_patrolled ) ) {
214 $rc = RecentChange::newFromRow( $obj );
215 $rc->counter = $counter++;
216
217 if ($wgShowUpdatedMarker
218 && !empty( $obj->wl_notificationtimestamp )
219 && ($obj->rc_timestamp >= $obj->wl_notificationtimestamp)) {
220 $rc->notificationtimestamp = true;
221 } else {
222 $rc->notificationtimestamp = false;
223 }
224
225 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
226 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" . $dbr->strencode($obj->rc_title) ."' AND wl_namespace=$obj->rc_namespace" ;
227 $res3 = $dbr->query( $sql3, 'wfSpecialRecentChanges');
228 $x = $dbr->fetchObject( $res3 );
229 $rc->numberofWatchingusers = $x->n;
230 } else {
231 $rc->numberofWatchingusers = 0;
232 }
233 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user ) );
234 --$limit;
235 }
236 }
237 $s .= $list->endRecentChangesList();
238 $wgOut->addHTML( $s );
239 }
240 }
241
242 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
243 global $messageMemc, $wgDBname, $wgFeedCacheTimeout;
244 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
245
246 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
247 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
248 return false;
249 }
250
251 $timekey = "$wgDBname:rcfeed:$feedFormat:timestamp";
252 $key = "$wgDBname:rcfeed:$feedFormat:limit:$limit:minor:$hideminor";
253
254 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
255 ' [' . $wgContLanguageCode . ']';
256 $feed = new $wgFeedClasses[$feedFormat](
257 $feedTitle,
258 htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
259 $wgTitle->getFullUrl() );
260
261 /**
262 * Bumping around loading up diffs can be pretty slow, so where
263 * possible we want to cache the feed output so the next visitor
264 * gets it quick too.
265 */
266 $cachedFeed = false;
267 if( $feedLastmod = $messageMemc->get( $timekey ) ) {
268 /**
269 * If the cached feed was rendered very recently, we may
270 * go ahead and use it even if there have been edits made
271 * since it was rendered. This keeps a swarm of requests
272 * from being too bad on a super-frequently edited wiki.
273 */
274 if( time() - wfTimestamp( TS_UNIX, $feedLastmod )
275 < $wgFeedCacheTimeout
276 || wfTimestamp( TS_UNIX, $feedLastmod )
277 > wfTimestamp( TS_UNIX, $lastmod ) ) {
278 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
279 $cachedFeed = $messageMemc->get( $key );
280 } else {
281 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
282 }
283 }
284 if( is_string( $cachedFeed ) ) {
285 wfDebug( "RC: Outputting cached feed\n" );
286 $feed->httpHeaders();
287 echo $cachedFeed;
288 } else {
289 wfDebug( "RC: rendering new feed and caching it\n" );
290 ob_start();
291 rcDoOutputFeed( $rows, $feed );
292 $cachedFeed = ob_get_contents();
293 ob_end_flush();
294
295 $expire = 3600 * 24; # One day
296 $messageMemc->set( $key, $cachedFeed );
297 $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
298 }
299 return true;
300 }
301
302 function rcDoOutputFeed( $rows, &$feed ) {
303 global $wgSitename, $wgFeedClasses, $wgContLanguageCode;
304
305 $feed->outHeader();
306
307 # Merge adjacent edits by one user
308 $sorted = array();
309 $n = 0;
310 foreach( $rows as $obj ) {
311 if( $n > 0 &&
312 $obj->rc_namespace >= 0 &&
313 $obj->rc_cur_id == $sorted[$n-1]->rc_cur_id &&
314 $obj->rc_user_text == $sorted[$n-1]->rc_user_text ) {
315 $sorted[$n-1]->rc_last_oldid = $obj->rc_last_oldid;
316 } else {
317 $sorted[$n] = $obj;
318 $n++;
319 }
320 $first = false;
321 }
322
323 foreach( $sorted as $obj ) {
324 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
325 $talkpage = $title->getTalkPage();
326 $item = new FeedItem(
327 $title->getPrefixedText(),
328 rcFormatDiff( $obj ),
329 $title->getFullURL(),
330 $obj->rc_timestamp,
331 $obj->rc_user_text,
332 $talkpage->getFullURL()
333 );
334 $feed->outItem( $item );
335 }
336 $feed->outFooter();
337 }
338
339 /**
340 *
341 */
342 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
343 global $wgUser, $wgLang, $wgContLang;
344 $sk = $wgUser->getSkin();
345 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
346 ($lim ? $wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
347 ($d ? "days={$d}&" : '') . 'limit='.$lim );
348 return $s;
349 }
350
351 /**
352 *
353 */
354 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
355 global $wgUser, $wgLang, $wgContLang;
356 $sk = $wgUser->getSkin();
357 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
358 ($d ? $wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
359 ($lim ? '&limit='.$lim : '') );
360 return $s;
361 }
362
363 /**
364 * Used by Recentchangeslinked
365 */
366 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
367 $botLink = '', $liuLink = '', $patrLink = '' ) {
368 if ($more != '') $more .= '&';
369 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
370 rcCountLink( 100, $days, $page, $more ) . ' | ' .
371 rcCountLink( 250, $days, $page, $more ) . ' | ' .
372 rcCountLink( 500, $days, $page, $more ) .
373 ( $doall ? ( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
374 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
375 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
376 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
377 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
378 rcDaysLink( $limit, 30, $page, $more ) .
379 ( $doall ? ( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
380 $shm = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
381 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
382 return $note;
383 }
384
385
386 /**
387 * Makes change an option link which carries all the other options
388 */
389 function makeOptionsLink( $title, $override, $options ) {
390 global $wgUser, $wgLang, $wgContLang;
391 $sk = $wgUser->getSkin();
392 return $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
393 $title, wfArrayToCGI( $override, $options ) );
394 }
395
396 /**
397 * Creates the options panel
398 */
399 function rcOptionsPanel( $defaults, $nondefaults ) {
400 global $wgLang;
401
402 $options = $nondefaults + $defaults;
403
404 if( $options['from'] )
405 $note = wfMsg( 'rcnotefrom', $wgLang->formatNum( $options['limit'] ), $wgLang->timeanddate( $options['from'], true ) );
406 else
407 $note = wfMsg( 'rcnote', $wgLang->formatNum( $options['limit'] ), $wgLang->formatNum( $options['days'] ) );
408
409 // limit links
410 $cl = '';
411 $options_limit = array(50, 100, 250, 500);
412 $i = 0;
413 while ( $i+1 < count($options_limit) ) {
414 $cl .= makeOptionsLink( $options_limit[$i], array( 'limit' => $options_limit[$i] ), $nondefaults) . ' | ' ;
415 $i++;
416 }
417 $cl .= makeOptionsLink( $options_limit[$i], array( 'limit' => $options_limit[$i] ), $nondefaults) ;
418
419 // day links, reset 'from' to none
420 $dl = '';
421 $options_days = array(1, 3, 7, 14, 30);
422 $i = 0;
423 while ( $i+1 < count($options_days) ) {
424 $dl .= makeOptionsLink( $options_days[$i], array( 'days' => $options_days[$i], 'from' => '' ), $nondefaults) . ' | ' ;
425 $i++;
426 }
427 $dl .= makeOptionsLink( $options_days[$i], array( 'days' => $options_days[$i], 'from' => '' ), $nondefaults) ;
428
429 // show/hide links
430 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
431 $minorLink = makeOptionsLink( $showhide[1-$options['hideminor']],
432 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
433 $botLink = makeOptionsLink( $showhide[1-$options['hidebots']],
434 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
435 $liuLink = makeOptionsLink( $showhide[1-$options['hideliu']],
436 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
437 $patrLink = makeOptionsLink( $showhide[1-$options['hidepatrolled']],
438 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
439
440 $hl = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
441
442 // show from this onward link
443 $now = $wgLang->timeanddate( wfTimestampNow(), true );
444 $tl = makeOptionsLink( $now, array( 'from' => wfTimestampNow()), $nondefaults );
445
446 $rclinks = wfMsg( 'rclinks', $cl, $dl, $hl );
447 $rclistfrom = wfMsg( 'rclistfrom', $tl );
448 return "$note<br />$rclinks<br />$rclistfrom";
449
450 }
451
452 /**<F2>
453 * Creates the choose namespace selection
454 *
455 * @access private
456 *
457 * @param mixed $namespace The key of the currently selected namespace, empty string
458 * if there is none
459 * @param bool $invert Whether to invert the namespace selection
460 * @param array $nondefaults An array of non default options to be remembered
461 *
462 * @return string
463 */
464 function rcNamespaceForm ( $namespace, $invert, $nondefaults ) {
465 global $wgContLang, $wgScript;
466 $t = Title::makeTitle( NS_SPECIAL, 'Recentchanges' );
467
468 $namespaceselect = HTMLnamespaceselector($namespace, '');
469 $submitbutton = '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . "\" />\n";
470 $invertbox = "<input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ? ' checked="checked"' : '' ) . ' />';
471
472 $out = "<div class='namespacesettings'><form method='get' action='{$wgScript}'>\n";
473
474 foreach ( $nondefaults as $key => $value ) {
475 if ($key != 'namespace' && $key != 'invert')
476 $out .= wfElement('input', array( 'type' => 'hidden', 'name' => $key, 'value' => $value));
477 }
478
479 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
480 $out .= "
481 <div id='nsselect' class='recentchanges'>
482 <label for='namespace'>" . wfMsgHtml('namespace') . "</label>
483 {$namespaceselect}{$submitbutton}{$invertbox} <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>\n</div>";
484 $out .= '</form></div>';
485 return $out;
486 }
487
488
489 /**
490 * Format a diff for the newsfeed
491 */
492 function rcFormatDiff( $row ) {
493 $fname = 'rcFormatDiff';
494 wfProfileIn( $fname );
495
496 require_once( 'DifferenceEngine.php' );
497 $comment = '<p>' . htmlspecialchars( $row->rc_comment ) . "</p>\n";
498
499 if( $row->rc_namespace >= 0 ) {
500 global $wgContLang;
501
502 #$diff =& new DifferenceEngine( $row->rc_this_oldid, $row->rc_last_oldid, $row->rc_id );
503 #$diff->showDiffPage();
504
505 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
506 $dbr =& wfGetDB( DB_SLAVE );
507 $newrev = Revision::newFromTitle( $titleObj, $row->rc_this_oldid );
508 if( $newrev ) {
509 $newtext = $newrev->getText();
510 } else {
511 $diffText = "<p>Can't load revision $row->rc_this_oldid</p>";
512 wfProfileOut( $fname );
513 return $comment . $diffText;
514 }
515
516 if( $row->rc_last_oldid ) {
517 wfProfileIn( "$fname-dodiff" );
518 $oldrev =& Revision::newFromId( $row->rc_last_oldid );
519 if( !$oldrev ) {
520 $diffText = "<p>Can't load old revision $row->rc_last_oldid</p>";
521 wfProfileOut( $fname );
522 return $comment . $diffText;
523 }
524 $oldtext = $oldrev->getText();
525
526 # Old entries may contain illegal characters
527 # which will damage output
528 $oldtext = UtfNormal::cleanUp( $oldtext );
529
530 global $wgFeedDiffCutoff;
531 if( strlen( $newtext ) > $wgFeedDiffCutoff ||
532 strlen( $oldtext ) > $wgFeedDiffCutoff ) {
533 $diffLink = $titleObj->escapeFullUrl(
534 'diff=' . $row->rc_this_oldid .
535 '&oldid=' . $row->rc_last_oldid );
536 $diffText = '<a href="' .
537 $diffLink .
538 '">' .
539 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
540 '</a>';
541 } else {
542 $diffText = DifferenceEngine::getDiff( $oldtext, $newtext,
543 wfMsg( 'revisionasof', $wgContLang->timeanddate( $row->rc_timestamp ) ),
544 wfMsg( 'currentrev' ) );
545 }
546 wfProfileOut( "$fname-dodiff" );
547 } else {
548 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
549 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
550 }
551
552 wfProfileOut( $fname );
553 return $comment . $diffText;
554 }
555
556 wfProfileOut( $fname );
557 return $comment;
558 }
559
560 ?>