public / private / static stuff when loading Special:Recentchanges
[lhc/web/wiklou.git] / includes / Feed.php
1 <?php
2 # Basic support for outputting syndication feeds in RSS, other formats
3 #
4 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
5 # http://www.mediawiki.org/
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # http://www.gnu.org/copyleft/gpl.html
21
22 /**
23 * Contain a feed class as well as classes to build rss / atom ... feeds
24 * Available feeds are defined in Defines.php
25 * @package MediaWiki
26 */
27
28
29 /**
30 * @todo document
31 * @package MediaWiki
32 */
33 class FeedItem {
34 /**#@+
35 * @var string
36 * @private
37 */
38 private
39 $Author = '',
40 $Date = '',
41 $Description = '',
42 $Title = 'Wiki',
43 $Url = '' ;
44 /**#@-*/
45
46 /**#@+
47 * @todo document
48 */
49 function FeedItem( $Title, $Description, $Url, $Date = '', $Author = '', $Comments = '' ) {
50 $this->Title = $Title;
51 $this->Description = $Description;
52 $this->Url = $Url;
53 $this->Date = $Date;
54 $this->Author = $Author;
55 $this->Comments = $Comments;
56 }
57
58 /**
59 * @static
60 */
61 function xmlEncode( $string ) {
62 $string = str_replace( "\r\n", "\n", $string );
63 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
64 return htmlspecialchars( $string );
65 }
66
67 function getTitle() { return $this->xmlEncode( $this->Title ); }
68 function getUrl() { return $this->xmlEncode( $this->Url ); }
69 function getDescription() { return $this->xmlEncode( $this->Description ); }
70 function getLanguage() {
71 global $wgContLanguageCode;
72 return $wgContLanguageCode;
73 }
74 function getDate() { return $this->Date; }
75 function getAuthor() { return $this->xmlEncode( $this->Author ); }
76 function getComments() { return $this->xmlEncode( $this->Comments ); }
77 /**#@-*/
78 }
79
80 /**
81 * @todo document
82 * @package MediaWiki
83 */
84 class ChannelFeed extends FeedItem {
85 /**#@+
86 * Abstract function, override!
87 * @abstract
88 */
89
90 /**
91 * Generate Header of the feed
92 */
93 function outHeader() {
94 # print "<feed>";
95 }
96
97 /**
98 * Generate an item
99 * @param $item
100 */
101 function outItem( $item ) {
102 # print "<item>...</item>";
103 }
104
105 /**
106 * Generate Footer of the feed
107 */
108 function outFooter() {
109 # print "</feed>";
110 }
111 /**#@-*/
112
113 /**
114 * Setup and send HTTP headers. Don't send any content;
115 * content might end up being cached and re-sent with
116 * these same headers later.
117 *
118 * This should be called from the outHeader() method,
119 * but can also be called separately.
120 *
121 * @public
122 */
123 function httpHeaders() {
124 global $wgOut;
125
126 # We take over from $wgOut, excepting its cache header info
127 $wgOut->disable();
128 $mimetype = $this->contentType();
129 header( "Content-type: $mimetype; charset=UTF-8" );
130 $wgOut->sendCacheControl();
131
132 }
133
134 /**
135 * Return an internet media type to be sent in the headers.
136 *
137 * @return string
138 * @private
139 */
140 function contentType() {
141 global $wgRequest;
142 $ctype = $wgRequest->getVal('ctype','application/xml');
143 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
144 return (in_array($ctype, $allowedctypes) ? $ctype : 'application/xml');
145 }
146
147 /**
148 * Output the initial XML headers with a stylesheet for legibility
149 * if someone finds it in a browser.
150 * @private
151 */
152 function outXmlHeader() {
153 global $wgServer, $wgStylePath;
154
155 $this->httpHeaders();
156 echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
157 echo '<?xml-stylesheet type="text/css" href="' .
158 htmlspecialchars( "$wgServer$wgStylePath/common/feed.css" ) . '"?' . ">\n";
159 }
160 }
161
162 /**
163 * Generate a RSS feed
164 * @todo document
165 * @package MediaWiki
166 */
167 class RSSFeed extends ChannelFeed {
168
169 /**
170 * Format a date given a timestamp
171 * @param integer $ts Timestamp
172 * @return string Date string
173 */
174 function formatTime( $ts ) {
175 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
176 }
177
178 /**
179 * Ouput an RSS 2.0 header
180 */
181 function outHeader() {
182 global $wgVersion;
183
184 $this->outXmlHeader();
185 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
186 <channel>
187 <title><?php print $this->getTitle() ?></title>
188 <link><?php print $this->getUrl() ?></link>
189 <description><?php print $this->getDescription() ?></description>
190 <language><?php print $this->getLanguage() ?></language>
191 <generator>MediaWiki <?php print $wgVersion ?></generator>
192 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
193 <?php
194 }
195
196 /**
197 * Output an RSS 2.0 item
198 * @param FeedItem item to be output
199 */
200 function outItem( $item ) {
201 ?>
202 <item>
203 <title><?php print $item->getTitle() ?></title>
204 <link><?php print $item->getUrl() ?></link>
205 <description><?php print $item->getDescription() ?></description>
206 <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
207 <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
208 <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
209 </item>
210 <?php
211 }
212
213 /**
214 * Ouput an RSS 2.0 footer
215 */
216 function outFooter() {
217 ?>
218 </channel>
219 </rss><?php
220 }
221 }
222
223 /**
224 * Generate an Atom feed
225 * @todo document
226 * @package MediaWiki
227 */
228 class AtomFeed extends ChannelFeed {
229 /**
230 * @todo document
231 */
232 function formatTime( $ts ) {
233 // need to use RFC 822 time format at least for rss2.0
234 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
235 }
236
237 /**
238 * Outputs a basic header for Atom 1.0 feeds.
239 */
240 function outHeader() {
241 global $wgVersion;
242
243 $this->outXmlHeader();
244 ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
245 <id><?php print $this->getFeedId() ?></id>
246 <title><?php print $this->getTitle() ?></title>
247 <link rel="self" type="application/atom+xml" href="<?php print $this->getSelfUrl() ?>"/>
248 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
249 <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
250 <subtitle><?php print $this->getDescription() ?></subtitle>
251 <generator>MediaWiki <?php print $wgVersion ?></generator>
252
253 <?php
254 }
255
256 /**
257 * Atom 1.0 requires a unique, opaque IRI as a unique indentifier
258 * for every feed we create. For now just use the URL, but who
259 * can tell if that's right? If we put options on the feed, do we
260 * have to change the id? Maybe? Maybe not.
261 *
262 * @return string
263 * @private
264 */
265 function getFeedId() {
266 return $this->getSelfUrl();
267 }
268
269 /**
270 * Atom 1.0 requests a self-reference to the feed.
271 * @return string
272 * @private
273 */
274 function getSelfUrl() {
275 global $wgRequest;
276 return htmlspecialchars( $wgRequest->getFullRequestURL() );
277 }
278
279 /**
280 * Output a given item.
281 * @param $item
282 */
283 function outItem( $item ) {
284 global $wgMimeType;
285 ?>
286 <entry>
287 <id><?php print $item->getUrl() ?></id>
288 <title><?php print $item->getTitle() ?></title>
289 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print $item->getUrl() ?>"/>
290 <?php if( $item->getDate() ) { ?>
291 <updated><?php print $this->formatTime( $item->getDate() ) ?>Z</updated>
292 <?php } ?>
293
294 <summary type="html"><?php print $item->getDescription() ?></summary>
295 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name></author><?php }?>
296 </entry>
297
298 <?php /* FIXME need to add comments
299 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
300 */
301 }
302
303 /**
304 * Outputs the footer for Atom 1.0 feed (basicly '\</feed\>').
305 */
306 function outFooter() {?>
307 </feed><?php
308 }
309 }
310
311 ?>