Load limiting work for Recentchanges RSS/Atom feeds:
[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 * Setup and send HTTP headers. Don't send any content;
138 * content might end up being cached and re-sent with
139 * these same headers later.
140 *
141 * This should be called from the outHeader() method,
142 * but can also be called separately.
143 *
144 * @access public
145 */
146 function httpHeaders() {
147 global $wgOut;
148
149 # We take over from $wgOut, excepting its cache header info
150 $wgOut->disable();
151 $mimetype = $this->contentType();
152 header( "Content-type: $mimetype; charset=UTF-8" );
153 $wgOut->sendCacheControl();
154
155 }
156
157 /**
158 * Return an internet media type to be sent in the headers.
159 *
160 * @return string
161 * @access private
162 */
163 function contentType() {
164 return 'application/xml';
165 }
166
167 /**
168 * Output the initial XML headers with a stylesheet for legibility
169 * if someone finds it in a browser.
170 * @access private
171 */
172 function outXmlHeader() {
173 global $wgServer, $wgStylePath;
174
175 $this->httpHeaders();
176 print '<' . '?xml version="1.0" encoding="utf-8"?' . ">\n";
177 print '<' . '?xml-stylesheet type="text/css" href="' .
178 htmlspecialchars( "$wgServer$wgStylePath/common/feed.css" ) . '"?' . ">\n";
179 }
180 }
181
182 /**
183 * Generate a RSS feed
184 * @todo document
185 * @package MediaWiki
186 */
187 class RSSFeed extends ChannelFeed {
188
189 /**
190 * Format a date given a timestamp
191 * @param integer $ts Timestamp
192 * @return string Date string
193 */
194 function formatTime( $ts ) {
195 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
196 }
197
198 /**
199 * Ouput an RSS 2.0 header
200 */
201 function outHeader() {
202 global $wgVersion;
203
204 $this->outXmlHeader();
205 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
206 <channel>
207 <title><?php print $this->getTitle() ?></title>
208 <link><?php print $this->getUrl() ?></link>
209 <description><?php print $this->getDescription() ?></description>
210 <language><?php print $this->getLanguage() ?></language>
211 <generator>MediaWiki <?php print $wgVersion ?></generator>
212 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
213 <?php
214 }
215
216 /**
217 * Output an RSS 2.0 item
218 * @param FeedItem item to be output
219 */
220 function outItem( $item ) {
221 ?>
222 <item>
223 <title><?php print $item->getTitle() ?></title>
224 <link><?php print $item->getUrl() ?></link>
225 <description><?php print $item->getDescription() ?></description>
226 <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
227 <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
228 <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
229 </item>
230 <?php
231 }
232
233 /**
234 * Ouput an RSS 2.0 footer
235 */
236 function outFooter() {
237 ?>
238 </channel>
239 </rss><?php
240 }
241 }
242
243 /**
244 * Generate an Atom feed
245 * @todo document
246 * @package MediaWiki
247 */
248 class AtomFeed extends ChannelFeed {
249 /**
250 * @todo document
251 */
252 function formatTime( $ts ) {
253 // need to use RFC 822 time format at least for rss2.0
254 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
255 }
256
257 /**
258 * @todo document
259 */
260 function outHeader() {
261 global $wgVersion, $wgOut;
262
263 $this->outXmlHeader();
264 ?><feed version="0.3" xml:lang="<?php print $this->getLanguage() ?>">
265 <title><?php print $this->getTitle() ?></title>
266 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
267 <modified><?php print $this->formatTime( wfTimestampNow() ) ?>Z</modified>
268 <tagline><?php print $this->getDescription() ?></tagline>
269 <generator>MediaWiki <?php print $wgVersion ?></generator>
270
271 <?php
272 }
273
274 /**
275 * @todo document
276 */
277 function outItem( $item ) {
278 global $wgMimeType;
279 ?>
280 <entry>
281 <title><?php print $item->getTitle() ?></title>
282 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print $item->getUrl() ?>"/>
283 <?php if( $item->getDate() ) { ?>
284 <modified><?php print $this->formatTime( $item->getDate() ) ?>Z</modified>
285 <issued><?php print $this->formatTime( $item->getDate() ) ?></issued>
286 <created><?php print $this->formatTime( $item->getDate() ) ?>Z</created><?php } ?>
287
288 <summary type="text/plain"><?php print $item->getDescription() ?></summary>
289 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name><!-- <url></url><email></email> --></author><?php }?>
290 <comment>foobar</comment>
291 </entry>
292
293 <?php /* FIXME need to add comments
294 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
295 */
296 }
297
298 /**
299 * @todo document
300 */
301 function outFooter() {?>
302 </feed><?php
303 }
304 }
305
306 ?>