protected function runConsole($params) { global $wgParser; $options = new ParserOptions(); $wgParser->startExternalParse($params['title'], $options, Parser::OT_HTML, true); $engine = Scribunto::getParserEngine($wgParser); try { $result = $engine->runConsole($params); } catch (ScribuntoException $e) { $trace = $e->getScriptTraceHtml(); $message = $e->getMessage(); $html = Html::element('p', array(), $message); if ($trace !== false) { $html .= Html::element('p', array(), $this->msg('scribunto-common-backtrace')->inContentLanguage()->text()) . $trace; } return array('type' => 'error', 'html' => $html, 'message' => $message, 'messagename' => $e->getMessageName()); } return array('type' => 'normal', 'print' => strval($result['print']), 'return' => strval($result['return'])); }
/** * Function executed by use of {{#infoboxbuilder:}} parser function. * It gets the code from InfoboxBuilder.lua and creates new module object * from it. The module is then invoked and the result is returned. * @param Parser $parser Parser object * @param PPFrame $frame PPFrame object * @param array $args Array of arguments passed from $frame object * @return string A string returned by InfoboxBuilder.lua */ public static function parserFunctionHook(\Parser $parser, $frame, $args) { wfProfileIn(__METHOD__); try { /** * Add the registered SCSS with the default theme */ $parser->getOutput()->addModuleStyles('ext.wikia.InfoboxBuilder'); $engine = \Scribunto::getParserEngine($parser); unset($args[0]); $childFrame = $frame->newChild($args, $parser->getTitle(), 1); $moduleText = file_get_contents(__DIR__ . '/includes/lua/InfoboxBuilder.lua'); $module = new \Scribunto_LuaModule($engine, $moduleText, 'InfoboxBuilder'); $result = $module->invoke('builder', $childFrame); $result = \UtfNormal::cleanUp(strval($result)); wfProfileOut(__METHOD__); return $result; } catch (\ScribuntoException $e) { $trace = $e->getScriptTraceHtml(array('msgOptions' => array('content'))); $html = \Html::element('p', array(), $e->getMessage()); if ($trace !== false) { $html .= \Html::element('p', array(), wfMessage('scribunto-common-backtrace')->inContentLanguage()->text()) . $trace; } $out = $parser->getOutput(); if (!isset($out->scribunto_errors)) { $out->addOutputHook('ScribuntoError'); $out->scribunto_errors = array(); $parser->addTrackingCategory('scribunto-common-error-category'); } $out->scribunto_errors[] = $html; $id = 'mw-scribunto-error-' . (count($out->scribunto_errors) - 1); $parserError = wfMessage('scribunto-parser-error')->inContentLanguage()->text() . $parser->insertStripItem('<!--' . htmlspecialchars($e->getMessage()) . '-->'); wfProfileOut(__METHOD__); // #iferror-compatible error element return "<strong class=\"error\"><span class=\"scribunto-error\" id=\"{$id}\">" . $parserError . "</span></strong>"; } }
/** * @param $editor EditPage * @param $text string * @param $error * @param $summary * @return bool */ public static function validateScript($editor, $text, &$error, $summary) { global $wgOut; $title = $editor->getTitle(); if ($title->getNamespace() != NS_MODULE) { return true; } $req = RequestContext::getMain()->getRequest(); if ($req->getBool('scribunto_ignore_errors')) { return true; } $engine = Scribunto::newDefaultEngine(); $engine->setTitle($title); $status = $engine->validate($text, $title->getPrefixedDBkey()); if ($status->isOK()) { return true; } $errmsg = $status->getWikiText('scribunto-error-short', 'scribunto-error-long'); $error = <<<WIKI <div class="errorbox"> {$errmsg} </div> <br clear="all" /> WIKI; if (isset($status->scribunto_error->params['module'])) { $module = $status->scribunto_error->params['module']; $line = $status->scribunto_error->params['line']; if ($module === $title->getPrefixedDBkey() && preg_match('/^\\d+$/', $line)) { $wgOut->addInlineScript('window.location.hash = ' . Xml::encodeJsVar("#mw-ce-l{$line}")); } } return true; }
/** * Parse the Content object and generate a ParserOutput from the result. * * @param $title Title The page title to use as a context for rendering * @param $revId null|int The revision being rendered (optional) * @param $options null|ParserOptions Any parser options * @param $generateHtml boolean Whether to generate HTML (default: true). * @param &$output ParserOutput representing the HTML form of the text. * @return ParserOutput */ protected function fillParserOutput(Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true, ParserOutput &$output) { global $wgParser, $wgScribuntoUseGeSHi, $wgUseSiteCss; $text = $this->getNativeData(); $output = null; // Get documentation, if any $output = new ParserOutput(); $doc = Scribunto::getDocPage($title); if ($doc) { $msg = wfMessage($doc->exists() ? 'scribunto-doc-page-show' : 'scribunto-doc-page-does-not-exist', $doc->getPrefixedText())->inContentLanguage(); if (!$msg->isDisabled()) { // We need the ParserOutput for categories and such, so we // can't use $msg->parse(). $docViewLang = $doc->getPageViewLanguage(); $dir = $docViewLang->getDir(); // Code is forced to be ltr, but the documentation can be rtl. // Correct direction class is needed for correct formatting. // The possible classes are // mw-content-ltr or mw-content-rtl $dirClass = "mw-content-{$dir}"; $docWikitext = Html::rawElement('div', array('lang' => $docViewLang->getHtmlCode(), 'dir' => $dir, 'class' => $dirClass), "\n" . $msg->plain() . "\n"); if (!$options) { // NOTE: use canonical options per default to produce cacheable output $options = ContentHandler::getForTitle($doc)->makeParserOptions('canonical'); } else { if ($options->getTargetLanguage() === null) { $options->setTargetLanguage($doc->getPageLanguage()); } } $output = $wgParser->parse($docWikitext, $title, $options, true, true, $revId); } // Mark the doc page as a transclusion, so we get purged when it // changes. $output->addTemplate($doc, $doc->getArticleID(), $doc->getLatestRevID()); } // Validate the script, and include an error message and tracking // category if it's invalid $engine = Scribunto::newDefaultEngine(); $engine->setTitle($title); $status = $engine->validate($text, $title->getPrefixedDBkey()); if (!$status->isOK()) { $output->setText($output->getText() . Html::rawElement('div', array('class' => 'errorbox'), $status->getHTML('scribunto-error-short', 'scribunto-error-long'))); $catmsg = wfMessage('scribunto-module-with-errors-category')->title($title)->inContentLanguage(); if (!$catmsg->isDisabled()) { $cat = Title::makeTitleSafe(NS_CATEGORY, $catmsg->text()); if ($cat) { $sort = (string) $output->getProperty('defaultsort'); $output->addCategory($cat->getDBkey(), $sort); } else { wfDebug(__METHOD__ . ": [[MediaWiki:scribunto-module-with-errors-category]] " . "is not a valid title!\n"); } } } if (!$generateHtml) { // We don't need the actual HTML $output->setText(''); return $output; } // Add HTML for the actual script $language = $engine->getGeSHiLanguage(); if ($wgScribuntoUseGeSHi && $language) { $geshi = SyntaxHighlight_GeSHi::prepare($text, $language); $geshi->set_language($language); if ($geshi instanceof GeSHi && !$geshi->error()) { $code = $geshi->parse_code(); if ($code) { // @todo Once we drop support for old versions of // Extension:SyntaxHighlight_GeSHi, drop the ugly test and // the BC case. global $wgAutoloadClasses; if (isset($wgAutoloadClasses['ResourceLoaderGeSHiModule'])) { $output->addModuleStyles("ext.geshi.language.{$language}"); } else { // Backwards compatibility $output->addHeadItem(SyntaxHighlight_GeSHi::buildHeadItem($geshi), "source-{$language}"); } if ($wgUseSiteCss) { $output->addModuleStyles('ext.geshi.local'); } $output->setText($output->getText() . $code); return $output; } } } // No GeSHi, or GeSHi can't parse it, use plain <pre> $output->setText($output->getText() . "<pre class='mw-code mw-script' dir='ltr'>\n" . htmlspecialchars($text) . "\n</pre>\n"); return $output; }
/** * @param Article &$article * @param bool &$outputDone * @param bool &$pcache * @return bool */ public static function showDocPageHeader(Article &$article, &$outputDone, &$pcache) { global $wgOut; $title = $article->getTitle(); if (Scribunto::isDocPage($title, $forModule)) { $wgOut->addHTML(wfMessage('scribunto-doc-page-header', $forModule->getPrefixedText())->parseAsBlock()); } return true; }