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 {
|
2012-02-23 21:12:54 +00:00
|
|
|
|
2010-11-04 23:19:00 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2012-01-28 17:05:20 +00:00
|
|
|
* @param $frame PPFrame parser context
|
2010-11-04 23:19:00 +00:00
|
|
|
*/
|
|
|
|
static function renderRss( $input, $args, $parser, $frame ) {
|
2012-02-27 01:10:57 +00:00
|
|
|
global $wgRSSCacheAge, $wgRSSCacheCompare, $wgRSSNamespaces,
|
|
|
|
$wgRSSUrlWhitelist,$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] ) ) {
|
2012-02-27 01:10:57 +00:00
|
|
|
return RSSUtils::RSSError( 'rss-ns-permission' );
|
2011-05-06 16:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
2010-11-04 23:19:00 +00:00
|
|
|
|
2012-02-23 21:12:54 +00:00
|
|
|
switch ( true ) {
|
|
|
|
|
2012-02-27 01:10:57 +00:00
|
|
|
case ( isset( $wgRSSAllowedFeeds ) ):
|
|
|
|
return RSSUtils::RSSError( 'rss-deprecated-wgrssallowedfeeds-found' );
|
|
|
|
break;
|
|
|
|
|
2012-02-23 22:24:00 +00:00
|
|
|
# disallow because there is no whitelist or empty whitelist
|
2012-02-23 21:12:54 +00:00
|
|
|
case ( !isset( $wgRSSUrlWhitelist )
|
|
|
|
|| !is_array( $wgRSSUrlWhitelist )
|
|
|
|
|| ( count( $wgRSSUrlWhitelist ) === 0 ) ):
|
|
|
|
return RSSUtils::RSSError( 'rss-empty-whitelist',
|
|
|
|
$input
|
|
|
|
);
|
|
|
|
break;
|
2011-07-09 01:59:02 +00:00
|
|
|
|
2012-02-23 21:12:54 +00:00
|
|
|
# allow
|
|
|
|
case ( in_array( "*", $wgRSSUrlWhitelist ) ):
|
|
|
|
case ( in_array( $input, $wgRSSUrlWhitelist ) ):
|
|
|
|
break;
|
|
|
|
|
|
|
|
# otherwise disallow
|
|
|
|
case ( !in_array( $input, $wgRSSUrlWhitelist ) ):
|
|
|
|
default:
|
|
|
|
$listOfAllowed = $parser->getFunctionLang()->listToText( $wgRSSUrlWhitelist );
|
|
|
|
$numberAllowed = $parser->getFunctionLang()->formatNum( count( $wgRSSUrlWhitelist ) );
|
|
|
|
return RSSUtils::RSSError( 'rss-url-is-not-whitelisted',
|
|
|
|
array( $input, $listOfAllowed, $numberAllowed )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-11-13 19:11:28 +00:00
|
|
|
if ( !Http::isValidURI( $input ) ) {
|
2012-02-29 20:00:43 +00:00
|
|
|
return RSSUtils::RSSError( 'rss-invalid-url', htmlspecialchars( $input ) );
|
2010-11-04 23:19:00 +00:00
|
|
|
}
|
|
|
|
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 ) ) {
|
2012-02-27 01:10:57 +00:00
|
|
|
return RSSUtils::RSSError( '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 );
|
|
|
|
}
|
2012-02-23 21:12:54 +00:00
|
|
|
|
2010-11-04 23:19:00 +00:00
|
|
|
}
|