24 use UtfNormal\Validator;
25 use Wikimedia\PSquare;
36 define(
'CONTENT_MODEL_SCRIBUNTO',
'Scribunto' );
48 $engine->getSoftwareInfo( $software );
81 $parser->scribunto_engine =
null;
96 global $wgScribuntoGatherFunctionStats;
99 if ( count(
$args ) < 2 ) {
106 if ( !
$title || !
$title->hasContentModel( CONTENT_MODEL_SCRIBUNTO ) ) {
108 [
'args' => [ $moduleName ] ] );
110 $module = $engine->fetchModuleFromParser(
$title );
113 [
'args' => [ $moduleName ] ] );
115 $functionName = trim( $frame->
expand(
$args[1] ) );
117 $bits =
$args[1]->splitArg();
126 if ( $wgScribuntoGatherFunctionStats ) {
127 $u0 = $engine->getResourceUsage( $engine::CPU_SECONDS );
128 $result = $module->invoke( $functionName, $childFrame );
129 $u1 = $engine->getResourceUsage( $engine::CPU_SECONDS );
132 $timingMs = (int)( 1000 * ( $u1 - $u0 ) );
135 if ( $timingMs > 20 ) {
140 $result = $module->invoke( $functionName, $childFrame );
143 return Validator::cleanUp( strval( $result ) );
146 $html = Html::element(
'p', [], $e->getMessage() );
147 if ( $trace !==
false ) {
148 $html .= Html::element(
'p',
150 wfMessage(
'scribunto-common-backtrace' )->inContentLanguage()->text()
153 $html .= Html::element(
'p',
155 wfMessage(
'scribunto-common-no-details' )->inContentLanguage()->text()
158 $out = $parser->getOutput();
159 $errors = $out->getExtensionData(
'ScribuntoErrors' );
160 if ( $errors ===
null ) {
163 $parser->addTrackingCategory(
'scribunto-common-error-category' );
164 $out->addModules(
'ext.scribunto.errors' );
167 $out->setExtensionData(
'ScribuntoErrors', $errors );
168 $out->addJsConfigVars(
'ScribuntoErrors', $errors );
169 $id =
'mw-scribunto-error-' . ( count( $errors ) - 1 );
170 $parserError = htmlspecialchars( $e->getMessage() );
173 return "<strong class=\"error\"><span class=\"scribunto-error\" id=\"$id\">" .
174 $parserError .
"</span></strong>";
185 public static function reportTiming( $moduleName, $functionName, $timing ) {
186 global $wgScribuntoGatherFunctionStats, $wgScribuntoSlowFunctionThreshold;
188 if ( !$wgScribuntoGatherFunctionStats ) {
192 $threshold = $wgScribuntoSlowFunctionThreshold;
193 if ( !( is_float( $threshold ) && $threshold > 0 && $threshold < 1 ) ) {
213 $key =
$cache->makeGlobalKey( __METHOD__, $cacheVersion, $threshold );
219 $ps =
$cache->get( $key ) ?:
new PSquare( $threshold );
220 $ps->addObservation( $timing );
221 $cache->set( $key, $ps, 60 );
223 if ( $ps->getCount() < 1000 || $timing < $ps->getValue() ) {
230 $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
233 $metricKey = sprintf(
'scribunto.traces.%s__%s__%s', wfWikiId(), $moduleName, $functionName );
234 $stats->timing( $metricKey, $timing );
243 global $wgScribuntoUseCodeEditor;
244 if ( $wgScribuntoUseCodeEditor &&
$title->hasContentModel( CONTENT_MODEL_SCRIBUNTO )
247 if ( $engine->getCodeEditorLanguage() ) {
248 $languageCode = $engine->getCodeEditorLanguage();
264 if ( $model ===
'sanitized-css' ) {
269 $model = CONTENT_MODEL_SCRIBUNTO;
285 $engine->reportLimitData(
$output );
300 public static function formatLimitData( $key, &$value, &$report, $isHTML, $localize ) {
302 return $engine->formatLimitData( $key, $value, $report, $isHTML, $localize );
314 if ( $editor->
getTitle()->hasContentModel( CONTENT_MODEL_SCRIBUNTO ) ) {
315 $output->addModules(
'ext.scribunto.edit' );
316 $editor->editFormTextAfterTools .=
'<div id="mw-scribunto-console"></div>';
329 if ( $editor->
getTitle()->hasContentModel( CONTENT_MODEL_SCRIBUNTO ) ) {
330 $output->addModules(
'ext.scribunto.edit' );
331 $editor->editFormTextAfterContent .=
'<div id="mw-scribunto-console"></div>';
345 if ( $editor->
getTitle()->hasContentModel( CONTENT_MODEL_SCRIBUNTO ) ) {
346 unset( $buttons[
'preview'] );
367 if ( $validateStatus->isOK() ) {
371 $status->merge( $validateStatus );
373 if ( isset( $validateStatus->scribunto_error->params[
'module'] ) ) {
374 $module = $validateStatus->scribunto_error->params[
'module'];
375 $line = $validateStatus->scribunto_error->params[
'line'];
376 if ( $module ===
$title->getPrefixedDBkey() && preg_match(
'/^\d+$/',
$line ) ) {
378 $out->addInlineScript(
'window.location.hash = ' .
Xml::encodeJsVar(
"#mw-ce-l$line" ) );
395 wfMessage(
'scribunto-doc-page-header', $forModule->getPrefixedText() )->parseAsBlock()