Debian Codegraph @

includes/DataScript.php

raw
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
23 namespace MediaWiki\Extension\CodeMirror;
24
25 use MediaWiki\MediaWikiServices;
26 use MediaWiki\Registration\ExtensionRegistry;
27 use MediaWiki\ResourceLoader as RL;
28 use MediaWiki\ResourceLoader\ResourceLoader;
29
30 /**
31 * ResourceLoader callback for ext.CodeMirror.data
32 */
33 class 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 'useV6' => $mwConfig->get( 'CodeMirrorV6' ),
62 'lineNumberingNamespaces' => $mwConfig->get( 'CodeMirrorLineNumberingNamespaces' ),
63 'templateFoldingNamespaces' => $mwConfig->get( 'CodeMirrorTemplateFoldingNamespaces' ),
64 'pluginModules' => $registry->getAttribute( 'CodeMirrorPluginModules' ),
65 'tagModes' => $tagModes,
66 'tags' => array_fill_keys( $tagNames, true ),
67 'doubleUnderscore' => [ [], [] ],
68 'functionSynonyms' => $parser->getFunctionSynonyms(),
69 'urlProtocols' => $parser->getUrlProtocols(),
70 'linkTrailCharacters' => $lang->linkTrail(),
71 ];
72
73 $mw = $lang->getMagicWords();
74 $magicWordFactory = $parser->getMagicWordFactory();
75 foreach ( $magicWordFactory->getDoubleUnderscoreArray()->getNames() as $name ) {
76 if ( isset( $mw[$name] ) ) {
77 $caseSensitive = array_shift( $mw[$name] ) == 0 ? 0 : 1;
78 foreach ( $mw[$name] as $n ) {
79 $n = $caseSensitive ? $n : $lang->lc( $n );
80 $config['doubleUnderscore'][$caseSensitive][$n] = $name;
81 }
82 } else {
83 $config['doubleUnderscore'][0][] = $name;
84 }
85 }
86
87 foreach ( $magicWordFactory->getVariableIDs() as $name ) {
88 if ( isset( $mw[$name] ) ) {
89 $caseSensitive = array_shift( $mw[$name] ) == 0 ? 0 : 1;
90 foreach ( $mw[$name] as $n ) {
91 $n = $caseSensitive ? $n : $lang->lc( $n );
92 $config['functionSynonyms'][$caseSensitive][$n] = $name;
93 }
94 }
95 }
96
97 return $config;
98 }
99 }