MediawikiExtensions/RSS
Integrate RSS newsfeeds into wiki pages using magpie RSS parser in a custom Mediawiki extension:
(For example to interwiki syndicate RecentChanges pages like on the recent near changes.)
Mutante 21:35, 25 Mar 2005 (CET)
Contents |
[edit] Syntax
<rss>URL</rss>
[edit] Example
<rss>http://rss.slashdot.org/Slashdot/slashdot|max=5</rss>
[edit] Result
<rss>http://rss.slashdot.org/Slashdot/slashdot%7Cmax=5</rss>
[edit] del.icio.us UTF-8 test
<rss>http://del.icio.us/rss/krcla/%EC%84%A0%EA%B1%B0</rss>
[edit] FFII News feed test
<rss>http://linuxfr.org/backend/news/rss20.rss</rss>
[edit] Linux ml
<rss>http://rss.gmane.org/messages/excerpts/gmane.linux.kernel</rss>
[edit] Source
<?php
# RSS-Feed Mediawiki extension
# using magpieRSS (http://magpierss.sourceforge.net/)
# by mutante 25.03.2005
# requiring magpie (see above)
require_once('magpierss-0.71.1/rss_fetch.inc');
# give it a name
$wgExtensionFunctions[] = "wfRssExtension";
# register with global parser (http://meta.wikimedia.org/wiki/Write_your_own_MediaWiki_extension)
function wfRssExtension() {
global $wgParser;
# set hook (trigger) to rss, means <rss> will be made active tag
$wgParser->setHook( "rss", "renderRss" );
}
# the actual function (taking input)
function renderRss( $input ) {
# maybe its a good idea to escape string user input so they dont try to attach nasty things
# $input = mysql_escape_string($input);
# fetch the feed (magpie's job)
$rss = fetch_rss($input);
# setting variables for table head
$link=$rss->channel['link'];
$title=$rss->channel['title'];
$cdesc=$rss->channel['description'];
# putting the html table head into the output variable
$output="<table><tr><th align='left' colspan='3'><i>RSS-feed included from:</th></tr>
<th colspan='2'><a href='$link'>$title</a></th>
<th><i>'$cdesc'</i></th></tr><tr><th>Date</th>
<th>Page</th><th>Description</th></tr>";
# now a loop to add table rows until none more are found
foreach ($rss->items as $item) {
# setting variables for table row
$href = $item['link'];
$title = $item['title'];
$date = $rss->dc['date'];
$description = $item['description'];
# adding each single row (still in loop) (.= appends = would overwrite)
$output.="<tr><td>date $date</td><td colspan='1'><a href='$href'>$title</a></td><td>$description</td></tr>";
}
# loop done ,adding final tag to close table properly
$output.="</table>";
# dump the output all at once
return $output;
}
?>
[edit] Extended version by Duesentrieb
This is an extended version of the RSS-feed extension by Mutante (http://meta.wikimedia.org/wiki/User:Mutante/RSSFeed). It's main features are charset conversion, nicer formating and output of the full description text of the news items. It also introduces a syntax for controlling those features.
<?php
# RSS-Feed Mediawiki extension
#
# original by mutante 25.03.2005
# extended by Duesentrieb 30.04.2005
#
# Requires:
# * magpie rss parser <http://magpierss.sourceforge.net/>
# * iconv <http://www.gnu.org/software/libiconv/>, see also <http://www.php.net/iconv>
#
# Installation:
# * put this file (rss.php) into the extension directory of your mediawiki installation
# * add the following to the end of LocalSettings.php: include("extensions/rss.php");
# * make sure magpie can be found by PHP.
#
# Usage:
# Use one section between <rss>-tags for each feed. The ress section may contain parameters
# separated by a pipe ("|"), just like links and templates. Two parameters are supported:
# * charset=... the charset used by the feed. iconv is used to convert this.
# * short do not show the description text for each news item.
#
# Example:
# <rss>http://slashdot.org/slashdot.rss|charset=UTF-8|short</rss>
#
#change this according to your magpie installation!
require_once('magpierss-0.71.1/rss_fetch.inc');
#install extension hook
$wgExtensionFunctions[] = "wfRssExtension";
#extension hook callback function
function wfRssExtension() {
global $wgParser;
#install parser hook for <rss> tags
$wgParser->setHook( "rss", "renderRss" );
}
#parser hook callback function
function renderRss( $input ) {
global $wgOutputEncoding;
# $input = mysql_escape_string($input);
if (!$input) return ""; #if <rss>-section is empty, return nothing
#parse fields in rss-section
$fields= explode("|",$input);
$url= @$fields[0];
$args= array();
for ($i=1; $i<sizeof($fields); $i++) {
$f= $fields[$i];
if (strpos($f,"=")===False) $args[strtolower(trim($f))]= True;
else {
list($k,$v)= explode("=",$f,2);
$args[strtolower(trim($k))]= trim($v);
}
}
#get charset from argument-array
$charset= @$args["charset"];
if (!$charset) $charset= $wgOutputEncoding;
#get short-flag from argument-array
#if short is set, no description text is printed
$short= @$args["short"];
#fetch rss. may be cached locally.
#Refer to the documentation of magpie for details.
$rss = @fetch_rss($url);
#check for errors.
if ($rss->ERROR) {
return "<div>Failed to load RSS feed from $url: ".$rss->ERROR."</div>"; #localize...
}
if (!is_array($rss->items)) {
return "<div>Failed to load RSS feed from $url!</div>"; #localize...
}
#Bild title line
$title= iconv($charset,$wgOutputEncoding,$rss->channel['title']);
if ($rss->channel['link']) $title= "<a href='".$rss->channel['link']."'>$title</a>";
$output="<h3>$title</h3>";
#Bild items
if ($short) { #short item list
$output.="<ul>";
foreach ($rss->items as $item) {
$href = trim(iconv($charset,$wgOutputEncoding,$item['link']));
$title = trim(iconv($charset,$wgOutputEncoding,$item['title']));
$output.="<li><a href='$href'>$title</a></li>";
}
$output.="</ul>";
}
else { #full item list
$output.="<dl>";
foreach ($rss->items as $item) {
$href = trim(iconv($charset,$wgOutputEncoding,$item['link']));
$title = trim(iconv($charset,$wgOutputEncoding,$item['title']));
#bild description text if desired
if ($item["description"]) {
$text= trim(iconv($charset,$wgOutputEncoding,$item['description']));
#avoid pre-tags
$text= str_replace("\r"," ",$text);
$text= str_replace("\n"," ",$text);
$text= str_replace("\t"," ",$text);
}
else $text = "";
$output.="<dt><a href='$href'>$title</a></dt>";
if ($text) $output.="<dd>$text</dd>\n";
}
$output.="</dl>";
}
return $output;
}
?>
from: http://meta.wikimedia.org/wiki/User:Duesentrieb/RSS
also this version has been extended. See http://meta.wikimedia.org/wiki/User:Alxndr/RSS !