MediaWiki  1.34.0
ScribuntoContent.php
Go to the documentation of this file.
1 <?php
16 
17  public function __construct( $text ) {
18  parent::__construct( $text, CONTENT_MODEL_SCRIBUNTO );
19  }
20 
27  public function validate( Title $title ) {
28  $engine = Scribunto::newDefaultEngine();
29  $engine->setTitle( $title );
30  return $engine->validate( $this->getText(), $title->getPrefixedDBkey() );
31  }
32 
33  public function prepareSave( WikiPage $page, $flags, $parentRevId, User $user ) {
34  return $this->validate( $page->getTitle() );
35  }
36 
47  protected function fillParserOutput(
48  Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output
49  ) {
50  global $wgParser;
51 
52  $text = $this->getText();
53 
54  // Get documentation, if any
55  $output = new ParserOutput();
56  $doc = Scribunto::getDocPage( $title );
57  if ( $doc ) {
58  $msg = wfMessage(
59  $doc->exists() ? 'scribunto-doc-page-show' : 'scribunto-doc-page-does-not-exist',
60  $doc->getPrefixedText()
61  )->inContentLanguage();
62 
63  if ( !$msg->isDisabled() ) {
64  // We need the ParserOutput for categories and such, so we
65  // can't use $msg->parse().
66  $docViewLang = $doc->getPageViewLanguage();
67  $dir = $docViewLang->getDir();
68 
69  // Code is forced to be ltr, but the documentation can be rtl.
70  // Correct direction class is needed for correct formatting.
71  // The possible classes are
72  // mw-content-ltr or mw-content-rtl
73  $dirClass = "mw-content-$dir";
74 
75  $docWikitext = Html::rawElement(
76  'div',
77  [
78  'lang' => $docViewLang->getHtmlCode(),
79  'dir' => $dir,
80  'class' => $dirClass,
81  ],
82  // Line breaks are needed so that wikitext would be
83  // appropriately isolated for correct parsing. See Bug 60664.
84  "\n" . $msg->plain() . "\n"
85  );
86 
87  if ( $options->getTargetLanguage() === null ) {
88  $options->setTargetLanguage( $doc->getPageLanguage() );
89  }
90 
91  $output = $wgParser->parse( $docWikitext, $title, $options, true, true, $revId );
92  }
93 
94  // Mark the doc page as a transclusion, so we get purged when it
95  // changes.
96  $output->addTemplate( $doc, $doc->getArticleID(), $doc->getLatestRevID() );
97  }
98 
99  // Validate the script, and include an error message and tracking
100  // category if it's invalid
101  $status = $this->validate( $title );
102  if ( !$status->isOK() ) {
103  $output->setText( $output->getRawText() .
104  Html::rawElement( 'div', [ 'class' => 'errorbox' ],
105  $status->getHTML( 'scribunto-error-short', 'scribunto-error-long' )
106  )
107  );
108  $output->addTrackingCategory( 'scribunto-module-with-errors-category', $title );
109  }
110 
111  if ( !$generateHtml ) {
112  // We don't need the actual HTML
113  $output->setText( '' );
114  return $output;
115  }
116 
117  $engine = Scribunto::newDefaultEngine();
118  $engine->setTitle( $title );
119  if ( $this->highlight( $text, $output, $engine ) ) {
120  return $output;
121  }
122 
123  // No GeSHi, or GeSHi can't parse it, use plain <pre>
124  $output->setText( $output->getRawText() .
125  "<pre class='mw-code mw-script' dir='ltr'>\n" .
126  htmlspecialchars( $text ) .
127  "\n</pre>\n"
128  );
129 
130  return $output;
131  }
132 
140  protected function highlight( $text, ParserOutput $output, ScribuntoEngineBase $engine ) {
141  global $wgScribuntoUseGeSHi;
142  $language = $engine->getGeSHiLanguage();
143  if ( $wgScribuntoUseGeSHi && class_exists( SyntaxHighlight::class ) && $language ) {
144  $status = SyntaxHighlight::highlight( $text, $language );
145  if ( $status->isGood() ) {
146  // @todo replace addModuleStyles line with the appropriate call on
147  // SyntaxHighlight once one is created
148  $output->addModuleStyles( 'ext.pygments' );
149  $output->setText( $output->getRawText() . $status->getValue() );
150  return true;
151  }
152  }
153  return false;
154  }
155 }
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:42
ScribuntoContent\fillParserOutput
fillParserOutput(Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output)
Parse the Content object and generate a ParserOutput from the result.
Definition: ScribuntoContent.php:47
ParserOutput
Definition: ParserOutput.php:25
$wgParser
$wgParser
Definition: Setup.php:892
Title\getPrefixedDBkey
getPrefixedDBkey()
Get the prefixed database key form.
Definition: Title.php:1806
ParserOptions\setTargetLanguage
setTargetLanguage( $x)
Target language for the parse.
Definition: ParserOptions.php:339
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:47
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
ScribuntoEngineBase\getGeSHiLanguage
getGeSHiLanguage()
Get the language for GeSHi syntax highlighter.
Definition: Base.php:216
TextContent\getText
getText()
Returns the text represented by this Content object, as a string.
Definition: TextContent.php:136
ScribuntoContent\prepareSave
prepareSave(WikiPage $page, $flags, $parentRevId, User $user)
Definition: ScribuntoContent.php:33
Scribunto\newDefaultEngine
static newDefaultEngine( $extraOptions=[])
Create a new engine object with default parameters.
Definition: Common.php:32
ScribuntoContent
Represents the content of a Scribunto script page.
Definition: ScribuntoContent.php:15
WikiPage\getTitle
getTitle()
Get the title object of the article.
Definition: WikiPage.php:298
ParserOptions\getTargetLanguage
getTargetLanguage()
Target language for the parse.
Definition: ParserOptions.php:330
$title
$title
Definition: testCompression.php:34
$output
$output
Definition: SyntaxHighlight.php:335
Scribunto\getDocPage
static getDocPage(Title $title)
Return the Title for the documentation page.
Definition: Common.php:122
ScribuntoContent\validate
validate(Title $title)
Checks whether the script is valid.
Definition: ScribuntoContent.php:27
ScribuntoEngineBase
Wikitext scripting infrastructure for MediaWiki: base classes.
Definition: Base.php:29
ScribuntoContent\__construct
__construct( $text)
Definition: ScribuntoContent.php:17
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:37
Title
Represents a title within MediaWiki.
Definition: Title.php:42
$status
return $status
Definition: SyntaxHighlight.php:347
ScribuntoContent\highlight
highlight( $text, ParserOutput $output, ScribuntoEngineBase $engine)
Adds syntax highlighting to the output (or do not touch it and return false).
Definition: ScribuntoContent.php:140
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51