changes: Split Feed.php into a class per file
[lhc/web/wiklou.git] / includes / changes / AtomFeed.php
1 <?php
2 /**
3 * Copyright © 2004 Brion Vibber <brion@pobox.com>
4 * https://www.mediawiki.org/
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 */
23
24 /**
25 * Generate an Atom feed.
26 *
27 * @ingroup Feed
28 */
29 class AtomFeed extends ChannelFeed {
30 /**
31 * Format a date given timestamp, if one is given.
32 *
33 * @param string|int|null $timestamp
34 * @return string|null
35 */
36 function formatTime( $timestamp ) {
37 if ( $timestamp ) {
38 // need to use RFC 822 time format at least for rss2.0
39 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $timestamp ) );
40 }
41 }
42
43 /**
44 * Outputs a basic header for Atom 1.0 feeds.
45 */
46 function outHeader() {
47 global $wgVersion;
48 $this->outXmlHeader();
49 // Manually escaping rather than letting Mustache do it because Mustache
50 // uses htmlentities, which does not work with XML
51 $templateParams = [
52 'language' => $this->xmlEncode( $this->getLanguage() ),
53 'feedID' => $this->getFeedId(),
54 'title' => $this->getTitle(),
55 'url' => $this->xmlEncode( wfExpandUrl( $this->getUrlUnescaped(), PROTO_CURRENT ) ),
56 'selfUrl' => $this->getSelfUrl(),
57 'timestamp' => $this->xmlEncode( $this->formatTime( wfTimestampNow() ) ),
58 'description' => $this->getDescription(),
59 'version' => $this->xmlEncode( $wgVersion ),
60 ];
61 print $this->templateParser->processTemplate( 'AtomHeader', $templateParams );
62 }
63
64 /**
65 * Atom 1.0 requires a unique, opaque IRI as a unique identifier
66 * for every feed we create. For now just use the URL, but who
67 * can tell if that's right? If we put options on the feed, do we
68 * have to change the id? Maybe? Maybe not.
69 *
70 * @return string
71 */
72 private function getFeedId() {
73 return $this->getSelfUrl();
74 }
75
76 /**
77 * Atom 1.0 requests a self-reference to the feed.
78 * @return string
79 */
80 private function getSelfUrl() {
81 global $wgRequest;
82 return htmlspecialchars( $wgRequest->getFullRequestURL() );
83 }
84
85 /**
86 * Output a given item.
87 * @param FeedItem $item
88 */
89 function outItem( $item ) {
90 global $wgMimeType;
91 // Manually escaping rather than letting Mustache do it because Mustache
92 // uses htmlentities, which does not work with XML
93 $templateParams = [
94 "uniqueID" => $item->getUniqueID(),
95 "title" => $item->getTitle(),
96 "mimeType" => $this->xmlEncode( $wgMimeType ),
97 "url" => $this->xmlEncode( wfExpandUrl( $item->getUrlUnescaped(), PROTO_CURRENT ) ),
98 "date" => $this->xmlEncode( $this->formatTime( $item->getDate() ) ),
99 "description" => $item->getDescription(),
100 "author" => $item->getAuthor()
101 ];
102 print $this->templateParser->processTemplate( 'AtomItem', $templateParams );
103 }
104
105 /**
106 * Outputs the footer for Atom 1.0 feed (basically '\</feed\>').
107 */
108 function outFooter() {
109 print "</feed>";
110 }
111 }