* Fixed unclosed <p> tag
[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 */
60 function xmlEncode( $string ) {
61 $string = str_replace( "\r\n", "\n", $string );
62 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
63 return htmlspecialchars( $string );
64 }
65
66 function getTitle() { return $this->xmlEncode( $this->Title ); }
67 function getUrl() { return $this->xmlEncode( $this->Url ); }
68 function getDescription() { return $this->xmlEncode( $this->Description ); }
69 function getLanguage() {
70 global $wgContLanguageCode;
71 return $wgContLanguageCode;
72 }
73 function getDate() { return $this->Date; }
74 function getAuthor() { return $this->xmlEncode( $this->Author ); }
75 function getComments() { return $this->xmlEncode( $this->Comments ); }
76 /**#@-*/
77 }
78
79 /**
80 * @todo document
81 * @package MediaWiki
82 */
83 class ChannelFeed extends FeedItem {
84 /**#@+
85 * Abstract function, override!
86 * @abstract
87 */
88
89 /**
90 * Generate Header of the feed
91 */
92 function outHeader() {
93 # print "<feed>";
94 }
95
96 /**
97 * Generate an item
98 * @param $item
99 */
100 function outItem( $item ) {
101 # print "<item>...</item>";
102 }
103
104 /**
105 * Generate Footer of the feed
106 */
107 function outFooter() {
108 # print "</feed>";
109 }
110 /**#@-*/
111
112 /**
113 * Setup and send HTTP headers. Don't send any content;
114 * content might end up being cached and re-sent with
115 * these same headers later.
116 *
117 * This should be called from the outHeader() method,
118 * but can also be called separately.
119 *
120 * @access public
121 */
122 function httpHeaders() {
123 global $wgOut;
124
125 # We take over from $wgOut, excepting its cache header info
126 $wgOut->disable();
127 $mimetype = $this->contentType();
128 header( "Content-type: $mimetype; charset=UTF-8" );
129 $wgOut->sendCacheControl();
130
131 }
132
133 /**
134 * Return an internet media type to be sent in the headers.
135 *
136 * @return string
137 * @access private
138 */
139 function contentType() {
140 global $wgRequest;
141 $ctype = $wgRequest->getVal('ctype','application/xml');
142 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
143 return (in_array($ctype, $allowedctypes) ? $ctype : 'application/xml');
144 }
145
146 /**
147 * Output the initial XML headers with a stylesheet for legibility
148 * if someone finds it in a browser.
149 * @access private
150 */
151 function outXmlHeader() {
152 global $wgServer, $wgStylePath;
153
154 $this->httpHeaders();
155 echo '<?xml version="1.0" encoding="utf-8"?' . ">\n";
156 echo '<?xml-stylesheet type="text/css" href="' .
157 htmlspecialchars( "$wgServer$wgStylePath/common/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 ?>