MediaWiki master
JavaScriptContentHandler.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Content;
22
24use Content;
33use WikiPage;
34
44
48 public function __construct( $modelId = CONTENT_MODEL_JAVASCRIPT ) {
49 parent::__construct( $modelId, [ CONTENT_FORMAT_JAVASCRIPT ] );
50 }
51
55 protected function getContentClass() {
56 return JavaScriptContent::class;
57 }
58
59 public function supportsRedirects() {
60 return true;
61 }
62
70 public function makeRedirectContent( Title $destination, $text = '' ) {
71 // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
72 $url = $destination->getFullURL( 'action=raw&ctype=text/javascript', false, PROTO_RELATIVE );
73 $class = $this->getContentClass();
74 // Don't needlessly encode ampersands in URLs (T107289).
75 // Avoid FormatJson or Html::encodeJsCall to ensure long-term byte-identical stability,
76 // as required for JavaScriptContent::getRedirectTarget validation.
77 $redirectContent = '/* #REDIRECT */mw.loader.load('
78 . json_encode( $url, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE )
79 . ');';
80 return new $class( $redirectContent );
81 }
82
83 public function preSaveTransform(
84 Content $content,
85 PreSaveTransformParams $pstParams
86 ): Content {
87 '@phan-var JavascriptContent $content';
88
89 $parserOptions = $pstParams->getParserOptions();
90 // @todo Make pre-save transformation optional for script pages (T34858)
92 if ( !$services->getUserOptionsLookup()->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) {
93 // Allow bot users to disable the pre-save transform for CSS/JS (T236828).
94 $parserOptions = clone $parserOptions;
95 $parserOptions->setPreSaveTransform( false );
96 }
97
98 $text = $content->getText();
99 $pst = $services->getParserFactory()->getInstance()->preSaveTransform(
100 $text,
101 $pstParams->getPage(),
102 $pstParams->getUser(),
103 $parserOptions
104 );
105
106 $contentClass = $this->getContentClass();
107 return new $contentClass( $pst );
108 }
109
126 protected function fillParserOutput(
127 Content $content,
128 ContentParseParams $cpoParams,
129 ParserOutput &$output
130 ) {
131 $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get(
133 '@phan-var JavaScriptContent $content';
134 if ( in_array( $content->getModel(), $textModelsToParse ) ) {
135 // parse just to get links etc into the database, HTML is replaced below.
136 $output = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
137 ->parse(
138 $content->getText(),
139 $cpoParams->getPage(),
140 WikiPage::makeParserOptionsFromTitleAndModel(
141 $cpoParams->getPage(),
142 $content->getModel(),
143 'canonical'
144 ),
145 true,
146 true,
147 $cpoParams->getRevId()
148 );
149 }
150
151 if ( $cpoParams->getGenerateHtml() ) {
152 // Return JavaScript wrapped in a <pre> tag.
153 $html = Html::element(
154 'pre',
155 [ 'class' => 'mw-code mw-js', 'dir' => 'ltr' ],
156 "\n" . $content->getText() . "\n"
157 ) . "\n";
158 } else {
159 $html = null;
160 }
161
162 $output->clearWrapperDivClass();
163 $output->setRawText( $html );
164 // Suppress the TOC (T307691)
165 $output->setOutputFlag( ParserOutputFlags::NO_TOC );
166 $output->setSections( [] );
167 }
168}
170class_alias( JavaScriptContentHandler::class, 'JavaScriptContentHandler' );
const CONTENT_FORMAT_JAVASCRIPT
For JS pages.
Definition Defines.php:240
const PROTO_RELATIVE
Definition Defines.php:206
const CONTENT_MODEL_JAVASCRIPT
Definition Defines.php:223
Content handler for code content such as CSS, JavaScript, JSON, etc.
makeRedirectContent(Title $destination, $text='')
Create a redirect that is also valid JavaScript.
supportsRedirects()
Returns true if this content model supports redirects.
preSaveTransform(Content $content, PreSaveTransformParams $pstParams)
Returns a $content object with pre-save transformations applied (or the same object if no transformat...
__construct( $modelId=CONTENT_MODEL_JAVASCRIPT)
fillParserOutput(Content $content, ContentParseParams $cpoParams, ParserOutput &$output)
Fills the provided ParserOutput object with information derived from the content.
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
A class containing constants representing the names of configuration variables.
const TextModelsToParse
Name constant for the TextModelsToParse setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
ParserOutput is a rendering of a Content object or a message.
setOutputFlag(string $name, bool $val=true)
Provides a uniform interface to various boolean flags stored in the ParserOutput.
clearWrapperDivClass()
Clears the CSS class to use for the wrapping div, effectively disabling the wrapper div until addWrap...
setRawText(?string $text)
Set the raw text of the ParserOutput.
setSections(array $sectionArray)
Represents a title within MediaWiki.
Definition Title.php:79
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition Title.php:2134
Base representation for an editable wiki page.
Definition WikiPage.php:81
Base interface for representing page content.
Definition Content.php:37
getModel()
Returns the ID of the content model used by this Content object.
element(SerializerNode $parent, SerializerNode $node, $contents)