2014-08-20 17:03:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class CodeMirrorHooks {
|
2015-08-07 14:45:17 +00:00
|
|
|
|
2015-03-16 05:00:43 +00:00
|
|
|
/**
|
|
|
|
* Checks, if CodeMirror should be loaded on this page or not.
|
|
|
|
*
|
|
|
|
* @param IContextSource $context The current ContextSource object
|
2017-04-28 13:42:30 +00:00
|
|
|
* @global bool $wgCodeMirrorEnableFrontend Should CodeMirror be loaded on this page
|
|
|
|
* @staticvar null|boolean $isEnabled Saves, if CodeMirror should be loaded on this page or not
|
2017-04-01 15:08:36 +00:00
|
|
|
* @return bool
|
2015-03-16 05:00:43 +00:00
|
|
|
*/
|
|
|
|
private static function isCodeMirrorEnabled( IContextSource $context ) {
|
2017-06-12 23:37:25 +00:00
|
|
|
global $wgCodeMirrorEnableFrontend, $wgCodeMirrorBetaFeature;
|
2017-04-28 13:42:30 +00:00
|
|
|
static $isEnabled = null;
|
2015-03-26 17:25:15 +00:00
|
|
|
|
2015-03-16 05:00:43 +00:00
|
|
|
// Check, if we already checked, if page action is editing, if not, do it now
|
2017-04-28 13:42:30 +00:00
|
|
|
if ( $isEnabled === null ) {
|
2017-06-12 23:37:25 +00:00
|
|
|
if ( $wgCodeMirrorEnableFrontend && !$wgCodeMirrorBetaFeature ) {
|
|
|
|
$isEnabled = in_array( Action::getActionName( $context ), [ 'edit', 'submit' ] );
|
|
|
|
} else {
|
|
|
|
$isEnabled = $wgCodeMirrorEnableFrontend &&
|
|
|
|
in_array( Action::getActionName( $context ), [ 'edit', 'submit' ] ) &&
|
|
|
|
$wgCodeMirrorBetaFeature &&
|
|
|
|
ExtensionRegistry::getInstance()->isLoaded( 'BetaFeatures' ) &&
|
|
|
|
BetaFeatures::isFeatureEnabled( $context->getUser(), 'codemirror-syntax-highlight' );
|
|
|
|
}
|
2014-08-29 04:05:50 +00:00
|
|
|
}
|
2015-03-16 05:00:43 +00:00
|
|
|
|
2017-04-28 13:42:30 +00:00
|
|
|
return $isEnabled;
|
|
|
|
}
|
2015-03-16 05:00:43 +00:00
|
|
|
|
2017-04-28 13:42:30 +00:00
|
|
|
/**
|
|
|
|
* This function are used by the MobileFrontend extension only and will be
|
|
|
|
* removed
|
|
|
|
* @deprecated since version 4.0.0
|
|
|
|
* @todo Remove usage in MobileFrontend and this function some time later
|
|
|
|
* @param IContextSource $context
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getGlobalVariables() {
|
|
|
|
MWDebug::deprecated( __METHOD__ );
|
|
|
|
return [];
|
2015-03-16 05:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of variables for CodeMirror to work (tags and so on)
|
|
|
|
*
|
|
|
|
* @param IContextSource $context The current ContextSource object
|
2017-04-28 13:42:30 +00:00
|
|
|
* @global Parser $wgParser
|
|
|
|
* @staticvar array $config Cached version of configuration
|
2015-03-16 05:00:43 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2017-04-28 13:42:30 +00:00
|
|
|
public static function getConfiguraton( IContextSource $context ) {
|
2015-03-16 05:00:43 +00:00
|
|
|
global $wgParser;
|
2017-04-28 13:42:30 +00:00
|
|
|
static $config = [];
|
2015-03-16 05:00:43 +00:00
|
|
|
|
|
|
|
// if we already created these variable array, return it
|
2017-04-28 13:42:30 +00:00
|
|
|
if ( !$config ) {
|
2015-03-16 05:00:43 +00:00
|
|
|
$contObj = $context->getLanguage();
|
|
|
|
|
|
|
|
if ( !isset( $wgParser->mFunctionSynonyms ) ) {
|
|
|
|
$wgParser->initialiseVariables();
|
|
|
|
$wgParser->firstCallInit();
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:42:30 +00:00
|
|
|
// initialize configuration
|
|
|
|
$config = [
|
|
|
|
'pluginModules' => ExtensionRegistry::getInstance()->getAttribute( 'CodeMirrorPluginModules' ),
|
|
|
|
'tagModes' => ExtensionRegistry::getInstance()->getAttribute( 'CodeMirrorTagModes' ),
|
|
|
|
'tags' => array_fill_keys( $wgParser->getTags(), true ),
|
|
|
|
'doubleUnderscore' => [ [], [] ],
|
|
|
|
'functionSynonyms' => $wgParser->mFunctionSynonyms,
|
|
|
|
'urlProtocols' => $wgParser->mUrlProtocols,
|
|
|
|
'linkTrailCharacters' => $contObj->linkTrail(),
|
2017-04-01 15:08:36 +00:00
|
|
|
];
|
2015-03-16 05:00:43 +00:00
|
|
|
|
|
|
|
$mw = $contObj->getMagicWords();
|
|
|
|
foreach ( MagicWord::getDoubleUnderscoreArray()->names as $name ) {
|
|
|
|
if ( isset( $mw[$name] ) ) {
|
|
|
|
$caseSensitive = array_shift( $mw[$name] ) == 0 ? 0 : 1;
|
|
|
|
foreach ( $mw[$name] as $n ) {
|
2017-04-28 13:42:30 +00:00
|
|
|
$config['doubleUnderscore'][$caseSensitive][ $caseSensitive ? $n : $contObj->lc( $n ) ] = $name;
|
2015-03-16 05:00:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-04-28 13:42:30 +00:00
|
|
|
$config['doubleUnderscore'][0][] = $name;
|
2014-09-04 04:23:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 05:00:43 +00:00
|
|
|
foreach ( MagicWord::getVariableIDs() as $name ) {
|
|
|
|
if ( isset( $mw[$name] ) ) {
|
|
|
|
$caseSensitive = array_shift( $mw[$name] ) == 0 ? 0 : 1;
|
|
|
|
foreach ( $mw[$name] as $n ) {
|
2017-04-28 13:42:30 +00:00
|
|
|
$config['functionSynonyms'][$caseSensitive][ $caseSensitive ? $n : $contObj->lc( $n ) ] = $name;
|
2015-03-16 05:00:43 +00:00
|
|
|
}
|
2014-09-04 03:37:11 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-16 05:00:43 +00:00
|
|
|
|
2014-09-04 03:37:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 13:42:30 +00:00
|
|
|
return $config;
|
2014-08-20 17:03:46 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 05:00:43 +00:00
|
|
|
/**
|
|
|
|
* MakeGlobalVariablesScript hook handler
|
|
|
|
*
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/MakeGlobalVariablesScript
|
|
|
|
*
|
2015-08-07 14:45:17 +00:00
|
|
|
* @param array $vars
|
|
|
|
* @param OutputPage $out
|
2015-03-16 05:00:43 +00:00
|
|
|
*/
|
|
|
|
public static function onMakeGlobalVariablesScript( array &$vars, OutputPage $out ) {
|
|
|
|
$context = $out->getContext();
|
2017-03-21 14:42:04 +00:00
|
|
|
// add CodeMirror vars on edit pages, or if VE is installed
|
|
|
|
if ( self::isCodeMirrorEnabled( $context ) || class_exists( 'VisualEditorHooks' ) ) {
|
2017-04-28 13:42:30 +00:00
|
|
|
$vars['extCodeMirrorConfig'] = self::getConfiguraton( $context );
|
2014-08-29 04:05:50 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-23 06:09:10 +00:00
|
|
|
|
2015-03-16 05:00:43 +00:00
|
|
|
/**
|
|
|
|
* BeforePageDisplay hook handler
|
|
|
|
*
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
|
|
|
|
*
|
2015-08-07 14:45:17 +00:00
|
|
|
* @param OutputPage $out
|
|
|
|
* @param Skin $skin
|
2015-03-16 05:00:43 +00:00
|
|
|
*/
|
|
|
|
public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin ) {
|
|
|
|
if ( self::isCodeMirrorEnabled( $out->getContext() ) ) {
|
2017-04-28 13:42:30 +00:00
|
|
|
$out->addModules( 'ext.CodeMirror' );
|
2015-03-16 05:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GetPreferences hook handler
|
|
|
|
*
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
|
|
|
|
*
|
2015-08-07 14:45:17 +00:00
|
|
|
* @param User $user
|
|
|
|
* @param array $defaultPreferences
|
2015-03-16 05:00:43 +00:00
|
|
|
*/
|
|
|
|
public static function onGetPreferences( User $user, &$defaultPreferences ) {
|
2015-04-07 09:13:43 +00:00
|
|
|
// CodeMirror is enabled by default for users.
|
|
|
|
// It can be changed by adding '$wgDefaultUserOptions['usecodemirror'] = 0;' into LocalSettings.php
|
2017-04-01 15:08:36 +00:00
|
|
|
$defaultPreferences['usecodemirror'] = [
|
2015-04-07 09:13:43 +00:00
|
|
|
'type' => 'api',
|
|
|
|
'default' => '1',
|
2017-04-01 15:08:36 +00:00
|
|
|
];
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-04-07 09:13:43 +00:00
|
|
|
|
2017-06-12 23:37:25 +00:00
|
|
|
/**
|
|
|
|
* GetBetaFeaturePreferences hook handler
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @param array $preferences
|
|
|
|
*/
|
|
|
|
public static function onGetBetaFeaturePreferences( User $user, &$preferences ) {
|
|
|
|
global $wgCodeMirrorEnableFrontend, $wgCodeMirrorBetaFeature, $wgExtensionAssetsPath;
|
|
|
|
if ( $wgCodeMirrorEnableFrontend && $wgCodeMirrorBetaFeature ) {
|
|
|
|
$preferences['codemirror-syntax-highlight'] = [
|
|
|
|
'label-message' => 'codemirror-beta-title',
|
|
|
|
'desc-message' => 'codemirror-beta-desc',
|
|
|
|
'screenshot' => [
|
|
|
|
'ltr' => $wgExtensionAssetsPath . '/CodeMirror/resources/images/codemirror-beta-LTR.svg',
|
|
|
|
'rtl' => $wgExtensionAssetsPath . '/CodeMirror/resources/images/codemirror-beta-RTL.svg'
|
|
|
|
],
|
|
|
|
'info-link' => 'https://meta.wikimedia.org/wiki/Community_Tech/Wikitext_editor_syntax_highlighting',
|
|
|
|
'discussion-link' => 'https://meta.wikimedia.org/wiki/Talk:Community_Tech/Wikitext_editor_syntax_highlighting'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 17:03:46 +00:00
|
|
|
}
|