And some more parameter documentation!!
[lhc/web/wiklou.git] / includes / Feed.php
1 <?php
2 /**
3 * Basic support for outputting syndication feeds in RSS, other formats.
4 *
5 * Contain a feed class as well as classes to build rss / atom ... feeds
6 * Available feeds are defined in Defines.php
7 *
8 * Copyright © 2004 Brion Vibber <brion@pobox.com>
9 * http://www.mediawiki.org/
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 *
26 * @file
27 */
28
29 /**
30 * @defgroup Feed Feed
31 */
32
33 /**
34 * A base class for basic support for outputting syndication feeds in RSS and other formats.
35 *
36 * @ingroup Feed
37 */
38 class FeedItem {
39 /**
40 * @var Title
41 */
42 var $Title = 'Wiki';
43 var $Description = '';
44 var $Url = '';
45 var $Date = '';
46 var $Author = '';
47 var $UniqueId = '';
48 var $RSSIsPermalink;
49
50 /**
51 * Constructor
52 *
53 * @param $Title String|Title Item's title
54 * @param $Description String
55 * @param $Url String: URL uniquely designating the item.
56 * @param $Date String: Item's date
57 * @param $Author String: Author's user name
58 * @param $Comments String
59 */
60 function __construct( $Title, $Description, $Url, $Date = '', $Author = '', $Comments = '' ) {
61 $this->Title = $Title;
62 $this->Description = $Description;
63 $this->Url = $Url;
64 $this->UniqueId = $Url;
65 $this->RSSIsPermalink = false;
66 $this->Date = $Date;
67 $this->Author = $Author;
68 $this->Comments = $Comments;
69 }
70
71 /**
72 * Get the last touched timestamp
73 *
74 * @return String last-touched timestamp
75 */
76 public function getLastMod() {
77 return $this->Title->getTouched();
78 }
79
80 /**
81 * Encode $string so that it can be safely embedded in a XML document
82 *
83 * @param $string String: string to encode
84 * @return String
85 */
86 public function xmlEncode( $string ) {
87 $string = str_replace( "\r\n", "\n", $string );
88 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
89 return htmlspecialchars( $string );
90 }
91
92 /**
93 * Get the unique id of this item
94 *
95 * @return String
96 */
97 public function getUniqueId() {
98 if ( $this->UniqueId ) {
99 return $this->xmlEncode( $this->UniqueId );
100 }
101 }
102
103 /**
104 * set the unique id of an item
105 *
106 * @param $uniqueId String: unique id for the item
107 * @param $RSSisPermalink Boolean: set to true if the guid (unique id) is a permalink (RSS feeds only)
108 */
109 public function setUniqueId($uniqueId, $RSSisPermalink = false) {
110 $this->UniqueId = $uniqueId;
111 $this->RSSIsPermalink = $RSSisPermalink;
112 }
113
114 /**
115 * Get the title of this item; already xml-encoded
116 *
117 * @return String
118 */
119 public function getTitle() {
120 return $this->xmlEncode( $this->Title );
121 }
122
123 /**
124 * Get the DB prefixed title
125 *
126 * @return String the prefixed title, with underscores and
127 * any interwiki and namespace prefixes
128 */
129 public function getDBPrefixedTitle() {
130 return $this->Title->getPrefixedDBKey();
131 }
132
133 /**
134 * Get the URL of this item; already xml-encoded
135 *
136 * @return String
137 */
138 public function getUrl() {
139 return $this->xmlEncode( $this->Url );
140 }
141
142 /**
143 * Get the description of this item; already xml-encoded
144 *
145 * @return String
146 */
147 public function getDescription() {
148 return $this->xmlEncode( $this->Description );
149 }
150
151 /**
152 * Get the language of this item
153 *
154 * @return String
155 */
156 public function getLanguage() {
157 global $wgLanguageCode;
158 return $wgLanguageCode;
159 }
160
161 /**
162 * Get the title of this item
163 *
164 * @return String
165 */
166 public function getDate() {
167 return $this->Date;
168 }
169
170 /**
171 * Get the author of this item; already xml-encoded
172 *
173 * @return String
174 */
175 public function getAuthor() {
176 return $this->xmlEncode( $this->Author );
177 }
178
179 /**
180 * Get the comment of this item; already xml-encoded
181 *
182 * @return String
183 */
184 public function getComments() {
185 return $this->xmlEncode( $this->Comments );
186 }
187
188 /**
189 * Quickie hack... strip out wikilinks to more legible form from the comment.
190 *
191 * @param $text String: wikitext
192 * @return String
193 */
194 public static function stripComment( $text ) {
195 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
196 }
197 /**#@-*/
198 }
199
200 /**
201 * @todo document (needs one-sentence top-level class description).
202 * @ingroup Feed
203 */
204 class ChannelFeed extends FeedItem {
205 /**#@+
206 * Abstract function, override!
207 * @abstract
208 */
209
210 /**
211 * Generate Header of the feed
212 */
213 function outHeader() {
214 # print "<feed>";
215 }
216
217 /**
218 * Generate an item
219 * @param $item
220 */
221 function outItem( $item ) {
222 # print "<item>...</item>";
223 }
224
225 /**
226 * Generate Footer of the feed
227 */
228 function outFooter() {
229 # print "</feed>";
230 }
231 /**#@-*/
232
233 /**
234 * Setup and send HTTP headers. Don't send any content;
235 * content might end up being cached and re-sent with
236 * these same headers later.
237 *
238 * This should be called from the outHeader() method,
239 * but can also be called separately.
240 */
241 public function httpHeaders() {
242 global $wgOut;
243
244 # We take over from $wgOut, excepting its cache header info
245 $wgOut->disable();
246 $mimetype = $this->contentType();
247 header( "Content-type: $mimetype; charset=UTF-8" );
248 $wgOut->sendCacheControl();
249
250 }
251
252 /**
253 * Return an internet media type to be sent in the headers.
254 *
255 * @return string
256 * @private
257 */
258 function contentType() {
259 global $wgRequest;
260 $ctype = $wgRequest->getVal('ctype','application/xml');
261 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
262 return (in_array($ctype, $allowedctypes) ? $ctype : 'application/xml');
263 }
264
265 /**
266 * Output the initial XML headers with a stylesheet for legibility
267 * if someone finds it in a browser.
268 * @private
269 */
270 function outXmlHeader() {
271 global $wgStylePath, $wgStyleVersion;
272
273 $this->httpHeaders();
274 echo '<?xml version="1.0"?>' . "\n";
275 echo '<?xml-stylesheet type="text/css" href="' .
276 htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion" ) ) .
277 '"?' . ">\n";
278 }
279 }
280
281 /**
282 * Generate a RSS feed
283 *
284 * @ingroup Feed
285 */
286 class RSSFeed extends ChannelFeed {
287
288 /**
289 * Format a date given a timestamp
290 *
291 * @param $ts Integer: timestamp
292 * @return String: date string
293 */
294 function formatTime( $ts ) {
295 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
296 }
297
298 /**
299 * Ouput an RSS 2.0 header
300 */
301 function outHeader() {
302 global $wgVersion;
303
304 $this->outXmlHeader();
305 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
306 <channel>
307 <title><?php print $this->getTitle() ?></title>
308 <link><?php print $this->getUrl() ?></link>
309 <description><?php print $this->getDescription() ?></description>
310 <language><?php print $this->getLanguage() ?></language>
311 <generator>MediaWiki <?php print $wgVersion ?></generator>
312 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
313 <?php
314 }
315
316 /**
317 * Output an RSS 2.0 item
318 * @param $item FeedItem: item to be output
319 */
320 function outItem( $item ) {
321 ?>
322 <item>
323 <title><?php print $item->getTitle() ?></title>
324 <link><?php print $item->getUrl() ?></link>
325 <guid<?php if( !$item->RSSIsPermalink ) print ' isPermaLink="false"' ?>><?php print $item->getUniqueId() ?></guid>
326 <description><?php print $item->getDescription() ?></description>
327 <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
328 <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
329 <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
330 </item>
331 <?php
332 }
333
334 /**
335 * Ouput an RSS 2.0 footer
336 */
337 function outFooter() {
338 ?>
339 </channel>
340 </rss><?php
341 }
342 }
343
344 /**
345 * Generate an Atom feed
346 *
347 * @ingroup Feed
348 */
349 class AtomFeed extends ChannelFeed {
350 /**
351 * @todo document
352 */
353 function formatTime( $ts ) {
354 // need to use RFC 822 time format at least for rss2.0
355 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
356 }
357
358 /**
359 * Outputs a basic header for Atom 1.0 feeds.
360 */
361 function outHeader() {
362 global $wgVersion;
363
364 $this->outXmlHeader();
365 ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
366 <id><?php print $this->getFeedId() ?></id>
367 <title><?php print $this->getTitle() ?></title>
368 <link rel="self" type="application/atom+xml" href="<?php print $this->getSelfUrl() ?>"/>
369 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
370 <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
371 <subtitle><?php print $this->getDescription() ?></subtitle>
372 <generator>MediaWiki <?php print $wgVersion ?></generator>
373
374 <?php
375 }
376
377 /**
378 * Atom 1.0 requires a unique, opaque IRI as a unique indentifier
379 * for every feed we create. For now just use the URL, but who
380 * can tell if that's right? If we put options on the feed, do we
381 * have to change the id? Maybe? Maybe not.
382 *
383 * @return string
384 * @private
385 */
386 function getFeedId() {
387 return $this->getSelfUrl();
388 }
389
390 /**
391 * Atom 1.0 requests a self-reference to the feed.
392 * @return string
393 * @private
394 */
395 function getSelfUrl() {
396 global $wgRequest;
397 return htmlspecialchars( $wgRequest->getFullRequestURL() );
398 }
399
400 /**
401 * Output a given item.
402 * @param $item
403 */
404 function outItem( $item ) {
405 global $wgMimeType;
406 ?>
407 <entry>
408 <id><?php print $item->getUniqueId() ?></id>
409 <title><?php print $item->getTitle() ?></title>
410 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print $item->getUrl() ?>"/>
411 <?php if( $item->getDate() ) { ?>
412 <updated><?php print $this->formatTime( $item->getDate() ) ?>Z</updated>
413 <?php } ?>
414
415 <summary type="html"><?php print $item->getDescription() ?></summary>
416 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name></author><?php }?>
417 </entry>
418
419 <?php /* @todo FIXME: Need to add comments
420 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
421 */
422 }
423
424 /**
425 * Outputs the footer for Atom 1.0 feed (basicly '\</feed\>').
426 */
427 function outFooter() {?>
428 </feed><?php
429 }
430 }