Autodiscovery <link> for RSS feed. Added helper functions for query stuff:
[mediawiki.git] / includes / SpecialRecentchanges.php
blob46a57071a4412bd607b39f6fd8df5ea54248c37a
1 <?php
3 include_once( "Feed.php" );
5 function wfSpecialRecentchanges( $par )
7 global $wgUser, $wgOut, $wgLang, $wgTitle, $wgMemc, $wgDBname;
8 global $wgRequest, $wgSitename, $wgLanguageCode;
9 global $days, $hideminor, $from, $hidebots, $hideliu; # From query string
10 $fname = "wfSpecialRecentchanges";
12 $feedFormat = $wgRequest->getVal( "feed" );
13 $feeding = ( $feedFormat == "rss" );
15 if( $par ) {
16 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
17 if( in_array( "hidebots", $bits ) ) $hidebots = 1;
18 if( in_array( "bots", $bits ) ) $hidebots = 0;
19 if( in_array( "hideminor", $bits ) ) $hideminor = 1;
20 if( in_array( "minor", $bits ) ) $hideminor = 0;
21 if( in_array( "hideliu", $bits) ) $hideliu = 1;
24 $sql = "SELECT MAX(rc_timestamp) AS lastmod FROM recentchanges";
25 $res = wfQuery( $sql, DB_READ, $fname );
26 $s = wfFetchObject( $res );
27 # 10 seconds server-side caching max
28 $wgOut->setSquidMaxage( 10 );
29 if( $wgOut->checkLastModified( $s->lastmod ) ){
30 # Client cache fresh and headers sent, nothing more to do.
31 return;
34 $rctext = wfMsg( "recentchangestext" );
36 # The next few lines can probably be commented out now that wfMsg can get text from the DB
37 $sql = "SELECT cur_text FROM cur WHERE cur_namespace=4 AND cur_title='Recentchanges'";
38 $res = wfQuery( $sql, DB_READ, $fname );
39 if( ( $s = wfFetchObject( $res ) ) and ( $s->cur_text != "" ) ) {
40 $rctext = $s->cur_text;
43 $wgOut->addWikiText( $rctext );
45 if ( ! $days ) {
46 $days = $wgUser->getOption( "rcdays" );
47 if ( ! $days ) { $days = 3; }
49 $days = (int)$days;
50 list( $limit, $offset ) = wfCheckLimits( 100, "rclimit" );
51 $now = wfTimestampNow();
52 $cutoff_unixtime = time() - ( $days * 86400 );
53 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime % 86400);
54 $cutoff = wfUnix2Timestamp( $cutoff_unixtime );
55 if(preg_match('/^[0-9]{14}$/', $from) and $from > $cutoff) {
56 $cutoff = $from;
57 } else {
58 unset($from);
61 $sk = $wgUser->getSkin();
62 $showhide = array( wfMsg( "show" ), wfMsg( "hide" ));
64 if ( ! isset( $hideminor ) ) {
65 $hideminor = $wgUser->getOption( "hideminor" );
67 $hideminor = ($hideminor ? 1 : 0);
68 if ( $hideminor ) {
69 $hidem = "AND rc_minor=0";
70 } else {
71 $hidem = "";
74 if ( !isset( $hidebots ) ) {
75 $hidebots = 1;
77 if( $hidebots ) {
78 $hidem .= " AND rc_bot=0";
80 $hidebots = ($hidebots ? 1 : 0);
82 if ( !isset( $hideliu ) ) {
83 $hideliu = 0;
85 if ( $hideliu ) {
86 $hidem .= " AND rc_user=0";
88 $hideliu = ($hideliu ? 1 : 0);
89 #$hideparams = "hideminor={$hideminor}&hideliu={$hideliu}&hidebots={$hidebots}";
90 $urlparams = array( "hideminor" => $hideminor, "hideliu" => $hideliu, "hidebots" => $hidebots );
91 $hideparams = wfArrayToCGI( $urlparams );
93 $minorLink = $sk->makeKnownLink( $wgLang->specialPage( "Recentchanges" ),
94 $showhide[1-$hideminor], wfArrayToCGI( array( "hideminor" => 1-$hideminor ), $urlparams ) );
95 $botLink = $sk->makeKnownLink( $wgLang->specialPage( "Recentchanges" ),
96 $showhide[1-$hidebots], wfArrayToCGI( array( "hidebots" => 1-$hidebots ), $urlparams ) );
97 $liuLink = $sk->makeKnownLink( $wgLang->specialPage( "Recentchanges" ),
98 $showhide[1-$hideliu], wfArrayToCGI( array( "hideliu" => 1-$hideliu ), $urlparams ) );
100 $uid = $wgUser->getID();
101 $sql2 = "SELECT recentchanges.*" . ($uid ? ",wl_user" : "") . " FROM recentchanges " .
102 ($uid ? "LEFT OUTER JOIN watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace & 65534 " : "") .
103 "WHERE rc_timestamp > '{$cutoff}' {$hidem} " .
104 "ORDER BY rc_timestamp DESC LIMIT {$limit}";
106 $res = wfQuery( $sql2, DB_READ, $fname );
107 $rows = array();
108 while( $row = wfFetchObject( $res ) ){
109 $rows[] = $row;
112 if(isset($from)) {
113 $note = wfMsg( "rcnotefrom", $wgLang->formatNum( $limit ),
114 $wgLang->timeanddate( $from, true ) );
115 } else {
116 $note = wfMsg( "rcnote", $wgLang->formatNum( $limit ), $wgLang->formatNum( $days ) );
118 $wgOut->addHTML( "\n<hr>\n{$note}\n<br>" );
120 $note = rcDayLimitLinks( $days, $limit, "Recentchanges", $hideparams, false, $minorLink, $botLink, $liuLink );
122 $note .= "<br>\n" . wfMsg( "rclistfrom",
123 $sk->makeKnownLink( $wgLang->specialPage( "Recentchanges" ),
124 $wgLang->timeanddate( $now, true ), "{$hideparams}&from=$now" ) );
126 $wgOut->addHTML( "{$note}\n" );
128 if( $feeding ) {
129 $wgOut->disable();
131 $feed = new RSSFeed(
132 $wgSitename . " - " . wfMsg( "recentchanges" ) . " [" . $wgLanguageCode . "]",
133 htmlspecialchars( wfMsg( "recentchangestext" ) ),
134 $wgTitle->getFullUrl() );
135 $feed->outHeader();
136 foreach( $rows as $obj ) {
137 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
138 $item = new FeedItem(
139 $title->getPrefixedText(),
140 htmlspecialchars( $obj->rc_comment ),
141 $title->getFullURL(),
142 $obj->rc_timestamp,
143 $obj->rc_user_text );
144 $feed->outItem( $item );
146 $feed->outFooter();
147 } else {
148 $wgOut->setSyndicated( true );
149 $s = $sk->beginRecentChangesList();
150 foreach( $rows as $obj ){
151 if( $limit == 0) {
152 break;
155 if ( ! ( $hideminor && $obj->rc_minor ) ) {
156 $rc = RecentChange::newFromRow( $obj );
157 $s .= $sk->recentChangesLine( $rc, !empty( $obj->wl_user ) );
158 --$limit;
161 $s .= $sk->endRecentChangesList();
162 $wgOut->addHTML( $s );
164 wfFreeResult( $res );
167 function rcCountLink( $lim, $d, $page="Recentchanges", $more="" )
169 global $wgUser, $wgLang;
170 $sk = $wgUser->getSkin();
171 $s = $sk->makeKnownLink( $wgLang->specialPage( $page ),
172 ($lim ? $wgLang->formatNum( "{$lim}" ) : wfMsg( "all" ) ), "{$more}" .
173 ($d ? "days={$d}&" : "") . "limit={$lim}" );
174 return $s;
177 function rcDaysLink( $lim, $d, $page="Recentchanges", $more="" )
179 global $wgUser, $wgLang;
180 $sk = $wgUser->getSkin();
181 $s = $sk->makeKnownLink( $wgLang->specialPage( $page ),
182 ($d ? $wgLang->formatNum( "{$d}" ) : wfMsg( "all" ) ), "{$more}days={$d}" .
183 ($lim ? "&limit={$lim}" : "") );
184 return $s;
187 function rcDayLimitLinks( $days, $limit, $page="Recentchanges", $more="", $doall = false, $minorLink = "",
188 $botLink = "", $liuLink = "" )
190 if ($more != "") $more .= "&";
191 $cl = rcCountLink( 50, $days, $page, $more ) . " | " .
192 rcCountLink( 100, $days, $page, $more ) . " | " .
193 rcCountLink( 250, $days, $page, $more ) . " | " .
194 rcCountLink( 500, $days, $page, $more ) .
195 ( $doall ? ( " | " . rcCountLink( 0, $days, $page, $more ) ) : "" );
196 $dl = rcDaysLink( $limit, 1, $page, $more ) . " | " .
197 rcDaysLink( $limit, 3, $page, $more ) . " | " .
198 rcDaysLink( $limit, 7, $page, $more ) . " | " .
199 rcDaysLink( $limit, 14, $page, $more ) . " | " .
200 rcDaysLink( $limit, 30, $page, $more ) .
201 ( $doall ? ( " | " . rcDaysLink( $limit, 0, $page, $more ) ) : "" );
202 $shm = wfMsg( "showhideminor", $minorLink, $botLink, $liuLink );
203 $note = wfMsg( "rclinks", $cl, $dl, $shm );
204 return $note;
207 # Obsolete? Isn't called from anywhere and $mlink isn't defined
208 function rcLimitLinks( $page="Recentchanges", $more="", $doall = false )
210 if ($more != "") $more .= "&";
211 $cl = rcCountLink( 50, 0, $page, $more ) . " | " .
212 rcCountLink( 100, 0, $page, $more ) . " | " .
213 rcCountLink( 250, 0, $page, $more ) . " | " .
214 rcCountLink( 500, 0, $page, $more ) .
215 ( $doall ? ( " | " . rcCountLink( 0, $days, $page, $more ) ) : "" );
216 $note = wfMsg( "rclinks", $cl, "", $mlink );
217 return $note;