Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
12.00% covered (danger)
12.00%
6 / 50
16.67% covered (danger)
16.67%
1 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
JavaScriptContentHandler
12.00% covered (danger)
12.00%
6 / 50
16.67% covered (danger)
16.67%
1 / 6
64.20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getContentClass
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 supportsRedirects
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 makeRedirectContent
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 preSaveTransform
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
6
 fillParserOutput
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
12
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21use MediaWiki\Content\Renderer\ContentParseParams;
22use MediaWiki\Content\Transform\PreSaveTransformParams;
23use MediaWiki\Html\Html;
24use MediaWiki\MainConfigNames;
25use MediaWiki\MediaWikiServices;
26use MediaWiki\Parser\ParserOutput;
27use MediaWiki\Parser\ParserOutputFlags;
28use MediaWiki\Title\Title;
29
30/**
31 * Content handler for JavaScript pages.
32 *
33 * @todo Create a ScriptContentHandler base class, do highlighting stuff there?
34 *
35 * @since 1.21
36 * @ingroup Content
37 */
38class JavaScriptContentHandler extends CodeContentHandler {
39
40    /**
41     * @param string $modelId
42     */
43    public function __construct( $modelId = CONTENT_MODEL_JAVASCRIPT ) {
44        parent::__construct( $modelId, [ CONTENT_FORMAT_JAVASCRIPT ] );
45    }
46
47    /**
48     * @return class-string<JavaScriptContent>
49     */
50    protected function getContentClass() {
51        return JavaScriptContent::class;
52    }
53
54    public function supportsRedirects() {
55        return true;
56    }
57
58    /**
59     * Create a redirect that is also valid JavaScript
60     *
61     * @param Title $destination
62     * @param string $text ignored
63     * @return JavaScriptContent
64     */
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
105    /**
106     * Fills the provided ParserOutput object with information derived from the content.
107     * Unless $cpo->getGenerateHtml was false, this includes an HTML representation of the content.
108     *
109     * For content models listed in $wgTextModelsToParse, this method will call the MediaWiki
110     * wikitext parser on the text to extract any (wikitext) links, magic words, etc.
111     *
112     * Subclasses may override this to provide custom content processing..
113     *
114     * @stable to override
115     *
116     * @since 1.38
117     * @param Content $content
118     * @param ContentParseParams $cpoParams
119     * @param ParserOutput &$output The output object to fill (reference).
120     */
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}