MediaWiki  1.29.2
Feed.php
Go to the documentation of this file.
1 <?php
38 class FeedItem {
40  public $title;
41 
42  public $description;
43 
44  public $url;
45 
46  public $date;
47 
48  public $author;
49 
50  public $uniqueId;
51 
52  public $comments;
53 
54  public $rssIsPermalink = false;
55 
66  function __construct( $title, $description, $url, $date = '', $author = '', $comments = '' ) {
67  $this->title = $title;
68  $this->description = $description;
69  $this->url = $url;
70  $this->uniqueId = $url;
71  $this->date = $date;
72  $this->author = $author;
73  $this->comments = $comments;
74  }
75 
82  public function xmlEncode( $string ) {
83  $string = str_replace( "\r\n", "\n", $string );
84  $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
85  return htmlspecialchars( $string );
86  }
87 
93  public function getUniqueId() {
94  if ( $this->uniqueId ) {
95  return $this->xmlEncode( wfExpandUrl( $this->uniqueId, PROTO_CURRENT ) );
96  }
97  }
98 
105  public function setUniqueId( $uniqueId, $rssIsPermalink = false ) {
106  $this->uniqueId = $uniqueId;
107  $this->rssIsPermalink = $rssIsPermalink;
108  }
109 
115  public function getTitle() {
116  return $this->xmlEncode( $this->title );
117  }
118 
124  public function getUrl() {
125  return $this->xmlEncode( $this->url );
126  }
127 
133  public function getDescription() {
134  return $this->xmlEncode( $this->description );
135  }
136 
142  public function getLanguage() {
144  return wfBCP47( $wgLanguageCode );
145  }
146 
152  public function getDate() {
153  return $this->date;
154  }
155 
161  public function getAuthor() {
162  return $this->xmlEncode( $this->author );
163  }
164 
170  public function getComments() {
171  return $this->xmlEncode( $this->comments );
172  }
173 
180  public static function stripComment( $text ) {
181  return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
182  }
184 }
185 
191 abstract class ChannelFeed extends FeedItem {
199  abstract public function outHeader();
200 
209  abstract public function outItem( $item );
210 
218  abstract public function outFooter();
219 
228  public function httpHeaders() {
230 
231  # We take over from $wgOut, excepting its cache header info
232  $wgOut->disable();
233  $mimetype = $this->contentType();
234  header( "Content-type: $mimetype; charset=UTF-8" );
235 
236  // Set a sane filename
237  $exts = MimeMagic::singleton()->getExtensionsForType( $mimetype );
238  $ext = $exts ? strtok( $exts, ' ' ) : 'xml';
239  header( "Content-Disposition: inline; filename=\"feed.{$ext}\"" );
240 
241  if ( $wgVaryOnXFP ) {
242  $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
243  }
244  $wgOut->sendCacheControl();
245  }
246 
252  private function contentType() {
254 
255  $ctype = $wgRequest->getVal( 'ctype', 'application/xml' );
256  $allowedctypes = [
257  'application/xml',
258  'text/xml',
259  'application/rss+xml',
260  'application/atom+xml'
261  ];
262 
263  return ( in_array( $ctype, $allowedctypes ) ? $ctype : 'application/xml' );
264  }
265 
269  protected function outXmlHeader() {
270  $this->httpHeaders();
271  echo '<?xml version="1.0"?>' . "\n";
272  }
273 }
274 
280 class RSSFeed extends ChannelFeed {
281 
288  function formatTime( $ts ) {
289  return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
290  }
291 
295  function outHeader() {
297 
298  $this->outXmlHeader();
299  ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
300  <channel>
301  <title><?php print $this->getTitle() ?></title>
302  <link><?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?></link>
303  <description><?php print $this->getDescription() ?></description>
304  <language><?php print $this->getLanguage() ?></language>
305  <generator>MediaWiki <?php print $wgVersion ?></generator>
306  <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
307 <?php
308  }
309 
314  function outItem( $item ) {
315  // @codingStandardsIgnoreStart Ignore long lines and formatting issues.
316  ?>
317  <item>
318  <title><?php print $item->getTitle(); ?></title>
319  <link><?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?></link>
320  <guid<?php if ( !$item->rssIsPermalink ) { print ' isPermaLink="false"'; } ?>><?php print $item->getUniqueId(); ?></guid>
321  <description><?php print $item->getDescription() ?></description>
322  <?php if ( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ); ?></pubDate><?php } ?>
323  <?php if ( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor(); ?></dc:creator><?php }?>
324  <?php if ( $item->getComments() ) { ?><comments><?php print wfExpandUrl( $item->getComments(), PROTO_CURRENT ); ?></comments><?php }?>
325  </item>
326 <?php
327  // @codingStandardsIgnoreEnd
328  }
329 
333  function outFooter() {
334  ?>
335  </channel>
336 </rss><?php
337  }
338 }
339 
345 class AtomFeed extends ChannelFeed {
352  function formatTime( $timestamp ) {
353  // need to use RFC 822 time format at least for rss2.0
354  return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $timestamp ) );
355  }
356 
360  function outHeader() {
362 
363  $this->outXmlHeader();
364  // @codingStandardsIgnoreStart Ignore long lines and formatting issues.
365  ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
366  <id><?php print $this->getFeedId() ?></id>
367  <title><?php print $this->getTitle() ?></title>
368  <link rel="self" type="application/atom+xml" href="<?php print wfExpandUrl( $this->getSelfUrl(), PROTO_CURRENT ) ?>"/>
369  <link rel="alternate" type="text/html" href="<?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?>"/>
370  <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
371  <subtitle><?php print $this->getDescription() ?></subtitle>
372  <generator>MediaWiki <?php print $wgVersion ?></generator>
373 
374 <?php
375  // @codingStandardsIgnoreEnd
376  }
377 
386  private function getFeedId() {
387  return $this->getSelfUrl();
388  }
389 
394  private function getSelfUrl() {
396  return htmlspecialchars( $wgRequest->getFullRequestURL() );
397  }
398 
403  function outItem( $item ) {
404  global $wgMimeType;
405  // @codingStandardsIgnoreStart Ignore long lines and formatting issues.
406  ?>
407  <entry>
408  <id><?php print $item->getUniqueId(); ?></id>
409  <title><?php print $item->getTitle(); ?></title>
410  <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?>"/>
411  <?php if ( $item->getDate() ) { ?>
412  <updated><?php print $this->formatTime( $item->getDate() ); ?>Z</updated>
413  <?php } ?>
414 
415  <summary type="html"><?php print $item->getDescription() ?></summary>
416  <?php if ( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor(); ?></name></author><?php }?>
417  </entry>
418 
419 <?php /* @todo FIXME: Need to add comments
420  <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
421  */
422  }
423 
427  function outFooter() {?>
428  </feed><?php
429  // @codingStandardsIgnoreEnd
430  }
431 }
FeedItem
A base class for basic support for outputting syndication feeds in RSS and other formats.
Definition: Feed.php:38
FeedItem\getUniqueId
getUniqueId()
Get the unique id of this item.
Definition: Feed.php:93
AtomFeed\getFeedId
print $this getFeedId() print $this getTitle() print wfExpandUrl($this->getSelfUrl(), PROTO_CURRENT) print wfExpandUrl($this->getUrl(), PROTO_CURRENT) print $this formatTime(wfTimestampNow()) print $this getDescription() print $wgVersion getFeedId()
Atom 1.0 requires a unique, opaque IRI as a unique identifier for every feed we create.
Definition: Feed.php:386
FeedItem\$url
$url
Definition: Feed.php:44
wfBCP47
wfBCP47( $code)
Get the normalised IETF language tag See unit test for examples.
Definition: GlobalFunctions.php:3370
AtomFeed\outHeader
outHeader()
Outputs a basic header for Atom 1.0 feeds.
Definition: Feed.php:360
FeedItem\$description
$description
Definition: Feed.php:42
FeedItem\$rssIsPermalink
$rssIsPermalink
Definition: Feed.php:54
version
Prior to version
Definition: maintenance.txt:1
MediaWiki\getTitle
getTitle()
Get the Title object that we'll be acting on, as specified in the WebRequest.
Definition: MediaWiki.php:137
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1994
$wgVersion
$wgVersion
MediaWiki version number.
Definition: DefaultSettings.php:78
rss
Allows to change the fields on the form that will be generated rss
Definition: hooks.txt:304
AtomFeed
Generate an Atom feed.
Definition: Feed.php:345
FeedItem\getLanguage
getLanguage()
Get the language of this item.
Definition: Feed.php:142
FeedItem\stripComment
static stripComment( $text)
Quickie hack...
Definition: Feed.php:180
link
usually copyright or history_copyright This message must be in HTML not wikitext if the section is included from a template to be included in the link
Definition: hooks.txt:2929
FeedItem\getTitle
getTitle()
Get the title of this item; already xml-encoded.
Definition: Feed.php:115
outItem
ChannelFeed FeedItem print $this getTitle() print wfExpandUrl($this->getUrl(), PROTO_CURRENT) print $this getDescription() print $this getLanguage() print $wgVersion print $this formatTime(wfTimestampNow()) outItem( $item)
Class to support the outputting of syndication feeds in Atom and RSS format.
Definition: Feed.php:314
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
contentType
contentType()
Return an internet media type to be sent in the headers.
Definition: Feed.php:375
FeedItem\getAuthor
getAuthor()
Get the author of this item; already xml-encoded.
Definition: Feed.php:161
outXmlHeader
outXmlHeader()
Output the initial XML headers.
Definition: Feed.php:392
PROTO_CURRENT
const PROTO_CURRENT
Definition: Defines.php:220
FeedItem\$date
$date
Definition: Feed.php:46
FeedItem\getDate
getDate()
Get the date of this item.
Definition: Feed.php:152
$wgVaryOnXFP
$wgVaryOnXFP
Add X-Forwarded-Proto to the Vary and Key headers for API requests and RSS/Atom feeds.
Definition: DefaultSettings.php:2655
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
AtomFeed\getSelfUrl
getSelfUrl()
Atom 1.0 requests a self-reference to the feed.
Definition: Feed.php:394
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:2023
MimeMagic\singleton
static singleton()
Get an instance of this class.
Definition: MimeMagic.php:33
AtomFeed\formatTime
formatTime( $timestamp)
Format a date given timestamp.
Definition: Feed.php:352
outHeader
outHeader()
Generate Header of the feed.
FeedItem\setUniqueId
setUniqueId( $uniqueId, $rssIsPermalink=false)
Set the unique id of an item.
Definition: Feed.php:105
FeedItem\getDescription
getDescription()
Get the description of this item; already xml-encoded.
Definition: Feed.php:133
title
title
Definition: parserTests.txt:211
language
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed we want its recipients to know that what they have is not the so that any problems introduced by others will not reflect on the original authors reputations any free program is threatened constantly by software patents We wish to avoid the danger that redistributors of a free program will individually obtain patent in effect making the program proprietary To prevent we have made it clear that any patent must be licensed for everyone s free use or not licensed at all The precise terms and conditions for distribution and modification follow GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR DISTRIBUTION AND MODIFICATION This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License The refers to any such program or and a work based on the Program means either the Program or any derivative work under copyright a work containing the Program or a portion of either verbatim or with modifications and or translated into another language(Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying
FeedItem\getUrl
getUrl()
Get the URL of this item; already xml-encoded.
Definition: Feed.php:124
$wgLanguageCode
$wgLanguageCode
Site language code.
Definition: DefaultSettings.php:2839
FeedItem\$comments
$comments
Definition: Feed.php:52
FeedItem\__construct
__construct( $title, $description, $url, $date='', $author='', $comments='')
Constructor.
Definition: Feed.php:66
AtomFeed\outFooter
if( $item->getDate()) print $item getDescription() if( $item->getAuthor()) outFooter()
Outputs the footer for Atom 1.0 feed (basically '</feed>').
Definition: Feed.php:427
httpHeaders
httpHeaders()
Setup and send HTTP headers.
Definition: Feed.php:351
Title
Represents a title within MediaWiki.
Definition: Title.php:39
type
This document describes the state of Postgres support in and is fairly well maintained The main code is very well while extensions are very hit and miss it is probably the most supported database after MySQL Much of the work in making MediaWiki database agnostic came about through the work of creating Postgres as and are nearing end of but without copying over all the usage comments General notes on the but these can almost always be programmed around *Although Postgres has a true BOOLEAN type
Definition: postgres.txt:22
$ext
$ext
Definition: NoLocalSettings.php:25
FeedItem\xmlEncode
xmlEncode( $string)
Encode $string so that it can be safely embedded in a XML document.
Definition: Feed.php:82
outFooter
outFooter()
Generate Footer of the feed.
Definition: Feed.php:333
FeedItem\$author
$author
Definition: Feed.php:48
name
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at name
Definition: design.txt:12
$wgRequest
if(! $wgDBerrorLogTZ) $wgRequest
Definition: Setup.php:639
FeedItem\$title
Title $title
Definition: Feed.php:40
FeedItem\getComments
getComments()
Get the comment of this item; already xml-encoded.
Definition: Feed.php:170
$wgOut
$wgOut
Definition: Setup.php:791
AtomFeed\outItem
outItem( $item)
Output a given item.
Definition: Feed.php:403
href
shown</td >< td > a href
Definition: All_system_messages.txt:2667
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:552
MediaWiki
The MediaWiki class is the helper class for the index.php entry point.
Definition: MediaWiki.php:33
FeedItem\$uniqueId
$uniqueId
Definition: Feed.php:50