Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 35 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
AtomFeed | |
0.00% |
0 / 34 |
|
0.00% |
0 / 5 |
42 | |
0.00% |
0 / 1 |
formatTime | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
outHeader | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
getSelfUrl | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
outItem | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
outFooter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Copyright © 2004 Brooke Vibber <bvibber@wikimedia.org> |
5 | * https://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 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
20 | * http://www.gnu.org/copyleft/gpl.html |
21 | * |
22 | * @file |
23 | */ |
24 | |
25 | namespace MediaWiki\Feed; |
26 | |
27 | use MediaWiki\MainConfigNames; |
28 | use MediaWiki\MediaWikiServices; |
29 | |
30 | /** |
31 | * Generate an Atom feed. |
32 | * |
33 | * @ingroup Feed |
34 | */ |
35 | class AtomFeed extends ChannelFeed { |
36 | /** |
37 | * Format a date given timestamp, if one is given. |
38 | * |
39 | * @param string|int|null $timestamp |
40 | * @return string|null |
41 | */ |
42 | private function formatTime( $timestamp ) { |
43 | if ( $timestamp ) { |
44 | // need to use RFC 822 time format at least for rss2.0 |
45 | return gmdate( 'Y-m-d\TH:i:s', (int)wfTimestamp( TS_UNIX, $timestamp ) ); |
46 | } |
47 | return null; |
48 | } |
49 | |
50 | /** |
51 | * Outputs a basic header for Atom 1.0 feeds. |
52 | */ |
53 | public function outHeader() { |
54 | $this->outXmlHeader(); |
55 | // Manually escaping rather than letting Mustache do it because Mustache |
56 | // uses htmlentities, which does not work with XML |
57 | $templateParams = [ |
58 | 'language' => $this->xmlEncode( $this->getLanguage() ), |
59 | // Atom 1.0 requires a unique, opaque IRI as a unique identifier |
60 | // for every feed we create. For now just use the URL, but who |
61 | // can tell if that's right? If we put options on the feed, do we |
62 | // have to change the id? Maybe? Maybe not. |
63 | 'feedID' => $this->getSelfUrl(), |
64 | 'title' => $this->getTitle(), |
65 | 'url' => $this->xmlEncode( |
66 | $this->urlUtils->expand( $this->getUrlUnescaped(), PROTO_CURRENT ) ?? '' |
67 | ), |
68 | 'selfUrl' => $this->getSelfUrl(), |
69 | 'timestamp' => $this->xmlEncode( $this->formatTime( wfTimestampNow() ) ), |
70 | 'description' => $this->getDescription(), |
71 | 'version' => $this->xmlEncode( MW_VERSION ), |
72 | ]; |
73 | print $this->templateParser->processTemplate( 'AtomHeader', $templateParams ); |
74 | } |
75 | |
76 | /** |
77 | * Atom 1.0 requests a self-reference to the feed. |
78 | * |
79 | * @return string |
80 | */ |
81 | private function getSelfUrl() { |
82 | global $wgRequest; |
83 | return htmlspecialchars( $wgRequest->getFullRequestURL() ); |
84 | } |
85 | |
86 | /** |
87 | * Output a given item. |
88 | * |
89 | * @param FeedItem $item |
90 | */ |
91 | public function outItem( $item ) { |
92 | $mimeType = MediaWikiServices::getInstance()->getMainConfig() |
93 | ->get( MainConfigNames::MimeType ); |
94 | // Manually escaping rather than letting Mustache do it because Mustache |
95 | // uses htmlentities, which does not work with XML |
96 | $templateParams = [ |
97 | "uniqueID" => $item->getUniqueID(), |
98 | "title" => $item->getTitle(), |
99 | "mimeType" => $this->xmlEncode( $mimeType ), |
100 | "url" => $this->xmlEncode( |
101 | $this->urlUtils->expand( $item->getUrlUnescaped(), PROTO_CURRENT ) ?? '' |
102 | ), |
103 | "date" => $this->xmlEncode( $this->formatTime( $item->getDate() ) ), |
104 | "description" => $item->getDescription(), |
105 | "author" => $item->getAuthor() |
106 | ]; |
107 | print $this->templateParser->processTemplate( 'AtomItem', $templateParams ); |
108 | } |
109 | |
110 | /** |
111 | * Outputs the footer for Atom 1.0 feed (basically '\</feed\>'). |
112 | */ |
113 | public function outFooter() { |
114 | print "</feed>"; |
115 | } |
116 | } |
117 | |
118 | /** @deprecated class alias since 1.40 */ |
119 | class_alias( AtomFeed::class, 'AtomFeed' ); |