2014-08-20 17:03:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class CodeMirrorHooks {
|
2015-08-07 14:45:17 +00:00
|
|
|
|
2017-08-30 21:53:55 +00:00
|
|
|
/**
|
|
|
|
* Checks if CodeMirror for textarea wikitext editor should be loaded on this page or not.
|
|
|
|
*
|
|
|
|
* @param IContextSource $context The current ContextSource object
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private static function isCodeMirrorOnPage( IContextSource $context ) {
|
|
|
|
return in_array( Action::getActionName( $context ), [ 'edit', 'submit' ] ) &&
|
2018-04-18 11:06:37 +00:00
|
|
|
// CodeMirror on textarea wikitext editors doesn't support RTL (T170001)
|
2018-06-13 22:22:37 +00:00
|
|
|
!$context->getTitle()->getPageLanguage()->isRTL();
|
2017-08-30 21:53:55 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 05:00:43 +00:00
|
|
|
/**
|
|
|
|
* BeforePageDisplay hook handler
|
|
|
|
*
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
|
|
|
|
*
|
2017-09-09 16:52:39 +00:00
|
|
|
* @param OutputPage &$out
|
|
|
|
* @param Skin &$skin
|
2015-03-16 05:00:43 +00:00
|
|
|
*/
|
|
|
|
public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin ) {
|
2017-08-30 21:53:55 +00:00
|
|
|
if ( self::isCodeMirrorOnPage( $out->getContext() ) ) {
|
2017-04-28 13:42:30 +00:00
|
|
|
$out->addModules( 'ext.CodeMirror' );
|
2015-03-16 05:00:43 +00:00
|
|
|
}
|
2018-06-13 22:22:37 +00:00
|
|
|
// TODO: for backwards compatibility. Remove in 2019.
|
|
|
|
$out->addJsConfigVars( 'wgCodeMirrorEnabled', true );
|
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
|
2017-09-09 16:52:39 +00:00
|
|
|
* @param array &$defaultPreferences
|
2015-03-16 05:00:43 +00:00
|
|
|
*/
|
|
|
|
public static function onGetPreferences( User $user, &$defaultPreferences ) {
|
2017-07-07 18:34:55 +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
|
|
|
|
2018-03-19 21:26:37 +00:00
|
|
|
/**
|
|
|
|
* Register test modules for CodeMirror.
|
|
|
|
* @param array &$modules
|
|
|
|
* @param ResourceLoader &$rl
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function onResourceLoaderTestModules( array &$modules, ResourceLoader &$rl ) {
|
|
|
|
$modules['qunit']['ext.CodeMirror.test'] = [
|
|
|
|
'scripts' => [
|
|
|
|
'resources/mode/mediawiki/tests/qunit/CodeMirror.mediawiki.test.js',
|
|
|
|
],
|
|
|
|
'dependencies' => [
|
|
|
|
'ext.CodeMirror.data',
|
|
|
|
'ext.CodeMirror.lib',
|
|
|
|
'ext.CodeMirror.mode.mediawiki',
|
|
|
|
],
|
|
|
|
'localBasePath' => __DIR__ . '/../',
|
|
|
|
'remoteExtPath' => 'CodeMirror',
|
|
|
|
];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-20 17:03:46 +00:00
|
|
|
}
|