/** mediawiki-extensions-Scribunto-e6f636f\includes\common\Hooks.php
* Hook function for {{#invoke:module|func}}
*
* @param Parser $parser
* @param PPFrame $frame
* @param array $args
* @throws MWException
* @throws ScribuntoException
* @return string
*/
public static function invokeHook( Parser $parser, PPFrame $frame, array $args ) {
global $wgScribuntoGatherFunctionStats; // default: false
try {
if ( count( $args ) < 2 ) {
throw new ScribuntoException( 'scribunto-common-nofunction' );
}
$moduleName = trim( $frame->expand( $args[0] ) );
$engine = Scribunto::getParserEngine( $parser );
$title = Title::makeTitleSafe( NS_MODULE, $moduleName );
if ( !$title || !$title->hasContentModel( CONTENT_MODEL_SCRIBUNTO ) ) {
throw new ScribuntoException( 'scribunto-common-nosuchmodule',
[ 'args' => [ $moduleName ] ] );
}
$module = $engine->fetchModuleFromParser( $title );
if ( !$module ) {
throw new ScribuntoException( 'scribunto-common-nosuchmodule',
[ 'args' => [ $moduleName ] ] );
}
$functionName = trim( $frame->expand( $args[1] ) );
$bits = $args[1]->splitArg();
unset( $args[0] );
unset( $args[1] );
// If $bits['index'] is empty, then the function name was parsed as a
// key=value pair (because of an equals sign in it), and since it didn't
// have an index, we don't need the index offset.
$childFrame = $frame->newChild( $args, $title, $bits['index'] === '' ? 0 : 1 );
if ( $wgScribuntoGatherFunctionStats ) {
$u0 = $engine->getResourceUsage( $engine::CPU_SECONDS );
$result = $module->invoke( $functionName, $childFrame );
$u1 = $engine->getResourceUsage( $engine::CPU_SECONDS );
if ( $u1 > $u0 ) {
$timingMs = (int)( 1000 * ( $u1 - $u0 ) );
// Since the overhead of stats is worst when when #invoke
// calls are very short, don't process measurements <= 20ms.
if ( $timingMs > 20 ) {
self::reportTiming( $moduleName, $functionName, $timingMs );
}
}
} else {
$result = $module->invoke( $functionName, $childFrame );
}
return Validator::cleanUp( strval( $result ) );
} catch ( ScribuntoException $e ) {
$trace = $e->getScriptTraceHtml( [ 'msgOptions' => [ 'content' ] ] );
$html = Html::element( 'p', [], $e->getMessage() );
if ( $trace !== false ) {
$html .= Html::element( 'p',
[],
wfMessage( 'scribunto-common-backtrace' )->inContentLanguage()->text()
) . $trace;
} else {
$html .= Html::element( 'p',
[],
wfMessage( 'scribunto-common-no-details' )->inContentLanguage()->text()
);
}
$out = $parser->getOutput();
$errors = $out->getExtensionData( 'ScribuntoErrors' );
if ( $errors === null ) {
// On first hook use, set up error array and output
$errors = [];
$parser->addTrackingCategory( 'scribunto-common-error-category' );
$out->addModules( 'ext.scribunto.errors' );
}
$errors[] = $html;
$out->setExtensionData( 'ScribuntoErrors', $errors );
$out->addJsConfigVars( 'ScribuntoErrors', $errors );
$id = 'mw-scribunto-error-' . ( count( $errors ) - 1 );
$parserError = htmlspecialchars( $e->getMessage() );
// #iferror-compatible error element
return "<strong class=\"error\"><span class=\"scribunto-error\" id=\"$id\">" .
$parserError . "</span></strong>";
}
}