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