mediawiki-extensions-CodeEd.../includes/CodeEditorHooks.php
Daimona Eaytoy b3fb04b463 Directly use $wgTitle instead of relying on getContextTitle fallback
This is apparently the only code relying on the fallback. Since it seems to do this most of the time, use $wgTitle directly.

Bug: T176526
Change-Id: I9edc7c3cb7b0f2b024873b2f6fbdb8c275225854
2019-09-30 17:38:56 +00:00

58 lines
1.6 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 ) {
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
*/
public static function getPreferences( User $user, &$defaultPreferences ) {
$defaultPreferences['usecodeeditor'] = [
'type' => 'api',
'default' => '1',
];
}
/**
* @param EditPage $editpage
* @param OutputPage $output
* @throws ErrorPageError
*/
public static function editPageShowEditFormInitial( EditPage $editpage, OutputPage $output ) {
global $wgTitle;
$model = $editpage->contentModel;
$format = $editpage->contentFormat;
$lang = self::getPageLanguage( $wgTitle, $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" );
}
}
}