rdbms: Avoid selectDB() call in LoadMonitor new connections
[mediawiki.git] / includes / Feed.php
blobf76a634d3f0860af9428883781389fa9638bc81c
1 <?php
2 /**
3 * Basic support for outputting syndication feeds in RSS, other formats.
5 * Contain a feed class as well as classes to build rss / atom ... feeds
6 * Available feeds are defined in Defines.php
8 * Copyright © 2004 Brion Vibber <brion@pobox.com>
9 * https://www.mediawiki.org/
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.
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.
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
26 * @file
29 /**
30 * @defgroup Feed Feed
33 /**
34 * A base class for basic support for outputting syndication feeds in RSS and other formats.
36 * @ingroup Feed
38 class FeedItem {
39 /** @var Title */
40 public $title;
42 public $description;
44 public $url;
46 public $date;
48 public $author;
50 public $uniqueId;
52 public $comments;
54 public $rssIsPermalink = false;
56 /**
57 * @param string|Title $title Item's title
58 * @param string $description
59 * @param string $url URL uniquely designating the item.
60 * @param string $date Item's date
61 * @param string $author Author's user name
62 * @param string $comments
64 function __construct( $title, $description, $url, $date = '', $author = '', $comments = '' ) {
65 $this->title = $title;
66 $this->description = $description;
67 $this->url = $url;
68 $this->uniqueId = $url;
69 $this->date = $date;
70 $this->author = $author;
71 $this->comments = $comments;
74 /**
75 * Encode $string so that it can be safely embedded in a XML document
77 * @param string $string String to encode
78 * @return string
80 public function xmlEncode( $string ) {
81 $string = str_replace( "\r\n", "\n", $string );
82 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
83 return htmlspecialchars( $string );
86 /**
87 * Get the unique id of this item
89 * @return string
91 public function getUniqueId() {
92 if ( $this->uniqueId ) {
93 return $this->xmlEncode( wfExpandUrl( $this->uniqueId, PROTO_CURRENT ) );
97 /**
98 * Set the unique id of an item
100 * @param string $uniqueId Unique id for the item
101 * @param bool $rssIsPermalink Set to true if the guid (unique id) is a permalink (RSS feeds only)
103 public function setUniqueId( $uniqueId, $rssIsPermalink = false ) {
104 $this->uniqueId = $uniqueId;
105 $this->rssIsPermalink = $rssIsPermalink;
109 * Get the title of this item; already xml-encoded
111 * @return string
113 public function getTitle() {
114 return $this->xmlEncode( $this->title );
118 * Get the URL of this item; already xml-encoded
120 * @return string
122 public function getUrl() {
123 return $this->xmlEncode( $this->url );
127 * Get the description of this item; already xml-encoded
129 * @return string
131 public function getDescription() {
132 return $this->xmlEncode( $this->description );
136 * Get the language of this item
138 * @return string
140 public function getLanguage() {
141 global $wgLanguageCode;
142 return wfBCP47( $wgLanguageCode );
146 * Get the date of this item
148 * @return string
150 public function getDate() {
151 return $this->date;
155 * Get the author of this item; already xml-encoded
157 * @return string
159 public function getAuthor() {
160 return $this->xmlEncode( $this->author );
164 * Get the comment of this item; already xml-encoded
166 * @return string
168 public function getComments() {
169 return $this->xmlEncode( $this->comments );
173 * Quickie hack... strip out wikilinks to more legible form from the comment.
175 * @param string $text Wikitext
176 * @return string
178 public static function stripComment( $text ) {
179 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
181 /**#@-*/
185 * Class to support the outputting of syndication feeds in Atom and RSS format.
187 * @ingroup Feed
189 abstract class ChannelFeed extends FeedItem {
191 * Generate Header of the feed
192 * @par Example:
193 * @code
194 * print "<feed>";
195 * @endcode
197 abstract public function outHeader();
200 * Generate an item
201 * @par Example:
202 * @code
203 * print "<item>...</item>";
204 * @endcode
205 * @param FeedItem $item
207 abstract public function outItem( $item );
210 * Generate Footer of the feed
211 * @par Example:
212 * @code
213 * print "</feed>";
214 * @endcode
216 abstract public function outFooter();
219 * Setup and send HTTP headers. Don't send any content;
220 * content might end up being cached and re-sent with
221 * these same headers later.
223 * This should be called from the outHeader() method,
224 * but can also be called separately.
226 public function httpHeaders() {
227 global $wgOut, $wgVaryOnXFP;
229 # We take over from $wgOut, excepting its cache header info
230 $wgOut->disable();
231 $mimetype = $this->contentType();
232 header( "Content-type: $mimetype; charset=UTF-8" );
233 if ( $wgVaryOnXFP ) {
234 $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
236 $wgOut->sendCacheControl();
240 * Return an internet media type to be sent in the headers.
242 * @return string
244 private function contentType() {
245 global $wgRequest;
247 $ctype = $wgRequest->getVal( 'ctype', 'application/xml' );
248 $allowedctypes = [
249 'application/xml',
250 'text/xml',
251 'application/rss+xml',
252 'application/atom+xml'
255 return ( in_array( $ctype, $allowedctypes ) ? $ctype : 'application/xml' );
259 * Output the initial XML headers.
261 protected function outXmlHeader() {
262 $this->httpHeaders();
263 echo '<?xml version="1.0"?>' . "\n";
268 * Generate a RSS feed
270 * @ingroup Feed
272 class RSSFeed extends ChannelFeed {
275 * Format a date given a timestamp
277 * @param int $ts Timestamp
278 * @return string Date string
280 function formatTime( $ts ) {
281 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
285 * Output an RSS 2.0 header
287 function outHeader() {
288 global $wgVersion;
290 $this->outXmlHeader();
291 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
292 <channel>
293 <title><?php print $this->getTitle() ?></title>
294 <link><?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?></link>
295 <description><?php print $this->getDescription() ?></description>
296 <language><?php print $this->getLanguage() ?></language>
297 <generator>MediaWiki <?php print $wgVersion ?></generator>
298 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
299 <?php
303 * Output an RSS 2.0 item
304 * @param FeedItem $item Item to be output
306 function outItem( $item ) {
307 // @codingStandardsIgnoreStart Ignore long lines and formatting issues.
309 <item>
310 <title><?php print $item->getTitle(); ?></title>
311 <link><?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?></link>
312 <guid<?php if ( !$item->rssIsPermalink ) { print ' isPermaLink="false"'; } ?>><?php print $item->getUniqueId(); ?></guid>
313 <description><?php print $item->getDescription() ?></description>
314 <?php if ( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ); ?></pubDate><?php } ?>
315 <?php if ( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor(); ?></dc:creator><?php }?>
316 <?php if ( $item->getComments() ) { ?><comments><?php print wfExpandUrl( $item->getComments(), PROTO_CURRENT ); ?></comments><?php }?>
317 </item>
318 <?php
319 // @codingStandardsIgnoreEnd
323 * Output an RSS 2.0 footer
325 function outFooter() {
327 </channel>
328 </rss><?php
333 * Generate an Atom feed
335 * @ingroup Feed
337 class AtomFeed extends ChannelFeed {
339 * Format a date given timestamp.
341 * @param string|int $timestamp
342 * @return string
344 function formatTime( $timestamp ) {
345 // need to use RFC 822 time format at least for rss2.0
346 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $timestamp ) );
350 * Outputs a basic header for Atom 1.0 feeds.
352 function outHeader() {
353 global $wgVersion;
355 $this->outXmlHeader();
356 // @codingStandardsIgnoreStart Ignore long lines and formatting issues.
357 ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
358 <id><?php print $this->getFeedId() ?></id>
359 <title><?php print $this->getTitle() ?></title>
360 <link rel="self" type="application/atom+xml" href="<?php print wfExpandUrl( $this->getSelfUrl(), PROTO_CURRENT ) ?>"/>
361 <link rel="alternate" type="text/html" href="<?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?>"/>
362 <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
363 <subtitle><?php print $this->getDescription() ?></subtitle>
364 <generator>MediaWiki <?php print $wgVersion ?></generator>
366 <?php
367 // @codingStandardsIgnoreEnd
371 * Atom 1.0 requires a unique, opaque IRI as a unique identifier
372 * for every feed we create. For now just use the URL, but who
373 * can tell if that's right? If we put options on the feed, do we
374 * have to change the id? Maybe? Maybe not.
376 * @return string
378 private function getFeedId() {
379 return $this->getSelfUrl();
383 * Atom 1.0 requests a self-reference to the feed.
384 * @return string
386 private function getSelfUrl() {
387 global $wgRequest;
388 return htmlspecialchars( $wgRequest->getFullRequestURL() );
392 * Output a given item.
393 * @param FeedItem $item
395 function outItem( $item ) {
396 global $wgMimeType;
397 // @codingStandardsIgnoreStart Ignore long lines and formatting issues.
399 <entry>
400 <id><?php print $item->getUniqueId(); ?></id>
401 <title><?php print $item->getTitle(); ?></title>
402 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?>"/>
403 <?php if ( $item->getDate() ) { ?>
404 <updated><?php print $this->formatTime( $item->getDate() ); ?>Z</updated>
405 <?php } ?>
407 <summary type="html"><?php print $item->getDescription() ?></summary>
408 <?php if ( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor(); ?></name></author><?php }?>
409 </entry>
411 <?php /* @todo FIXME: Need to add comments
412 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
417 * Outputs the footer for Atom 1.0 feed (basically '\</feed\>').
419 function outFooter() {?>
420 </feed><?php
421 // @codingStandardsIgnoreEnd