Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
AtomFeed
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 5
42
0.00% covered (danger)
0.00%
0 / 1
 formatTime
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 outHeader
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 getSelfUrl
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 outItem
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 outFooter
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
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
25namespace MediaWiki\Feed;
26
27use MediaWiki\MainConfigNames;
28use MediaWiki\MediaWikiServices;
29
30/**
31 * Generate an Atom feed.
32 *
33 * @ingroup Feed
34 */
35class 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( wfExpandUrl( $this->getUrlUnescaped(), PROTO_CURRENT ) ),
66            'selfUrl' => $this->getSelfUrl(),
67            'timestamp' => $this->xmlEncode( $this->formatTime( wfTimestampNow() ) ),
68            'description' => $this->getDescription(),
69            'version' => $this->xmlEncode( MW_VERSION ),
70        ];
71        print $this->templateParser->processTemplate( 'AtomHeader', $templateParams );
72    }
73
74    /**
75     * Atom 1.0 requests a self-reference to the feed.
76     * @return string
77     */
78    private function getSelfUrl() {
79        global $wgRequest;
80        return htmlspecialchars( $wgRequest->getFullRequestURL() );
81    }
82
83    /**
84     * Output a given item.
85     * @param FeedItem $item
86     */
87    public function outItem( $item ) {
88        $mimeType = MediaWikiServices::getInstance()->getMainConfig()
89            ->get( MainConfigNames::MimeType );
90        // Manually escaping rather than letting Mustache do it because Mustache
91        // uses htmlentities, which does not work with XML
92        $templateParams = [
93            "uniqueID" => $item->getUniqueID(),
94            "title" => $item->getTitle(),
95            "mimeType" => $this->xmlEncode( $mimeType ),
96            "url" => $this->xmlEncode( wfExpandUrl( $item->getUrlUnescaped(), PROTO_CURRENT ) ),
97            "date" => $this->xmlEncode( $this->formatTime( $item->getDate() ) ),
98            "description" => $item->getDescription(),
99            "author" => $item->getAuthor()
100        ];
101        print $this->templateParser->processTemplate( 'AtomItem', $templateParams );
102    }
103
104    /**
105     * Outputs the footer for Atom 1.0 feed (basically '\</feed\>').
106     */
107    public function outFooter() {
108        print "</feed>";
109    }
110}
111
112/** @deprecated class alias since 1.40 */
113class_alias( AtomFeed::class, 'AtomFeed' );