(bug 15128) Documentation for xmlEncode is wrong, it's not static.
[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 function xmlEncode( $string ) {
56 $string = str_replace( "\r\n", "\n", $string );
57 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
58 return htmlspecialchars( $string );
59 }
60
61 function getTitle() { return $this->xmlEncode( $this->Title ); }
62 function getUrl() { return $this->xmlEncode( $this->Url ); }
63 function getDescription() { return $this->xmlEncode( $this->Description ); }
64 function getLanguage() {
65 global $wgContLanguageCode;
66 return $wgContLanguageCode;
67 }
68 function getDate() { return $this->Date; }
69 function getAuthor() { return $this->xmlEncode( $this->Author ); }
70 function getComments() { return $this->xmlEncode( $this->Comments ); }
71 /**#@-*/
72 }
73
74 /**
75 * @todo document (needs one-sentence top-level class description).
76 */
77 class ChannelFeed extends FeedItem {
78 /**#@+
79 * Abstract function, override!
80 * @abstract
81 */
82
83 /**
84 * Generate Header of the feed
85 */
86 function outHeader() {
87 # print "<feed>";
88 }
89
90 /**
91 * Generate an item
92 * @param $item
93 */
94 function outItem( $item ) {
95 # print "<item>...</item>";
96 }
97
98 /**
99 * Generate Footer of the feed
100 */
101 function outFooter() {
102 # print "</feed>";
103 }
104 /**#@-*/
105
106 /**
107 * Setup and send HTTP headers. Don't send any content;
108 * content might end up being cached and re-sent with
109 * these same headers later.
110 *
111 * This should be called from the outHeader() method,
112 * but can also be called separately.
113 *
114 * @public
115 */
116 function httpHeaders() {
117 global $wgOut;
118
119 # We take over from $wgOut, excepting its cache header info
120 $wgOut->disable();
121 $mimetype = $this->contentType();
122 header( "Content-type: $mimetype; charset=UTF-8" );
123 $wgOut->sendCacheControl();
124
125 }
126
127 /**
128 * Return an internet media type to be sent in the headers.
129 *
130 * @return string
131 * @private
132 */
133 function contentType() {
134 global $wgRequest;
135 $ctype = $wgRequest->getVal('ctype','application/xml');
136 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
137 return (in_array($ctype, $allowedctypes) ? $ctype : 'application/xml');
138 }
139
140 /**
141 * Output the initial XML headers with a stylesheet for legibility
142 * if someone finds it in a browser.
143 * @private
144 */
145 function outXmlHeader() {
146 global $wgStylePath, $wgStyleVersion;
147
148 $this->httpHeaders();
149 echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
150 echo '<?xml-stylesheet type="text/css" href="' .
151 htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion" ) ) .
152 '"?' . ">\n";
153 }
154 }
155
156 /**
157 * Generate a RSS feed
158 */
159 class RSSFeed extends ChannelFeed {
160
161 /**
162 * Format a date given a timestamp
163 * @param integer $ts Timestamp
164 * @return string Date string
165 */
166 function formatTime( $ts ) {
167 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
168 }
169
170 /**
171 * Ouput an RSS 2.0 header
172 */
173 function outHeader() {
174 global $wgVersion;
175
176 $this->outXmlHeader();
177 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
178 <channel>
179 <title><?php print $this->getTitle() ?></title>
180 <link><?php print $this->getUrl() ?></link>
181 <description><?php print $this->getDescription() ?></description>
182 <language><?php print $this->getLanguage() ?></language>
183 <generator>MediaWiki <?php print $wgVersion ?></generator>
184 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
185 <?php
186 }
187
188 /**
189 * Output an RSS 2.0 item
190 * @param FeedItem item to be output
191 */
192 function outItem( $item ) {
193 ?>
194 <item>
195 <title><?php print $item->getTitle() ?></title>
196 <link><?php print $item->getUrl() ?></link>
197 <description><?php print $item->getDescription() ?></description>
198 <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
199 <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
200 <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
201 </item>
202 <?php
203 }
204
205 /**
206 * Ouput an RSS 2.0 footer
207 */
208 function outFooter() {
209 ?>
210 </channel>
211 </rss><?php
212 }
213 }
214
215 /**
216 * Generate an Atom feed
217 */
218 class AtomFeed extends ChannelFeed {
219 /**
220 * @todo document
221 */
222 function formatTime( $ts ) {
223 // need to use RFC 822 time format at least for rss2.0
224 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
225 }
226
227 /**
228 * Outputs a basic header for Atom 1.0 feeds.
229 */
230 function outHeader() {
231 global $wgVersion;
232
233 $this->outXmlHeader();
234 ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
235 <id><?php print $this->getFeedId() ?></id>
236 <title><?php print $this->getTitle() ?></title>
237 <link rel="self" type="application/atom+xml" href="<?php print $this->getSelfUrl() ?>"/>
238 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
239 <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
240 <subtitle><?php print $this->getDescription() ?></subtitle>
241 <generator>MediaWiki <?php print $wgVersion ?></generator>
242
243 <?php
244 }
245
246 /**
247 * Atom 1.0 requires a unique, opaque IRI as a unique indentifier
248 * for every feed we create. For now just use the URL, but who
249 * can tell if that's right? If we put options on the feed, do we
250 * have to change the id? Maybe? Maybe not.
251 *
252 * @return string
253 * @private
254 */
255 function getFeedId() {
256 return $this->getSelfUrl();
257 }
258
259 /**
260 * Atom 1.0 requests a self-reference to the feed.
261 * @return string
262 * @private
263 */
264 function getSelfUrl() {
265 global $wgRequest;
266 return htmlspecialchars( $wgRequest->getFullRequestURL() );
267 }
268
269 /**
270 * Output a given item.
271 * @param $item
272 */
273 function outItem( $item ) {
274 global $wgMimeType;
275 ?>
276 <entry>
277 <id><?php print $item->getUrl() ?></id>
278 <title><?php print $item->getTitle() ?></title>
279 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print $item->getUrl() ?>"/>
280 <?php if( $item->getDate() ) { ?>
281 <updated><?php print $this->formatTime( $item->getDate() ) ?>Z</updated>
282 <?php } ?>
283
284 <summary type="html"><?php print $item->getDescription() ?></summary>
285 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name></author><?php }?>
286 </entry>
287
288 <?php /* FIXME need to add comments
289 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
290 */
291 }
292
293 /**
294 * Outputs the footer for Atom 1.0 feed (basicly '\</feed\>').
295 */
296 function outFooter() {?>
297 </feed><?php
298 }
299 }