Jump to content

User:Rdb78/RSS

From Meta, a Wikimedia project coordination wiki
 
A proposal to move this page to MediaWiki.org was rejected.

This is a slightly modified version of the RSS engine from User:Duesentrieb, which in turn is an extended version of the RSS-feed extension by Mutante

It now supports limiting the number of headlines displayed by the feed to a number passed as an argument, of the form max=x where x is the number of headlines you want.

Further features added : highlighting and filtering, see RSS - Mafs 20:42, 10 July 2005 (UTC)

<?php
 # RSS-Feed Mediawiki extension
 # 
 # original by mutante 25.03.2005
 # extended by Duesentrieb 30.04.2005
 # extended by Rdb78 07.07.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.
 #    * max=x      Shows x most recent headlines.
 #
 # Example: 
 #    <rss>http://slashdot.org/slashdot.rss|charset=UTF-8|short|max=5</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 max number of headlines from argument-array
    $maxheads = @$args["max"];
    $headcnt = 0;
    
    #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>";	

		#Cut off output when maxheads is reached:
		if (++$headcnt == $maxheads)
			break;
        }
        $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";

		#Cut off output when maxheads is reached:
		if (++$headcnt == $maxheads)
			break;
        }
        $output.="</dl>";
    }
    
    return $output;
    
 }
 
 ?>