New RSS feed should be easier to integrate with any QueryPage. Sample for Newpages.
authorBrion Vibber <brion@users.mediawiki.org>
Fri, 5 Mar 2004 10:16:46 +0000 (10:16 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Fri, 5 Mar 2004 10:16:46 +0000 (10:16 +0000)
includes/Feed.php [new file with mode: 0644]
includes/QueryPage.php
includes/SpecialNewpages.php

diff --git a/includes/Feed.php b/includes/Feed.php
new file mode 100644 (file)
index 0000000..e5c7c18
--- /dev/null
@@ -0,0 +1,86 @@
+<?php
+
+$wgFeedClasses = array(
+       "rss" => "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 "<feed>";
+       }
+       function outItem( $item ) {
+               # print "<item>...</item>";
+       }
+       function outFooter() {
+               # print "</feed>";
+       }
+}
+
+class RSSFeed extends ChannelFeed {
+       function outHeader() {
+               print '<' . '?xml version="1.0" encoding="utf-8"?' . ">\n";
+               ?><!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd">
+<rss version="0.91">
+       <channel>
+               <title><?php print $this->getTitle() ?></title>
+               <link><?php print $this->getUrl() ?></link>
+               <description><?php print $this->getDescription() ?></description>
+               <language><?php print $this->getLanguage() ?></language>
+<?php
+       }
+       
+       function outItem( $item ) {
+       ?>
+               <item>
+                       <title><?php print $item->getTitle() ?></title>
+                       <link><?php print $item->getUrl() ?></link>
+                       <description><?php print $item->getDescription() ?></description>
+               </item>
+<?php
+       }
+
+       function outFooter() {
+       ?>
+       </channel>
+</rss><?php
+       }
+}
+
+?>
\ No newline at end of file
index ea87c26..d19390f 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 
 include_once ( "LogPage.php" ) ;
+include_once ( "Feed.php" );
 
 # This is a class for doing query pages; since they're almost all the same,
 # we factor out some of the functionality into a superclass, and let
@@ -36,7 +37,7 @@ class QueryPage {
        function formatResult( $skin, $result ) {
                return "";
        }
-
+       
        # This is the actual workhorse. It does everything needed to make a
        # real, honest-to-gosh query page.
 
@@ -87,6 +88,82 @@ class QueryPage {
                        $logpage->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
index baf504d..ecd0e62 100644 (file)
@@ -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 );
+       }
 }
 
 ?>