MediaWiki master
JavaScriptContentHandler.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Content;
8
19
29
30 private readonly array $textModelsToParse;
31
32 public function __construct(
33 string $modelId,
34 Config $config,
35 private readonly ParserFactory $parserFactory,
36 private readonly UserOptionsLookup $userOptionsLookup,
37 private readonly CodeHighlighter $codeHighlighter,
38 ) {
39 parent::__construct( $modelId, [ CONTENT_FORMAT_JAVASCRIPT ] );
40 $this->textModelsToParse = $config->get( MainConfigNames::TextModelsToParse ) ?? [];
41 }
42
46 protected function getContentClass() {
47 return JavaScriptContent::class;
48 }
49
51 public function supportsRedirects() {
52 return true;
53 }
54
62 public function makeRedirectContent( Title $destination, $text = '' ) {
63 // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
64 $url = $destination->getFullURL( 'action=raw&ctype=text/javascript', false, PROTO_RELATIVE );
65 $class = $this->getContentClass();
66 // Don't needlessly encode ampersands in URLs (T107289).
67 // Avoid FormatJson or Html::encodeJsCall to ensure long-term byte-identical stability,
68 // as required for JavaScriptContent::getRedirectTarget validation.
69 $redirectContent = '/* #REDIRECT */mw.loader.load('
70 . json_encode( $url, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE )
71 . ');';
72 return new $class( $redirectContent );
73 }
74
75 public function preSaveTransform(
76 Content $content,
77 PreSaveTransformParams $pstParams
78 ): Content {
79 '@phan-var JavascriptContent $content';
80
81 $parserOptions = $pstParams->getParserOptions();
82 // @todo Make pre-save transformation optional for script pages (T34858)
83 if ( !$this->userOptionsLookup->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) {
84 // Allow bot users to disable the pre-save transform for CSS/JS (T236828).
85 $parserOptions = clone $parserOptions;
86 $parserOptions->setPreSaveTransform( false );
87 }
88
89 $text = $content->getText();
90 $pst = $this->parserFactory->getInstance()->preSaveTransform(
91 $text,
92 $pstParams->getPage(),
93 $pstParams->getUser(),
94 $parserOptions
95 );
96
97 $contentClass = $this->getContentClass();
98 return new $contentClass( $pst );
99 }
100
117 protected function fillParserOutput(
118 Content $content,
119 ContentParseParams $cpoParams,
120 ParserOutput &$output
121 ) {
122 '@phan-var JavaScriptContent $content';
123 if ( in_array( $content->getModel(), $this->textModelsToParse ) ) {
124 // parse just to get links etc into the database, HTML is replaced below.
125 $output = $this->parserFactory->getInstance()
126 ->parse(
127 $content->getText(),
128 $cpoParams->getPage(),
129 WikiPage::makeParserOptionsFromTitleAndModel(
130 $cpoParams->getPage(),
131 $content->getModel(),
132 'canonical'
133 ),
134 true,
135 true,
136 $cpoParams->getRevId()
137 );
138 }
139
140 if ( $cpoParams->getGenerateHtml() ) {
141 $highlightOutput = $this->codeHighlighter->highlight( $content->getText(), new CodeHighlighterOptions(
142 language: 'javascript',
143 classes: [ 'mw-js' ],
144 includeLineNumbers: true,
145 includeLineLinks: true,
146 ) );
147 $html = $highlightOutput->getHtml();
148 $highlightOutput->getMetadata()->addToParserOutput( $output );
149 } else {
150 $html = null;
151 }
152
153 $output->clearWrapperDivClass();
154 $output->setContentHolderText( $html );
155 // Suppress the TOC (T307691)
156 $output->setOutputFlag( ParserOutputFlags::NO_TOC );
157 $output->setSections( [] );
158 }
159}
161class_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.
Service for syntax highlighting.
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, private readonly ParserFactory $parserFactory, private readonly UserOptionsLookup $userOptionsLookup, private readonly CodeHighlighter $codeHighlighter,)
fillParserOutput(Content $content, ContentParseParams $cpoParams, ParserOutput &$output)
Fills the provided ParserOutput object with information derived from the content.
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:83
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...
setContentHolderText(?string $text)
Sets the body fragment 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:2153
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.