Extension:RSS

From MediaWiki.org

Jump to: navigation, search

           

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
RSS

Release status: stable

Implementation  Tag
Description Displays an RSS feed on a wiki page
Author(s)  mutante, Daniel Kinzler, Rdb, Mafs, Alxndr, K001
Last Version  1.6 (26th of January, 2010)
License No license specified
Download Download
Change Log
Example  <rss>http://rss.slashdot.org/Slashdot/slashdot|charset=UTF-8|short|date|max=5</rss>

check usage (experimental)

Displays an RSS feed on a wiki page

Contents

[edit] Installation

  1. Save the source in your /extensions directory in a file named rss.php
  2. Download and save the Magpie RSS parser into the same directory. Magpie can be downloaded from SourceForge. Don't install Magpie and ignore Magpie's installation instructions - just drop the parser into your /extensions directory.
    Optional - Check that iconv is installed; this can be done with a simple phpinfo(); script.[1]
  3. Place the following text in your LocalSettings.php file: require_once("$IP/extensions/rss.php");
    (Make sure there's a semicolon (;) at the end of that line)
  4. Make sure Magpie can be found by PHP.
  5. Finally, load your wiki, and have fun with RSS feeds!

[edit] Usage

Use one section between <rss>-tags for each feed. The rss section may contain parameters separated by a pipe ("|"), just like links and templates. These 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
date[=format] Shows date/time stamp for each news item. You can optionally specify date format using strftime() conventions. If not set detaults to d M Y H:i
max=x Shows at most x most recent headlines
highlight=term1 term2 The terms separated by a space are highlighted
filter=term1 term2 Show only RSS items containing at least one of the terms
filterout=term1 term2 Do not show any RSS items containing any terms
reverse Display the RSS items in reverse order

This extension supports filtering-out (i.e. grep -v) items based on title when in short list mode. It only displays unique titles.

[edit] Example

<rss>http://rss.slashdot.org/Slashdot/slashdot|charset=UTF-8|short|date|max=5</rss>

[edit] Source

<?php
/**
 * RSS-Feed MediaWiki extension
 *
 * @file
 * @ingroup Extensions
 * @version 1.6
 * @author mutante, Duesentrieb, Rdb, Mafs, Alxndr, Cmreigrut, K001
 * @copyright © mutante, Duesentrieb, Rdb, Mafs, Alxndr, Cmreigrut, K001
 * @link http://www.mediawiki.org/wiki/Extension:RSS Documentation
 *
 * Requires: 
 *  # magpie rss parser <http://magpierss.sourceforge.net/>
 *  # iconv <http://www.gnu.org/software/libiconv/>, see also <http://www.php.net/iconv>
 *
 *  07.05.2008 compatible/checked with MediaWiki 1.12
 */
 
if( !defined( 'MEDIAWIKI' ) ) {
	die( "This is not a valid entry point.\n" );
}
 
$wgExtensionCredits['parserhook'][] = array(
	'name' => 'RSS feed',
	'author' => array('mutante', 'Duesentrieb', 'Rdb', 'Mafs', 'Alxndr', 'Wikinaut', 'Cmreigrut', 'K001'),
	'version' => '1.6',
	'url' => 'http://www.mediawiki.org/wiki/Extension:RSS',
	'description' => 'Displays an RSS feed on a wiki page'
);
 
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
 
#change this according to your magpie installation!
require_once('extensions/magpierss/rss_fetch.inc');
 
// Avoid unstubbing $wgParser too early on modern (1.12+) MW versions, as per r35980
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
	$wgHooks['ParserFirstCallInit'][] = 'wfRssExtension';
} else {
	$wgExtensionFunctions[] = 'wfRssExtension';
}
 
#Extension hook callback function
function wfRssExtension() { 
	global $wgParser;
 
	#Install parser hook for <rss> tags
	$wgParser->setHook( 'rss', 'renderRss' );
	return true;
}
 
