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