tweak some comments (schema blah blah)
[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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 * @access private
37 */
38 var $Title = 'Wiki';
39 var $Description = '';
40 var $Url = '';
41 var $Date = '';
42 var $Author = '';
43 /**#@-*/
44
45 /**
46 * @todo document
47 */
48 function FeedItem( $Title, $Description, $Url, $Date = '', $Author = '', $Comments = '' ) {
49 $this->Title = $Title;
50 $this->Description = $Description;
51 $this->Url = $Url;
52 $this->Date = $Date;
53 $this->Author = $Author;
54 $this->Comments = $Comments;
55 }
56
57 /**
58 * @static
59 * @todo document
60 */
61 function xmlEncode( $string ) {
62 global $wgInputEncoding, $wgContLang;
63 $string = str_replace( "\r\n", "\n", $string );
64 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
65 if( strcasecmp( $wgInputEncoding, 'utf-8' ) != 0 ) {
66 $string = $wgContLang->iconv( $wgInputEncoding, 'utf-8', $string );
67 }
68 return htmlspecialchars( $string );
69 }
70
71 /**
72 * @todo document
73 */
74 function getTitle() { return $this->xmlEncode( $this->Title ); }
75 /**
76 * @todo document
77 */
78 function getUrl() { return $this->xmlEncode( $this->Url ); }
79 /**
80 * @todo document
81 */
82 function getDescription() { return $this->xmlEncode( $this->Description ); }
83 /**
84 * @todo document
85 */
86 function getLanguage() {
87 global $wgContLanguageCode;
88 return $wgContLanguageCode;
89 }
90 /**
91 * @todo document
92 */
93 function getDate() { return $this->Date; }
94 /**
95 * @todo document
96 */
97 function getAuthor() { return $this->xmlEncode( $this->Author ); }
98 /**
99 * @todo document
100 */
101 function getComments() { return $this->xmlEncode( $this->Comments ); }
102 }
103
104 /**
105 * @todo document
106 * @package MediaWiki
107 */
108 class ChannelFeed extends FeedItem {
109 /**#@+
110 * Abstract function, override!
111 * @abstract
112 */
113
114 /**
115 * Generate Header of the feed
116 */
117 function outHeader() {
118 # print "<feed>";
119 }
120
121 /**
122 * Generate an item
123 * @param $item
124 */
125 function outItem( $item ) {
126 # print "<item>...</item>";
127 }
128
129 /**
130 * Generate Footer of the feed
131 */
132 function outFooter() {
133 # print "</feed>";
134 }
135 /**#@-*/
136
137 /**
138 * Setup and send HTTP headers. Don't send any content;
139 * content might end up being cached and re-sent with
140 * these same headers later.
141 *
142 * This should be called from the outHeader() method,
143 * but can also be called separately.
144 *
145 * @access public
146 */
147 function httpHeaders() {
148 global $wgOut;
149
150 # We take over from $wgOut, excepting its cache header info
151 $wgOut->disable();
152 $mimetype = $this->contentType();
153 header( "Content-type: $mimetype; charset=UTF-8" );
154 $wgOut->sendCacheControl();
155
156 }
157
158 /**
159 * Return an internet media type to be sent in the headers.
160 *
161 * @return string
162 * @access private
163 */
164 function contentType() {
165 return 'application/xml';
166 }
167
168 /**
169 * Output the initial XML headers with a stylesheet for legibility
170 * if someone finds it in a browser.
171 * @access private
172 */
173 function outXmlHeader() {
174 global $wgServer, $wgStylePath;
175
176 $this->httpHeaders();
177 print '<' . '?xml version="1.0" encoding="utf-8"?' . ">\n";
178 print '<' . '?xml-stylesheet type="text/css" href="' .
179 htmlspecialchars( "$wgServer$wgStylePath/common/feed.css" ) . '"?' . ">\n";
180 }
181 }
182
183 /**
184 * Generate a RSS feed
185 * @todo document
186 * @package MediaWiki
187 */
188 class RSSFeed extends ChannelFeed {
189
190 /**
191 * Format a date given a timestamp
192 * @param integer $ts Timestamp
193 * @return string Date string
194 */
195 function formatTime( $ts ) {
196 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
197 }
198
199 /**
200 * Ouput an RSS 2.0 header
201 */
202 function outHeader() {
203 global $wgVersion;
204
205 $this->outXmlHeader();
206 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
207 <channel>
208 <title><?php print $this->getTitle() ?></title>
209 <link><?php print $this->getUrl() ?></link>
210 <description><?php print $this->getDescription() ?></description>
211 <language><?php print $this->getLanguage() ?></language>
212 <generator>MediaWiki <?php print $wgVersion ?></generator>
213 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
214 <?php
215 }
216
217 /**
218 * Output an RSS 2.0 item
219 * @param FeedItem item to be output
220 */
221 function outItem( $item ) {
222 ?>
223 <item>
224 <title><?php print $item->getTitle() ?></title>
225 <link><?php print $item->getUrl() ?></link>
226 <description><?php print $item->getDescription() ?></description>
227 <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
228 <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
229 <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
230 </item>
231 <?php
232 }
233
234 /**
235 * Ouput an RSS 2.0 footer
236 */
237 function outFooter() {
238 ?>
239 </channel>
240 </rss><?php
241 }
242 }
243
244 /**
245 * Generate an Atom feed
246 * @todo document
247 * @package MediaWiki
248 */
249 class AtomFeed extends ChannelFeed {
250 /**
251 * @todo document
252 */
253 function formatTime( $ts ) {
254 // need to use RFC 822 time format at least for rss2.0
255 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
256 }
257
258 /**
259 * @todo document
260 */
261 function outHeader() {
262 global $wgVersion, $wgOut;
263
264 $this->outXmlHeader();
265 ?><feed version="0.3" xml:lang="<?php print $this->getLanguage() ?>">
266 <title><?php print $this->getTitle() ?></title>
267 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
268 <modified><?php print $this->formatTime( wfTimestampNow() ) ?>Z</modified>
269 <tagline><?php print $this->getDescription() ?></tagline>
270 <generator>MediaWiki <?php print $wgVersion ?></generator>
271
272 <?php
273 }
274
275 /**
276 * @todo document
277 */
278 function outItem( $item ) {
279 global $wgMimeType;
280 ?>
281 <entry>
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 <modified><?php print $this->formatTime( $item->getDate() ) ?>Z</modified>
286 <issued><?php print $this->formatTime( $item->getDate() ) ?></issued>
287 <created><?php print $this->formatTime( $item->getDate() ) ?>Z</created><?php } ?>
288
289 <summary type="text/plain"><?php print $item->getDescription() ?></summary>
290 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name><!-- <url></url><email></email> --></author><?php }?>
291 <comment>foobar</comment>
292 </entry>
293
294 <?php /* FIXME need to add comments
295 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
296 */
297 }
298
299 /**
300 * @todo document
301 */
302 function outFooter() {?>
303 </feed><?php
304 }
305 }
306
307 ?>