2013-02-20 22:00:42 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Scribunto Content Model
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
* @ingroup Scribunto
|
|
|
|
*
|
|
|
|
* @author Brad Jorsch <bjorsch@wikimedia.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the content of a Scribunto script page
|
|
|
|
*/
|
|
|
|
class ScribuntoContent extends TextContent {
|
|
|
|
|
|
|
|
function __construct( $text ) {
|
|
|
|
parent::__construct( $text, 'Scribunto' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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).
|
|
|
|
* @return ParserOutput
|
|
|
|
*/
|
|
|
|
public function getParserOutput( Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
|
|
|
|
global $wgParser, $wgScribuntoUseGeSHi;
|
|
|
|
|
|
|
|
$text = $this->getNativeData();
|
|
|
|
$output = null;
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
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();
|
|
|
|
$docWikitext = '<div lang="' . htmlspecialchars( $docViewLang->getHtmlCode() ) . '"'
|
2014-01-31 14:42:12 +00:00
|
|
|
. ' dir="' . $docViewLang->getDir() . "\">\n" . $msg->plain() . "\n</div>";
|
2013-05-29 15:08:31 +00:00
|
|
|
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 );
|
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
|
|
|
|
$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"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 22:00:42 +00:00
|
|
|
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 ) {
|
|
|
|
$output->addHeadItem( SyntaxHighlight_GeSHi::buildHeadItem( $geshi ), "source-{$language}" );
|
2013-05-29 15:08:31 +00:00
|
|
|
$output->setText( $output->getText() . $code );
|
2013-02-20 22:00:42 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a Content object with pre-save transformations applied (or this
|
|
|
|
* object if no transformations apply).
|
|
|
|
*
|
|
|
|
* @param $title Title
|
|
|
|
* @param $user User
|
|
|
|
* @param $parserOptions null|ParserOptions
|
|
|
|
* @return Content
|
|
|
|
*/
|
|
|
|
public function preSaveTransform( Title $title, User $user, ParserOptions $parserOptions ) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|