MediaWiki  1.28.3
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  }
247 
253  private function contentType() {
255 
256  $ctype = $wgRequest->getVal( 'ctype', 'application/xml' );
257  $allowedctypes = [
258  'application/xml',
259  'text/xml',
260  'application/rss+xml',
261  'application/atom+xml'
262  ];
263 
264  return ( in_array( $ctype, $allowedctypes ) ? $ctype : 'application/xml' );
265  }
266 
270  protected function outXmlHeader() {
271  $this->httpHeaders();
272  echo '<?xml version="1.0"?>' . "\n";
273  }
274 }
275 
281 class RSSFeed extends ChannelFeed {
282 
289  function formatTime( $ts ) {
290  return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
291  }
292 
296  function outHeader() {
298 
299  $this->outXmlHeader();
300  ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
301  <channel>
302  <title><?php print $this->getTitle() ?></title>
303  <link><?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?></link>
304  <description><?php print $this->getDescription() ?></description>
305  <language><?php print $this->getLanguage() ?></language>
306  <generator>MediaWiki <?php print $wgVersion ?></generator>
307  <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
308 <?php
309  }
310 
315  function outItem( $item ) {
316  // @codingStandardsIgnoreStart Ignore long lines and formatting issues.
317  ?>
318  <item>
319  <title><?php print $item->getTitle(); ?></title>
320  <link><?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?></link>
321  <guid<?php if ( !$item->rssIsPermalink ) { print ' isPermaLink="false"'; } ?>><?php print $item->getUniqueId(); ?></guid>
322  <description><?php print $item->getDescription() ?></description>
323  <?php if ( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ); ?></pubDate><?php } ?>
324  <?php if ( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor(); ?></dc:creator><?php }?>
325  <?php if ( $item->getComments() ) { ?><comments><?php print wfExpandUrl( $item->getComments(), PROTO_CURRENT ); ?></comments><?php }?>
326  </item>
327 <?php
328  // @codingStandardsIgnoreEnd
329  }
330 
334  function outFooter() {
335  ?>
336  </channel>
337 </rss><?php
338  }
339 }
340 
346 class AtomFeed extends ChannelFeed {
353  function formatTime( $timestamp ) {
354  // need to use RFC 822 time format at least for rss2.0
355  return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $timestamp ) );
356  }
357 
361  function outHeader() {
363 
364  $this->outXmlHeader();
365  // @codingStandardsIgnoreStart Ignore long lines and formatting issues.
366  ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
367  <id><?php print $this->getFeedId() ?></id>
368  <title><?php print $this->getTitle() ?></title>
369  <link rel="self" type="application/atom+xml" href="<?php print wfExpandUrl( $this->getSelfUrl(), PROTO_CURRENT ) ?>"/>
370  <link rel="alternate" type="text/html" href="<?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?>"/>
371  <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
372  <subtitle><?php print $this->getDescription() ?></subtitle>
373  <generator>MediaWiki <?php print $wgVersion ?></generator>
374 
375 <?php
376  // @codingStandardsIgnoreEnd
377  }
378 
387  private function getFeedId() {
388  return $this->getSelfUrl();
389  }
390 
395  private function getSelfUrl() {
397  return htmlspecialchars( $wgRequest->getFullRequestURL() );
398  }
399 
404  function outItem( $item ) {
406  // @codingStandardsIgnoreStart Ignore long lines and formatting issues.
407  ?>
408  <entry>
409  <id><?php print $item->getUniqueId(); ?></id>
410  <title><?php print $item->getTitle(); ?></title>
411  <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?>"/>
412  <?php if ( $item->getDate() ) { ?>
413  <updated><?php print $this->formatTime( $item->getDate() ); ?>Z</updated>
414  <?php } ?>
415 
416  <summary type="html"><?php print $item->getDescription() ?></summary>
417  <?php if ( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor(); ?></name></author><?php }?>
418  </entry>
419 
420 <?php /* @todo FIXME: Need to add comments
421  <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
422  */
423  }
424 
428  function outFooter() {?>
429  </feed><?php
430  // @codingStandardsIgnoreEnd
431  }
432 }
outHeader()
Output an RSS 2.0 header.
Definition: Feed.php:296
outXmlHeader()
Output the initial XML headers.
Definition: Feed.php:270
outFooter()
Outputs the footer for Atom 1.0 feed (basically '\').
Definition: Feed.php:428
Generate a RSS feed.
Definition: Feed.php:281
$author
Definition: Feed.php:48
Class to support the outputting of syndication feeds in Atom and RSS format.
Definition: Feed.php:191
$uniqueId
Definition: Feed.php:50
contentType()
Return an internet media type to be sent in the headers.
Definition: Feed.php:253
$wgVersion
MediaWiki version number.
httpHeaders()
Setup and send HTTP headers.
Definition: Feed.php:228
outFooter()
Output an RSS 2.0 footer.
Definition: Feed.php:334
$url
Definition: Feed.php:44
xmlEncode($string)
Encode $string so that it can be safely embedded in a XML document.
Definition: Feed.php:82
$description
Definition: Feed.php:42
getDate()
Get the date of this item.
Definition: Feed.php:152
if(!$wgDBerrorLogTZ) $wgRequest
Definition: Setup.php:664
static singleton()
Get an instance of this class.
Definition: MimeMagic.php:29
$wgMimeType
The default Content-Type header.
formatTime($timestamp)
Format a date given timestamp.
Definition: Feed.php:353
getComments()
Get the comment of this item; already xml-encoded.
Definition: Feed.php:170
const PROTO_CURRENT
Definition: Defines.php:226
getSelfUrl()
Atom 1.0 requests a self-reference to the feed.
Definition: Feed.php:395
The MediaWiki class is the helper class for the index.php entry point.
Definition: MediaWiki.php:29
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
wfExpandUrl($url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Allows to change the fields on the form that will be generated rss
Definition: hooks.txt:304
title
static stripComment($text)
Quickie hack...
Definition: Feed.php:180
$wgVaryOnXFP
Add X-Forwarded-Proto to the Vary and Key headers for API requests and RSS/Atom feeds.
setUniqueId($uniqueId, $rssIsPermalink=false)
Set the unique id of an item.
Definition: Feed.php:105
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: defines.php:6
wfTimestamp($outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Prior to version
Definition: maintenance.txt:1
$wgLanguageCode
Site language code.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object to manipulate or replace but no entry for that model exists in $wgContentHandlers if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok inclusive false for true for descending in case the handler function wants to provide a converted Content object Note that $result getContentModel() must return $toModel. 'CustomEditor'$rcid is used in generating this variable which contains information about the new such as the revision s author
Definition: hooks.txt:1160
$rssIsPermalink
Definition: Feed.php:54
$comments
Definition: Feed.php:52
getAuthor()
Get the author of this item; already xml-encoded.
Definition: Feed.php:161
if($limit) $timestamp
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
__construct($title, $description, $url, $date= '', $author= '', $comments= '')
Constructor.
Definition: Feed.php:66
shown</td >< td > a href
wfBCP47($code)
Get the normalised IETF language tag See unit test for examples.
outItem($item)
Output an RSS 2.0 item.
Definition: Feed.php:315
A base class for basic support for outputting syndication feeds in RSS and other formats.
Definition: Feed.php:38
outFooter()
Generate Footer of the feed.
getLanguage()
Get the language of this item.
Definition: Feed.php:142
outItem($item)
Generate an item.
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
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
Generate an Atom feed.
Definition: Feed.php:346
outHeader()
Outputs a basic header for Atom 1.0 feeds.
Definition: Feed.php:361
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
outHeader()
Generate Header of the feed.
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:2893
getTitle()
Get the title of this item; already xml-encoded.
Definition: Feed.php:115
Title $title
Definition: Feed.php:40
getUniqueId()
Get the unique id of this item.
Definition: Feed.php:93
$wgOut
Definition: Setup.php:816
getFeedId()
Atom 1.0 requires a unique, opaque IRI as a unique identifier for every feed we create.
Definition: Feed.php:387
$date
Definition: Feed.php:46
outItem($item)
Output a given item.
Definition: Feed.php:404
getUrl()
Get the URL of this item; already xml-encoded.
Definition: Feed.php:124
getDescription()
Get the description of this item; already xml-encoded.
Definition: Feed.php:133
formatTime($ts)
Format a date given a timestamp.
Definition: Feed.php:289