* (bug 1949) Profiling typo in rare error case
[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 $string = str_replace( "\r\n", "\n", $string );
63 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
64 return htmlspecialchars( $string );
65 }
66
67 /**
68 * @todo document
69 */
70 function getTitle() { return $this->xmlEncode( $this->Title ); }
71 /**
72 * @todo document
73 */
74 function getUrl() { return $this->xmlEncode( $this->Url ); }
75 /**
76 * @todo document
77 */
78 function getDescription() { return $this->xmlEncode( $this->Description ); }
79 /**
80 * @todo document
81 */
82 function getLanguage() {
83 global $wgContLanguageCode;
84 return $wgContLanguageCode;
85 }
86 /**
87 * @todo document
88 */
89 function getDate() { return $this->Date; }
90 /**
91 * @todo document
92 */
93 function getAuthor() { return $this->xmlEncode( $this->Author ); }
94 /**
95 * @todo document
96 */
97 function getComments() { return $this->xmlEncode( $this->Comments ); }
98 }
99
100 /**
101 * @todo document
102 * @package MediaWiki
103 */
104 class ChannelFeed extends FeedItem {
105 /**#@+
106 * Abstract function, override!
107 * @abstract
108 */
109
110 /**
111 * Generate Header of the feed
112 */
113 function outHeader() {
114 # print "<feed>";
115 }
116
117 /**
118 * Generate an item
119 * @param $item
120 */
121 function outItem( $item ) {
122 # print "<item>...</item>";
123 }
124
125 /**
126 * Generate Footer of the feed
127 */
128 function outFooter() {
129 # print "</feed>";
130 }
131 /**#@-*/
132
133 /**
134 * Setup and send HTTP headers. Don't send any content;
135 * content might end up being cached and re-sent with
136 * these same headers later.
137 *
138 * This should be called from the outHeader() method,
139 * but can also be called separately.
140 *
141 * @access public
142 */
143 function httpHeaders() {
144 global $wgOut;
145
146 # We take over from $wgOut, excepting its cache header info
147 $wgOut->disable();
148 $mimetype = $this->contentType();
149 header( "Content-type: $mimetype; charset=UTF-8" );
150 $wgOut->sendCacheControl();
151
152 }
153
154 /**
155 * Return an internet media type to be sent in the headers.
156 *
157 * @return string
158 * @access private
159 */
160 function contentType() {
161 return 'application/xml';
162 }
163
164 /**
165 * Output the initial XML headers with a stylesheet for legibility
166 * if someone finds it in a browser.
167 * @access private
168 */
169 function outXmlHeader() {
170 global $wgServer, $wgStylePath;
171
172 $this->httpHeaders();
173 print '<' . '?xml version="1.0" encoding="utf-8"?' . ">\n";
174 print '<' . '?xml-stylesheet type="text/css" href="' .
175 htmlspecialchars( "$wgServer$wgStylePath/common/feed.css" ) . '"?' . ">\n";
176 }
177 }
178
179 /**
180 * Generate a RSS feed
181 * @todo document
182 * @package MediaWiki
183 */
184 class RSSFeed extends ChannelFeed {
185
186 /**
187 * Format a date given a timestamp
188 * @param integer $ts Timestamp
189 * @return string Date string
190 */
191 function formatTime( $ts ) {
192 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
193 }
194
195 /**
196 * Ouput an RSS 2.0 header
197 */
198 function outHeader() {
199 global $wgVersion;
200
201 $this->outXmlHeader();
202 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
203 <channel>
204 <title><?php print $this->getTitle() ?></title>
205 <link><?php print $this->getUrl() ?></link>
206 <description><?php print $this->getDescription() ?></description>
207 <language><?php print $this->getLanguage() ?></language>
208 <generator>MediaWiki <?php print $wgVersion ?></generator>
209 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
210 <?php
211 }
212
213 /**
214 * Output an RSS 2.0 item
215 * @param FeedItem item to be output
216 */
217 function outItem( $item ) {
218 ?>
219 <item>
220 <title><?php print $item->getTitle() ?></title>
221 <link><?php print $item->getUrl() ?></link>
222 <description><?php print $item->getDescription() ?></description>
223 <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
224 <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
225 <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
226 </item>
227 <?php
228 }
229
230 /**
231 * Ouput an RSS 2.0 footer
232 */
233 function outFooter() {
234 ?>
235 </channel>
236 </rss><?php
237 }
238 }
239
240 /**
241 * Generate an Atom feed
242 * @todo document
243 * @package MediaWiki
244 */
245 class AtomFeed extends ChannelFeed {
246 /**
247 * @todo document
248 */
249 function formatTime( $ts ) {
250 // need to use RFC 822 time format at least for rss2.0
251 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
252 }
253
254 /**
255 * @todo document
256 */
257 function outHeader() {
258 global $wgVersion, $wgOut;
259
260 $this->outXmlHeader();
261 ?><feed version="0.3" xml:lang="<?php print $this->getLanguage() ?>">
262 <title><?php print $this->getTitle() ?></title>
263 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
264 <modified><?php print $this->formatTime( wfTimestampNow() ) ?>Z</modified>
265 <tagline><?php print $this->getDescription() ?></tagline>
266 <generator>MediaWiki <?php print $wgVersion ?></generator>
267
268 <?php
269 }
270
271 /**
272 * @todo document
273 */
274 function outItem( $item ) {
275 global $wgMimeType;
276 ?>
277 <entry>
278 <title><?php print $item->getTitle() ?></title>
279 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print $item->getUrl() ?>"/>
280 <?php if( $item->getDate() ) { ?>
281 <modified><?php print $this->formatTime( $item->getDate() ) ?>Z</modified>
282 <issued><?php print $this->formatTime( $item->getDate() ) ?></issued>
283 <created><?php print $this->formatTime( $item->getDate() ) ?>Z</created><?php } ?>
284
285 <summary type="text/plain"><?php print $item->getDescription() ?></summary>
286 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name><!-- <url></url><email></email> --></author><?php }?>
287 <comment>foobar</comment>
288 </entry>
289
290 <?php /* FIXME need to add comments
291 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
292 */
293 }
294
295 /**
296 * @todo document
297 */
298 function outFooter() {?>
299 </feed><?php
300 }
301 }
302
303 ?>