Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.22% covered (success)
97.22%
35 / 36
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DataScript
97.22% covered (success)
97.22%
35 / 36
50.00% covered (danger)
50.00%
1 / 2
12
0.00% covered (danger)
0.00%
0 / 1
 makeScript
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getFrontendConfiguraton
96.97% covered (success)
96.97%
32 / 33
0.00% covered (danger)
0.00%
0 / 1
11
1<?php
2/**
3 * ResourceLoader callback for ext.CodeMirror.data module
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23namespace MediaWiki\Extension\CodeMirror;
24
25use ExtensionRegistry;
26use MediaWiki\MediaWikiServices;
27use MediaWiki\ResourceLoader as RL;
28use MediaWiki\ResourceLoader\ResourceLoader;
29
30/**
31 * ResourceLoader callback for ext.CodeMirror.data
32 */
33class DataScript {
34    /**
35     * @param RL\Context $context
36     * @return string
37     */
38    public static function makeScript( RL\Context $context ) {
39        return ResourceLoader::makeConfigSetScript(
40                [ 'extCodeMirrorConfig' => self::getFrontendConfiguraton() ]
41            );
42    }
43
44    /**
45     * Returns an array of variables for CodeMirror to work (tags and so on)
46     *
47     * @return array
48     */
49    private static function getFrontendConfiguraton() {
50        // Use the content language, not the user language. (See T170130.)
51        $lang = MediaWikiServices::getInstance()->getContentLanguage();
52        $registry = ExtensionRegistry::getInstance();
53        $parser = MediaWikiServices::getInstance()->getParser();
54        $mwConfig = MediaWikiServices::getInstance()->getMainConfig();
55
56        $tagModes = $registry->getAttribute( 'CodeMirrorTagModes' );
57        $tagNames = array_merge( $parser->getTags(), array_keys( $tagModes ) );
58
59        // initialize configuration
60        $config = [
61            'lineNumberingNamespaces' => $mwConfig->get( 'CodeMirrorLineNumberingNamespaces' ),
62            'templateFoldingNamespaces' => $mwConfig->get( 'CodeMirrorTemplateFoldingNamespaces' ),
63            'pluginModules' => $registry->getAttribute( 'CodeMirrorPluginModules' ),
64            'tagModes' => $tagModes,
65            'tags' => array_fill_keys( $tagNames, true ),
66            'doubleUnderscore' => [ [], [] ],
67            'functionSynonyms' => $parser->getFunctionSynonyms(),
68            'urlProtocols' => $parser->getUrlProtocols(),
69            'linkTrailCharacters' => $lang->linkTrail(),
70        ];
71
72        $mw = $lang->getMagicWords();
73        $magicWordFactory = $parser->getMagicWordFactory();
74        foreach ( $magicWordFactory->getDoubleUnderscoreArray()->getNames() as $name ) {
75            if ( isset( $mw[$name] ) ) {
76                $caseSensitive = array_shift( $mw[$name] ) == 0 ? 0 : 1;
77                foreach ( $mw[$name] as $n ) {
78                    $n = $caseSensitive ? $n : $lang->lc( $n );
79                    $config['doubleUnderscore'][$caseSensitive][$n] = $name;
80                }
81            } else {
82                $config['doubleUnderscore'][0][] = $name;
83            }
84        }
85
86        foreach ( $magicWordFactory->getVariableIDs() as $name ) {
87            if ( isset( $mw[$name] ) ) {
88                $caseSensitive = array_shift( $mw[$name] ) == 0 ? 0 : 1;
89                foreach ( $mw[$name] as $n ) {
90                    $n = $caseSensitive ? $n : $lang->lc( $n );
91                    $config['functionSynonyms'][$caseSensitive][$n] = $name;
92                }
93            }
94        }
95
96        return $config;
97    }
98}