#Parser hook callback function
function renderRss( $input ) {
	global $wgOutputEncoding, $wgParser;
 
	// Kill parser cache
	$wgParser->disableCache();
 
	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))] = false;
		else {
			list( $k, $v ) = explode( "=", $f, 2 );
			if ( trim( $v ) == false ) $args[strtolower(trim($k))] = false;
			else $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
	if( isset( $args['short'] ) ) $short = true; else $short = false;
	#Get reverse-flag from argument-array
	if( isset( $args['reverse'] ) ) $reverse = true; else $reverse = false;
 
	# Get date format from argument-array
	if (isset($args["date"])) {
		$date =  @$args["date"];
		if ($date == '')
			$date = 'd M Y H:i';
	}
	else
		$date = false;
 
	#Get highlight terms from argument array    
	$rssHighlight = @$args['highlight'];
	$rssHighlight = str_replace( '  ', ' ', $rssHighlight );
	$rssHighlight = explode( ' ', trim( $rssHighlight ) );
 
	#Get filter terms from argument-array    
	$rssFilter = @$args['filter'];
	$rssFilter = str_replace( '  ', ' ', $rssFilter );
	$rssFilter = explode( ' ', trim( $rssFilter ) );
 
	#Filterout terms
	$rssFilterout = @$args['filterout'];
	$rssFilterout = str_replace( '  ', ' ', $rssFilterout );
	$rssFilterout = explode( ' ', trim( $rssFilterout ) );
 
	#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…
	}
 
	#Build title line    
	#$title = iconv($charset, $wgOutputEncoding, $rss->channel['title']);
	#if( $rss->channel['link'] ) $title = "<a href='".$rss->channel['link']."'>$title</a>";

	$output = '';
	if( $reverse ) $rss->items = array_reverse( $rss->items );
	$description = false;
	foreach ( $rss->items as $item ) {
		if ( $item['description'] ) {
			$description = true;
			break;
		}
	}
 
	#Build items
	if ( !$short and $description ) { #full item list
		$output.= '<dl>';
 
		foreach ( $rss->items as $item ) {
			$d_text = true;
			$d_title = true;
 
			$href = htmlspecialchars( trim( iconv( $charset, $wgOutputEncoding, $item['link'] ) ) );
			$title = htmlspecialchars( trim( iconv( $charset, $wgOutputEncoding, $item['title'] ) ) );
 
			if ($date) {
				$pubdate = trim( iconv( $charset, $wgOutputEncoding, $item['pubdate'] ) );
				$pubdate = date( $date, strtotime( $pubdate ) );
			}
 
			$d_title = wfRssFilter( $title, $rssFilter );
			$d_title = wfRssFilterout( $title, $rssFilterout );
			$title = wfRssHighlight( $title, $rssHighlight );
 
			#Build 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 );
				$text = str_replace( '<br>', '', $text );
 
				$d_text = wfRssFilter( $text, $rssFilter );
				$d_text = wfRssFilterout( $text, $rssFilterout );
				$text = wfRssHighlight( $text, $rssHighlight );
				$display = $d_text or $d_title;
 			} else {
				$text = '';
				$display = $d_title;
			}
			if ( $display ) {
				$output.= "<dt><a href='$href'><b>$title</b></a></dt>";
				if ( $date ) $output.= " ($pubdate)";
				if ( $text ) $output.= "<dd>$text <b>[<a href='$href'>?</a>]</b></dd>";
			}
			#Cut off output when maxheads is reached:
			if ( ++$headcnt == $maxheads ) break;
		}
 
		$output.= '</dl>';
	} else { #short item list
		## HACKY HACKY HACKY
		$output.= '<ul>';
		$displayed = array();
		foreach ( $rss->items as $item ) {
			$href = htmlspecialchars( trim( iconv( $charset, $wgOutputEncoding, $item['link'] ) ) );
			$title = htmlspecialchars( trim( iconv( $charset, $wgOutputEncoding, $item['title'] ) ) );
			$d_title = wfRssFilter( $title, $rssFilter ) && wfRssFilterout( $title, $rssFilterout );
			$title = wfRssHighlight( $title, $rssHighlight );
			if ($date) {
				$pubdate = trim( iconv( $charset, $wgOutputEncoding, $item['pubdate'] ) );
				if ( $pubdate == '' ) {
					$pubdate = trim( iconv( $charset, $wgOutputEncoding, $item['dc']['date'] ) );
			}
				$pubdate = date( $date, strtotime( $pubdate ) );
			}
			if ( $d_title && !in_array( $title, $displayed ) ) {
				// Add date to ouput if specified
				$output.= '<li><a href="'.$href.'" title="'.$title.'">'.$title.'</a>';
				if( $date ) {
					$output.= " ($pubdate)";
				}
				$output.= '</li>';
 
				$displayed[] = $title;
				#Cut off output when maxheads is reached:
				if ( ++$headcnt == $maxheads ) break;
			}
		}
		$output.= '</ul>';
	}
 
	return $output;
}
 
