Stop loading feed.php every time by moving available feeds classes in defines.php...
[lhc/web/wiklou.git] / includes / Feed.php
1 <?php
2 # $Id$
3 # Basic support for outputting syndication feeds in RSS, other formats
4 #
5 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
6 # http://www.mediawiki.org/
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with this program; if not, write to the Free Software Foundation, Inc.,
20 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 # http://www.gnu.org/copyleft/gpl.html
22
23 /**
24 * Contain a feed class as well as classes to build rss / atom ... feeds
25 * Available feeds are defined in Defines.php
26 * @package MediaWiki
27 */
28
29
30 /**
31 * @todo document
32 * @package MediaWiki
33 */
34 class FeedItem {
35 /**#@+
36 * @var string
37 * @access private
38 */
39 var $Title = 'Wiki';
40 var $Description = '';
41 var $Url = '';
42 var $Date = '';
43 var $Author = '';
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 * @todo document
61 */
62 function xmlEncode( $string ) {
63 global $wgInputEncoding, $wgLang;
64 $string = str_replace( "\r\n", "\n", $string );
65 if( strcasecmp( $wgInputEncoding, 'utf-8' ) != 0 ) {
66 $string = $wgLang->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 $wgLanguageCode;
88 return $wgLanguageCode;
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 * @todo document
139 * @param string $mimetype (optional) type of output
140 */
141 function outXmlHeader( $mimetype='application/xml' ) {
142 global $wgServer, $wgStylePath, $wgOut;
143
144 # We take over from $wgOut, excepting its cache header info
145 $wgOut->disable();
146 header( "Content-type: $mimetype; charset=UTF-8" );
147 $wgOut->sendCacheControl();
148
149 print '<' . '?xml version="1.0" encoding="utf-8"?' . ">\n";
150 print '<' . '?xml-stylesheet type="text/css" href="' .
151 htmlspecialchars( "$wgServer$wgStylePath/feed.css" ) . '"?' . ">\n";
152 }
153 }
154
155 /**
156 * Generate a RSS feed
157 * @todo document
158 * @package MediaWiki
159 */
160 class RSSFeed extends ChannelFeed {
161
162 /**
163 * Format a date given a timestamp
164 * @param integer $ts Timestamp
165 * @return string Date string
166 */
167 function formatTime( $ts ) {
168 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
169 }
170
171 /**
172 * Ouput an RSS 2.0 header
173 */
174 function outHeader() {
175 global $wgVersion;
176
177 $this->outXmlHeader();
178 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
179 <channel>
180 <title><?php print $this->getTitle() ?></title>
181 <link><?php print $this->getUrl() ?></link>
182 <description><?php print $this->getDescription() ?></description>
183 <language><?php print $this->getLanguage() ?></language>
184 <generator>MediaWiki <?php print $wgVersion ?></generator>
185 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
186 <?php
187 }
188
189 /**
190 * Output an RSS 2.0 item
191 * @param FeedItem item to be output
192 */
193 function outItem( $item ) {
194 ?>
195 <item>
196 <title><?php print $item->getTitle() ?></title>
197 <link><?php print $item->getUrl() ?></link>
198 <description><?php print $item->getDescription() ?></description>
199 <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
200 <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
201 <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
202 </item>
203 <?php
204 }
205
206 /**
207 * Ouput an RSS 2.0 footer
208 */
209 function outFooter() {
210 ?>
211 </channel>
212 </rss><?php
213 }
214 }
215
216 /**
217 * Generate an Atom feed
218 * @todo document
219 * @package MediaWiki
220 */
221 class AtomFeed extends ChannelFeed {
222 /**
223 * @todo document
224 */
225 function formatTime( $ts ) {
226 // need to use RFC 822 time format at least for rss2.0
227 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
228 }
229
230 /**
231 * @todo document
232 */
233 function outHeader() {
234 global $wgVersion, $wgOut;
235
236 $this->outXmlHeader();
237 ?><feed version="0.3" xml:lang="<?php print $this->getLanguage() ?>">
238 <title><?php print $this->getTitle() ?></title>
239 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
240 <modified><?php print $this->formatTime( wfTimestampNow() ) ?>Z</modified>
241 <tagline><?php print $this->getDescription() ?></tagline>
242 <generator>MediaWiki <?php print $wgVersion ?></generator>
243
244 <?php
245 }
246
247 /**
248 * @todo document
249 */
250 function outItem( $item ) {
251 global $wgMimeType;
252 ?>
253 <entry>
254 <title><?php print $item->getTitle() ?></title>
255 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print $item->getUrl() ?>"/>
256 <?php if( $item->getDate() ) { ?>
257 <modified><?php print $this->formatTime( $item->getDate() ) ?>Z</modified>
258 <issued><?php print $this->formatTime( $item->getDate() ) ?></issued>
259 <created><?php print $this->formatTime( $item->getDate() ) ?>Z</created><?php } ?>
260
261 <summary type="text/plain"><?php print $item->getDescription() ?></summary>
262 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name><!-- <url></url><email></email> --></author><?php }?>
263 <comment>foobar</comment>
264 </entry>
265
266 <?php /* FIXME need to add comments
267 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
268 */
269 }
270
271 /**
272 * @todo document
273 */
274 function outFooter() {?>
275 </feed><?php
276 }
277 }
278
279 ?>