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
|
|
|
/** @var null|array Cached version of global variables, if available, otherwise null */
|
|
|
|
private static $globalVariableScript = null;
|
2017-04-01 15:08:36 +00:00
|
|
|
|
2015-03-16 05:00:43 +00:00
|
|
|
/** @var null|boolean Saves, if CodeMirror should be loaded on this page or not */
|
|
|
|
private static $isEnabled = null;
|
2017-04-01 15:08:36 +00:00
|
|
|
|
2015-03-16 05:00:43 +00:00
|
|
|
/** @var array values passed from other extensions for use in self::getGlobalVariables() */
|
2017-04-01 15:08:36 +00:00
|
|
|
private static $extModes = [];
|
2014-08-29 04:05:50 +00:00
|
|
|
|
|
|
|
/**
|
2015-03-16 05:00:43 +00:00
|
|
|
* ResourceLoaderRegisterModules hook handler to conditionally register CodeMirror modules
|
2014-08-29 04:05:50 +00:00
|
|
|
*
|
2015-03-16 05:00:43 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderRegisterModules
|
|
|
|
*
|
2015-08-07 14:45:17 +00:00
|
|
|
* @param ResourceLoader &$rl The ResourceLoader object
|
2014-08-29 04:05:50 +00:00
|
|
|
*/
|
2015-03-16 05:00:43 +00:00
|
|
|
public static function onResourceLoaderRegisterModules( ResourceLoader $rl ) {
|
2017-04-01 15:08:36 +00:00
|
|
|
$codeMirrorResourceTemplate = [
|
2015-08-11 07:12:51 +00:00
|
|
|
'localBasePath' => __DIR__ . '/resources',
|
|
|
|
'remoteExtPath' => 'CodeMirror/resources',
|
2017-04-01 15:08:36 +00:00
|
|
|
];
|
2014-10-23 06:09:10 +00:00
|
|
|
|
2017-04-01 15:08:36 +00:00
|
|
|
self::$extModes = [
|
|
|
|
'tag' => [
|
2014-10-23 06:09:10 +00:00
|
|
|
'pre' => 'mw-tag-pre',
|
|
|
|
'nowiki' => 'mw-tag-nowiki',
|
2017-04-01 15:08:36 +00:00
|
|
|
],
|
|
|
|
'func' => [],
|
|
|
|
'data' => [],
|
|
|
|
];
|
|
|
|
$extResources = [
|
|
|
|
'scripts' => [],
|
|
|
|
'styles' => [],
|
|
|
|
'messages' => [],
|
|
|
|
'dependencies' => [ 'ext.CodeMirror.lib' => true ],
|
|
|
|
];
|
2015-03-16 05:00:43 +00:00
|
|
|
|
|
|
|
// enable other extensions to add additional resources and modes
|
2017-04-01 15:08:36 +00:00
|
|
|
Hooks::run( 'CodeMirrorGetAdditionalResources', [ &$extResources, &self::$extModes ] );
|
2015-03-16 05:00:43 +00:00
|
|
|
|
|
|
|
// Prepare array of resources for ResourceLoader
|
2017-04-01 15:08:36 +00:00
|
|
|
$codeMirror = [
|
2015-03-16 05:00:43 +00:00
|
|
|
'scripts' => array_keys( $extResources['scripts'] ),
|
|
|
|
'styles' => array_keys( $extResources['styles'] ),
|
|
|
|
'messages' => array_keys( $extResources['messages'] ),
|
|
|
|
'dependencies' => array_keys( $extResources['dependencies'] ),
|
|
|
|
'group' => 'ext.CodeMirror',
|
2017-04-01 15:08:36 +00:00
|
|
|
] + $codeMirrorResourceTemplate;
|
2015-03-16 05:00:43 +00:00
|
|
|
|
2017-04-01 15:08:36 +00:00
|
|
|
$rl->register( [ 'ext.CodeMirror.other' => $codeMirror ] );
|
2015-03-16 05:00:43 +00:00
|
|
|
}
|
2014-10-23 06:09:10 +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-01 15:08:36 +00:00
|
|
|
* @return bool
|
2015-03-16 05:00:43 +00:00
|
|
|
*/
|
|
|
|
private static function isCodeMirrorEnabled( IContextSource $context ) {
|
2015-03-26 17:25:15 +00:00
|
|
|
global $wgCodeMirrorEnableFrontend;
|
|
|
|
|
2015-03-16 05:00:43 +00:00
|
|
|
// Check, if we already checked, if page action is editing, if not, do it now
|
|
|
|
if ( is_null( self::$isEnabled ) ) {
|
|
|
|
// edit can be 'edit' and 'submit'
|
2015-03-26 17:25:15 +00:00
|
|
|
self::$isEnabled = $wgCodeMirrorEnableFrontend &&
|
|
|
|
in_array(
|
|
|
|
Action::getActionName( $context ),
|
2017-04-01 15:08:36 +00:00
|
|
|
[ 'edit', 'submit' ]
|
2015-03-26 17:25:15 +00:00
|
|
|
);
|
2014-08-29 04:05:50 +00:00
|
|
|
}
|
2015-03-16 05:00:43 +00:00
|
|
|
|
|
|
|
return self::$isEnabled;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of variables for CodeMirror to work (tags and so on)
|
|
|
|
*
|
|
|
|
* @param IContextSource $context The current ContextSource object
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getGlobalVariables( IContextSource $context ) {
|
2017-04-04 00:04:36 +00:00
|
|
|
/** @var Parser $wgParser */
|
2015-03-16 05:00:43 +00:00
|
|
|
global $wgParser;
|
|
|
|
|
|
|
|
// if we already created these variable array, return it
|
|
|
|
if ( !self::$globalVariableScript ) {
|
|
|
|
$contObj = $context->getLanguage();
|
|
|
|
|
|
|
|
if ( !isset( $wgParser->mFunctionSynonyms ) ) {
|
|
|
|
$wgParser->initialiseVariables();
|
|
|
|
$wgParser->firstCallInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize global vars
|
2017-04-01 15:08:36 +00:00
|
|
|
$globalVariableScript = [
|
2015-03-16 05:00:43 +00:00
|
|
|
'ExtModes' => self::$extModes,
|
|
|
|
'Tags' => array_fill_keys( $wgParser->getTags(), true ),
|
2017-04-01 15:08:36 +00:00
|
|
|
'DoubleUnderscore' => [ [], [] ],
|
2015-03-16 05:00:43 +00:00
|
|
|
'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 ) {
|
|
|
|
$globalVariableScript['DoubleUnderscore'][$caseSensitive][ $caseSensitive ? $n : $contObj->lc( $n ) ] = $name;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$globalVariableScript['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 ) {
|
|
|
|
$globalVariableScript['FunctionSynonyms'][$caseSensitive][ $caseSensitive ? $n : $contObj->lc( $n ) ] = $name;
|
|
|
|
}
|
2014-09-04 03:37:11 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-16 05:00:43 +00:00
|
|
|
|
|
|
|
// prefix all variables and save it into class variable
|
|
|
|
foreach ( $globalVariableScript as $key=> $value ) {
|
|
|
|
self::$globalVariableScript["extCodeMirror$key"] = $value;
|
|
|
|
}
|
2014-09-04 03:37:11 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 05:00:43 +00:00
|
|
|
return self::$globalVariableScript;
|
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();
|
|
|
|
// add CodeMirror vars only for edit pages
|
|
|
|
if ( self::isCodeMirrorEnabled( $context ) ) {
|
|
|
|
$vars += self::getGlobalVariables( $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() ) ) {
|
|
|
|
$out->addModules( 'ext.CodeMirror.init' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2014-08-20 17:03:46 +00:00
|
|
|
}
|