2010-07-22 22:07:15 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-07-23 15:26:27 +00:00
|
|
|
* RSS-Feed MediaWiki extension
|
2010-07-23 14:14:05 +00:00
|
|
|
*
|
2010-07-23 15:26:27 +00:00
|
|
|
* @file
|
2010-07-23 14:01:46 +00:00
|
|
|
* @ingroup Extensions
|
2010-07-23 15:26:27 +00:00
|
|
|
* @version 1.7
|
|
|
|
* @author mutante, Daniel Kinzler, Rdb, Mafs, Alxndr, Chris Reigrut, K001
|
|
|
|
* @author Kellan Elliott-McCrea <kellan@protest.net> -- author of MagpieRSS
|
|
|
|
* @author Jeroen De Dauw
|
|
|
|
* @author Jack Phoenix <jack@countervandalism.net>
|
|
|
|
* @copyright © Kellan Elliott-McCrea <kellan@protest.net>
|
|
|
|
* @copyright © mutante, Daniel Kinzler, Rdb, Mafs, Alxndr, Chris Reigrut, K001
|
|
|
|
* @link http://www.mediawiki.org/wiki/Extension:RSS Documentation
|
2010-07-22 22:07:15 +00:00
|
|
|
*/
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-23 15:26:27 +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 15:26:27 +00:00
|
|
|
// Extension credits that will show up on Special:Version
|
2010-07-22 22:07:15 +00:00
|
|
|
$wgExtensionCredits['parserhook'][] = array(
|
|
|
|
'name' => 'RSS feed',
|
2010-07-23 14:14:05 +00:00
|
|
|
'author' => array(
|
2010-07-23 15:26:27 +00:00
|
|
|
'Kellan Elliott-McCrea',
|
2010-07-23 14:01:46 +00:00
|
|
|
'mutante',
|
2010-07-23 15:26:27 +00:00
|
|
|
'Daniel Kinzler',
|
2010-07-23 14:01:46 +00:00
|
|
|
'Rdb',
|
|
|
|
'Mafs',
|
|
|
|
'Alxndr',
|
|
|
|
'Wikinaut',
|
2010-07-23 15:26:27 +00:00
|
|
|
'Chris Reigrut',
|
2010-07-23 14:01:46 +00:00
|
|
|
'K001',
|
2010-07-23 15:26:27 +00:00
|
|
|
'Jack Phoenix',
|
|
|
|
'Jeroen De Dauw'
|
2010-07-23 14:01:46 +00:00
|
|
|
),
|
2010-07-23 15:26:27 +00:00
|
|
|
'version' => '1.7',
|
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 15:26:27 +00:00
|
|
|
// Internationalization file and autoloadable classes
|
|
|
|
$dir = dirname( __FILE__ ) . '/';
|
|
|
|
$wgExtensionMessagesFiles['RSS'] = $dir . 'RSS.i18n.php';
|
2010-10-19 21:54:53 +00:00
|
|
|
$wgAutoloadClasses['RSSData'] = $dir . 'RSSData.php';
|
2010-07-23 15:26:27 +00:00
|
|
|
$wgAutoloadClasses['RSSCache'] = $dir . 'RSSCache.php';
|
2010-07-23 08:02:27 +00:00
|
|
|
|
2010-07-23 15:26:27 +00:00
|
|
|
$wgHooks['ParserFirstCallInit'][] = 'wfRssExtension';
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# Extension hook callback function
|
2010-07-23 15:26:27 +00:00
|
|
|
function wfRssExtension( &$parser ) {
|
2010-07-23 14:14:05 +00:00
|
|
|
# Install parser hook for <rss> tags
|
2010-07-23 15:26:27 +00:00
|
|
|
$parser->setHook( 'rss', 'renderRss' );
|
2010-07-22 22:07:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# Parser hook callback function
|
2010-10-19 21:54:53 +00:00
|
|
|
function renderRss( $input, $args, $parser, $frame ) {
|
|
|
|
global $wgOutputEncoding;
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
// Kill parser cache
|
2010-10-19 21:54:53 +00:00
|
|
|
$parser->disableCache();
|
2010-07-22 22:07:15 +00:00
|
|
|
|
2010-07-23 15:26:27 +00:00
|
|
|
if ( !$input ) {
|
|
|
|
return ''; # if <rss>-section is empty, return nothing
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-23 15:26:27 +00:00
|
|
|
# Parse fields in rss section
|
2010-10-19 21:54:53 +00:00
|
|
|
$url = $input;
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-23 15:26:27 +00:00
|
|
|
# Get charset from argument array
|
2010-10-19 21:54:53 +00:00
|
|
|
if( isset( $args['charset'] ) ) {
|
|
|
|
$charset = $args['charset'];
|
|
|
|
} else {
|
2010-07-23 15:26:27 +00:00
|
|
|
$charset = $wgOutputEncoding;
|
|
|
|
}
|
|
|
|
|
2010-07-23 14:14:05 +00:00
|
|
|
# Get max number of headlines from argument-array
|
2010-10-19 21:54:53 +00:00
|
|
|
if( isset( $args['max'] ) ) {
|
|
|
|
$maxheads = $args['max'];
|
|
|
|
} else {
|
|
|
|
$maxheads = 32;
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-23 15:26:27 +00:00
|
|
|
# Get short flag from argument array
|
2010-07-23 14:14:05 +00:00
|
|
|
# If short is set, no description text is printed
|
2010-07-23 15:26:27 +00:00
|
|
|
if( isset( $args['short'] ) ) {
|
|
|
|
$short = true;
|
|
|
|
} else {
|
|
|
|
$short = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get reverse flag from argument array
|
|
|
|
if( isset( $args['reverse'] ) ) {
|
2010-10-19 21:54:53 +00:00
|
|
|
$rss->items = array_reverse( $rss->items );
|
2010-07-22 22:07:15 +00:00
|
|
|
}
|
2010-07-23 15:26:27 +00:00
|
|
|
|
|
|
|
# Get date format from argument array
|
|
|
|
if ( isset( $args['date'] ) ) {
|
|
|
|
$date = $args['date'];
|
|
|
|
} else {
|
2010-10-19 21:54:53 +00:00
|
|
|
$date = 'd M Y H:i';
|
2010-07-23 15:26:27 +00:00
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# Get highlight terms from argument array
|
2010-10-19 21:54:53 +00:00
|
|
|
if( isset( $args['highlight'] ) ) {
|
|
|
|
$rssHighlight = $args['highlight'];
|
|
|
|
$rssHighlight = str_replace( ' ', ' ', $rssHighlight );
|
|
|
|
$rssHighlight = explode( ' ', trim( $rssHighlight ) );
|
|
|
|
} else {
|
|
|
|
$rssHighlight = false;
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-23 15:26:27 +00:00
|
|
|
# Get filter terms from argument array
|
2010-10-19 21:54:53 +00:00
|
|
|
if( isset( $args['filter'] ) ) {
|
|
|
|
$rssFilter = $args['filter'];
|
|
|
|
$rssFilter = str_replace( ' ', ' ', $rssFilter );
|
|
|
|
$rssFilter = explode( ' ', trim( $rssFilter ) );
|
|
|
|
} else {
|
|
|
|
$rssFilter = false;
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# Filterout terms
|
2010-10-19 21:54:53 +00:00
|
|
|
if( isset( $args['filterout'] ) ) {
|
|
|
|
$rssFilterout = $args['filterout'];
|
|
|
|
$rssFilterout = str_replace( ' ', ' ', $rssFilterout );
|
|
|
|
$rssFilterout = explode( ' ', trim( $rssFilterout ) );
|
|
|
|
} else {
|
|
|
|
$rssFilterout = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( isset( $args['template'] ) ) {
|
|
|
|
$template = 'Template:'.$args['template'];
|
|
|
|
} else {
|
|
|
|
$template = wfMsgNoTrans("rss-item");
|
|
|
|
}
|
|
|
|
|
|
|
|
$headcnt = 0;
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# Fetch RSS. May be cached locally.
|
2010-07-23 15:26:27 +00:00
|
|
|
# Refer to the documentation of MagpieRSS for details.
|
|
|
|
if ( !function_exists( 'fetch_rss' ) ) {
|
|
|
|
include( dirname( __FILE__ ) . '/RSSFetch.php' ); // provides fetch_rss() function
|
|
|
|
}
|
|
|
|
$rss = fetch_rss( $url );
|
2010-07-23 14:14:05 +00:00
|
|
|
|
|
|
|
# Check for errors.
|
2010-07-23 15:26:27 +00:00
|
|
|
if ( empty( $rss ) ) {
|
|
|
|
return wfMsg( 'rss-empty', $url );
|
|
|
|
}
|
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
if ( $rss->ERROR ) {
|
2010-07-23 15:26:27 +00:00
|
|
|
return '<div>' . wfMsg( 'rss-error', $url, $rss->ERROR ) . '</div>';
|
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 15:26:27 +00:00
|
|
|
return '<div>' . wfMsg( 'rss-empty', $url ) . '</div>';
|
2010-07-22 22:07:15 +00:00
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
$output = '';
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-10-19 21:54:53 +00:00
|
|
|
/* This would be better served by preg_replace_callback, but
|
|
|
|
* I can't create a callback that carries $item in PHP < 5.3
|
|
|
|
*/
|
|
|
|
if ( $template ) {
|
|
|
|
$headcnt = 0;
|
|
|
|
foreach( $rss->items as $item ) {
|
|
|
|
if($maxheads > 0 && $headcnt >= $maxheads) continue;
|
|
|
|
|
|
|
|
$decision = true;
|
|
|
|
foreach(array('title', 'author', 'description', 'category') as $check) {
|
|
|
|
if( isset( $item[$check] ) ) {
|
|
|
|
$decision &= wfRssFilter($item[$check], $rssFilter) & wfRssFilterout($item[$check], $rssFilterout);
|
|
|
|
if( !$decision ) continue 2;
|
|
|
|
|
|
|
|
$item[$check] = wfRssHighlight( $item[$check], $rssHighlight );
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-07-22 22:07:15 +00:00
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-10-19 21:54:53 +00:00
|
|
|
$rssTemp = "";
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-10-19 21:54:53 +00:00
|
|
|
foreach(explode("|", $template) as $bit) {
|
|
|
|
$bits = explode("=", $bit);
|
|
|
|
if( count($bits) == 2 ) {
|
|
|
|
$left = trim($bits[0]);
|
2010-07-23 15:26:27 +00:00
|
|
|
|
2010-10-19 21:54:53 +00:00
|
|
|
if( isset( $item[$left] ) ) {
|
|
|
|
$right = $item[$left];
|
|
|
|
}
|
2010-07-23 14:14:05 +00:00
|
|
|
|
2010-10-19 21:54:53 +00:00
|
|
|
$rssTemp .= implode( " = ", array($left, $right) );
|
|
|
|
} else {
|
|
|
|
$rssTemp .= $bit;
|
2010-07-23 15:26:27 +00:00
|
|
|
}
|
2010-10-19 21:54:53 +00:00
|
|
|
$rssTemp .= "|";
|
2010-07-22 22:07:15 +00:00
|
|
|
}
|
2010-10-19 21:54:53 +00:00
|
|
|
$rssTemp .= "}}";
|
|
|
|
|
|
|
|
$output .= $parser->recursiveTagParse( $rssTemp, $frame );
|
|
|
|
$headcnt++;
|
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 15:26:27 +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;
|
|
|
|
}
|
|
|
|
}
|
2010-07-23 15:26:27 +00:00
|
|
|
if ( $display ) {
|
|
|
|
break;
|
|
|
|
}
|
2010-07-22 22:07:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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 15:26:27 +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++;
|
2010-07-23 15:26:27 +00:00
|
|
|
if ( $i == $count_color ) {
|
|
|
|
$i = 0;
|
|
|
|
}
|
2010-07-22 22:07:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-10-19 21:54:53 +00:00
|
|
|
}
|