function wfRssFilter( $text, $rssFilter ) {
	$display = true;
	if ( is_array( $rssFilter ) ) {
		foreach( $rssFilter as $term ) {
			if ( $term ) {
				$display = false;
				if ( preg_match( "|$term|i", $text, $a ) ) {
					$display = true;
					return $display;
				}
			}
			if ( $display ) break;
		}
	}
	return $display;
}
 
function wfRssFilterout( $text, $rssFilterout ) {
	$display = true;
	if ( is_array( $rssFilterout ) ) {
		foreach ( $rssFilterout as $term ) {
			if ( $term ) {
				if ( preg_match( "|$term|i", $text, $a ) ) {
					$display = false;
					return $display;
				}
			}
		}
	}
	return $display;
}
 
function wfRssHighlight( $text, $rssHighlight ) {
	$i = 0;
	$starttag = 'v8x5u3t3u8h';
	$endtag = 'q8n4f6n4n4x';
 
	$color[] = 'coral';
	$color[] = 'greenyellow';
	$color[] = 'lightskyblue';
	$color[] = 'gold';
	$color[] = 'violet';
	$count_color = count( $color );
 
	if ( is_array( $rssHighlight ) ) {
		foreach( $rssHighlight as $term ) {
			if ( $term ) {
				$text = preg_replace("|\b(\w*?".$term."\w*?)\b|i", "$starttag"."_".$i."\\1$endtag", $text);
				$i++;
				if ( $i == $count_color ) $i = 0;
			}
		}
	}
 
	#To avoid trouble should someone wants to highlight the terms "span", "style", …
	for ( $i = 0; $i < 5; $i++ ) {
		$text = preg_replace( "|$starttag"."_".$i."|", "<span style=\"background-color:".$color[$i]."; font-weight: bold;\">", $text );
		$text = preg_replace( "|$endtag|", '</span>', $text );
	}
 
	return $text;
}
#PHP closing tag intentionally left blank

[edit] Technical history

RSS extension is a modified version of

  • the RSS engine from Mafs, which is a modified version of
  • the RSS engine from Rdb78, which is a modified version of
  • the RSS engine from Duesentrieb, which is a modified version of
  • the RSS-feed extension by Mutante.

[edit] Change Log

  • original by mutante 25.03.2005
  • extended by Duesentrieb 30.04.2005
  • extended by Rdb78 07.07.2005
  • extended by Mafs 10.07.2005, 24.07.2005
  • extended by Niffler 28.02.2006
  • modified by Dzag 07.2006
  • modified by Alxndr 09.2006
  • modified by Svanslyck 02.2008, replacing all « and » with "
  • This has been updated to work better on newer (1.9) MediaWiki software, with the help of User:Duesentrieb. --CryptoQuick 14:26, 24 January 2007 (UTC)
    • This appears not to be true; I have received numerous emails about it not working with 1.9+. I would love to help debug and fix the extension, but my host has not upgraded to PHP 5 and I'm thus stuck at MediaWiki 1.6.8, so that's as far as this is guaranteed to work properly. If anyone develops a fix, please post a link to it here! —Alxndr (t) 02:02, 16 June 2007 (UTC)
      • I just found this fork that purports to have a fix for the new loss of wfStrEncode(). I can't test it though so can anyone else verify that it works? —Alxndr (t) 02:18, 16 June 2007 (UTC)
  • modified by --Wikinaut 11:17, 7 May 2008 (UTC) : changed method to disable chaching; Extension is now compatible to MediaWiki 1.12
  • modified by Cmreigrut 19:05, 19 November 2008 (UTC): added date (if specified) to short output
  • modified by Peter Newman: 03:15, 7 October 2009 (UTC) Added htmlspecialchars escaping to the displayed strings
  • modified by K001 15:15, 26 January 2010 (UTC): version 1.6, added support for date formats

[edit] Notes

  1. Paste:
    <nowiki><?php
    phpinfo ();
    ?></nowiki> Into a text file. See What is PHPinfo? How can I run it?

[edit] See also

Alternate extensions: