2010-07-22 22:07:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2010-07-23 14:01:46 +00:00
|
|
|
* RSS-Feed MediaWiki extension.
|
2010-07-22 22:07:15 +00:00
|
|
|
* @link http://www.mediawiki.org/wiki/Extension:RSS Documentation
|
2010-07-23 14:14:05 +00:00
|
|
|
*
|
2010-07-23 14:01:46 +00:00
|
|
|
* @file RSS.php
|
|
|
|
* @ingroup Extensions
|
2010-07-22 22:07:15 +00:00
|
|
|
*
|
2010-07-23 14:01:46 +00:00
|
|
|
* TODO: replace all @ by wfSurpressWarnings and wfResumeWarnings
|
2010-07-22 22:07:15 +00:00
|
|
|
*/
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
if ( !defined( 'MEDIAWIKI' ) ) {
|
2010-07-22 22:07:15 +00:00
|
|
|
die( "This is not a valid entry point.\n" );
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-23 14:17:46 +00:00
|
|
|
define( 'RSS_VERSION', '1.7' );
|
2010-07-23 14:01:46 +00:00
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
$wgExtensionCredits['parserhook'][] = array(
|
2010-07-23 08:02:27 +00:00
|
|
|
'path' => __FILE__,
|
2010-07-22 22:07:15 +00:00
|
|
|
'name' => 'RSS feed',
|
2010-07-23 14:14:05 +00:00
|
|
|
'author' => array(
|
2010-07-23 14:01:46 +00:00
|
|
|
'mutante',
|
|
|
|
'Duesentrieb',
|
|
|
|
'Rdb',
|
|
|
|
'Mafs',
|
|
|
|
'Alxndr',
|
|
|
|
'Wikinaut',
|
|
|
|
'Cmreigrut',
|
|
|
|
'K001',
|
|
|
|
'[http://www.mediawiki.org/wiki/User:Jeroen_De_Dauw Jeroen De Dauw]'
|
|
|
|
),
|
|
|
|
'version' => RSS_VERSION,
|
2010-07-22 22:07:15 +00:00
|
|
|
'url' => 'http://www.mediawiki.org/wiki/Extension:RSS',
|
2010-07-23 08:02:27 +00:00
|
|
|
'descriptionmsg' => 'rss-desc',
|
2010-07-22 22:07:15 +00:00
|
|
|
);
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-23 08:02:27 +00:00
|
|
|
$dir = dirname( __FILE__ );
|
|
|
|
$wgExtensionMessagesFiles['RSS'] = "$dir/RSS.i18n.php";
|
|
|
|
|
2010-07-23 14:01:46 +00:00
|
|
|
define( 'MAGPIE_OUTPUT_ENCODING', 'UTF-8' );
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# change this according to your magpie installation!
|
2010-07-23 14:01:46 +00:00
|
|
|
require_once( dirname( __FILE__ ) . '/magpierss/rss_fetch.inc' );
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
// 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';
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# Extension hook callback function
|
|
|
|
function wfRssExtension() {
|
2010-07-22 22:07:15 +00:00
|
|
|
global $wgParser;
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# Install parser hook for <rss> tags
|
2010-07-22 22:07:15 +00:00
|
|
|
$wgParser->setHook( 'rss', 'renderRss' );
|
|
|
|
return true;
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# Parser hook callback function
|
2010-07-22 22:07:15 +00:00
|
|
|
function renderRss( $input ) {
|
|
|
|
global $wgOutputEncoding, $wgParser;
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
// Kill parser cache
|
|
|
|
$wgParser->disableCache();
|
|
|
|
|
2010-07-23 14:14:05 +00:00
|
|
|
if ( !$input ) return ''; # if <rss>-section is empty, return nothing
|
|
|
|
|
|
|
|
# Parse fields in rss-section
|
2010-07-23 14:01:46 +00:00
|
|
|
$fields = explode( '|', $input );
|
2010-07-22 22:07:15 +00:00
|
|
|
$url = @$fields[0];
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
$args = array();
|
|
|
|
for ( $i = 1; $i < sizeof( $fields ); $i++ ) {
|
|
|
|
$f = $fields[$i];
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
if ( strpos( $f, '=' ) === false ) $args[strtolower( trim( $f ) )] = false;
|
2010-07-22 22:07:15 +00:00
|
|
|
else {
|
2010-07-23 14:01:46 +00:00
|
|
|
list( $k, $v ) = explode( '=', $f, 2 );
|
2010-07-23 14:14:05 +00:00
|
|
|
if ( trim( $v ) == false ) $args[strtolower( trim( $k ) )] = false;
|
|
|
|
else $args[strtolower( trim( $k ) )] = trim( $v );
|
2010-07-22 22:07:15 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# Get charset from argument-array
|
2010-07-22 22:07:15 +00:00
|
|
|
$charset = @$args['charset'];
|
2010-07-23 14:14:05 +00:00
|
|
|
if ( !$charset ) $charset = $wgOutputEncoding;
|
|
|
|
# Get max number of headlines from argument-array
|
2010-07-22 22:07:15 +00:00
|
|
|
$maxheads = @$args['max'];
|
|
|
|
$headcnt = 0;
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# 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;
|
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
# Get date format from argument-array
|
2010-07-23 14:14:05 +00:00
|
|
|
if ( isset( $args["date"] ) ) {
|
2010-07-22 22:07:15 +00:00
|
|
|
$date = @$args["date"];
|
2010-07-23 14:14:05 +00:00
|
|
|
if ( $date == '' )
|
2010-07-22 22:07:15 +00:00
|
|
|
$date = 'd M Y H:i';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$date = false;
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# Get highlight terms from argument array
|
2010-07-22 22:07:15 +00:00
|
|
|
$rssHighlight = @$args['highlight'];
|
|
|
|
$rssHighlight = str_replace( ' ', ' ', $rssHighlight );
|
|
|
|
$rssHighlight = explode( ' ', trim( $rssHighlight ) );
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# Get filter terms from argument-array
|
2010-07-22 22:07:15 +00:00
|
|
|
$rssFilter = @$args['filter'];
|
|
|
|
$rssFilter = str_replace( ' ', ' ', $rssFilter );
|
|
|
|
$rssFilter = explode( ' ', trim( $rssFilter ) );
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# Filterout terms
|
2010-07-22 22:07:15 +00:00
|
|
|
$rssFilterout = @$args['filterout'];
|
|
|
|
$rssFilterout = str_replace( ' ', ' ', $rssFilterout );
|
|
|
|
$rssFilterout = explode( ' ', trim( $rssFilterout ) );
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# Fetch RSS. May be cached locally.
|
|
|
|
# Refer to the documentation of magpie for details.
|
2010-07-22 22:07:15 +00:00
|
|
|
$rss = @fetch_rss( $url );
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# Check for errors.
|
2010-07-22 22:07:15 +00:00
|
|
|
if ( $rss->ERROR ) {
|
2010-07-23 14:14:05 +00:00
|
|
|
return "<div>Failed to load RSS feed from $url: " . $rss->ERROR . "</div>"; # localize…
|
2010-07-22 22:07:15 +00:00
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
if ( !is_array( $rss->items ) ) {
|
2010-07-23 14:14:05 +00:00
|
|
|
return "<div>Failed to load RSS feed from $url!</div>"; # localize…
|
2010-07-22 22:07:15 +00:00
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# Build title line
|
|
|
|
# $title = iconv($charset, $wgOutputEncoding, $rss->channel['title']);
|
|
|
|
# if( $rss->channel['link'] ) $title = "<a href='".$rss->channel['link']."'>$title</a>";
|
2010-07-22 22:07:15 +00:00
|
|
|
|
|
|
|
$output = '';
|
2010-07-23 14:14:05 +00:00
|
|
|
if ( $reverse ) $rss->items = array_reverse( $rss->items );
|
2010-07-22 22:07:15 +00:00
|
|
|
$description = false;
|
|
|
|
foreach ( $rss->items as $item ) {
|
|
|
|
if ( $item['description'] ) {
|
|
|
|
$description = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# Build items
|
|
|
|
if ( !$short and $description ) { # full item list
|
|
|
|
$output .= '<dl>';
|
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
foreach ( $rss->items as $item ) {
|
|
|
|
$d_text = true;
|
|
|
|
$d_title = true;
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
$href = htmlspecialchars( trim( iconv( $charset, $wgOutputEncoding, $item['link'] ) ) );
|
|
|
|
$title = htmlspecialchars( trim( iconv( $charset, $wgOutputEncoding, $item['title'] ) ) );
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
if ( $date ) {
|
2010-07-22 22:07:15 +00:00
|
|
|
$pubdate = trim( iconv( $charset, $wgOutputEncoding, $item['pubdate'] ) );
|
|
|
|
$pubdate = date( $date, strtotime( $pubdate ) );
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
$d_title = wfRssFilter( $title, $rssFilter );
|
|
|
|
$d_title = wfRssFilterout( $title, $rssFilterout );
|
|
|
|
$title = wfRssHighlight( $title, $rssHighlight );
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# Build description text if desired
|
2010-07-22 22:07:15 +00:00
|
|
|
if ( $item['description'] ) {
|
|
|
|
$text = trim( iconv( $charset, $wgOutputEncoding, $item['description'] ) );
|
2010-07-23 14:14:05 +00:00
|
|
|
# Avoid pre-tags
|
2010-07-22 22:07:15 +00:00
|
|
|
$text = str_replace( "\r", ' ', $text );
|
|
|
|
$text = str_replace( "\n", ' ', $text );
|
|
|
|
$text = str_replace( "\t", ' ', $text );
|
|
|
|
$text = str_replace( '<br>', '', $text );
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
$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 ) {
|
2010-07-23 14:14:05 +00:00
|
|
|
$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>";
|
2010-07-22 22:07:15 +00:00
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
# Cut off output when maxheads is reached:
|
2010-07-22 22:07:15 +00:00
|
|
|
if ( ++$headcnt == $maxheads ) break;
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
$output .= '</dl>';
|
|
|
|
} else { # short item list
|
|
|
|
# # HACKY HACKY HACKY
|
|
|
|
$output .= '<ul>';
|
2010-07-22 22:07:15 +00:00
|
|
|
$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 );
|
2010-07-23 14:14:05 +00:00
|
|
|
if ( $date ) {
|
2010-07-22 22:07:15 +00:00
|
|
|
$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
|
2010-07-23 14:14:05 +00:00
|
|
|
$output .= '<li><a href="' . $href . '" title="' . $title . '">' . $title . '</a>';
|
|
|
|
if ( $date ) {
|
|
|
|
$output .= " ($pubdate)";
|
2010-07-22 22:07:15 +00:00
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
$output .= '</li>';
|
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
$displayed[] = $title;
|
2010-07-23 14:14:05 +00:00
|
|
|
# Cut off output when maxheads is reached:
|
2010-07-22 22:07:15 +00:00
|
|
|
if ( ++$headcnt == $maxheads ) break;
|
|
|
|
}
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
$output .= '</ul>';
|
2010-07-22 22:07:15 +00:00
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
return $output;
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
function wfRssFilter( $text, $rssFilter ) {
|
|
|
|
$display = true;
|
|
|
|
if ( is_array( $rssFilter ) ) {
|
2010-07-23 14:14:05 +00:00
|
|
|
foreach ( $rssFilter as $term ) {
|
2010-07-22 22:07:15 +00:00
|
|
|
if ( $term ) {
|
|
|
|
$display = false;
|
|
|
|
if ( preg_match( "|$term|i", $text, $a ) ) {
|
|
|
|
$display = true;
|
|
|
|
return $display;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $display ) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $display;
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
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;
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
function wfRssHighlight( $text, $rssHighlight ) {
|
|
|
|
$i = 0;
|
|
|
|
$starttag = 'v8x5u3t3u8h';
|
|
|
|
$endtag = 'q8n4f6n4n4x';
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
$color[] = 'coral';
|
|
|
|
$color[] = 'greenyellow';
|
|
|
|
$color[] = 'lightskyblue';
|
|
|
|
$color[] = 'gold';
|
|
|
|
$color[] = 'violet';
|
|
|
|
$count_color = count( $color );
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
if ( is_array( $rssHighlight ) ) {
|
2010-07-23 14:14:05 +00:00
|
|
|
foreach ( $rssHighlight as $term ) {
|
2010-07-22 22:07:15 +00:00
|
|
|
if ( $term ) {
|
2010-07-23 14:14:05 +00:00
|
|
|
$text = preg_replace( "|\b(\w*?" . $term . "\w*?)\b|i", "$starttag" . "_" . $i . "\\1$endtag", $text );
|
2010-07-22 22:07:15 +00:00
|
|
|
$i++;
|
|
|
|
if ( $i == $count_color ) $i = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# To avoid trouble should someone wants to highlight the terms "span", "style", …
|
2010-07-22 22:07:15 +00:00
|
|
|
for ( $i = 0; $i < 5; $i++ ) {
|
2010-07-23 14:14:05 +00:00
|
|
|
$text = preg_replace( "|$starttag" . "_" . $i . "|", "<span style=\"background-color:" . $color[$i] . "; font-weight: bold;\">", $text );
|
2010-07-22 22:07:15 +00:00
|
|
|
$text = preg_replace( "|$endtag|", '</span>', $text );
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
return $text;
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
# PHP closing tag intentionally left blank
|