MediaWiki master
JavaScriptContentHandler.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Content;
8
20
30
31 private array $textModelsToParse;
32 private ParserFactory $parserFactory;
33 private UserOptionsLookup $userOptionsLookup;
34
35 public function __construct(
36 string $modelId,
37 Config $config,
38 ParserFactory $parserFactory,
39 UserOptionsLookup $userOptionsLookup
40 ) {
41 parent::__construct( $modelId, [ CONTENT_FORMAT_JAVASCRIPT ] );
42 $this->textModelsToParse = $config->get( MainConfigNames::TextModelsToParse ) ?? [];
43 $this->parserFactory = $parserFactory;
44 $this->userOptionsLookup = $userOptionsLookup;
45 }
46
50 protected function getContentClass() {
51 return JavaScriptContent::class;
52 }
53
55 public function supportsRedirects() {
56 return true;
57 }
58
66 public function makeRedirectContent( Title $destination, $text = '' ) {
67 // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
68 $url = $destination->getFullURL( 'action=raw&ctype=text/javascript', false, PROTO_RELATIVE );
69 $class = $this->getContentClass();
70 // Don't needlessly encode ampersands in URLs (T107289).
71 // Avoid FormatJson or Html::encodeJsCall to ensure long-term byte-identical stability,
72 // as required for JavaScriptContent::getRedirectTarget validation.
73 $redirectContent = '/* #REDIRECT */mw.loader.load('
74 . json_encode( $url, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE )
75 . ');';
76 return new $class( $redirectContent );
77 }
78
79 public function preSaveTransform(
80 Content $content,
81 PreSaveTransformParams $pstParams
82 ): Content {
83 '@phan-var JavascriptContent $content';
84
85 $parserOptions = $pstParams->getParserOptions();
86 // @todo Make pre-save transformation optional for script pages (T34858)
87 if ( !$this->userOptionsLookup->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 = $this->parserFactory->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 '@phan-var JavaScriptContent $content';
127 if ( in_array( $content->getModel(), $this->textModelsToParse ) ) {
128 // parse just to get links etc into the database, HTML is replaced below.
129 $output = $this->parserFactory->getInstance()
130 ->parse(
131 $content->getText(),
132 $cpoParams->getPage(),
133 WikiPage::makeParserOptionsFromTitleAndModel(
134 $cpoParams->getPage(),
135 $content->getModel(),
136 'canonical'
137 ),
138 true,
139 true,
140 $cpoParams->getRevId()
141 );
142 }
143
144 if ( $cpoParams->getGenerateHtml() ) {
145 // Return JavaScript wrapped in a <pre> tag.
146 $html = Html::element(
147 'pre',
148 [ 'class' => [ 'mw-code', 'mw-js' ], 'dir' => 'ltr' ],
149 "\n" . $content->getText() . "\n"
150 ) . "\n";
151 } else {
152 $html = null;
153 }
154
155 $output->clearWrapperDivClass();
156 $output->setRawText( $html );
157 // Suppress the TOC (T307691)
158 $output->setOutputFlag( ParserOutputFlags::NO_TOC );
159 $output->setSections( [] );
160 }
161}
163class_alias( JavaScriptContentHandler::class, 'JavaScriptContentHandler' );
const CONTENT_FORMAT_JAVASCRIPT
For JS pages.
Definition Defines.php:255
const PROTO_RELATIVE
Definition Defines.php:219
Content handler for pages with source code as content (e.g.
makeRedirectContent(Title $destination, $text='')
Create a redirect that is also valid JavaScript.
supportsRedirects()
Returns true if this content model supports redirects.This default implementation returns false....
preSaveTransform(Content $content, PreSaveTransformParams $pstParams)
Returns a $content object with pre-save transformations applied (or the same object if no transformat...
__construct(string $modelId, Config $config, ParserFactory $parserFactory, UserOptionsLookup $userOptionsLookup)
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:43
A class containing constants representing the names of configuration variables.
const TextModelsToParse
Name constant for the TextModelsToParse setting, for use with Config::get()
Base representation for an editable wiki page.
Definition WikiPage.php:82
ParserOutput is a rendering of a Content object or a message.
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)
setOutputFlag(ParserOutputFlags|string $name, bool $val=true)
Provides a uniform interface to various boolean flags stored in the ParserOutput.
Represents a title within MediaWiki.
Definition Title.php:69
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition Title.php:2152
Provides access to user options.
Interface for configuration instances.
Definition Config.php:18
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
Content objects represent page content, e.g.
Definition Content.php:28
getModel()
Get the content model ID.
element(SerializerNode $parent, SerializerNode $node, $contents)