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
|
|
|
* @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-08-24 15:53:56 +00:00
|
|
|
global $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-08-24 15:53:56 +00:00
|
|
|
if ( !$wgCodeMirrorBetaFeature ) {
|
2017-06-12 23:37:25 +00:00
|
|
|
$isEnabled = in_array( Action::getActionName( $context ), [ 'edit', 'submit' ] );
|
|
|
|
} else {
|
2017-08-24 15:53:56 +00:00
|
|
|
$isEnabled = in_array( Action::getActionName( $context ), [ 'edit', 'submit' ] ) &&
|
2017-06-12 23:37:25 +00:00
|
|
|
$wgCodeMirrorBetaFeature &&
|
|
|
|
ExtensionRegistry::getInstance()->isLoaded( 'BetaFeatures' ) &&
|
2017-07-07 18:34:55 +00:00
|
|
|
BetaFeatures::isFeatureEnabled(
|
|
|
|
$context->getUser(), 'codemirror-syntax-highlight' );
|
2017-06-12 23:37:25 +00:00
|
|
|
}
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ) {
|
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
|
|
|
|
2017-06-12 23:37:25 +00:00
|
|
|
/**
|
|
|
|
* GetBetaFeaturePreferences hook handler
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @param array $preferences
|
|
|
|
*/
|
|
|
|
public static function onGetBetaFeaturePreferences( User $user, &$preferences ) {
|
2017-08-24 15:53:56 +00:00
|
|
|
global $wgCodeMirrorBetaFeature, $wgExtensionAssetsPath;
|
|
|
|
if ( $wgCodeMirrorBetaFeature ) {
|
2017-06-12 23:37:25 +00:00
|
|
|
$preferences['codemirror-syntax-highlight'] = [
|
|
|
|
'label-message' => 'codemirror-beta-title',
|
|
|
|
'desc-message' => 'codemirror-beta-desc',
|
|
|
|
'screenshot' => [
|
2017-07-07 11:29:56 +00:00
|
|
|
'ltr' => $wgExtensionAssetsPath . '/CodeMirror/resources/images/codemirror-beta-ltr.svg',
|
|
|
|
'rtl' => $wgExtensionAssetsPath . '/CodeMirror/resources/images/codemirror-beta-rtl.svg'
|
2017-06-12 23:37:25 +00:00
|
|
|
],
|
2017-07-07 18:34:55 +00:00
|
|
|
'info-link' => 'https://meta.wikimedia.org/wiki/' .
|
|
|
|
'Community_Tech/Wikitext_editor_syntax_highlighting',
|
2017-07-06 20:13:25 +00:00
|
|
|
'discussion-link' => 'https://www.mediawiki.org/wiki/' .
|
|
|
|
'Extension_talk:CodeMirror'
|
2017-06-12 23:37:25 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 17:03:46 +00:00
|
|
|
}
|