MediaWiki  REL1_20
Feed.php
Go to the documentation of this file.
00001 <?php
00038 class FeedItem {
00042         var $title;
00043 
00044         var $description;
00045         var $url;
00046         var $date;
00047         var $author;
00048         var $uniqueId;
00049         var $comments;
00050         var $rssIsPermalink = false;
00051 
00062         function __construct( $title, $description, $url, $date = '', $author = '', $comments = '' ) {
00063                 $this->title = $title;
00064                 $this->description = $description;
00065                 $this->url = $url;
00066                 $this->uniqueId = $url;
00067                 $this->date = $date;
00068                 $this->author = $author;
00069                 $this->comments = $comments;
00070         }
00071 
00078         public function xmlEncode( $string ) {
00079                 $string = str_replace( "\r\n", "\n", $string );
00080                 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
00081                 return htmlspecialchars( $string );
00082         }
00083 
00089         public function getUniqueId() {
00090                 if ( $this->uniqueId ) {
00091                         return $this->xmlEncode( $this->uniqueId );
00092                 }
00093         }
00094 
00101         public function setUniqueId( $uniqueId, $rssIsPermalink = false ) {
00102                 $this->uniqueId = $uniqueId;
00103                 $this->rssIsPermalink = $rssIsPermalink;
00104         }
00105 
00111         public function getTitle() {
00112                 return $this->xmlEncode( $this->title );
00113         }
00114 
00120         public function getUrl() {
00121                 return $this->xmlEncode( $this->url );
00122         }
00123 
00129         public function getDescription() {
00130                 return $this->xmlEncode( $this->description );
00131         }
00132 
00138         public function getLanguage() {
00139                 global $wgLanguageCode;
00140                 return $wgLanguageCode;
00141         }
00142 
00148         public function getDate() {
00149                 return $this->date;
00150         }
00151 
00157         public function getAuthor() {
00158                 return $this->xmlEncode( $this->author );
00159         }
00160 
00166         public function getComments() {
00167                 return $this->xmlEncode( $this->comments );
00168         }
00169 
00176         public static function stripComment( $text ) {
00177                 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
00178         }
00180 }
00181 
00186 abstract class ChannelFeed extends FeedItem {
00195         abstract public function outHeader();
00196 
00205         abstract public function outItem( $item );
00206 
00214         abstract public function outFooter();
00215 
00224         public function httpHeaders() {
00225                 global $wgOut, $wgVaryOnXFP;
00226 
00227                 # We take over from $wgOut, excepting its cache header info
00228                 $wgOut->disable();
00229                 $mimetype = $this->contentType();
00230                 header( "Content-type: $mimetype; charset=UTF-8" );
00231                 if ( $wgVaryOnXFP ) {
00232                         $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
00233                 }
00234                 $wgOut->sendCacheControl();
00235 
00236         }
00237 
00244         function contentType() {
00245                 global $wgRequest;
00246                 $ctype = $wgRequest->getVal('ctype','application/xml');
00247                 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
00248                 return (in_array($ctype, $allowedctypes) ? $ctype : 'application/xml');
00249         }
00250 
00256         function outXmlHeader() {
00257                 global $wgStylePath, $wgStyleVersion;
00258 
00259                 $this->httpHeaders();
00260                 echo '<?xml version="1.0"?>' . "\n";
00261                 echo '<?xml-stylesheet type="text/css" href="' .
00262                         htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion", PROTO_CURRENT ) ) .
00263                         '"?' . ">\n";
00264         }
00265 }
00266 
00272 class RSSFeed extends ChannelFeed {
00273 
00280         function formatTime( $ts ) {
00281                 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
00282         }
00283 
00287         function outHeader() {
00288                 global $wgVersion;
00289 
00290                 $this->outXmlHeader();
00291                 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
00292         <channel>
00293                 <title><?php print $this->getTitle() ?></title>
00294                 <link><?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?></link>
00295                 <description><?php print $this->getDescription() ?></description>
00296                 <language><?php print $this->getLanguage() ?></language>
00297                 <generator>MediaWiki <?php print $wgVersion ?></generator>
00298                 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
00299 <?php
00300         }
00301 
00306         function outItem( $item ) {
00307         ?>
00308                 <item>
00309                         <title><?php print $item->getTitle() ?></title>
00310                         <link><?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ) ?></link>
00311                         <guid<?php if( !$item->rssIsPermalink ) print ' isPermaLink="false"' ?>><?php print $item->getUniqueId() ?></guid>
00312                         <description><?php print $item->getDescription() ?></description>
00313                         <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
00314                         <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
00315                         <?php if( $item->getComments() ) { ?><comments><?php print wfExpandUrl( $item->getComments(), PROTO_CURRENT ) ?></comments><?php }?>
00316                 </item>
00317 <?php
00318         }
00319 
00323         function outFooter() {
00324         ?>
00325         </channel>
00326 </rss><?php
00327         }
00328 }
00329 
00335 class AtomFeed extends ChannelFeed {
00340         function formatTime( $ts ) {
00341                 // need to use RFC 822 time format at least for rss2.0
00342                 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
00343         }
00344 
00348         function outHeader() {
00349                 global $wgVersion;
00350 
00351                 $this->outXmlHeader();
00352                 ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
00353                 <id><?php print $this->getFeedId() ?></id>
00354                 <title><?php print $this->getTitle() ?></title>
00355                 <link rel="self" type="application/atom+xml" href="<?php print wfExpandUrl( $this->getSelfUrl(), PROTO_CURRENT ) ?>"/>
00356                 <link rel="alternate" type="text/html" href="<?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?>"/>
00357                 <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
00358                 <subtitle><?php print $this->getDescription() ?></subtitle>
00359                 <generator>MediaWiki <?php print $wgVersion ?></generator>
00360 
00361 <?php
00362         }
00363 
00373         function getFeedId() {
00374                 return $this->getSelfUrl();
00375         }
00376 
00382         function getSelfUrl() {
00383                 global $wgRequest;
00384                 return htmlspecialchars( $wgRequest->getFullRequestURL() );
00385         }
00386 
00391         function outItem( $item ) {
00392                 global $wgMimeType;
00393         ?>
00394         <entry>
00395                 <id><?php print $item->getUniqueId() ?></id>
00396                 <title><?php print $item->getTitle() ?></title>
00397                 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ) ?>"/>
00398                 <?php if( $item->getDate() ) { ?>
00399                 <updated><?php print $this->formatTime( $item->getDate() ) ?>Z</updated>
00400                 <?php } ?>
00401 
00402                 <summary type="html"><?php print $item->getDescription() ?></summary>
00403                 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name></author><?php }?>
00404         </entry>
00405 
00406 <?php /* @todo FIXME: Need to add comments
00407         <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
00408           */
00409         }
00410 
00414         function outFooter() {?>
00415         </feed><?php
00416         }
00417 }