pvedocs-include.php: avoid to use javascript

Use a ParserAfterTidy hook to avoid JS. This hack was
found in the htmlet tag extension.
This commit is contained in:
Dietmar Maurer 2016-10-17 10:06:39 +02:00
parent 349e8c55b5
commit 208f75bc96

View File

@ -10,6 +10,7 @@ $wgExtensionCredits['parserhook'][] = array(
# Define a setup function # Define a setup function
$wgHooks['ParserFirstCallInit'][] = 'efPvedocsParserFunction_Setup'; $wgHooks['ParserFirstCallInit'][] = 'efPvedocsParserFunction_Setup';
$wgHooks['ParserAfterTidy'][] = 'efPvedocsPostProcessFunction';
# Add a hook to initialise the magic word # Add a hook to initialise the magic word
$wgHooks['LanguageGetMagic'][] = 'efPvedocsParserFunction_Magic'; $wgHooks['LanguageGetMagic'][] = 'efPvedocsParserFunction_Magic';
@ -17,13 +18,23 @@ $wgHooks['LanguageGetMagic'][] = 'efPvedocsParserFunction_Magic';
function efPvedocsParserFunction_Setup(&$parser) { function efPvedocsParserFunction_Setup(&$parser) {
# Set a function hook associating the "pvedocs" magic # Set a function hook associating the "pvedocs" magic
# word with our function # word with our function
$parser->setFunctionHook( 'pvedocs', 'efPvedocsParserFunction_Render' ); $parser->setFunctionHook('pvedocs', 'efPvedocsParserFunction_Render');
$parser->setHook('pvehide', 'renderTagPveHideContent' ); $parser->setHook('pvehide', 'renderTagPveHideContent');
return true; return true;
} }
# similar code as in <htmlet> tag...
function efPvedocsPostProcessFunction($parser, &$text) {
$text = preg_replace_callback(
'/<--- @PVEDOCSHACK@ ([0-9a-zA-Z\\+\\/]+=*) @PVEDOCSHACK@ -->/sm',
function ($m) { return base64_decode("$m[1]"); },
$text);
return true;
}
// Render <pvehide> // Render <pvehide>
function renderTagPveHideContent($input, array $args, Parser $parser, function renderTagPveHideContent($input, array $args, Parser $parser,
PPFrame $frame ) { PPFrame $frame ) {
@ -43,11 +54,6 @@ function efPvedocsParserFunction_Magic(&$magicWords, $langCode) {
return true; return true;
} }
function encodeURI($uri) {
return preg_replace_callback("{[^0-9a-z_.!~*'();,/?:@&=+$#-]}i",
function ($m) { return sprintf('%%%02X', ord($m[0])); }, $uri);
}
function efPvedocsParserFunction_Render($parser, $param1 = '', $param2 = '') { function efPvedocsParserFunction_Render($parser, $param1 = '', $param2 = '') {
$parser->disableCache(); $parser->disableCache();
@ -60,21 +66,8 @@ function efPvedocsParserFunction_Render($parser, $param1 = '', $param2 = '') {
$content = file_get_contents("/usr/share/pve-docs/$param1"); $content = file_get_contents("/usr/share/pve-docs/$param1");
$output = "<noscript><div><p>" . $output = '<--- @PVEDOCSHACK@ '.base64_encode($content).' @PVEDOCSHACK@ -->';
"This page requires java-script. To view " . return array($output, 'noparse' => true, 'isHTML' => true);
"this page without java-script goto " .
"<a href='/pve-docs/$param1'>$param1</a>" .
"</div></noscript>\n";
# hack to inject html without modifications my mediawiki parser
$encHtml = encodeURI($content);
$output .= "<div id='pve_embed_data'></div>";
$output .= "<script>" .
"var data = decodeURI(\"".$encHtml."\");" .
"document.getElementById('pve_embed_data').innerHTML = data;" .
"</script>";
return array($output, 'noparse' => true, 'isHTML' => true);
} }
?> ?>