2013-02-20 22:00:42 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Scribunto Content Model
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
* @ingroup Scribunto
|
|
|
|
*
|
|
|
|
* @author Brad Jorsch <bjorsch@wikimedia.org>
|
|
|
|
*/
|
|
|
|
|
2019-10-28 20:17:06 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
2013-02-20 22:00:42 +00:00
|
|
|
/**
|
|
|
|
* Represents the content of a Scribunto script page
|
|
|
|
*/
|
|
|
|
class ScribuntoContent extends TextContent {
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
public function __construct( $text ) {
|
2014-09-16 03:25:53 +00:00
|
|
|
parent::__construct( $text, CONTENT_MODEL_SCRIBUNTO );
|
2013-02-20 22:00:42 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 23:41:13 +00:00
|
|
|
/**
|
|
|
|
* Checks whether the script is valid
|
|
|
|
*
|
|
|
|
* @param Title $title
|
|
|
|
* @return Status
|
|
|
|
*/
|
|
|
|
public function validate( Title $title ) {
|
|
|
|
$engine = Scribunto::newDefaultEngine();
|
|
|
|
$engine->setTitle( $title );
|
2019-03-27 15:02:17 +00:00
|
|
|
return $engine->validate( $this->getText(), $title->getPrefixedDBkey() );
|
2016-09-13 23:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function prepareSave( WikiPage $page, $flags, $parentRevId, User $user ) {
|
|
|
|
return $this->validate( $page->getTitle() );
|
|
|
|
}
|
|
|
|
|
2013-02-20 22:00:42 +00:00
|
|
|
/**
|
|
|
|
* Parse the Content object and generate a ParserOutput from the result.
|
|
|
|
*
|
2017-09-24 17:38:14 +00:00
|
|
|
* @param Title $title The page title to use as a context for rendering
|
|
|
|
* @param null|int $revId The revision being rendered (optional)
|
2018-07-11 16:34:25 +00:00
|
|
|
* @param ParserOptions $options Any parser options
|
2017-09-24 17:38:14 +00:00
|
|
|
* @param bool $generateHtml Whether to generate HTML (default: true).
|
|
|
|
* @param ParserOutput &$output ParserOutput representing the HTML form of the text.
|
2013-02-20 22:00:42 +00:00
|
|
|
* @return ParserOutput
|
|
|
|
*/
|
2016-05-17 14:52:05 +00:00
|
|
|
protected function fillParserOutput(
|
|
|
|
Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output
|
|
|
|
) {
|
2019-10-28 20:17:06 +00:00
|
|
|
$parser = MediaWikiServices::getInstance()->getParser();
|
2013-02-20 22:00:42 +00:00
|
|
|
|
2019-03-27 15:02:17 +00:00
|
|
|
$text = $this->getText();
|
2013-02-20 22:00:42 +00:00
|
|
|
|
|
|
|
// Get documentation, if any
|
|
|
|
$output = new ParserOutput();
|
2013-03-08 17:01:50 +00:00
|
|
|
$doc = Scribunto::getDocPage( $title );
|
2013-02-20 22:00:42 +00:00
|
|
|
if ( $doc ) {
|
|
|
|
$msg = wfMessage(
|
2013-03-08 17:01:50 +00:00
|
|
|
$doc->exists() ? 'scribunto-doc-page-show' : 'scribunto-doc-page-does-not-exist',
|
2013-02-20 22:00:42 +00:00
|
|
|
$doc->getPrefixedText()
|
|
|
|
)->inContentLanguage();
|
2014-05-08 08:24:08 +00:00
|
|
|
|
2013-02-20 22:00:42 +00:00
|
|
|
if ( !$msg->isDisabled() ) {
|
|
|
|
// We need the ParserOutput for categories and such, so we
|
|
|
|
// can't use $msg->parse().
|
2013-05-29 15:08:31 +00:00
|
|
|
$docViewLang = $doc->getPageViewLanguage();
|
2014-05-08 08:24:08 +00:00
|
|
|
$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',
|
2017-06-15 17:19:00 +00:00
|
|
|
[
|
2014-05-08 08:24:08 +00:00
|
|
|
'lang' => $docViewLang->getHtmlCode(),
|
|
|
|
'dir' => $dir,
|
|
|
|
'class' => $dirClass,
|
2017-06-15 17:19:00 +00:00
|
|
|
],
|
2014-05-08 08:24:08 +00:00
|
|
|
// Line breaks are needed so that wikitext would be
|
|
|
|
// appropriately isolated for correct parsing. See Bug 60664.
|
|
|
|
"\n" . $msg->plain() . "\n"
|
|
|
|
);
|
|
|
|
|
2018-07-11 16:34:25 +00:00
|
|
|
if ( $options->getTargetLanguage() === null ) {
|
|
|
|
$options->setTargetLanguage( $doc->getPageLanguage() );
|
2013-05-29 15:08:31 +00:00
|
|
|
}
|
2014-05-08 08:24:08 +00:00
|
|
|
|
2019-10-28 20:17:06 +00:00
|
|
|
$output = $parser->parse( $docWikitext, $title, $options, true, true, $revId );
|
2013-02-20 22:00:42 +00:00
|
|
|
}
|
|
|
|
|
2013-03-08 17:01:50 +00:00
|
|
|
// Mark the doc page as a transclusion, so we get purged when it
|
|
|
|
// changes.
|
2013-02-20 22:00:42 +00:00
|
|
|
$output->addTemplate( $doc, $doc->getArticleID(), $doc->getLatestRevID() );
|
|
|
|
}
|
|
|
|
|
2013-12-06 17:51:09 +00:00
|
|
|
// Validate the script, and include an error message and tracking
|
|
|
|
// category if it's invalid
|
2016-09-13 23:41:13 +00:00
|
|
|
$status = $this->validate( $title );
|
2015-06-21 04:38:39 +00:00
|
|
|
if ( !$status->isOK() ) {
|
2018-01-10 14:19:13 +00:00
|
|
|
$output->setText( $output->getRawText() .
|
2017-06-15 17:19:00 +00:00
|
|
|
Html::rawElement( 'div', [ 'class' => 'errorbox' ],
|
2013-12-06 17:51:09 +00:00
|
|
|
$status->getHTML( 'scribunto-error-short', 'scribunto-error-long' )
|
|
|
|
)
|
|
|
|
);
|
2016-09-14 18:52:43 +00:00
|
|
|
$output->addTrackingCategory( 'scribunto-module-with-errors-category', $title );
|
2013-12-06 17:51:09 +00:00
|
|
|
}
|
|
|
|
|
2013-02-20 22:00:42 +00:00
|
|
|
if ( !$generateHtml ) {
|
|
|
|
// We don't need the actual HTML
|
|
|
|
$output->setText( '' );
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2016-09-13 23:41:13 +00:00
|
|
|
$engine = Scribunto::newDefaultEngine();
|
|
|
|
$engine->setTitle( $title );
|
2015-10-12 19:32:17 +00:00
|
|
|
if ( $this->highlight( $text, $output, $engine ) ) {
|
|
|
|
return $output;
|
2013-02-20 22:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// No GeSHi, or GeSHi can't parse it, use plain <pre>
|
2018-01-10 14:19:13 +00:00
|
|
|
$output->setText( $output->getRawText() .
|
2014-09-22 02:51:26 +00:00
|
|
|
"<pre class='mw-code mw-script' dir='ltr'>\n" .
|
2013-02-20 22:00:42 +00:00
|
|
|
htmlspecialchars( $text ) .
|
|
|
|
"\n</pre>\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
2015-10-12 19:32:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds syntax highlighting to the output (or do not touch it and return false).
|
|
|
|
* @param string $text
|
|
|
|
* @param ParserOutput $output
|
|
|
|
* @param ScribuntoEngineBase $engine
|
|
|
|
* @return bool Success status
|
|
|
|
*/
|
|
|
|
protected function highlight( $text, ParserOutput $output, ScribuntoEngineBase $engine ) {
|
|
|
|
global $wgScribuntoUseGeSHi;
|
|
|
|
$language = $engine->getGeSHiLanguage();
|
|
|
|
if ( $wgScribuntoUseGeSHi && class_exists( SyntaxHighlight::class ) && $language ) {
|
|
|
|
$status = SyntaxHighlight::highlight( $text, $language );
|
|
|
|
if ( $status->isGood() ) {
|
|
|
|
// @todo replace addModuleStyles line with the appropriate call on
|
|
|
|
// SyntaxHighlight once one is created
|
|
|
|
$output->addModuleStyles( 'ext.pygments' );
|
|
|
|
$output->setText( $output->getRawText() . $status->getValue() );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-20 22:00:42 +00:00
|
|
|
}
|