This commit is contained in:
Jeroen De Dauw 2010-07-23 14:14:05 +00:00
parent c9b9f3025f
commit b94a7002b7

203
RSS.php
View file

@ -3,24 +3,23 @@
/**
* RSS-Feed MediaWiki extension.
* @link http://www.mediawiki.org/wiki/Extension:RSS Documentation
*
*
* @file RSS.php
* @ingroup Extensions
*
* TODO: stylize
* TODO: replace all @ by wfSurpressWarnings and wfResumeWarnings
*/
if( !defined( 'MEDIAWIKI' ) ) {
if ( !defined( 'MEDIAWIKI' ) ) {
die( "This is not a valid entry point.\n" );
}
define( 'RSS_VERSION', '1.7 alpha' );
$wgExtensionCredits['parserhook'][] = array(
'path' => __FILE__,
'name' => 'RSS feed',
'author' => array(
'author' => array(
'mutante',
'Duesentrieb',
'Rdb',
@ -35,112 +34,112 @@ $wgExtensionCredits['parserhook'][] = array(
'url' => 'http://www.mediawiki.org/wiki/Extension:RSS',
'descriptionmsg' => 'rss-desc',
);
$dir = dirname( __FILE__ );
$wgExtensionMessagesFiles['RSS'] = "$dir/RSS.i18n.php";
define( 'MAGPIE_OUTPUT_ENCODING', 'UTF-8' );
#change this according to your magpie installation!
# change this according to your magpie installation!
require_once( dirname( __FILE__ ) . '/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() {
# Extension hook callback function
function wfRssExtension() {
global $wgParser;
#Install parser hook for <rss> tags
# Install parser hook for <rss> tags
$wgParser->setHook( 'rss', 'renderRss' );
return true;
}
#Parser hook callback function
# 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
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;
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);
if ( trim( $v ) == false ) $args[strtolower( trim( $k ) )] = false;
else $args[strtolower( trim( $k ) )] = trim( $v );
}
}
#Get charset from argument-array
# Get charset from argument-array
$charset = @$args['charset'];
if( !$charset ) $charset = $wgOutputEncoding;
#Get max number of headlines from argument-array
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 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"])) {
if ( isset( $args["date"] ) ) {
$date = @$args["date"];
if ($date == '')
if ( $date == '' )
$date = 'd M Y H:i';
}
else
$date = false;
#Get highlight terms from argument array
# Get highlight terms from argument array
$rssHighlight = @$args['highlight'];
$rssHighlight = str_replace( ' ', ' ', $rssHighlight );
$rssHighlight = explode( ' ', trim( $rssHighlight ) );
#Get filter terms from argument-array
# Get filter terms from argument-array
$rssFilter = @$args['filter'];
$rssFilter = str_replace( ' ', ' ', $rssFilter );
$rssFilter = explode( ' ', trim( $rssFilter ) );
#Filterout terms
# 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.
# Fetch RSS. May be cached locally.
# Refer to the documentation of magpie for details.
$rss = @fetch_rss( $url );
#Check for errors.
# Check for errors.
if ( $rss->ERROR ) {
return "<div>Failed to load RSS feed from $url: ".$rss->ERROR."</div>"; #localize…
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…
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>";
# 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 );
if ( $reverse ) $rss->items = array_reverse( $rss->items );
$description = false;
foreach ( $rss->items as $item ) {
if ( $item['description'] ) {
@ -148,36 +147,36 @@ function renderRss( $input ) {
break;
}
}
#Build items
if ( !$short and $description ) { #full item list
$output.= '<dl>';
# 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) {
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
# Build description text if desired
if ( $item['description'] ) {
$text = trim( iconv( $charset, $wgOutputEncoding, $item['description'] ) );
#Avoid pre-tags
# 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 );
@ -187,25 +186,25 @@ function renderRss( $input ) {
$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>";
$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:
# Cut off output when maxheads is reached:
if ( ++$headcnt == $maxheads ) break;
}
$output.= '</dl>';
} else { #short item list
## HACKY HACKY HACKY
$output.= '<ul>';
$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) {
if ( $date ) {
$pubdate = trim( iconv( $charset, $wgOutputEncoding, $item['pubdate'] ) );
if ( $pubdate == '' ) {
$pubdate = trim( iconv( $charset, $wgOutputEncoding, $item['dc']['date'] ) );
@ -214,27 +213,27 @@ function renderRss( $input ) {
}
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><a href="' . $href . '" title="' . $title . '">' . $title . '</a>';
if ( $date ) {
$output .= " ($pubdate)";
}
$output.= '</li>';
$output .= '</li>';
$displayed[] = $title;
#Cut off output when maxheads is reached:
# Cut off output when maxheads is reached:
if ( ++$headcnt == $maxheads ) break;
}
}
$output.= '</ul>';
$output .= '</ul>';
}
return $output;
}
function wfRssFilter( $text, $rssFilter ) {
$display = true;
if ( is_array( $rssFilter ) ) {
foreach( $rssFilter as $term ) {
foreach ( $rssFilter as $term ) {
if ( $term ) {
$display = false;
if ( preg_match( "|$term|i", $text, $a ) ) {
@ -247,7 +246,7 @@ function wfRssFilter( $text, $rssFilter ) {
}
return $display;
}
function wfRssFilterout( $text, $rssFilterout ) {
$display = true;
if ( is_array( $rssFilterout ) ) {
@ -262,35 +261,35 @@ function wfRssFilterout( $text, $rssFilterout ) {
}
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 ) {
foreach ( $rssHighlight as $term ) {
if ( $term ) {
$text = preg_replace("|\b(\w*?".$term."\w*?)\b|i", "$starttag"."_".$i."\\1$endtag", $text);
$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", …
# 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( "|$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
# PHP closing tag intentionally left blank