r2615 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r2614‎ | r2615 | r2616 >
Date:10:16, 5 March 2004
Author:vibber
Status:old
Tags:
Comment:
New RSS feed should be easier to integrate with any QueryPage. Sample for Newpages.
Modified paths:
  • /trunk/phase3/includes/Feed.php (added) (history)
  • /trunk/phase3/includes/QueryPage.php (modified) (history)
  • /trunk/phase3/includes/SpecialNewpages.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Feed.php
@@ -0,0 +1,86 @@
 2+<?php
 3+
 4+$wgFeedClasses = array(
 5+ "rss" => "RSSFeed",
 6+ # "atom" => "AtomFeed",
 7+ );
 8+
 9+class FeedItem {
 10+ var $Title = "Wiki";
 11+ var $Description = "";
 12+ var $Url = "";
 13+
 14+ function FeedItem( $Title, $Description, $Url ) {
 15+ $this->Title = $Title;
 16+ $this->Description = $Description;
 17+ $this->Url = $Url;
 18+ }
 19+
 20+ /* Static... */
 21+ function xmlEncode( $string ) {
 22+ global $wgInputEncoding, $wgLang;
 23+ $string = str_replace( "\r\n", "\n", $string );
 24+ if( strcasecmp( $wgInputEncoding, "utf-8" ) != 0 ) {
 25+ $string = $wgLang->iconv( $wgInputEncoding, "utf-8" );
 26+ }
 27+ return htmlspecialchars( $string );
 28+ }
 29+ function getTitle() {
 30+ return $this->xmlEncode( $this->Title );
 31+ }
 32+ function getUrl() {
 33+ return $this->xmlEncode( $this->Url );
 34+ }
 35+ function getDescription() {
 36+ return $this->xmlEncode( $this->Description );
 37+ }
 38+ function getLanguage() {
 39+ global $wgLanguageCode;
 40+ return $wgLanguageCode;
 41+ }
 42+}
 43+
 44+class ChannelFeed extends FeedItem {
 45+ /* Abstract functions, override! */
 46+ function outHeader() {
 47+ # print "<feed>";
 48+ }
 49+ function outItem( $item ) {
 50+ # print "<item>...</item>";
 51+ }
 52+ function outFooter() {
 53+ # print "</feed>";
 54+ }
 55+}
 56+
 57+class RSSFeed extends ChannelFeed {
 58+ function outHeader() {
 59+ print '<' . '?xml version="1.0" encoding="utf-8"?' . ">\n";
 60+ ?><!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd">
 61+<rss version="0.91">
 62+ <channel>
 63+ <title><?php print $this->getTitle() ?></title>
 64+ <link><?php print $this->getUrl() ?></link>
 65+ <description><?php print $this->getDescription() ?></description>
 66+ <language><?php print $this->getLanguage() ?></language>
 67+<?php
 68+ }
 69+
 70+ function outItem( $item ) {
 71+ ?>
 72+ <item>
 73+ <title><?php print $item->getTitle() ?></title>
 74+ <link><?php print $item->getUrl() ?></link>
 75+ <description><?php print $item->getDescription() ?></description>
 76+ </item>
 77+<?php
 78+ }
 79+
 80+ function outFooter() {
 81+ ?>
 82+ </channel>
 83+</rss><?php
 84+ }
 85+}
 86+
 87+?>
\ No newline at end of file
Property changes on: trunk/phase3/includes/Feed.php
___________________________________________________________________
Added: svn:eol-style
188 + native
Added: svn:keywords
289 + Author Date Id Revision
Index: trunk/phase3/includes/QueryPage.php
@@ -1,6 +1,7 @@
22 <?php
33
44 include_once ( "LogPage.php" ) ;
 5+include_once ( "Feed.php" );
56
67 # This is a class for doing query pages; since they're almost all the same,
78 # we factor out some of the functionality into a superclass, and let
@@ -36,7 +37,7 @@
3738 function formatResult( $skin, $result ) {
3839 return "";
3940 }
40 -
 41+
4142 # This is the actual workhorse. It does everything needed to make a
4243 # real, honest-to-gosh query page.
4344
@@ -87,6 +88,82 @@
8889 $logpage->replaceContent( $s );
8990 }
9091 }
 92+
 93+ # Similar to above, but packaging in a syndicated feed instead of a web page
 94+ function doFeed( $class = "" ) {
 95+ global $wgFeedClasses;
 96+ global $wgOut, $wgLanguageCode, $wgLang;
 97+ if( $class == "rss" ) {
 98+ $wgOut->disable();
 99+
 100+ $feed = new RSSFeed(
 101+ $this->feedTitle(),
 102+ $this->feedDesc(),
 103+ $this->feedUrl() );
 104+ $feed->outHeader();
 105+
 106+ $sql = $this->getSQL( 0, 50 );
 107+ $res = wfQuery( $sql, DB_READ, "QueryPage::doFeed" );
 108+ while( $obj = wfFetchObject( $res ) ) {
 109+ $item = $this->feedResult( $obj );
 110+ if( $item ) $feed->outItem( $item );
 111+ }
 112+ wfFreeResult( $res );
 113+
 114+ $feed->outFooter();
 115+ return true;
 116+ } else {
 117+ return false;
 118+ }
 119+ }
 120+
 121+ # Override for custom handling. If the titles/links are ok, just do feedItemDesc()
 122+ function feedResult( $result ) {
 123+ if( isset( $result->cur_title ) ) {
 124+ $title = Title::MakeTitle( $result->cur_namespace, $result->cur_title );
 125+ } elseif( isset( $result->old_title ) ) {
 126+ $title = Title::MakeTitle( $result->old_namespace, $result->old_title );
 127+ } elseif( isset( $result->rc_title ) ) {
 128+ $title = Title::MakeTitle( $result->rc_namespace, $result->rc_title );
 129+ } else {
 130+ return NULL;
 131+ }
 132+ if( $title ) {
 133+ return new FeedItem(
 134+ $title->getText(),
 135+ $this->feedItemDesc( $result ),
 136+ wfFullUrl( $title->getUrl() ) );
 137+ } else {
 138+ return NULL;
 139+ }
 140+ }
 141+
 142+ function feedItemDesc( $row ) {
 143+ if( isset( $row->cur_comment ) ) {
 144+ return $row->cur_comment;
 145+ } elseif( isset( $row->old_comment ) ) {
 146+ return $row->old_comment;
 147+ } elseif( isset( $row->rc_comment ) ) {
 148+ return $row->rc_comment;
 149+ }
 150+ return "";
 151+ }
 152+
 153+ function feedTitle() {
 154+ global $wgLanguageCode, $wgSitename, $wgLang;
 155+ $pages = $wgLang->getValidSpecialPages();
 156+ $title = $pages[$this->getName()];
 157+ return "$title - $wgSitename [$wgLanguageCode]";
 158+ }
 159+
 160+ function feedDesc() {
 161+ return wfMsg( "fromwikipedia" );
 162+ }
 163+
 164+ function feedUrl() {
 165+ global $wgLang;
 166+ return wfFullUrl( $wgLang->SpecialPage( $this->getName() ) );
 167+ }
91168 }
92169
93170 # This is a subclass for very simple queries that are just looking for page
Index: trunk/phase3/includes/SpecialNewpages.php
@@ -7,7 +7,7 @@
88 function getName() {
99 return "Newpages";
1010 }
11 -
 11+
1212 function isExpensive() {
1313 return parent::isExpensive();
1414 }
@@ -54,7 +54,10 @@
5555
5656 $npp = new NewPagesPage();
5757
58 - $npp->doQuery( $offset, $limit );
 58+
 59+ if( !$npp->doFeed( $_GET["feed"] ) ) {
 60+ $npp->doQuery( $offset, $limit );
 61+ }
5962 }
6063
6164 ?>

Status & tagging log