MediaWiki master
AtomFeed.php
Go to the documentation of this file.
1<?php
2
12
15use Wikimedia\Timestamp\TimestampFormat as TS;
16
22class AtomFeed extends ChannelFeed {
29 private function formatTime( $timestamp ) {
30 if ( $timestamp ) {
31 // need to use RFC 822 time format at least for rss2.0
32 return gmdate( 'Y-m-d\TH:i:s', (int)wfTimestamp( TS::UNIX, $timestamp ) );
33 }
34 return null;
35 }
36
40 public function outHeader() {
41 $this->outXmlHeader();
42 // Manually escaping rather than letting Mustache do it because Mustache
43 // uses htmlentities, which does not work with XML
44 $templateParams = [
45 'language' => $this->xmlEncode( $this->getLanguage() ),
46 // Atom 1.0 requires a unique, opaque IRI as a unique identifier
47 // for every feed we create. For now just use the URL, but who
48 // can tell if that's right? If we put options on the feed, do we
49 // have to change the id? Maybe? Maybe not.
50 'feedID' => $this->getSelfUrl(),
51 'title' => $this->getTitle(),
52 'url' => $this->xmlEncode(
53 $this->urlUtils->expand( $this->getUrlUnescaped(), PROTO_CURRENT ) ?? ''
54 ),
55 'selfUrl' => $this->getSelfUrl(),
56 'timestamp' => $this->xmlEncode( $this->formatTime( wfTimestampNow() ) ),
57 'description' => $this->getDescription(),
58 'version' => $this->xmlEncode( MW_VERSION ),
59 ];
60 print $this->templateParser->processTemplate( 'AtomHeader', $templateParams );
61 }
62
68 private function getSelfUrl() {
69 global $wgRequest;
70 return htmlspecialchars( $wgRequest->getFullRequestURL() );
71 }
72
78 public function outItem( $item ) {
79 $mimeType = MediaWikiServices::getInstance()->getMainConfig()
81 // Manually escaping rather than letting Mustache do it because Mustache
82 // uses htmlentities, which does not work with XML
83 $templateParams = [
84 "uniqueID" => $item->getUniqueID(),
85 "title" => $item->getTitle(),
86 "mimeType" => $this->xmlEncode( $mimeType ),
87 "url" => $this->xmlEncode(
88 $this->urlUtils->expand( $item->getUrlUnescaped(), PROTO_CURRENT ) ?? ''
89 ),
90 "date" => $this->xmlEncodeNullable( $this->formatTime( $item->getDate() ) ),
91 "description" => $item->getDescription(),
92 "author" => $item->getAuthor()
93 ];
94 print $this->templateParser->processTemplate( 'AtomItem', $templateParams );
95 }
96
100 public function outFooter() {
101 print "</feed>";
102 }
103}
const PROTO_CURRENT
Definition Defines.php:222
const MW_VERSION
The running version of MediaWiki.
Definition Defines.php:23
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
global $wgRequest
Definition Setup.php:434
Generate an Atom feed.
Definition AtomFeed.php:22
outFooter()
Outputs the footer for Atom 1.0 feed (basically '</feed>').
Definition AtomFeed.php:100
outHeader()
Outputs a basic header for Atom 1.0 feeds.
Definition AtomFeed.php:40
outItem( $item)
Output a given item.
Definition AtomFeed.php:78
Class to support the outputting of syndication feeds in Atom and RSS format.
outXmlHeader()
Output the initial XML headers.
xmlEncodeNullable(?string $string)
Encode $string so that it can be safely embedded in a XML document, returning null if $string was nul...
Definition FeedItem.php:93
getTitle()
Get the title of this item; already xml-encoded.
Definition FeedItem.php:138
getDescription()
Get the description of this item; already xml-encoded.
Definition FeedItem.php:164
getLanguage()
Get the language of this item.
Definition FeedItem.php:182
xmlEncode( $string)
Encode $string so that it can be safely embedded in a XML document.
Definition FeedItem.php:81
A class containing constants representing the names of configuration variables.
const MimeType
Name constant for the MimeType setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.