Removed CVS keywords from files, to make merging between branches
[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 if( strcasecmp( $wgInputEncoding, 'utf-8' ) != 0 ) {
65 $string = $wgContLang->iconv( $wgInputEncoding, 'utf-8', $string );
66 }
67 return htmlspecialchars( $string );
68 }
69
70 /**
71 * @todo document
72 */
73 function getTitle() { return $this->xmlEncode( $this->Title ); }
74 /**
75 * @todo document
76 */
77 function getUrl() { return $this->xmlEncode( $this->Url ); }
78 /**
79 * @todo document
80 */
81 function getDescription() { return $this->xmlEncode( $this->Description ); }
82 /**
83 * @todo document
84 */
85 function getLanguage() {
86 global $wgContLanguageCode;
87 return $wgContLanguageCode;
88 }
89 /**
90 * @todo document
91 */
92 function getDate() { return $this->Date; }
93 /**
94 * @todo document
95 */
96 function getAuthor() { return $this->xmlEncode( $this->Author ); }
97 /**
98 * @todo document
99 */
100 function getComments() { return $this->xmlEncode( $this->Comments ); }
101 }
102
103 /**
104 * @todo document
105 * @package MediaWiki
106 */
107 class ChannelFeed extends FeedItem {
108 /**#@+
109 * Abstract function, override!
110 * @abstract
111 */
112
113 /**
114 * Generate Header of the feed
115 */
116 function outHeader() {
117 # print "<feed>";
118 }
119
120 /**
121 * Generate an item
122 * @param $item
123 */
124 function outItem( $item ) {
125 # print "<item>...</item>";
126 }
127
128 /**
129 * Generate Footer of the feed
130 */
131 function outFooter() {
132 # print "</feed>";
133 }
134 /**#@-*/
135
136 /**
137 * @todo document
138 * @param string $mimetype (optional) type of output
139 */
140 function outXmlHeader( $mimetype='application/xml' ) {
141 global $wgServer, $wgStylePath, $wgOut;
142
143 # We take over from $wgOut, excepting its cache header info
144 $wgOut->disable();
145 header( "Content-type: $mimetype; charset=UTF-8" );
146 $wgOut->sendCacheControl();
147
148 print '<' . '?xml version="1.0" encoding="utf-8"?' . ">\n";
149 print '<' . '?xml-stylesheet type="text/css" href="' .
150 htmlspecialchars( "$wgServer$wgStylePath/feed.css" ) . '"?' . ">\n";
151 }
152 }
153
154 /**
155 * Generate a RSS feed
156 * @todo document
157 * @package MediaWiki
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 * @todo document
218 * @package MediaWiki
219 */
220 class AtomFeed extends ChannelFeed {
221 /**
222 * @todo document
223 */
224 function formatTime( $ts ) {
225 // need to use RFC 822 time format at least for rss2.0
226 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
227 }
228
229 /**
230 * @todo document
231 */
232 function outHeader() {
233 global $wgVersion, $wgOut;
234
235 $this->outXmlHeader();
236 ?><feed version="0.3" xml:lang="<?php print $this->getLanguage() ?>">
237 <title><?php print $this->getTitle() ?></title>
238 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
239 <modified><?php print $this->formatTime( wfTimestampNow() ) ?>Z</modified>
240 <tagline><?php print $this->getDescription() ?></tagline>
241 <generator>MediaWiki <?php print $wgVersion ?></generator>
242
243 <?php
244 }
245
246 /**
247 * @todo document
248 */
249 function outItem( $item ) {
250 global $wgMimeType;
251 ?>
252 <entry>
253 <title><?php print $item->getTitle() ?></title>
254 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print $item->getUrl() ?>"/>
255 <?php if( $item->getDate() ) { ?>
256 <modified><?php print $this->formatTime( $item->getDate() ) ?>Z</modified>
257 <issued><?php print $this->formatTime( $item->getDate() ) ?></issued>
258 <created><?php print $this->formatTime( $item->getDate() ) ?>Z</created><?php } ?>
259
260 <summary type="text/plain"><?php print $item->getDescription() ?></summary>
261 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name><!-- <url></url><email></email> --></author><?php }?>
262 <comment>foobar</comment>
263 </entry>
264
265 <?php /* FIXME need to add comments
266 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
267 */
268 }
269
270 /**
271 * @todo document
272 */
273 function outFooter() {?>
274 </feed><?php
275 }
276 }
277
278 ?>