MediaWiki master
JavaScriptContentHandler.php
Go to the documentation of this file.
1<?php
29
39
43 public function __construct( $modelId = CONTENT_MODEL_JAVASCRIPT ) {
44 parent::__construct( $modelId, [ CONTENT_FORMAT_JAVASCRIPT ] );
45 }
46
50 protected function getContentClass() {
51 return JavaScriptContent::class;
52 }
53
54 public function supportsRedirects() {
55 return true;
56 }
57
65 public function makeRedirectContent( Title $destination, $text = '' ) {
66 // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
67 $url = $destination->getFullURL( 'action=raw&ctype=text/javascript', false, PROTO_RELATIVE );
68 $class = $this->getContentClass();
69 // Don't needlessly encode ampersands in URLs (T107289).
70 // Avoid FormatJson or Html::encodeJsCall to ensure long-term byte-identical stability,
71 // as required for JavaScriptContent::getRedirectTarget validation.
72 $redirectContent = '/* #REDIRECT */mw.loader.load('
73 . json_encode( $url, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE )
74 . ');';
75 return new $class( $redirectContent );
76 }
77
78 public function preSaveTransform(
79 Content $content,
80 PreSaveTransformParams $pstParams
81 ): Content {
82 '@phan-var JavascriptContent $content';
83
84 $parserOptions = $pstParams->getParserOptions();
85 // @todo Make pre-save transformation optional for script pages (T34858)
86 $services = MediaWikiServices::getInstance();
87 if ( !$services->getUserOptionsLookup()->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) {
88 // Allow bot users to disable the pre-save transform for CSS/JS (T236828).
89 $parserOptions = clone $parserOptions;
90 $parserOptions->setPreSaveTransform( false );
91 }
92
93 $text = $content->getText();
94 $pst = $services->getParserFactory()->getInstance()->preSaveTransform(
95 $text,
96 $pstParams->getPage(),
97 $pstParams->getUser(),
98 $parserOptions
99 );
100
101 $contentClass = $this->getContentClass();
102 return new $contentClass( $pst );
103 }
104
121 protected function fillParserOutput(
122 Content $content,
123 ContentParseParams $cpoParams,
124 ParserOutput &$output
125 ) {
126 $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get(
127 MainConfigNames::TextModelsToParse );
128 '@phan-var JavaScriptContent $content';
129 if ( in_array( $content->getModel(), $textModelsToParse ) ) {
130 // parse just to get links etc into the database, HTML is replaced below.
131 $output = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
132 ->parse(
133 $content->getText(),
134 $cpoParams->getPage(),
135 WikiPage::makeParserOptionsFromTitleAndModel(
136 $cpoParams->getPage(),
137 $content->getModel(),
138 'canonical'
139 ),
140 true,
141 true,
142 $cpoParams->getRevId()
143 );
144 }
145
146 if ( $cpoParams->getGenerateHtml() ) {
147 // Return JavaScript wrapped in a <pre> tag.
148 $html = Html::element(
149 'pre',
150 [ 'class' => 'mw-code mw-js', 'dir' => 'ltr' ],
151 "\n" . $content->getText() . "\n"
152 ) . "\n";
153 } else {
154 $html = null;
155 }
156
157 $output->clearWrapperDivClass();
158 $output->setRawText( $html );
159 // Suppress the TOC (T307691)
160 $output->setOutputFlag( ParserOutputFlags::NO_TOC );
161 $output->setSections( [] );
162 }
163}
const CONTENT_FORMAT_JAVASCRIPT
For JS pages.
Definition Defines.php:236
const PROTO_RELATIVE
Definition Defines.php:202
const CONTENT_MODEL_JAVASCRIPT
Definition Defines.php:219
Content handler for code content such as CSS, JavaScript, JSON, etc.
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.
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.
Service locator for MediaWiki core services.
Rendered output of a wiki page, as parsed from wikitext.
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:2133
Base interface for representing page content.
Definition Content.php:37
getModel()
Returns the ID of the content model used by this Content object.