MediaWiki master
AtomFeed.php
Go to the documentation of this file.
1<?php
2
26
29
35class AtomFeed extends ChannelFeed {
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
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
81 private function getSelfUrl() {
82 global $wgRequest;
83 return htmlspecialchars( $wgRequest->getFullRequestURL() );
84 }
85
91 public function outItem( $item ) {
92 $mimeType = MediaWikiServices::getInstance()->getMainConfig()
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
113 public function outFooter() {
114 print "</feed>";
115 }
116}
117
119class_alias( AtomFeed::class, 'AtomFeed' );
const PROTO_CURRENT
Definition Defines.php:209
const MW_VERSION
The running version of MediaWiki.
Definition Defines.php:37
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:420
Generate an Atom feed.
Definition AtomFeed.php:35
outFooter()
Outputs the footer for Atom 1.0 feed (basically '</feed>').
Definition AtomFeed.php:113
outHeader()
Outputs a basic header for Atom 1.0 feeds.
Definition AtomFeed.php:53
outItem( $item)
Output a given item.
Definition AtomFeed.php:91
Class to support the outputting of syndication feeds in Atom and RSS format.
outXmlHeader()
Output the initial XML headers.
getTitle()
Get the title of this item; already xml-encoded.
Definition FeedItem.php:144
getDescription()
Get the description of this item; already xml-encoded.
Definition FeedItem.php:170
getLanguage()
Get the language of this item.
Definition FeedItem.php:188
xmlEncode( $string)
Encode $string so that it can be safely embedded in a XML document.
Definition FeedItem.php:96
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.