2017-08-04 00:48:59 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ResourceLoader ext.CodeMirror module
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
2022-02-06 15:13:05 +00:00
|
|
|
namespace MediaWiki\Extension\CodeMirror;
|
|
|
|
|
|
|
|
use ExtensionRegistry;
|
2018-08-04 22:29:24 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-05-20 02:11:31 +00:00
|
|
|
use MediaWiki\ResourceLoader as RL;
|
|
|
|
use MediaWiki\ResourceLoader\ResourceLoader;
|
2018-08-04 22:29:24 +00:00
|
|
|
|
2017-08-04 00:48:59 +00:00
|
|
|
/**
|
|
|
|
* ResourceLoader module for ext.CodeMirror
|
|
|
|
*/
|
2022-05-20 02:11:31 +00:00
|
|
|
class ResourceLoaderCodeMirrorModule extends RL\FileModule {
|
2017-08-04 00:48:59 +00:00
|
|
|
/**
|
2017-09-09 16:52:39 +00:00
|
|
|
* @inheritDoc
|
2017-08-04 00:48:59 +00:00
|
|
|
*/
|
2022-05-20 02:11:31 +00:00
|
|
|
public function getScript( RL\Context $context ) {
|
2017-08-04 00:48:59 +00:00
|
|
|
return ResourceLoader::makeConfigSetScript(
|
|
|
|
[ 'extCodeMirrorConfig' => $this->getFrontendConfiguraton() ]
|
|
|
|
)
|
2017-08-21 18:41:18 +00:00
|
|
|
. "\n"
|
|
|
|
. parent::getScript( $context );
|
2017-08-04 00:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-09 16:52:39 +00:00
|
|
|
* @inheritDoc
|
2017-08-04 00:48:59 +00:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-09 16:52:39 +00:00
|
|
|
* @inheritDoc
|
2017-08-04 00:48:59 +00:00
|
|
|
*/
|
|
|
|
public function enableModuleContentVersion() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of variables for CodeMirror to work (tags and so on)
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getFrontendConfiguraton() {
|
|
|
|
// Use the content language, not the user language. (See T170130.)
|
2018-08-04 22:29:24 +00:00
|
|
|
$lang = MediaWikiServices::getInstance()->getContentLanguage();
|
2017-08-04 00:48:59 +00:00
|
|
|
$registry = ExtensionRegistry::getInstance();
|
2018-08-05 12:43:17 +00:00
|
|
|
$parser = MediaWikiServices::getInstance()->getParser();
|
2017-08-04 00:48:59 +00:00
|
|
|
|
|
|
|
// initialize configuration
|
|
|
|
$config = [
|
|
|
|
'pluginModules' => $registry->getAttribute( 'CodeMirrorPluginModules' ),
|
|
|
|
'tagModes' => $registry->getAttribute( 'CodeMirrorTagModes' ),
|
2018-08-05 12:43:17 +00:00
|
|
|
'tags' => array_fill_keys( $parser->getTags(), true ),
|
2017-08-04 00:48:59 +00:00
|
|
|
'doubleUnderscore' => [ [], [] ],
|
2018-08-14 05:55:13 +00:00
|
|
|
'functionSynonyms' => $parser->getFunctionSynonyms(),
|
|
|
|
'urlProtocols' => $parser->getUrlProtocols(),
|
2017-08-04 00:48:59 +00:00
|
|
|
'linkTrailCharacters' => $lang->linkTrail(),
|
|
|
|
];
|
|
|
|
|
|
|
|
$mw = $lang->getMagicWords();
|
2018-08-13 04:44:24 +00:00
|
|
|
$magicWordFactory = $parser->getMagicWordFactory();
|
|
|
|
foreach ( $magicWordFactory->getDoubleUnderscoreArray()->getNames() as $name ) {
|
2017-08-04 00:48:59 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 04:44:24 +00:00
|
|
|
foreach ( $magicWordFactory->getVariableIDs() as $name ) {
|
2017-08-04 00:48:59 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|