MediaWiki master
JavaScriptContentHandler.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Content;
22
31use WikiPage;
32
42
46 public function __construct( $modelId = CONTENT_MODEL_JAVASCRIPT ) {
47 parent::__construct( $modelId, [ CONTENT_FORMAT_JAVASCRIPT ] );
48 }
49
53 protected function getContentClass() {
54 return JavaScriptContent::class;
55 }
56
57 public function supportsRedirects() {
58 return true;
59 }
60
68 public function makeRedirectContent( Title $destination, $text = '' ) {
69 // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
70 $url = $destination->getFullURL( 'action=raw&ctype=text/javascript', false, PROTO_RELATIVE );
71 $class = $this->getContentClass();
72 // Don't needlessly encode ampersands in URLs (T107289).
73 // Avoid FormatJson or Html::encodeJsCall to ensure long-term byte-identical stability,
74 // as required for JavaScriptContent::getRedirectTarget validation.
75 $redirectContent = '/* #REDIRECT */mw.loader.load('
76 . json_encode( $url, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE )
77 . ');';
78 return new $class( $redirectContent );
79 }
80
81 public function preSaveTransform(
82 Content $content,
83 PreSaveTransformParams $pstParams
84 ): Content {
85 '@phan-var JavascriptContent $content';
86
87 $parserOptions = $pstParams->getParserOptions();
88 // @todo Make pre-save transformation optional for script pages (T34858)
90 if ( !$services->getUserOptionsLookup()->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) {
91 // Allow bot users to disable the pre-save transform for CSS/JS (T236828).
92 $parserOptions = clone $parserOptions;
93 $parserOptions->setPreSaveTransform( false );
94 }
95
96 $text = $content->getText();
97 $pst = $services->getParserFactory()->getInstance()->preSaveTransform(
98 $text,
99 $pstParams->getPage(),
100 $pstParams->getUser(),
101 $parserOptions
102 );
103
104 $contentClass = $this->getContentClass();
105 return new $contentClass( $pst );
106 }
107
124 protected function fillParserOutput(
125 Content $content,
126 ContentParseParams $cpoParams,
127 ParserOutput &$output
128 ) {
129 $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get(
131 '@phan-var JavaScriptContent $content';
132 if ( in_array( $content->getModel(), $textModelsToParse ) ) {
133 // parse just to get links etc into the database, HTML is replaced below.
134 $output = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
135 ->parse(
136 $content->getText(),
137 $cpoParams->getPage(),
138 WikiPage::makeParserOptionsFromTitleAndModel(
139 $cpoParams->getPage(),
140 $content->getModel(),
141 'canonical'
142 ),
143 true,
144 true,
145 $cpoParams->getRevId()
146 );
147 }
148
149 if ( $cpoParams->getGenerateHtml() ) {
150 // Return JavaScript wrapped in a <pre> tag.
151 $html = Html::element(
152 'pre',
153 [ 'class' => 'mw-code mw-js', 'dir' => 'ltr' ],
154 "\n" . $content->getText() . "\n"
155 ) . "\n";
156 } else {
157 $html = null;
158 }
159
160 $output->clearWrapperDivClass();
161 $output->setRawText( $html );
162 // Suppress the TOC (T307691)
163 $output->setOutputFlag( ParserOutputFlags::NO_TOC );
164 $output->setSections( [] );
165 }
166}
168class_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:78
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition Title.php:2130
Base representation for an editable wiki page.
Definition WikiPage.php:83
Base interface for representing page content.
Definition Content.php:39
getModel()
Returns the ID of the content model used by this Content object.
element(SerializerNode $parent, SerializerNode $node, $contents)