(test with linuxfr, problem with UTF8 encoding) |
imported>mutante |
||
(15 intermediate revisions by 11 users not shown) | |||
Line 1: | Line 1: | ||
Integrate [[RssNewsfeeds|RSS newsfeeds]] into wiki pages using [[magpie]] RSS parser in a custom [[Mediawiki]] extension: |
Integrate [[RssNewsfeeds|RSS newsfeeds]] into wiki pages using [[magpie]] RSS parser in a custom [[Mediawiki]] extension: |
||
(For example to |
(For example to interwiki syndicate RecentChanges pages like on the [[RecentNearChanges|recent near changes]].) |
||
[[User:mutante|mutante]] 21:35, 25 Mar 2005 (CET) |
[[User:mutante|mutante]] 21:35, 25 Mar 2005 (CET) |
||
Line 9: | Line 9: | ||
==== Example ==== |
==== Example ==== |
||
<nowiki><rss>http://rss.slashdot.org/Slashdot/slashdot|max=5</rss></nowiki> |
|||
<nowiki><rss>http://slashdot.org/slashdot.rss</rss></nowiki> |
|||
=== Result === |
=== Result === |
||
<rss>http://slashdot.org/slashdot |
<rss>http://rss.slashdot.org/Slashdot/slashdot|max=5</rss> |
||
===del.icio.us UTF-8 test === |
|||
<rss>http://del.icio.us/rss/krcla/%EC%84%A0%EA%B1%B0</rss> |
|||
=== FFII News feed test === |
=== FFII News feed test === |
||
<rss>http://linuxfr.org/backend/news/rss20.rss</rss> |
<rss>http://linuxfr.org/backend/news/rss20.rss</rss> |
||
=== Linux ml === |
|||
<rss>http://rss.gmane.org/messages/excerpts/gmane.linux.kernel</rss> |
|||
=== Source === |
=== Source === |
||
Line 96: | Line 104: | ||
<nowiki> |
<nowiki> |
||
<?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 ) { |
|||
#change this according to your magpie installation! |
|||
global $wgOutputEncoding; |
|||
require_once('magpierss-0.71.1/rss_fetch.inc'); |
|||
# $input = mysql_escape_string($input); |
|||
#install extension hook |
|||
$wgExtensionFunctions[] = "wfRssExtension"; |
|||
if (!$input) return ""; #if <rss>-section is empty, return nothing |
|||
#extension hook callback function |
|||
#parse fields in rss-section |
|||
function wfRssExtension() { |
|||
$fields= explode("|",$input); |
|||
global $wgParser; |
|||
$url= @$fields[0]; |
|||
#install parser hook for <rss> tags |
|||
$args= array(); |
|||
$wgParser->setHook( "rss", "renderRss" ); |
|||
for ($i=1; $i<sizeof($fields); $i++) { |
|||
} |
|||
$f= $fields[$i]; |
|||
#parser hook callback function |
|||
if (strpos($f,"=")===False) $args[strtolower(trim($f))]= True; |
|||
function renderRss( $input ) { |
|||
else { |
|||
global $wgOutputEncoding; |
|||
list($k,$v)= explode("=",$f,2); |
|||
$args[strtolower(trim($k))]= trim($v); |
|||
# $input = mysql_escape_string($input); |
|||
} |
|||
} |
|||
if (!$input) return ""; #if <rss>-section is empty, return nothing |
|||
#get charset from argument-array |
|||
#parse fields in rss-section |
|||
$charset= @$args["charset"]; |
|||
if (!$charset) $charset= $wgOutputEncoding; |
|||
$url= @$fields[0]; |
|||
#get short-flag from argument-array |
|||
#if short is set, no description text is printed |
|||
for ($i=1; $i<sizeof($fields); $i++) { |
|||
$short= @$args["short"]; |
|||
if (strpos($f,"=")===False) $args[strtolower(trim($f))]= True; |
|||
#fetch rss. may be cached locally. |
|||
#Refer to the documentation of magpie for details. |
|||
list($k,$v)= explode("=",$f,2); |
|||
$rss = @fetch_rss($url); |
|||
$args[strtolower(trim($k))]= trim($v); |
|||
#check for errors. |
|||
if ($rss->ERROR) { |
|||
#get charset from argument-array |
|||
return "<div>Failed to load RSS feed from $url: ".$rss->ERROR."</div>"; #localize... |
|||
$charset= @$args["charset"]; |
|||
} |
|||
if (!$charset) $charset= $wgOutputEncoding; |
|||
if (!is_array($rss->items)) { |
|||
#get short-flag from argument-array |
|||
return "<div>Failed to load RSS feed from $url!</div>"; #localize... |
|||
#if short is set, no description text is printed |
|||
} |
|||
$short= @$args["short"]; |
|||
#Bild title line |
|||
$title= iconv($charset,$wgOutputEncoding,$rss->channel['title']); |
|||
#fetch rss. may be cached locally. |
|||
if ($rss->channel['link']) $title= "<a href='".$rss->channel['link']."'>$title</a>"; |
|||
#Refer to the documentation of magpie for details. |
|||
$rss = @fetch_rss($url); |
|||
$output="<h3>$title</h3>"; |
|||
#Bild items |
|||
if ($short) { #short item list |
|||
$output.="<ul>"; |
|||
return "<div>Failed to load RSS feed from $url: ".$rss->ERROR."</div>"; #localize... |
|||
foreach ($rss->items as $item) { |
|||
} |
|||
$href = trim(iconv($charset,$wgOutputEncoding,$item['link'])); |
|||
$title = trim(iconv($charset,$wgOutputEncoding,$item['title'])); |
|||
if (!is_array($rss->items)) { |
|||
return "<div>Failed to load RSS feed from $url!</div>"; #localize... |
|||
$output.="<li><a href='$href'>$title</a></li>"; |
|||
} |
|||
} |
|||
$output.="</ul>"; |
|||
#Bild title line |
|||
} |
|||
$title= iconv($charset,$wgOutputEncoding,$rss->channel['title']); |
|||
else { #full item list |
|||
if ($rss->channel['link']) $title= "<a href='".$rss->channel['link']."'>$title</a>"; |
|||
$output.="<dl>"; |
|||
foreach ($rss->items as $item) { |
|||
$output="<h3>$title</h3>"; |
|||
$href = trim(iconv($charset,$wgOutputEncoding,$item['link'])); |
|||
$title = trim(iconv($charset,$wgOutputEncoding,$item['title'])); |
|||
#Bild items |
|||
#bild description text if desired |
|||
$output.="<ul>"; |
|||
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 = ""; |
|||
else { #full item list |
|||
$output.="<dt><a href='$href'>$title</a></dt>"; |
|||
foreach ($rss->items as $item) { |
|||
$ |
if ($text) $output.="<dd>$text</dd>\n"; |
||
} |
|||
$title = trim(iconv($charset,$wgOutputEncoding,$item['title'])); |
|||
$output.="</dl>"; |
|||
} |
|||
#bild description text if desired |
|||
if ($item["description"]) { |
|||
return $output; |
|||
$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; |
|||
} |
|||
?> |
|||
</nowiki> |
</nowiki> |
||
from: http://meta.wikimedia.org/wiki/User:Duesentrieb/RSS |
from: http://meta.wikimedia.org/wiki/User:Duesentrieb/RSS |
||
also this version has been extended. See http://meta.wikimedia.org/wiki/User:Alxndr/RSS ! |
|||
[[Category:Mediawiki Extensions]] |
[[Category:Mediawiki Extensions]] |
Latest revision as of 19:02, 12 March 2007
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)
Syntax[edit]
<rss>URL</rss>
Example[edit]
<rss>http://rss.slashdot.org/Slashdot/slashdot|max=5</rss>
Result[edit]
<rss>http://rss.slashdot.org/Slashdot/slashdot%7Cmax=5</rss>
del.icio.us UTF-8 test[edit]
<rss>http://del.icio.us/rss/krcla/%EC%84%A0%EA%B1%B0</rss>
FFII News feed test[edit]
<rss>http://linuxfr.org/backend/news/rss20.rss</rss>
Linux ml[edit]
<rss>http://rss.gmane.org/messages/excerpts/gmane.linux.kernel</rss>
Source[edit]
<?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; } ?>
Extended version by Duesentrieb[edit]
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 !