mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/CodeEditor
synced 2024-11-27 16:40:07 +00:00
Supply model/format directly to hook
Most of the time people want to trigger on content model, or content format. This allows triggering correctly even if you are viewing an old revision with a different content model, or are using a content format that is different than the default for the current model. Also remove back compat code for pre 1.23 Change-Id: I9706eaae0983e2b7b99f83c9564ea04c5ec252ac
This commit is contained in:
parent
9789c1e745
commit
7a95deea76
|
@ -1,30 +1,23 @@
|
|||
<?php
|
||||
|
||||
class CodeEditorHooks {
|
||||
static function getPageLanguage( Title $title ) {
|
||||
static function getPageLanguage( Title $title, $model, $format ) {
|
||||
global $wgCodeEditorEnableCore;
|
||||
|
||||
if ( $wgCodeEditorEnableCore && method_exists( $title, "hasContentModel" ) ) {
|
||||
if ( $title->hasContentModel( CONTENT_MODEL_JAVASCRIPT ) ) {
|
||||
if ( $wgCodeEditorEnableCore ) {
|
||||
if ( $model === CONTENT_MODEL_JAVASCRIPT ) {
|
||||
return 'javascript';
|
||||
} elseif ( $title->hasContentModel( CONTENT_MODEL_CSS ) ) {
|
||||
} elseif ( $model === CONTENT_MODEL_CSS ) {
|
||||
return 'css';
|
||||
} elseif ( $title->hasContentModel( CONTENT_MODEL_JSON ) ) {
|
||||
} elseif ( $model === CONTENT_MODEL_JSON ) {
|
||||
return 'json';
|
||||
}
|
||||
} elseif ( $wgCodeEditorEnableCore && ( $title->isCssOrJsPage() || $title->isCssJsSubpage() ) ) {
|
||||
// This block is deprecated. Remove after 1.23 release
|
||||
if ( preg_match( '/\.js$/', $title->getText() ) ) {
|
||||
return 'javascript';
|
||||
}
|
||||
if ( preg_match( '/\.css$/', $title->getText() ) ) {
|
||||
return 'css';
|
||||
}
|
||||
}
|
||||
|
||||
// Give extensions a chance
|
||||
// Note: $model and $format were added around the time of MediaWiki 1.28.
|
||||
$lang = null;
|
||||
Hooks::run( 'CodeEditorGetPageLanguage', [ $title, &$lang ] );
|
||||
Hooks::run( 'CodeEditorGetPageLanguage', [ $title, &$lang, $model, $format ] );
|
||||
|
||||
return $lang;
|
||||
}
|
||||
|
@ -39,17 +32,15 @@ class CodeEditorHooks {
|
|||
|
||||
public static function editPageShowEditFormInitial( $editpage, $output ) {
|
||||
$output->addModuleStyles( 'ext.wikiEditor.toolbar.styles' );
|
||||
$lang = self::getPageLanguage( $editpage->getContextTitle() );
|
||||
|
||||
$title = $editpage->getContextTitle();
|
||||
$model = $editpage->contentModel;
|
||||
$format = $editpage->contentFormat;
|
||||
|
||||
$lang = self::getPageLanguage( $title, $model, $format );
|
||||
if ( $lang && $output->getUser()->getOption( 'usebetatoolbar' ) ) {
|
||||
$output->addModules( 'ext.codeEditor' );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function onMakeGlobalVariablesScript( &$vars, $output ) {
|
||||
$lang = self::getPageLanguage( $output->getTitle() );
|
||||
if ( $lang ) {
|
||||
$vars['wgCodeEditorCurrentLanguage'] = $lang;
|
||||
$output->addJsConfigVars( 'wgCodeEditorCurrentLanguage', $lang );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -22,9 +22,6 @@
|
|||
"BeforePageDisplay": [
|
||||
"CodeEditorHooks::onBeforePageDisplay"
|
||||
],
|
||||
"MakeGlobalVariablesScript": [
|
||||
"CodeEditorHooks::onMakeGlobalVariablesScript"
|
||||
],
|
||||
"GetPreferences": [
|
||||
"CodeEditorHooks::getPreferences"
|
||||
]
|
||||
|
|
Loading…
Reference in a new issue