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( 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
78 private function getSelfUrl() {
79 global $wgRequest;
80 return htmlspecialchars( $wgRequest->getFullRequestURL() );
81 }
82
87 public function outItem( $item ) {
88 $mimeType = MediaWikiServices::getInstance()->getMainConfig()
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
107 public function outFooter() {
108 print "</feed>";
109 }
110}
111
113class_alias( AtomFeed::class, 'AtomFeed' );
const PROTO_CURRENT
Definition Defines.php:205
const MW_VERSION
The running version of MediaWiki.
Definition Defines.php:36
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL using $wgServer (or one of its alternatives).
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
global $wgRequest
Definition Setup.php:415
Generate an Atom feed.
Definition AtomFeed.php:35
outFooter()
Outputs the footer for Atom 1.0 feed (basically '</feed>').
Definition AtomFeed.php:107
outHeader()
Outputs a basic header for Atom 1.0 feeds.
Definition AtomFeed.php:53
outItem( $item)
Output a given item.
Definition AtomFeed.php:87
Class to support the outputting of syndication feeds in Atom and RSS format.
outXmlHeader()
Output the initial XML headers.
getUrlUnescaped()
Get the URL of this item without any escaping.
Definition FeedItem.php:144
getTitle()
Get the title of this item; already xml-encoded.
Definition FeedItem.php:127
getDescription()
Get the description of this item; already xml-encoded.
Definition FeedItem.php:153
getLanguage()
Get the language of this item.
Definition FeedItem.php:171
xmlEncode( $string)
Encode $string so that it can be safely embedded in a XML document.
Definition FeedItem.php:84
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.