(bug 454) Merge e-notif 2.00
[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
14 /**
15 * Constructor
16 */
17 function wfSpecialRecentchanges( $par ) {
18 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgTitle, $wgMemc, $wgDBname;
19 global $wgRequest, $wgSitename, $wgLanguageCode, $wgContLanguageCode;
20 global $wgFeedClasses, $wgUseRCPatrol;
21 global $wgRCUseModStyle, $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
22 $fname = 'wfSpecialRecentchanges';
23
24 # Get query parameters
25 $feedFormat = $wgRequest->getVal( 'feed' );
26
27 $defaultDays = $wgUser->getOption( 'rcdays' );
28 if ( !$defaultDays ) { $defaultDays = 3; }
29
30 $days = $wgRequest->getInt( 'days', $defaultDays );
31 $hideminor = $wgRequest->getBool( 'hideminor', $wgUser->getOption( 'hideminor' ) ) ? 1 : 0;
32 list( $limit, $offset ) = wfCheckLimits( 100, 'rclimit' );
33
34 # As a feed, use limited settings only
35 if( $feedFormat ) {
36 $from = null;
37 $hidebots = 1;
38 $hideliu = 0;
39 $hidepatrolled = 0;
40 global $wgFeedLimit;
41 if( $limit > $wgFeedLimit ) {
42 $limit = $wgFeedLimit;
43 }
44 } else {
45 $from = $wgRequest->getText( 'from' );
46 $hidebots = $wgRequest->getBool( 'hidebots', true ) ? 1 : 0;
47 $hideliu = $wgRequest->getBool( 'hideliu', false ) ? 1 : 0;
48 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled', false ) ? 1 : 0;
49
50 # Get query parameters from path
51 if( $par ) {
52 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
53 if( in_array( 'hidebots', $bits ) ) $hidebots = 1;
54 if( in_array( 'bots', $bits ) ) $hidebots = 0;
55 if( in_array( 'hideminor', $bits ) ) $hideminor = 1;
56 if( in_array( 'minor', $bits ) ) $hideminor = 0;
57 if( in_array( 'hideliu', $bits) ) $hideliu = 1;
58 if( in_array( 'hidepatrolled', $bits) ) $hidepatrolled = 1;
59 }
60 }
61
62
63 # Database connection and caching
64 $dbr =& wfGetDB( DB_SLAVE );
65 extract( $dbr->tableNames( 'recentchanges', 'watchlist' ) );
66
67
68 # 10 seconds server-side caching max
69 $wgOut->setSquidMaxage( 10 );
70
71 # Get last modified date, for client caching
72 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
73 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, $fname );
74 if ( $feedFormat || !$wgUseRCPatrol ) {
75 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
76 # Client cache fresh and headers sent, nothing more to do.
77 return;
78 }
79 }
80
81 # Output header
82 $rctext = wfMsg( "recentchangestext" );
83 $wgOut->addWikiText( $rctext );
84
85
86 $now = wfTimestampNow();
87 $cutoff_unixtime = time() - ( $days * 86400 );
88 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime % 86400);
89 $cutoff = $dbr->timestamp( $cutoff_unixtime );
90 if(preg_match('/^[0-9]{14}$/', $from) and $from > wfTimestamp(TS_MW,$cutoff)) {
91 $cutoff = $dbr->timestamp($from);
92 } else {
93 unset($from);
94 }
95
96 $sk = $wgUser->getSkin();
97
98 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
99
100 $hidem = ( $hideminor ) ? 'AND rc_minor=0' : '';
101 $hidem .= ( $hidebots ) ? ' AND rc_bot=0' : '';
102 $hidem .= ( $hideliu ) ? ' AND rc_user=0' : '';
103 $hidem .= ( $hidepatrolled )? ' AND rc_patrolled=0' : '';
104
105 $urlparams = array( 'hideminor' => $hideminor, 'hideliu' => $hideliu,
106 'hidebots' => $hidebots, 'hidepatrolled' => $hidepatrolled,
107 'limit' => $limit );
108 $hideparams = wfArrayToCGI( $urlparams );
109
110 $minorLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
111 $showhide[1-$hideminor], wfArrayToCGI( array( 'hideminor' => 1-$hideminor ), $urlparams ) );
112 $botLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
113 $showhide[1-$hidebots], wfArrayToCGI( array( 'hidebots' => 1-$hidebots ), $urlparams ) );
114 $liuLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
115 $showhide[1-$hideliu], wfArrayToCGI( array( 'hideliu' => 1-$hideliu ), $urlparams ) );
116 $patrLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
117 $showhide[1-$hidepatrolled], wfArrayToCGI( array( 'hidepatrolled' => 1-$hidepatrolled ), $urlparams ) );
118
119 $RCUseModStyle = ($wgRCUseModStyle && $wgUser->getOption('rcusemodstyle')) ? 'AND rc_this_oldid=0 ' : '' ;
120
121 $uid = $wgUser->getID();
122 # Patch for showing "updated since last visit" marker
123 $sql2 = "SELECT $recentchanges.*" . ($uid ? ",wl_user,wl_notificationtimestamp" : "") . " FROM $recentchanges " .
124 ($uid ? "LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "") .
125 "WHERE rc_timestamp > '{$cutoff}' {$hidem} " . $RCUseModStyle .
126 "ORDER BY rc_timestamp DESC LIMIT {$limit}";
127
128 $res = $dbr->query( $sql2, DB_SLAVE, $fname );
129 $rows = array();
130 while( $row = $dbr->fetchObject( $res ) ){
131 $rows[] = $row;
132 }
133 $dbr->freeResult( $res );
134
135 if(isset($from)) {
136 $note = wfMsg( 'rcnotefrom', $wgLang->formatNum( $limit ),
137 $wgLang->timeanddate( $from, true ) );
138 } else {
139 $note = wfMsg( 'rcnote', $wgLang->formatNum( $limit ), $wgLang->formatNum( $days ) );
140 }
141 $wgOut->addHTML( "\n<hr />\n{$note}\n<br />" );
142
143 $note = rcDayLimitLinks( $days, $limit, 'Recentchanges', $hideparams, false, $minorLink, $botLink, $liuLink, $patrLink );
144
145 $note .= "<br />\n" . wfMsg( 'rclistfrom',
146 $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
147 $wgLang->timeanddate( $now, true ), $hideparams.'&from='.$now ) );
148
149 $wgOut->addHTML( $note."\n" );
150
151 if( $feedFormat ) {
152 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
153 } else {
154 # Web output...
155 $wgOut->setSyndicated( true );
156 $list =& new ChangesList( $sk );
157 $s = $list->beginRecentChangesList();
158 $counter = 1;
159 foreach( $rows as $obj ){
160 if( $limit == 0) {
161 break;
162 }
163
164 if ( ! ( $hideminor && $obj->rc_minor ) &&
165 ! ( $hidepatrolled && $obj->rc_patrolled ) ) {
166 $rc = RecentChange::newFromRow( $obj );
167 $rc->counter = $counter++;
168
169 if ($wgShowUpdatedMarker
170 && $wgUser->getOption( 'showupdated' )
171 && $obj->wl_notificationtimestamp
172 && ($obj->rc_timestamp >= $obj->wl_notificationtimestamp)) {
173 $rc->notificationtimestamp = true;
174 } else {
175 $rc->notificationtimestamp = false;
176 }
177
178 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
179 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" . $dbr->strencode($obj->rc_title) ."' AND wl_namespace=$obj->rc_namespace" ;
180 $res3 = $dbr->query( $sql3, 'wfSpecialRecentChanges');
181 $x = $dbr->fetchObject( $res3 );
182 $rc->numberofWatchingusers = $x->n;
183 } else {
184 $rc->numberofWatchingusers = 0;
185 }
186 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user ) );
187 --$limit;
188 }
189 }
190 $s .= $list->endRecentChangesList();
191 $wgOut->addHTML( $s );
192 }
193 }
194
195 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
196 global $messageMemc, $wgDBname, $wgFeedCacheTimeout;
197 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
198
199 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
200 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
201 return false;
202 }
203
204 $timekey = "$wgDBname:rcfeed:timestamp";
205 $key = "$wgDBname:rcfeed:$feedFormat:limit:$limit:minor:$hideminor";
206
207 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
208 ' [' . $wgContLanguageCode . ']';
209 $feed = new $wgFeedClasses[$feedFormat](
210 $feedTitle,
211 htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
212 $wgTitle->getFullUrl() );
213
214 /**
215 * Bumping around loading up diffs can be pretty slow, so where
216 * possible we want to cache the feed output so the next visitor
217 * gets it quick too.
218 */
219 $cachedFeed = false;
220 if( $feedLastmod = $messageMemc->get( $timekey ) ) {
221 /**
222 * If the cached feed was rendered very recently, we may
223 * go ahead and use it even if there have been edits made
224 * since it was rendered. This keeps a swarm of requests
225 * from being too bad on a super-frequently edited wiki.
226 */
227 if( time() - wfTimestamp( TS_UNIX, $feedLastmod )
228 < $wgFeedCacheTimeout
229 || wfTimestamp( TS_UNIX, $feedLastmod )
230 > wfTimestamp( TS_UNIX, $lastmod ) ) {
231 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
232 $cachedFeed = $messageMemc->get( $key );
233 } else {
234 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
235 }
236 }
237 if( is_string( $cachedFeed ) ) {
238 wfDebug( "RC: Outputting cached feed\n" );
239 $feed->httpHeaders();
240 echo $cachedFeed;
241 } else {
242 wfDebug( "RC: rendering new feed and caching it\n" );
243 ob_start();
244 rcDoOutputFeed( $rows, $feed );
245 $cachedFeed = ob_get_contents();
246 ob_end_flush();
247
248 $expire = 3600 * 24; # One day
249 $messageMemc->set( $key, $cachedFeed );
250 $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
251 }
252 return true;
253 }
254
255 function rcDoOutputFeed( $rows, &$feed ) {
256 global $wgSitename, $wgFeedClasses, $wgContLanguageCode;
257
258 $feed->outHeader();
259
260 # Merge adjacent edits by one user
261 $sorted = array();
262 $n = 0;
263 foreach( $rows as $obj ) {
264 if( $n > 0 &&
265 $obj->rc_namespace >= 0 &&
266 $obj->rc_cur_id == $sorted[$n-1]->rc_cur_id &&
267 $obj->rc_user_text == $sorted[$n-1]->rc_user_text ) {
268 $sorted[$n-1]->rc_last_oldid = $obj->rc_last_oldid;
269 } else {
270 $sorted[$n] = $obj;
271 $n++;
272 }
273 $first = false;
274 }
275
276 foreach( $sorted as $obj ) {
277 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
278 $talkpage = $title->getTalkPage();
279 $item = new FeedItem(
280 $title->getPrefixedText(),
281 rcFormatDiff( $obj ),
282 $title->getFullURL(),
283 $obj->rc_timestamp,
284 $obj->rc_user_text,
285 $talkpage->getFullURL()
286 );
287 $feed->outItem( $item );
288 }
289 $feed->outFooter();
290 }
291
292 /**
293 *
294 */
295 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
296 global $wgUser, $wgLang, $wgContLang;
297 $sk = $wgUser->getSkin();
298 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
299 ($lim ? $wgLang->formatNum( "{$lim}" ) : wfMsg( 'all' ) ), "{$more}" .
300 ($d ? "days={$d}&" : '') . 'limit='.$lim );
301 return $s;
302 }
303
304 /**
305 *
306 */
307 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
308 global $wgUser, $wgLang, $wgContLang;
309 $sk = $wgUser->getSkin();
310 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
311 ($d ? $wgLang->formatNum( "{$d}" ) : wfMsg( "all" ) ), $more.'days='.$d .
312 ($lim ? '&limit='.$lim : '') );
313 return $s;
314 }
315
316 /**
317 * Used also by Recentchangeslinked
318 */
319 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
320 $botLink = '', $liuLink = '', $patrLink = '' ) {
321 if ($more != '') $more .= '&';
322 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
323 rcCountLink( 100, $days, $page, $more ) . ' | ' .
324 rcCountLink( 250, $days, $page, $more ) . ' | ' .
325 rcCountLink( 500, $days, $page, $more ) .
326 ( $doall ? ( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
327 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
328 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
329 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
330 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
331 rcDaysLink( $limit, 30, $page, $more ) .
332 ( $doall ? ( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
333 $shm = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
334 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
335 return $note;
336 }
337
338 /**
339 * Format a diff for the newsfeed
340 */
341 function rcFormatDiff( $row ) {
342 $fname = 'rcFormatDiff';
343 wfProfileIn( $fname );
344
345 require_once( 'DifferenceEngine.php' );
346 $comment = "<p>" . htmlspecialchars( $row->rc_comment ) . "</p>\n";
347
348 if( $row->rc_namespace >= 0 ) {
349 global $wgContLang;
350
351 #$diff =& new DifferenceEngine( $row->rc_this_oldid, $row->rc_last_oldid, $row->rc_id );
352 #$diff->showDiffPage();
353
354 $dbr =& wfGetDB( DB_SLAVE );
355 if( $row->rc_this_oldid ) {
356 $newrow = $dbr->selectRow( 'old',
357 array( 'old_flags', 'old_text' ),
358 array( 'old_id' => $row->rc_this_oldid ) );
359 $newtext = Article::getRevisionText( $newrow );
360 } else {
361 $newrow = $dbr->selectRow( 'cur',
362 array( 'cur_text' ),
363 array( 'cur_id' => $row->rc_cur_id ) );
364 $newtext = $newrow->cur_text;
365 }
366 if( $row->rc_last_oldid ) {
367 wfProfileIn( "$fname-dodiff" );
368 $oldrow = $dbr->selectRow( 'old',
369 array( 'old_flags', 'old_text' ),
370 array( 'old_id' => $row->rc_last_oldid ) );
371 $oldtext = Article::getRevisionText( $oldrow );
372
373 global $wgFeedDiffCutoff;
374 if( strlen( $newtext ) > $wgFeedDiffCutoff ||
375 strlen( $oldtext ) > $wgFeedDiffCutoff ) {
376 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
377 $diffLink = $titleObj->escapeFullUrl(
378 'diff=' . $row->rc_this_oldid .
379 '&oldid=' . $row->rc_last_oldid );
380 $diffText = '<a href="' .
381 $diffLink .
382 '">' .
383 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
384 '</a>';
385 } else {
386 $diffText = DifferenceEngine::getDiff( $oldtext, $newtext,
387 wfMsg( 'revisionasof', $wgContLang->timeanddate( $row->rc_timestamp ) ),
388 wfMsg( 'currentrev' ) );
389 }
390 wfProfileOut( "$fname-dodiff" );
391 } else {
392 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
393 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
394 }
395
396 wfProfileOut( $fname );
397 return $comment . $diffText;
398 }
399
400 wfProfileOut( $fname );
401 return $comment;
402 }
403
404 ?>