Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
Hooks | |
0.00% |
0 / 38 |
|
0.00% |
0 / 2 |
272 | |
0.00% |
0 / 1 |
onParserFirstCallInit | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
renderRss | |
0.00% |
0 / 37 |
|
0.00% |
0 / 1 |
240 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\RSS; |
4 | |
5 | use MediaWiki\Hook\ParserFirstCallInitHook; |
6 | use MediaWiki\Parser\Parser; |
7 | use MediaWiki\Status\Status; |
8 | use MWHttpRequest; |
9 | use PPFrame; |
10 | |
11 | class Hooks implements ParserFirstCallInitHook { |
12 | |
13 | /** |
14 | * Tell the parser how to handle <rss> elements |
15 | * @param Parser $parser Parser Object |
16 | */ |
17 | public function onParserFirstCallInit( $parser ) { |
18 | // Install parser hook for <rss> tags |
19 | $parser->setHook( 'rss', [ __CLASS__, 'renderRss' ] ); |
20 | } |
21 | |
22 | /** |
23 | * Static function wrapping RSSParser to handle rendering of RSS elements |
24 | * @param string $input text inside the tags. |
25 | * @param array $args value associative list of the element attributes and |
26 | * their values. |
27 | * @param Parser $parser |
28 | * @param PPFrame $frame parser context |
29 | * @return string |
30 | */ |
31 | public static function renderRss( $input, array $args, Parser $parser, PPFrame $frame ) { |
32 | global $wgRSSCacheAge, $wgRSSCacheCompare, $wgRSSNamespaces, |
33 | $wgRSSUrlWhitelist, $wgRSSAllowedFeeds; |
34 | |
35 | if ( is_array( $wgRSSNamespaces ) && count( $wgRSSNamespaces ) ) { |
36 | $nsUsed = $parser->getTitle()->getNamespace(); |
37 | $authorizedNamespace = array_flip( $wgRSSNamespaces ); |
38 | |
39 | if ( !isset( $authorizedNamespace[$nsUsed] ) ) { |
40 | return Utils::getErrorHtml( 'rss-ns-permission' ); |
41 | } |
42 | } |
43 | |
44 | if ( isset( $wgRSSAllowedFeeds ) ) { |
45 | return Utils::getErrorHtml( 'rss-deprecated-wgrssallowedfeeds-found' ); |
46 | } |
47 | |
48 | # disallow because there is no whitelist at all or an empty whitelist |
49 | |
50 | if ( !isset( $wgRSSUrlWhitelist ) |
51 | || !is_array( $wgRSSUrlWhitelist ) |
52 | || ( count( $wgRSSUrlWhitelist ) === 0 ) |
53 | ) { |
54 | return Utils::getErrorHtml( 'rss-empty-allow-list', |
55 | $input |
56 | ); |
57 | } |
58 | |
59 | # disallow the feed url because the url is not allowed; or |
60 | # disallow because the wildcard joker is not present to allow any feed url |
61 | # which can be dangerous |
62 | |
63 | if ( !( in_array( $input, $wgRSSUrlWhitelist ) ) |
64 | && !( in_array( "*", $wgRSSUrlWhitelist ) ) ) { |
65 | $listOfAllowed = $parser->getTargetLanguage()->listToText( $wgRSSUrlWhitelist ); |
66 | $numberAllowed = $parser->getTargetLanguage()->formatNum( count( $wgRSSUrlWhitelist ) ); |
67 | |
68 | return Utils::getErrorHtml( 'rss-url-is-not-allowed', |
69 | [ $input, $listOfAllowed, $numberAllowed ] |
70 | ); |
71 | |
72 | } |
73 | |
74 | if ( !MWHttpRequest::isValidURI( $input ) ) { |
75 | return Utils::getErrorHtml( 'rss-invalid-url', htmlspecialchars( $input ) ); |
76 | } |
77 | |
78 | if ( $wgRSSCacheCompare ) { |
79 | $timeout = $wgRSSCacheCompare; |
80 | } else { |
81 | $timeout = $wgRSSCacheAge; |
82 | } |
83 | |
84 | $parser->getOutput()->updateCacheExpiry( $timeout ); |
85 | |
86 | $rss = new RSSParser( $input, $args ); |
87 | |
88 | $status = $rss->fetch(); |
89 | |
90 | # Check for errors. |
91 | if ( !$status->isGood() ) { |
92 | return wfMessage( |
93 | 'rss-error', htmlspecialchars( $input ), Status::wrap( $status )->getWikitext() |
94 | )->escaped(); |
95 | } |
96 | |
97 | if ( !is_object( $rss->rss ) || !is_array( $rss->rss->items ) ) { |
98 | return Utils::getErrorHtml( 'rss-empty', htmlspecialchars( $input ) ); |
99 | } |
100 | |
101 | return $rss->renderFeed( $parser, $frame ); |
102 | } |
103 | |
104 | } |