MediaWiki REL1_39
JavaScriptContentHandler.php
Go to the documentation of this file.
1<?php
25
35
39 public function __construct( $modelId = CONTENT_MODEL_JAVASCRIPT ) {
40 parent::__construct( $modelId, [ CONTENT_FORMAT_JAVASCRIPT ] );
41 }
42
46 protected function getContentClass() {
47 return JavaScriptContent::class;
48 }
49
50 public function supportsRedirects() {
51 return true;
52 }
53
61 public function makeRedirectContent( Title $destination, $text = '' ) {
62 // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
63 $url = $destination->getFullURL( 'action=raw&ctype=text/javascript', false, PROTO_RELATIVE );
64 $class = $this->getContentClass();
65 return new $class( '/* #REDIRECT */' . Xml::encodeJsCall( 'mw.loader.load', [ $url ] ) );
66 }
67
68 public function preSaveTransform(
70 PreSaveTransformParams $pstParams
71 ): Content {
72 $shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
74 $pstParams
75 );
76
77 if ( $shouldCallDeprecatedMethod ) {
78 return $this->callDeprecatedContentPST(
80 $pstParams
81 );
82 }
83
84 '@phan-var JavascriptContent $content';
85
86 $parserOptions = $pstParams->getParserOptions();
87 // @todo Make pre-save transformation optional for script pages (T34858)
88 $services = MediaWikiServices::getInstance();
89 if ( !$services->getUserOptionsLookup()->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) {
90 // Allow bot users to disable the pre-save transform for CSS/JS (T236828).
91 $parserOptions = clone $parserOptions;
92 $parserOptions->setPreSaveTransform( false );
93 }
94
95 $text = $content->getText();
96 $pst = $services->getParserFactory()->getInstance()->preSaveTransform(
97 $text,
98 $pstParams->getPage(),
99 $pstParams->getUser(),
100 $parserOptions
101 );
102
103 $contentClass = $this->getContentClass();
104 return new $contentClass( $pst );
105 }
106
123 protected function fillParserOutput(
125 ContentParseParams $cpoParams,
126 ParserOutput &$output
127 ) {
128 $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get(
129 MainConfigNames::TextModelsToParse );
130 '@phan-var JavaScriptContent $content';
131 if ( in_array( $content->getModel(), $textModelsToParse ) ) {
132 // parse just to get links etc into the database, HTML is replaced below.
133 $output = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
134 ->parse(
135 $content->getText(),
136 $cpoParams->getPage(),
137 WikiPage::makeParserOptionsFromTitleAndModel(
138 $cpoParams->getPage(),
139 $content->getModel(),
140 'canonical'
141 ),
142 true,
143 true,
144 $cpoParams->getRevId()
145 );
146 }
147
148 if ( $cpoParams->getGenerateHtml() ) {
149 // Return JavaScript wrapped in a <pre> tag.
150 $html = Html::element(
151 'pre',
152 [ 'class' => 'mw-code mw-js', 'dir' => 'ltr' ],
153 "\n" . $content->getText() . "\n"
154 ) . "\n";
155 } else {
156 $html = null;
157 }
158
159 $output->clearWrapperDivClass();
160 $output->setText( $html );
161 }
162}
const CONTENT_FORMAT_JAVASCRIPT
For JS pages.
Definition Defines.php:229
const PROTO_RELATIVE
Definition Defines.php:195
const CONTENT_MODEL_JAVASCRIPT
Definition Defines.php:212
Content handler for code content such as CSS, JavaScript, JSON, etc.
shouldCallDeprecatedContentTransformMethod(Content $content, $params)
Check if we need to provide content overrides deprecated Content method.
callDeprecatedContentPST(Content $content, PreSaveTransformParams $params)
Provided content overrides deprecated Content::preSaveTransform, call it and return.
Content handler for JavaScript pages.
__construct( $modelId=CONTENT_MODEL_JAVASCRIPT)
fillParserOutput(Content $content, ContentParseParams $cpoParams, ParserOutput &$output)
Fills the provided ParserOutput object with information derived from the content.
preSaveTransform(Content $content, PreSaveTransformParams $pstParams)
Returns a $content object with pre-save transformations applied (or the same object if no transformat...
makeRedirectContent(Title $destination, $text='')
Create a redirect that is also valid JavaScript.
supportsRedirects()
Returns true if this content model supports redirects.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
clearWrapperDivClass()
Clears the CSS class to use for the wrapping div, effectively disabling the wrapper div until addWrap...
setText( $text)
Set the text of the ParserOutput.
Represents a title within MediaWiki.
Definition Title.php:49
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition Title.php:2180
Base interface for content objects.
Definition Content.php:35
$content
Definition router.php:76