MediaWiki REL1_34
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 ) {
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();
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}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
$wgParser
Definition Setup.php:891
Set options of the Parser.
getTargetLanguage()
Target language for the parse.
setTargetLanguage( $x)
Target language for the parse.
addTrackingCategory( $msg, $title)
Add a tracking category, getting the title from a system message, or print a debug message if the tit...
addTemplate( $title, $page_id, $rev_id)
Register a template dependency for this output.
getRawText()
Get the cacheable text with <mw:editsection> markers still in it.
addModuleStyles( $modules)
Represents the content of a Scribunto script page.
highlight( $text, ParserOutput $output, ScribuntoEngineBase $engine)
Adds syntax highlighting to the output (or do not touch it and return false).
validate(Title $title)
Checks whether the script is valid.
fillParserOutput(Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output)
Parse the Content object and generate a ParserOutput from the result.
prepareSave(WikiPage $page, $flags, $parentRevId, User $user)
Wikitext scripting infrastructure for MediaWiki: base classes.
Definition Base.php:29
getGeSHiLanguage()
Get the language for GeSHi syntax highlighter.
Definition Base.php:216
static newDefaultEngine( $extraOptions=[])
Create a new engine object with default parameters.
Definition Common.php:32
static getDocPage(Title $title)
Return the Title for the documentation page.
Definition Common.php:122
static highlight( $code, $lang=null, $args=[])
Highlight a code-block using a particular lexer.
Content object implementation for representing flat text.
getText()
Returns the text represented by this Content object, as a string.
Represents a title within MediaWiki.
Definition Title.php:42
getPrefixedDBkey()
Get the prefixed database key form.
Definition Title.php:1806
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
Class representing a MediaWiki article and history.
Definition WikiPage.php:47
getTitle()
Get the title object of the article.
Definition WikiPage.php:298