mediawiki-extensions-CodeEd.../includes/CodeEditorHooks.php
Kunal Mehta fefe8893a4 Set an explicit visibility on getPageLanguage()
Change-Id: I37407f8aa5f0f9d3066583f1a54e79de0c5ca129
2018-04-14 01:44:58 +00:00

64 lines
1.7 KiB
PHP

<?php
class CodeEditorHooks {
/**
* @param Title $title
* @param string $model
* @param string $format
* @return null|string
*/
public static function getPageLanguage( Title $title, $model, $format ) {
global $wgCodeEditorEnableCore;
if ( $wgCodeEditorEnableCore ) {
if ( $model === CONTENT_MODEL_JAVASCRIPT ) {
return 'javascript';
} elseif ( $model === CONTENT_MODEL_CSS ) {
return 'css';
} elseif ( $model === CONTENT_MODEL_JSON ) {
return 'json';
}
}
// Give extensions a chance
// Note: $model and $format were added around the time of MediaWiki 1.28.
$lang = null;
Hooks::run( 'CodeEditorGetPageLanguage', [ $title, &$lang, $model, $format ] );
return $lang;
}
/**
* @param User $user
* @param array &$defaultPreferences
* @return bool
*/
public static function getPreferences( $user, &$defaultPreferences ) {
$defaultPreferences['usecodeeditor'] = [
'type' => 'api',
'default' => '1',
];
return true;
}
/**
* @param EditPage $editpage
* @param OutputPage $output
* @return bool
*/
public static function editPageShowEditFormInitial( $editpage, $output ) {
$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' );
$output->addJsConfigVars( 'wgCodeEditorCurrentLanguage', $lang );
} elseif ( !ExtensionRegistry::getInstance()->isLoaded( "WikiEditor" ) ) {
throw new ErrorPageError( "codeeditor-error-title", "codeeditor-error-message" );
}
return true;
}
}