Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / content / JavaScriptContentHandler.php
blobcbb3a2462f20d2b9f66b22a8304e0ef849851e23
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 namespace MediaWiki\Content;
23 use MediaWiki\Content\Renderer\ContentParseParams;
24 use MediaWiki\Content\Transform\PreSaveTransformParams;
25 use MediaWiki\Html\Html;
26 use MediaWiki\MainConfigNames;
27 use MediaWiki\MediaWikiServices;
28 use MediaWiki\Parser\ParserOutput;
29 use MediaWiki\Parser\ParserOutputFlags;
30 use MediaWiki\Title\Title;
31 use WikiPage;
33 /**
34 * Content handler for JavaScript pages.
36 * @todo Create a ScriptContentHandler base class, do highlighting stuff there?
38 * @since 1.21
39 * @ingroup Content
41 class JavaScriptContentHandler extends CodeContentHandler {
43 /**
44 * @param string $modelId
46 public function __construct( $modelId = CONTENT_MODEL_JAVASCRIPT ) {
47 parent::__construct( $modelId, [ CONTENT_FORMAT_JAVASCRIPT ] );
50 /**
51 * @return class-string<JavaScriptContent>
53 protected function getContentClass() {
54 return JavaScriptContent::class;
57 public function supportsRedirects() {
58 return true;
61 /**
62 * Create a redirect that is also valid JavaScript
64 * @param Title $destination
65 * @param string $text ignored
66 * @return JavaScriptContent
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 );
81 public function preSaveTransform(
82 Content $content,
83 PreSaveTransformParams $pstParams
84 ): Content {
85 '@phan-var JavascriptContent $content';
87 $parserOptions = $pstParams->getParserOptions();
88 // @todo Make pre-save transformation optional for script pages (T34858)
89 $services = MediaWikiServices::getInstance();
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 );
96 $text = $content->getText();
97 $pst = $services->getParserFactory()->getInstance()->preSaveTransform(
98 $text,
99 $pstParams->getPage(),
100 $pstParams->getUser(),
101 $parserOptions
104 $contentClass = $this->getContentClass();
105 return new $contentClass( $pst );
109 * Fills the provided ParserOutput object with information derived from the content.
110 * Unless $cpo->getGenerateHtml was false, this includes an HTML representation of the content.
112 * For content models listed in $wgTextModelsToParse, this method will call the MediaWiki
113 * wikitext parser on the text to extract any (wikitext) links, magic words, etc.
115 * Subclasses may override this to provide custom content processing..
117 * @stable to override
119 * @since 1.38
120 * @param Content $content
121 * @param ContentParseParams $cpoParams
122 * @param ParserOutput &$output The output object to fill (reference).
124 protected function fillParserOutput(
125 Content $content,
126 ContentParseParams $cpoParams,
127 ParserOutput &$output
129 $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get(
130 MainConfigNames::TextModelsToParse );
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'
143 true,
144 true,
145 $cpoParams->getRevId()
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;
160 $output->clearWrapperDivClass();
161 $output->setRawText( $html );
162 // Suppress the TOC (T307691)
163 $output->setOutputFlag( ParserOutputFlags::NO_TOC );
164 $output->setSections( [] );
167 /** @deprecated class alias since 1.43 */
168 class_alias( JavaScriptContentHandler::class, 'JavaScriptContentHandler' );