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.
|
|
|
|
*
|
2019-03-11 06:04:20 +00:00
|
|
|
* @param OutputPage $out
|
2017-08-30 21:53:55 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2019-03-11 06:04:20 +00:00
|
|
|
private static function isCodeMirrorOnPage( OutputPage $out ) {
|
|
|
|
// Disable CodeMirror when CodeEditor is active on this page
|
|
|
|
// Depends on ext.codeEditor being added by EditPage::showEditForm:initial
|
|
|
|
if ( in_array( 'ext.codeEditor', $out->getModules() ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-11 06:17:13 +00:00
|
|
|
// Disable CodeMirror when the WikiEditor toolbar is not enabled in preferences
|
|
|
|
if ( !$out->getUser()->getOption( 'usebetatoolbar' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-11 06:04:20 +00:00
|
|
|
$context = $out->getContext();
|
2017-08-30 21:53:55 +00:00
|
|
|
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
|
|
|
|
*
|
2019-04-03 23:38:24 +00:00
|
|
|
* @param OutputPage $out
|
|
|
|
* @param Skin $skin
|
2015-03-16 05:00:43 +00:00
|
|
|
*/
|
2019-04-03 23:38:24 +00:00
|
|
|
public static function onBeforePageDisplay( OutputPage $out, Skin $skin ) {
|
2019-03-11 06:04:20 +00:00
|
|
|
if ( self::isCodeMirrorOnPage( $out ) ) {
|
2017-04-28 13:42:30 +00:00
|
|
|
$out->addModules( 'ext.CodeMirror' );
|
2019-03-09 15:52:49 +00:00
|
|
|
|
|
|
|
if ( $out->getUser()->getOption( 'usecodemirror' ) ) {
|
|
|
|
// These modules are predelivered for performance when needed
|
|
|
|
// keep these modules in sync with ext.CodeMirror.js
|
|
|
|
$out->addModules( [ 'ext.CodeMirror.lib', 'ext.CodeMirror.mode.mediawiki' ] );
|
|
|
|
}
|
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
|
|
|
*/
|
2019-05-07 14:44:14 +00:00
|
|
|
public static function onGetPreferences( User $user, array &$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
|
|
|
|
2014-08-20 17:03:46 +00:00
|
|
|
}
|