2010-11-04 23:19:00 +00:00
|
|
|
<?php
|
2010-11-05 00:59:28 +00:00
|
|
|
|
2010-11-04 23:19:00 +00:00
|
|
|
class RSSHooks {
|
|
|
|
/**
|
|
|
|
* Tell the parser how to handle <rss> elements
|
2011-01-08 01:33:42 +00:00
|
|
|
* @param $parser Parser Object
|
2010-11-04 23:19:00 +00:00
|
|
|
*/
|
|
|
|
static function parserInit( $parser ) {
|
|
|
|
# Install parser hook for <rss> tags
|
|
|
|
$parser->setHook( 'rss', array( __CLASS__, 'renderRss' ) );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static function wrapping RSSParser to handle rendering of RSS elements
|
2010-11-05 00:59:28 +00:00
|
|
|
* @param $input String: text inside the tags.
|
|
|
|
* @param $args Array: value associative list of the element attributes and
|
|
|
|
* their values.
|
2011-01-08 01:33:42 +00:00
|
|
|
* @param $parser Parser
|
2010-11-05 00:59:28 +00:00
|
|
|
* @param $frame Frame parser context
|
2010-11-04 23:19:00 +00:00
|
|
|
*/
|
|
|
|
static function renderRss( $input, $args, $parser, $frame ) {
|
2011-07-09 01:59:02 +00:00
|
|
|
global $wgRSSCacheAge, $wgRSSCacheCompare, $wgRSSNamespaces, $wgRSSAllowedFeeds;
|
2011-05-06 16:21:43 +00:00
|
|
|
|
2011-05-06 16:40:14 +00:00
|
|
|
if ( is_array( $wgRSSNamespaces ) && count( $wgRSSNamespaces ) ) {
|
2011-05-06 16:21:43 +00:00
|
|
|
$ns = $parser->getTitle()->getNamespace();
|
2011-05-06 16:40:14 +00:00
|
|
|
$checkNS = array_flip( $wgRSSNamespaces );
|
2011-05-06 16:21:43 +00:00
|
|
|
|
|
|
|
if( !isset( $checkNS[$ns] ) ) {
|
|
|
|
return wfMsg( 'rss-ns-permission' );
|
|
|
|
}
|
|
|
|
}
|
2010-11-04 23:19:00 +00:00
|
|
|
|
2011-07-09 01:59:02 +00:00
|
|
|
if ( count( $wgRSSAllowedFeeds ) && !in_array( $input, $wgRSSAllowedFeeds ) ) {
|
2011-07-09 02:17:08 +00:00
|
|
|
return wfMsg( 'rss-url-permission' );
|
2011-07-09 01:59:02 +00:00
|
|
|
}
|
|
|
|
|
2010-11-13 19:11:28 +00:00
|
|
|
if ( !Http::isValidURI( $input ) ) {
|
2010-11-04 23:19:00 +00:00
|
|
|
return wfMsg( 'rss-invalid-url', htmlspecialchars( $input ) );
|
|
|
|
}
|
|
|
|
if ( $wgRSSCacheCompare ) {
|
|
|
|
$timeout = $wgRSSCacheCompare;
|
|
|
|
} else {
|
|
|
|
$timeout = $wgRSSCacheAge;
|
|
|
|
}
|
2010-11-16 22:06:59 +00:00
|
|
|
|
2010-11-15 23:47:12 +00:00
|
|
|
$parser->getOutput()->updateCacheExpiry( $timeout );
|
2010-11-04 23:19:00 +00:00
|
|
|
|
|
|
|
$rss = new RSSParser( $input, $args );
|
|
|
|
|
|
|
|
$status = $rss->fetch();
|
|
|
|
|
|
|
|
# Check for errors.
|
2010-11-05 00:59:28 +00:00
|
|
|
if ( !$status->isGood() ) {
|
|
|
|
return wfMsg( 'rss-error', htmlspecialchars( $input ), $status->getWikiText() );
|
|
|
|
}
|
2010-11-04 23:19:00 +00:00
|
|
|
|
2010-11-05 00:59:28 +00:00
|
|
|
if ( !is_object( $rss->rss ) || !is_array( $rss->rss->items ) ) {
|
2010-11-04 23:19:00 +00:00
|
|
|
return wfMsg( 'rss-empty', htmlspecialchars( $input ) );
|
2010-11-05 00:59:28 +00:00
|
|
|
}
|
2010-11-04 23:19:00 +00:00
|
|
|
|
|
|
|
return $rss->renderFeed( $parser, $frame );
|
|
|
|
}
|
|
|
|
}
|