MediaWiki  REL1_24
Feed.php
Go to the documentation of this file.
00001 <?php
00038 class FeedItem {
00040     public $title;
00041 
00042     public $description;
00043 
00044     public $url;
00045 
00046     public $date;
00047 
00048     public $author;
00049 
00050     public $uniqueId;
00051 
00052     public $comments;
00053 
00054     public $rssIsPermalink = false;
00055 
00066     function __construct( $title, $description, $url, $date = '', $author = '', $comments = '' ) {
00067         $this->title = $title;
00068         $this->description = $description;
00069         $this->url = $url;
00070         $this->uniqueId = $url;
00071         $this->date = $date;
00072         $this->author = $author;
00073         $this->comments = $comments;
00074     }
00075 
00082     public function xmlEncode( $string ) {
00083         $string = str_replace( "\r\n", "\n", $string );
00084         $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
00085         return htmlspecialchars( $string );
00086     }
00087 
00093     public function getUniqueId() {
00094         if ( $this->uniqueId ) {
00095             return $this->xmlEncode( $this->uniqueId );
00096         }
00097     }
00098 
00105     public function setUniqueId( $uniqueId, $rssIsPermalink = false ) {
00106         $this->uniqueId = $uniqueId;
00107         $this->rssIsPermalink = $rssIsPermalink;
00108     }
00109 
00115     public function getTitle() {
00116         return $this->xmlEncode( $this->title );
00117     }
00118 
00124     public function getUrl() {
00125         return $this->xmlEncode( $this->url );
00126     }
00127 
00133     public function getDescription() {
00134         return $this->xmlEncode( $this->description );
00135     }
00136 
00142     public function getLanguage() {
00143         global $wgLanguageCode;
00144         return $wgLanguageCode;
00145     }
00146 
00152     public function getDate() {
00153         return $this->date;
00154     }
00155 
00161     public function getAuthor() {
00162         return $this->xmlEncode( $this->author );
00163     }
00164 
00170     public function getComments() {
00171         return $this->xmlEncode( $this->comments );
00172     }
00173 
00180     public static function stripComment( $text ) {
00181         return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
00182     }
00184 }
00185 
00190 abstract class ChannelFeed extends FeedItem {
00198     abstract public function outHeader();
00199 
00208     abstract public function outItem( $item );
00209 
00217     abstract public function outFooter();
00218 
00227     public function httpHeaders() {
00228         global $wgOut, $wgVaryOnXFP;
00229 
00230         # We take over from $wgOut, excepting its cache header info
00231         $wgOut->disable();
00232         $mimetype = $this->contentType();
00233         header( "Content-type: $mimetype; charset=UTF-8" );
00234         if ( $wgVaryOnXFP ) {
00235             $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
00236         }
00237         $wgOut->sendCacheControl();
00238 
00239     }
00240 
00246     private function contentType() {
00247         global $wgRequest;
00248 
00249         $ctype = $wgRequest->getVal( 'ctype', 'application/xml' );
00250         $allowedctypes = array(
00251             'application/xml',
00252             'text/xml',
00253             'application/rss+xml',
00254             'application/atom+xml'
00255         );
00256 
00257         return ( in_array( $ctype, $allowedctypes ) ? $ctype : 'application/xml' );
00258     }
00259 
00263     protected function outXmlHeader() {
00264         $this->httpHeaders();
00265         echo '<?xml version="1.0"?>' . "\n";
00266     }
00267 }
00268 
00274 class RSSFeed extends ChannelFeed {
00275 
00282     function formatTime( $ts ) {
00283         return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
00284     }
00285 
00289     function outHeader() {
00290         global $wgVersion;
00291 
00292         $this->outXmlHeader();
00293         ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
00294     <channel>
00295         <title><?php print $this->getTitle() ?></title>
00296         <link><?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?></link>
00297         <description><?php print $this->getDescription() ?></description>
00298         <language><?php print $this->getLanguage() ?></language>
00299         <generator>MediaWiki <?php print $wgVersion ?></generator>
00300         <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
00301 <?php
00302     }
00303 
00308     function outItem( $item ) {
00309         // @codingStandardsIgnoreStart Ignore long lines and formatting issues.
00310     ?>
00311         <item>
00312             <title><?php print $item->getTitle(); ?></title>
00313             <link><?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?></link>
00314             <guid<?php if ( !$item->rssIsPermalink ) { print ' isPermaLink="false"'; } ?>><?php print $item->getUniqueId(); ?></guid>
00315             <description><?php print $item->getDescription() ?></description>
00316             <?php if ( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ); ?></pubDate><?php } ?>
00317             <?php if ( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor(); ?></dc:creator><?php }?>
00318             <?php if ( $item->getComments() ) { ?><comments><?php print wfExpandUrl( $item->getComments(), PROTO_CURRENT ); ?></comments><?php }?>
00319         </item>
00320 <?php
00321         // @codingStandardsIgnoreEnd
00322     }
00323 
00327     function outFooter() {
00328     ?>
00329     </channel>
00330 </rss><?php
00331     }
00332 }
00333 
00339 class AtomFeed extends ChannelFeed {
00345     function formatTime( $ts ) {
00346         // need to use RFC 822 time format at least for rss2.0
00347         return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
00348     }
00349 
00353     function outHeader() {
00354         global $wgVersion;
00355 
00356         $this->outXmlHeader();
00357         // @codingStandardsIgnoreStart Ignore long lines and formatting issues.
00358         ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
00359         <id><?php print $this->getFeedId() ?></id>
00360         <title><?php print $this->getTitle() ?></title>
00361         <link rel="self" type="application/atom+xml" href="<?php print wfExpandUrl( $this->getSelfUrl(), PROTO_CURRENT ) ?>"/>
00362         <link rel="alternate" type="text/html" href="<?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?>"/>
00363         <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
00364         <subtitle><?php print $this->getDescription() ?></subtitle>
00365         <generator>MediaWiki <?php print $wgVersion ?></generator>
00366 
00367 <?php
00368         // @codingStandardsIgnoreEnd
00369     }
00370 
00379     private function getFeedId() {
00380         return $this->getSelfUrl();
00381     }
00382 
00387     private function getSelfUrl() {
00388         global $wgRequest;
00389         return htmlspecialchars( $wgRequest->getFullRequestURL() );
00390     }
00391 
00396     function outItem( $item ) {
00397         global $wgMimeType;
00398         // @codingStandardsIgnoreStart Ignore long lines and formatting issues.
00399     ?>
00400     <entry>
00401         <id><?php print $item->getUniqueId(); ?></id>
00402         <title><?php print $item->getTitle(); ?></title>
00403         <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?>"/>
00404         <?php if ( $item->getDate() ) { ?>
00405         <updated><?php print $this->formatTime( $item->getDate() ); ?>Z</updated>
00406         <?php } ?>
00407 
00408         <summary type="html"><?php print $item->getDescription() ?></summary>
00409         <?php if ( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor(); ?></name></author><?php }?>
00410     </entry>
00411 
00412 <?php /* @todo FIXME: Need to add comments
00413     <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
00414       */
00415     }
00416 
00420     function outFooter() {?>
00421     </feed><?php
00422         // @codingStandardsIgnoreEnd
00423     }
00424 }