diff --git a/CodeMirror.hooks.php b/CodeMirror.hooks.php index 1bd9c404..90c3effc 100644 --- a/CodeMirror.hooks.php +++ b/CodeMirror.hooks.php @@ -31,84 +31,6 @@ class CodeMirrorHooks { return $isEnabled; } - /** - * Returns an array of variables for CodeMirror to work (tags and so on) - * - * @global Parser $wgParser - * @global Language $wgContLang - * @staticvar array $config Cached version of configuration - * @return array - */ - public static function getConfiguraton() { - global $wgParser, $wgContLang; - static $config = []; - - // if we already created these variable array, return it - if ( !$config ) { - // Use the content language, not the user language. (See T170130.) - $lang = $wgContLang; - $registry = ExtensionRegistry::getInstance(); - - if ( !isset( $wgParser->mFunctionSynonyms ) ) { - $wgParser->initialiseVariables(); - $wgParser->firstCallInit(); - } - - // initialize configuration - $config = [ - 'pluginModules' => $registry->getAttribute( 'CodeMirrorPluginModules' ), - 'tagModes' => $registry->getAttribute( 'CodeMirrorTagModes' ), - 'tags' => array_fill_keys( $wgParser->getTags(), true ), - 'doubleUnderscore' => [ [], [] ], - 'functionSynonyms' => $wgParser->mFunctionSynonyms, - 'urlProtocols' => $wgParser->mUrlProtocols, - 'linkTrailCharacters' => $lang->linkTrail(), - ]; - - $mw = $lang->getMagicWords(); - foreach ( MagicWord::getDoubleUnderscoreArray()->names as $name ) { - if ( isset( $mw[$name] ) ) { - $caseSensitive = array_shift( $mw[$name] ) == 0 ? 0 : 1; - foreach ( $mw[$name] as $n ) { - $n = $caseSensitive ? $n : $lang->lc( $n ); - $config['doubleUnderscore'][$caseSensitive][$n] = $name; - } - } else { - $config['doubleUnderscore'][0][] = $name; - } - } - - foreach ( MagicWord::getVariableIDs() as $name ) { - if ( isset( $mw[$name] ) ) { - $caseSensitive = array_shift( $mw[$name] ) == 0 ? 0 : 1; - foreach ( $mw[$name] as $n ) { - $n = $caseSensitive ? $n : $lang->lc( $n ); - $config['functionSynonyms'][$caseSensitive][$n] = $name; - } - } - } - - } - - return $config; - } - - /** - * MakeGlobalVariablesScript hook handler - * - * @see https://www.mediawiki.org/wiki/Manual:Hooks/MakeGlobalVariablesScript - * - * @param array $vars - * @param OutputPage $out - */ - public static function onMakeGlobalVariablesScript( array &$vars, OutputPage $out ) { - $context = $out->getContext(); - // add CodeMirror vars on edit pages, or if VE is installed - if ( self::isCodeMirrorEnabled( $context ) || class_exists( 'VisualEditorHooks' ) ) { - $vars['extCodeMirrorConfig'] = self::getConfiguraton(); - } - } - /** * BeforePageDisplay hook handler * diff --git a/ResourceLoaderCodeMirrorModule.php b/ResourceLoaderCodeMirrorModule.php new file mode 100644 index 00000000..457d5cdb --- /dev/null +++ b/ResourceLoaderCodeMirrorModule.php @@ -0,0 +1,109 @@ + $this->getFrontendConfiguraton() ] + ) + . "\n" + . parent::getScript( $context ); + } + + /** + * @inheritdoc + */ + public function supportsURLLoading() { + // This module does not support loading URLs, because it inserts + // JS config vars into the module by the getScript function. + return false; + } + + /** + * @inheritdoc + */ + public function enableModuleContentVersion() { + return true; + } + + /** + * Returns an array of variables for CodeMirror to work (tags and so on) + * + * @global Parser $wgParser + * @global Language $wgContLang + * @return array + */ + private function getFrontendConfiguraton() { + global $wgParser, $wgContLang; + + // Use the content language, not the user language. (See T170130.) + $lang = $wgContLang; + $registry = ExtensionRegistry::getInstance(); + + if ( !isset( $wgParser->mFunctionSynonyms ) ) { + $wgParser->initialiseVariables(); + $wgParser->firstCallInit(); + } + + // initialize configuration + $config = [ + 'pluginModules' => $registry->getAttribute( 'CodeMirrorPluginModules' ), + 'tagModes' => $registry->getAttribute( 'CodeMirrorTagModes' ), + 'tags' => array_fill_keys( $wgParser->getTags(), true ), + 'doubleUnderscore' => [ [], [] ], + 'functionSynonyms' => $wgParser->mFunctionSynonyms, + 'urlProtocols' => $wgParser->mUrlProtocols, + 'linkTrailCharacters' => $lang->linkTrail(), + ]; + + $mw = $lang->getMagicWords(); + foreach ( MagicWord::getDoubleUnderscoreArray()->names as $name ) { + if ( isset( $mw[$name] ) ) { + $caseSensitive = array_shift( $mw[$name] ) == 0 ? 0 : 1; + foreach ( $mw[$name] as $n ) { + $n = $caseSensitive ? $n : $lang->lc( $n ); + $config['doubleUnderscore'][$caseSensitive][$n] = $name; + } + } else { + $config['doubleUnderscore'][0][] = $name; + } + } + + foreach ( MagicWord::getVariableIDs() as $name ) { + if ( isset( $mw[$name] ) ) { + $caseSensitive = array_shift( $mw[$name] ) == 0 ? 0 : 1; + foreach ( $mw[$name] as $n ) { + $n = $caseSensitive ? $n : $lang->lc( $n ); + $config['functionSynonyms'][$caseSensitive][$n] = $name; + } + } + } + + return $config; + } +} diff --git a/extension.json b/extension.json index 35c5daed..70033f32 100644 --- a/extension.json +++ b/extension.json @@ -18,10 +18,12 @@ ] }, "AutoloadClasses": { - "CodeMirrorHooks": "CodeMirror.hooks.php" + "CodeMirrorHooks": "CodeMirror.hooks.php", + "ResourceLoaderCodeMirrorModule": "ResourceLoaderCodeMirrorModule.php" }, "ResourceModules": { "ext.CodeMirror": { + "class": "ResourceLoaderCodeMirrorModule", "dependencies": [ "ext.CodeMirror.lib", "ext.CodeMirror.mode.mediawiki", @@ -141,9 +143,6 @@ "remoteExtPath": "CodeMirror/resources" }, "Hooks": { - "MakeGlobalVariablesScript": [ - "CodeMirrorHooks::onMakeGlobalVariablesScript" - ], "BeforePageDisplay": [ "CodeMirrorHooks::onBeforePageDisplay" ], diff --git a/phpcs.xml b/phpcs.xml index df8c2b5a..b0b9e1c6 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -4,6 +4,7 @@ + .