Revert "jquery.textSelection: Remove hardcoded checks for removed WikiEditor iframe...
[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 * https://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;
43
44 var $description;
45 var $url;
46 var $date;
47 var $author;
48 var $uniqueId;
49 var $comments;
50 var $rssIsPermalink = false;
51
52 /**
53 * Constructor
54 *
55 * @param string|Title $title Item's title
56 * @param string $description
57 * @param string $url URL uniquely designating the item.
58 * @param string $date Item's date
59 * @param string $author Author's user name
60 * @param string $comments
61 */
62 function __construct( $title, $description, $url, $date = '', $author = '', $comments = '' ) {
63 $this->title = $title;
64 $this->description = $description;
65 $this->url = $url;
66 $this->uniqueId = $url;
67 $this->date = $date;
68 $this->author = $author;
69 $this->comments = $comments;
70 }
71
72 /**
73 * Encode $string so that it can be safely embedded in a XML document
74 *
75 * @param string $string String to encode
76 * @return string
77 */
78 public function xmlEncode( $string ) {
79 $string = str_replace( "\r\n", "\n", $string );
80 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
81 return htmlspecialchars( $string );
82 }
83
84 /**
85 * Get the unique id of this item
86 *
87 * @return string
88 */
89 public function getUniqueId() {
90 if ( $this->uniqueId ) {
91 return $this->xmlEncode( $this->uniqueId );
92 }
93 }
94
95 /**
96 * set the unique id of an item
97 *
98 * @param string $uniqueId Unique id for the item
99 * @param bool $rssIsPermalink Set to true if the guid (unique id) is a permalink (RSS feeds only)
100 */
101 public function setUniqueId( $uniqueId, $rssIsPermalink = false ) {
102 $this->uniqueId = $uniqueId;
103 $this->rssIsPermalink = $rssIsPermalink;
104 }
105
106 /**
107 * Get the title of this item; already xml-encoded
108 *
109 * @return string
110 */
111 public function getTitle() {
112 return $this->xmlEncode( $this->title );
113 }
114
115 /**
116 * Get the URL of this item; already xml-encoded
117 *
118 * @return string
119 */
120 public function getUrl() {
121 return $this->xmlEncode( $this->url );
122 }
123
124 /**
125 * Get the description of this item; already xml-encoded
126 *
127 * @return string
128 */
129 public function getDescription() {
130 return $this->xmlEncode( $this->description );
131 }
132
133 /**
134 * Get the language of this item
135 *
136 * @return string
137 */
138 public function getLanguage() {
139 global $wgLanguageCode;
140 return $wgLanguageCode;
141 }
142
143 /**
144 * Get the title of this item
145 *
146 * @return string
147 */
148 public function getDate() {
149 return $this->date;
150 }
151
152 /**
153 * Get the author of this item; already xml-encoded
154 *
155 * @return string
156 */
157 public function getAuthor() {
158 return $this->xmlEncode( $this->author );
159 }
160
161 /**
162 * Get the comment of this item; already xml-encoded
163 *
164 * @return string
165 */
166 public function getComments() {
167 return $this->xmlEncode( $this->comments );
168 }
169
170 /**
171 * Quickie hack... strip out wikilinks to more legible form from the comment.
172 *
173 * @param string $text wikitext
174 * @return string
175 */
176 public static function stripComment( $text ) {
177 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
178 }
179 /**#@-*/
180 }
181
182 /**
183 * @todo document (needs one-sentence top-level class description).
184 * @ingroup Feed
185 */
186 abstract class ChannelFeed extends FeedItem {
187 /**
188 * Generate Header of the feed
189 * @par Example:
190 * @code
191 * print "<feed>";
192 * @endcode
193 */
194 abstract public function outHeader();
195
196 /**
197 * Generate an item
198 * @par Example:
199 * @code
200 * print "<item>...</item>";
201 * @endcode
202 * @param FeedItem $item
203 */
204 abstract public function outItem( $item );
205
206 /**
207 * Generate Footer of the feed
208 * @par Example:
209 * @code
210 * print "</feed>";
211 * @endcode
212 */
213 abstract public function outFooter();
214
215 /**
216 * Setup and send HTTP headers. Don't send any content;
217 * content might end up being cached and re-sent with
218 * these same headers later.
219 *
220 * This should be called from the outHeader() method,
221 * but can also be called separately.
222 */
223 public function httpHeaders() {
224 global $wgOut, $wgVaryOnXFP;
225
226 # We take over from $wgOut, excepting its cache header info
227 $wgOut->disable();
228 $mimetype = $this->contentType();
229 header( "Content-type: $mimetype; charset=UTF-8" );
230 if ( $wgVaryOnXFP ) {
231 $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
232 }
233 $wgOut->sendCacheControl();
234
235 }
236
237 /**
238 * Return an internet media type to be sent in the headers.
239 *
240 * @return string
241 * @private
242 */
243 function contentType() {
244 global $wgRequest;
245 $ctype = $wgRequest->getVal( 'ctype', 'application/xml' );
246 $allowedctypes = array( 'application/xml', 'text/xml', 'application/rss+xml', 'application/atom+xml' );
247 return ( in_array( $ctype, $allowedctypes ) ? $ctype : 'application/xml' );
248 }
249
250 /**
251 * Output the initial XML headers with a stylesheet for legibility
252 * if someone finds it in a browser.
253 * @private
254 */
255 function outXmlHeader() {
256 global $wgStylePath, $wgStyleVersion;
257
258 $this->httpHeaders();
259 echo '<?xml version="1.0"?>' . "\n";
260 echo '<?xml-stylesheet type="text/css" href="' .
261 htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion", PROTO_CURRENT ) ) .
262 '"?' . ">\n";
263 }
264 }
265
266 /**
267 * Generate a RSS feed
268 *
269 * @ingroup Feed
270 */
271 class RSSFeed extends ChannelFeed {
272
273 /**
274 * Format a date given a timestamp
275 *
276 * @param int $ts Timestamp
277 * @return string Date string
278 */
279 function formatTime( $ts ) {
280 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
281 }
282
283 /**
284 * Output an RSS 2.0 header
285 */
286 function outHeader() {
287 global $wgVersion;
288
289 $this->outXmlHeader();
290 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
291 <channel>
292 <title><?php print $this->getTitle() ?></title>
293 <link><?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?></link>
294 <description><?php print $this->getDescription() ?></description>
295 <language><?php print $this->getLanguage() ?></language>
296 <generator>MediaWiki <?php print $wgVersion ?></generator>
297 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
298 <?php
299 }
300
301 /**
302 * Output an RSS 2.0 item
303 * @param FeedItem $item Item to be output
304 */
305 function outItem( $item ) {
306 ?>
307 <item>
308 <title><?php print $item->getTitle(); ?></title>
309 <link><?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?></link>
310 <guid<?php if ( !$item->rssIsPermalink ) { print ' isPermaLink="false"'; } ?>><?php print $item->getUniqueId(); ?></guid>
311 <description><?php print $item->getDescription() ?></description>
312 <?php if ( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ); ?></pubDate><?php } ?>
313 <?php if ( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor(); ?></dc:creator><?php }?>
314 <?php if ( $item->getComments() ) { ?><comments><?php print wfExpandUrl( $item->getComments(), PROTO_CURRENT ); ?></comments><?php }?>
315 </item>
316 <?php
317 }
318
319 /**
320 * Output an RSS 2.0 footer
321 */
322 function outFooter() {
323 ?>
324 </channel>
325 </rss><?php
326 }
327 }
328
329 /**
330 * Generate an Atom feed
331 *
332 * @ingroup Feed
333 */
334 class AtomFeed extends ChannelFeed {
335 /**
336 * @todo document
337 * @return string
338 */
339 function formatTime( $ts ) {
340 // need to use RFC 822 time format at least for rss2.0
341 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
342 }
343
344 /**
345 * Outputs a basic header for Atom 1.0 feeds.
346 */
347 function outHeader() {
348 global $wgVersion;
349
350 $this->outXmlHeader();
351 ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
352 <id><?php print $this->getFeedId() ?></id>
353 <title><?php print $this->getTitle() ?></title>
354 <link rel="self" type="application/atom+xml" href="<?php print wfExpandUrl( $this->getSelfUrl(), PROTO_CURRENT ) ?>"/>
355 <link rel="alternate" type="text/html" href="<?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?>"/>
356 <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
357 <subtitle><?php print $this->getDescription() ?></subtitle>
358 <generator>MediaWiki <?php print $wgVersion ?></generator>
359
360 <?php
361 }
362
363 /**
364 * Atom 1.0 requires a unique, opaque IRI as a unique identifier
365 * for every feed we create. For now just use the URL, but who
366 * can tell if that's right? If we put options on the feed, do we
367 * have to change the id? Maybe? Maybe not.
368 *
369 * @return string
370 * @private
371 */
372 function getFeedId() {
373 return $this->getSelfUrl();
374 }
375
376 /**
377 * Atom 1.0 requests a self-reference to the feed.
378 * @return string
379 * @private
380 */
381 function getSelfUrl() {
382 global $wgRequest;
383 return htmlspecialchars( $wgRequest->getFullRequestURL() );
384 }
385
386 /**
387 * Output a given item.
388 * @param FeedItem $item
389 */
390 function outItem( $item ) {
391 global $wgMimeType;
392 ?>
393 <entry>
394 <id><?php print $item->getUniqueId(); ?></id>
395 <title><?php print $item->getTitle(); ?></title>
396 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?>"/>
397 <?php if ( $item->getDate() ) { ?>
398 <updated><?php print $this->formatTime( $item->getDate() ); ?>Z</updated>
399 <?php } ?>
400
401 <summary type="html"><?php print $item->getDescription() ?></summary>
402 <?php if ( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor(); ?></name></author><?php }?>
403 </entry>
404
405 <?php /* @todo FIXME: Need to add comments
406 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
407 */
408 }
409
410 /**
411 * Outputs the footer for Atom 1.0 feed (basically '\</feed\>').
412 */
413 function outFooter() {?>
414 </feed><?php
415 }
416 }