Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
66.67% |
34 / 51 |
|
50.00% |
3 / 6 |
CRAP | |
0.00% |
0 / 1 |
JavaScriptContentHandler | |
68.00% |
34 / 50 |
|
50.00% |
3 / 6 |
11.65 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getContentClass | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
supportsRedirects | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
makeRedirectContent | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
preSaveTransform | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
fillParserOutput | |
96.30% |
26 / 27 |
|
0.00% |
0 / 1 |
3 |
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 | |
21 | namespace MediaWiki\Content; |
22 | |
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; |
32 | |
33 | /** |
34 | * Content handler for JavaScript pages. |
35 | * |
36 | * @todo Create a ScriptContentHandler base class, do highlighting stuff there? |
37 | * |
38 | * @since 1.21 |
39 | * @ingroup Content |
40 | */ |
41 | class JavaScriptContentHandler extends CodeContentHandler { |
42 | |
43 | /** |
44 | * @param string $modelId |
45 | */ |
46 | public function __construct( $modelId = CONTENT_MODEL_JAVASCRIPT ) { |
47 | parent::__construct( $modelId, [ CONTENT_FORMAT_JAVASCRIPT ] ); |
48 | } |
49 | |
50 | /** |
51 | * @return class-string<JavaScriptContent> |
52 | */ |
53 | protected function getContentClass() { |
54 | return JavaScriptContent::class; |
55 | } |
56 | |
57 | public function supportsRedirects() { |
58 | return true; |
59 | } |
60 | |
61 | /** |
62 | * Create a redirect that is also valid JavaScript |
63 | * |
64 | * @param Title $destination |
65 | * @param string $text ignored |
66 | * @return JavaScriptContent |
67 | */ |
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 ); |
79 | } |
80 | |
81 | public function preSaveTransform( |
82 | Content $content, |
83 | PreSaveTransformParams $pstParams |
84 | ): Content { |
85 | '@phan-var JavascriptContent $content'; |
86 | |
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 ); |
94 | } |
95 | |
96 | $text = $content->getText(); |
97 | $pst = $services->getParserFactory()->getInstance()->preSaveTransform( |
98 | $text, |
99 | $pstParams->getPage(), |
100 | $pstParams->getUser(), |
101 | $parserOptions |
102 | ); |
103 | |
104 | $contentClass = $this->getContentClass(); |
105 | return new $contentClass( $pst ); |
106 | } |
107 | |
108 | /** |
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. |
111 | * |
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. |
114 | * |
115 | * Subclasses may override this to provide custom content processing.. |
116 | * |
117 | * @stable to override |
118 | * |
119 | * @since 1.38 |
120 | * @param Content $content |
121 | * @param ContentParseParams $cpoParams |
122 | * @param ParserOutput &$output The output object to fill (reference). |
123 | */ |
124 | protected function fillParserOutput( |
125 | Content $content, |
126 | ContentParseParams $cpoParams, |
127 | ParserOutput &$output |
128 | ) { |
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' |
142 | ), |
143 | true, |
144 | true, |
145 | $cpoParams->getRevId() |
146 | ); |
147 | } |
148 | |
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; |
158 | } |
159 | |
160 | $output->clearWrapperDivClass(); |
161 | $output->setRawText( $html ); |
162 | // Suppress the TOC (T307691) |
163 | $output->setOutputFlag( ParserOutputFlags::NO_TOC ); |
164 | $output->setSections( [] ); |
165 | } |
166 | } |
167 | /** @deprecated class alias since 1.43 */ |
168 | class_alias( JavaScriptContentHandler::class, 'JavaScriptContentHandler' ); |