Namespace extension

Change-Id: I0894842014e1a73f419c90cf11c8334ac102242a
This commit is contained in:
Reedy 2022-04-06 01:05:34 +01:00
parent b386fefd94
commit 80d1b67f19
5 changed files with 47 additions and 31 deletions

View file

@ -27,15 +27,12 @@
"i18n"
]
},
"AutoloadClasses": {
"RSSHooks": "includes/RSSHooks.php",
"RSSParser": "includes/RSSParser.php",
"RSSUtils": "includes/RSSUtils.php",
"RSSData": "includes/RSSData.php"
"AutoloadNamespaces": {
"MediaWiki\\Extension\\RSS\\": "includes/"
},
"Hooks": {
"ParserFirstCallInit": [
"RSSHooks::onParserFirstCallInit"
"MediaWiki\\Extension\\RSS\\Hooks::onParserFirstCallInit"
]
},
"TrackingCategories": [

View file

@ -1,6 +1,13 @@
<?php
class RSSHooks {
namespace MediaWiki\Extension\RSS;
use MWHttpRequest;
use Parser;
use PPFrame;
use Status;
class Hooks {
/**
* Tell the parser how to handle <rss> elements
@ -31,23 +38,23 @@ class RSSHooks {
$authorizedNamespace = array_flip( $wgRSSNamespaces );
if ( !isset( $authorizedNamespace[$nsUsed] ) ) {
return RSSUtils::getErrorHtml( 'rss-ns-permission' );
return Utils::getErrorHtml( 'rss-ns-permission' );
}
}
if ( isset( $wgRSSAllowedFeeds ) ) {
return RSSUtils::getErrorHtml( 'rss-deprecated-wgrssallowedfeeds-found' );
return Utils::getErrorHtml( 'rss-deprecated-wgrssallowedfeeds-found' );
}
# disallow because there is no whitelist at all or an empty whitelist
if ( !isset( $wgRSSUrlWhitelist )
|| !is_array( $wgRSSUrlWhitelist )
|| ( count( $wgRSSUrlWhitelist ) === 0 ) ) {
return RSSUtils::getErrorHtml( 'rss-empty-allow-list',
|| ( count( $wgRSSUrlWhitelist ) === 0 )
) {
return Utils::getErrorHtml( 'rss-empty-allow-list',
$input
);
}
# disallow the feed url because the url is not allowed; or
@ -59,14 +66,14 @@ class RSSHooks {
$listOfAllowed = $parser->getFunctionLang()->listToText( $wgRSSUrlWhitelist );
$numberAllowed = $parser->getFunctionLang()->formatNum( count( $wgRSSUrlWhitelist ) );
return RSSUtils::getErrorHtml( 'rss-url-is-not-allowed',
return Utils::getErrorHtml( 'rss-url-is-not-allowed',
[ $input, $listOfAllowed, $numberAllowed ]
);
}
if ( !MWHttpRequest::isValidURI( $input ) ) {
return RSSUtils::getErrorHtml( 'rss-invalid-url', htmlspecialchars( $input ) );
return Utils::getErrorHtml( 'rss-invalid-url', htmlspecialchars( $input ) );
}
if ( $wgRSSCacheCompare ) {
@ -89,7 +96,7 @@ class RSSHooks {
}
if ( !is_object( $rss->rss ) || !is_array( $rss->rss->items ) ) {
return RSSUtils::getErrorHtml( 'rss-empty', htmlspecialchars( $input ) );
return Utils::getErrorHtml( 'rss-empty', htmlspecialchars( $input ) );
}
return $rss->renderFeed( $parser, $frame );

View file

@ -1,5 +1,10 @@
<?php
namespace MediaWiki\Extension\RSS;
use DOMDocument;
use DOMXPath;
class RSSData {
public $error;
public $items;

View file

@ -1,6 +1,17 @@
<?php
namespace MediaWiki\Extension\RSS;
use DOMDocument;
use MediaWiki\MediaWikiServices;
use MWHttpRequest;
use Parser;
use PPFrame;
use Sanitizer;
use Status;
use TextContent;
use Title;
use WANObjectCache;
use Wikimedia\AtEase\AtEase;
class RSSParser {
@ -189,8 +200,7 @@ class RSSParser {
}
wfDebugLog( 'RSS', 'Cache Failed, fetching ' . $this->url . ' from remote.' );
$status = $this->fetchRemote( $key );
return $status;
return $this->fetchRemote( $key );
}
/**
@ -322,8 +332,7 @@ class RSSParser {
return $fetch;
}
$ret = $this->responseToXML( $key );
return $ret;
return $this->responseToXML( $key );
}
/**
@ -341,7 +350,7 @@ class RSSParser {
);
$stripItems = $this->stripItems;
$text = preg_replace_callback(
return preg_replace_callback(
"/{$this->markerString}-(\d+)-{$this->markerString}/",
static function ( array $matches ) use ( $stripItems ) {
$markerIndex = (int)$matches[1];
@ -349,7 +358,6 @@ class RSSParser {
},
$result->getText()
);
return $text;
}
/**
@ -439,9 +447,7 @@ class RSSParser {
// nullify all remaining info items in the template
// without a corresponding info in the current feed item
$renderedItem = preg_replace( "!{{{[^}]+}}}!U", "", $renderedItem );
return $renderedItem;
return preg_replace( "!{{{[^}]+}}}!U", "", $renderedItem );
}
/**
@ -491,22 +497,19 @@ class RSSParser {
$extraInclude = [];
$extraExclude = [ "iframe" ];
if ( isset( $wgRSSAllowLinkTag ) && $wgRSSAllowLinkTag ) {
if ( $wgRSSAllowLinkTag ) {
$extraInclude[] = "a";
} else {
$extraExclude[] = "a";
}
if ( isset( $wgRSSAllowImageTag ) && $wgRSSAllowImageTag ) {
if ( $wgRSSAllowImageTag ) {
$extraInclude[] = "img";
} else {
$extraExclude[] = "img";
}
// @phan-suppress-next-line PhanRedundantCondition
if ( ( isset( $wgRSSAllowLinkTag ) && $wgRSSAllowLinkTag )
|| ( isset( $wgRSSAllowImageTag ) && $wgRSSAllowImageTag ) ) {
// @phan-suppress-previous-line PhanRedundantCondition
if ( $wgRSSAllowLinkTag || $wgRSSAllowImageTag ) {
$ret = Sanitizer::removeSomeTags( $text, [
'extraTags' => $extraInclude,
'removeTags' => $extraExclude,

View file

@ -1,6 +1,10 @@
<?php
class RSSUtils {
namespace MediaWiki\Extension\RSS;
use Html;
class Utils {
/**
* Output an error message, all wrapped up nicely in HTML.