If 'tables' is a string that starts with a space, treat it as user-enforced FROM...
[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 */
26
27
28 /**
29 * @todo document
30 */
31 class FeedItem {
32 /**#@+
33 * @var string
34 * @private
35 */
36 var $Title = 'Wiki';
37 var $Description = '';
38 var $Url = '';
39 var $Date = '';
40 var $Author = '';
41 /**#@-*/
42
43 /**#@+
44 * @todo document
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
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
151 $this->httpHeaders();
152 echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
153 echo '<?xml-stylesheet type="text/css" href="' .
154 htmlspecialchars( "$wgServer$wgStylePath/common/feed.css?$wgStyleVersion" ) . '"?' . ">\n";
155 }
156 }
157
158 /**
159 * Generate a RSS feed
160 * @todo document
161 */
162 class RSSFeed extends ChannelFeed {
163
164 /**
165 * Format a date given a timestamp
166 * @param integer $ts Timestamp
167 * @return string Date string
168 */
169 function formatTime( $ts ) {
170 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
171 }
172
173 /**
174 * Ouput an RSS 2.0 header
175 */
176 function outHeader() {
177 global $wgVersion;
178
179 $this->outXmlHeader();
180 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
181 <channel>
182 <title><?php print $this->getTitle() ?></title>
183 <link><?php print $this->getUrl() ?></link>
184 <description><?php print $this->getDescription() ?></description>
185 <language><?php print $this->getLanguage() ?></language>
186 <generator>MediaWiki <?php print $wgVersion ?></generator>
187 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
188 <?php
189 }
190
191 /**
192 * Output an RSS 2.0 item
193 * @param FeedItem item to be output
194 */
195 function outItem( $item ) {
196 ?>
197 <item>
198 <title><?php print $item->getTitle() ?></title>
199 <link><?php print $item->getUrl() ?></link>
200 <description><?php print $item->getDescription() ?></description>
201 <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
202 <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
203 <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
204 </item>
205 <?php
206 }
207
208 /**
209 * Ouput an RSS 2.0 footer
210 */
211 function outFooter() {
212 ?>
213 </channel>
214 </rss><?php
215 }
216 }
217
218 /**
219 * Generate an Atom feed
220 * @todo document
221 */
222 class AtomFeed extends ChannelFeed {
223 /**
224 * @todo document
225 */
226 function formatTime( $ts ) {
227 // need to use RFC 822 time format at least for rss2.0
228 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
229 }
230
231 /**
232 * Outputs a basic header for Atom 1.0 feeds.
233 */
234 function outHeader() {
235 global $wgVersion;
236
237 $this->outXmlHeader();
238 ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
239 <id><?php print $this->getFeedId() ?></id>
240 <title><?php print $this->getTitle() ?></title>
241 <link rel="self" type="application/atom+xml" href="<?php print $this->getSelfUrl() ?>"/>
242 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
243 <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
244 <subtitle><?php print $this->getDescription() ?></subtitle>
245 <generator>MediaWiki <?php print $wgVersion ?></generator>
246
247 <?php
248 }
249
250 /**
251 * Atom 1.0 requires a unique, opaque IRI as a unique indentifier
252 * for every feed we create. For now just use the URL, but who
253 * can tell if that's right? If we put options on the feed, do we
254 * have to change the id? Maybe? Maybe not.
255 *
256 * @return string
257 * @private
258 */
259 function getFeedId() {
260 return $this->getSelfUrl();
261 }
262
263 /**
264 * Atom 1.0 requests a self-reference to the feed.
265 * @return string
266 * @private
267 */
268 function getSelfUrl() {
269 global $wgRequest;
270 return htmlspecialchars( $wgRequest->getFullRequestURL() );
271 }
272
273 /**
274 * Output a given item.
275 * @param $item
276 */
277 function outItem( $item ) {
278 global $wgMimeType;
279 ?>
280 <entry>
281 <id><?php print $item->getUrl() ?></id>
282 <title><?php print $item->getTitle() ?></title>
283 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print $item->getUrl() ?>"/>
284 <?php if( $item->getDate() ) { ?>
285 <updated><?php print $this->formatTime( $item->getDate() ) ?>Z</updated>
286 <?php } ?>
287
288 <summary type="html"><?php print $item->getDescription() ?></summary>
289 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name></author><?php }?>
290 </entry>
291
292 <?php /* FIXME need to add comments
293 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
294 */
295 }
296
297 /**
298 * Outputs the footer for Atom 1.0 feed (basicly '\</feed\>').
299 */
300 function outFooter() {?>
301 </feed><?php
302 }
303 }
304
305 ?>