From 0f6d8818be27bdb534a7ebd037fd27a1e2ac63bf Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Thu, 3 Aug 2017 17:48:59 -0700 Subject: [PATCH] Move JS config vars from page HTML into RL module Bug: T172458 Change-Id: I60bfab0028a21c3942508f5ad6377df9a285c7de --- CodeMirror.hooks.php | 78 --------------------- ResourceLoaderCodeMirrorModule.php | 109 +++++++++++++++++++++++++++++ extension.json | 7 +- phpcs.xml | 1 + 4 files changed, 113 insertions(+), 82 deletions(-) create mode 100644 ResourceLoaderCodeMirrorModule.php 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 83621133..15ab20e8 100644 --- a/extension.json +++ b/extension.json @@ -18,13 +18,15 @@ ] }, "AutoloadClasses": { - "CodeMirrorHooks": "CodeMirror.hooks.php" + "CodeMirrorHooks": "CodeMirror.hooks.php", + "ResourceLoaderCodeMirrorModule": "ResourceLoaderCodeMirrorModule.php" }, "ResourceModules": { "ext.CodeMirror.loader": { "loaderScripts": "ext.CodeMirror.loader.js" }, "ext.CodeMirror": { + "class": "ResourceLoaderCodeMirrorModule", "dependencies": [ "ext.CodeMirror.lib", "ext.CodeMirror.mode.mediawiki", @@ -144,9 +146,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 @@ + .