From 361c83706363c10685dd4baccd24164886094994 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Fri, 5 Mar 2004 10:16:46 +0000 Subject: [PATCH] New RSS feed should be easier to integrate with any QueryPage. Sample for Newpages. --- includes/Feed.php | 86 ++++++++++++++++++++++++++++++++++++ includes/QueryPage.php | 79 ++++++++++++++++++++++++++++++++- includes/SpecialNewpages.php | 7 ++- 3 files changed, 169 insertions(+), 3 deletions(-) create mode 100644 includes/Feed.php diff --git a/includes/Feed.php b/includes/Feed.php new file mode 100644 index 0000000000..e5c7c18bc1 --- /dev/null +++ b/includes/Feed.php @@ -0,0 +1,86 @@ + "RSSFeed", + # "atom" => "AtomFeed", + ); + +class FeedItem { + var $Title = "Wiki"; + var $Description = ""; + var $Url = ""; + + function FeedItem( $Title, $Description, $Url ) { + $this->Title = $Title; + $this->Description = $Description; + $this->Url = $Url; + } + + /* Static... */ + function xmlEncode( $string ) { + global $wgInputEncoding, $wgLang; + $string = str_replace( "\r\n", "\n", $string ); + if( strcasecmp( $wgInputEncoding, "utf-8" ) != 0 ) { + $string = $wgLang->iconv( $wgInputEncoding, "utf-8" ); + } + return htmlspecialchars( $string ); + } + function getTitle() { + return $this->xmlEncode( $this->Title ); + } + function getUrl() { + return $this->xmlEncode( $this->Url ); + } + function getDescription() { + return $this->xmlEncode( $this->Description ); + } + function getLanguage() { + global $wgLanguageCode; + return $wgLanguageCode; + } +} + +class ChannelFeed extends FeedItem { + /* Abstract functions, override! */ + function outHeader() { + # print ""; + } + function outItem( $item ) { + # print "..."; + } + function outFooter() { + # print ""; + } +} + +class RSSFeed extends ChannelFeed { + function outHeader() { + print '<' . '?xml version="1.0" encoding="utf-8"?' . ">\n"; + ?> + + + <?php print $this->getTitle() ?> + getUrl() ?> + getDescription() ?> + getLanguage() ?> + + + <?php print $item->getTitle() ?> + getUrl() ?> + getDescription() ?> + + + + \ No newline at end of file diff --git a/includes/QueryPage.php b/includes/QueryPage.php index ea87c26b3d..d19390f6bc 100644 --- a/includes/QueryPage.php +++ b/includes/QueryPage.php @@ -1,6 +1,7 @@ replaceContent( $s ); } } + + # Similar to above, but packaging in a syndicated feed instead of a web page + function doFeed( $class = "" ) { + global $wgFeedClasses; + global $wgOut, $wgLanguageCode, $wgLang; + if( $class == "rss" ) { + $wgOut->disable(); + + $feed = new RSSFeed( + $this->feedTitle(), + $this->feedDesc(), + $this->feedUrl() ); + $feed->outHeader(); + + $sql = $this->getSQL( 0, 50 ); + $res = wfQuery( $sql, DB_READ, "QueryPage::doFeed" ); + while( $obj = wfFetchObject( $res ) ) { + $item = $this->feedResult( $obj ); + if( $item ) $feed->outItem( $item ); + } + wfFreeResult( $res ); + + $feed->outFooter(); + return true; + } else { + return false; + } + } + + # Override for custom handling. If the titles/links are ok, just do feedItemDesc() + function feedResult( $result ) { + if( isset( $result->cur_title ) ) { + $title = Title::MakeTitle( $result->cur_namespace, $result->cur_title ); + } elseif( isset( $result->old_title ) ) { + $title = Title::MakeTitle( $result->old_namespace, $result->old_title ); + } elseif( isset( $result->rc_title ) ) { + $title = Title::MakeTitle( $result->rc_namespace, $result->rc_title ); + } else { + return NULL; + } + if( $title ) { + return new FeedItem( + $title->getText(), + $this->feedItemDesc( $result ), + wfFullUrl( $title->getUrl() ) ); + } else { + return NULL; + } + } + + function feedItemDesc( $row ) { + if( isset( $row->cur_comment ) ) { + return $row->cur_comment; + } elseif( isset( $row->old_comment ) ) { + return $row->old_comment; + } elseif( isset( $row->rc_comment ) ) { + return $row->rc_comment; + } + return ""; + } + + function feedTitle() { + global $wgLanguageCode, $wgSitename, $wgLang; + $pages = $wgLang->getValidSpecialPages(); + $title = $pages[$this->getName()]; + return "$title - $wgSitename [$wgLanguageCode]"; + } + + function feedDesc() { + return wfMsg( "fromwikipedia" ); + } + + function feedUrl() { + global $wgLang; + return wfFullUrl( $wgLang->SpecialPage( $this->getName() ) ); + } } # This is a subclass for very simple queries that are just looking for page diff --git a/includes/SpecialNewpages.php b/includes/SpecialNewpages.php index baf504d998..ecd0e62eda 100644 --- a/includes/SpecialNewpages.php +++ b/includes/SpecialNewpages.php @@ -7,7 +7,7 @@ class NewPagesPage extends QueryPage { function getName() { return "Newpages"; } - + function isExpensive() { return parent::isExpensive(); } @@ -54,7 +54,10 @@ function wfSpecialNewpages() $npp = new NewPagesPage(); - $npp->doQuery( $offset, $limit ); + + if( !$npp->doFeed( $_GET["feed"] ) ) { + $npp->doQuery( $offset, $limit ); + } } ?> -- 2.20.1