2011-06-09 21:50:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class CodeEditorHooks {
|
2016-12-13 18:01:34 +00:00
|
|
|
/**
|
|
|
|
* @param Title $title
|
2017-10-11 19:01:37 +00:00
|
|
|
* @param string $model
|
|
|
|
* @param string $format
|
2016-12-13 18:01:34 +00:00
|
|
|
* @return null|string
|
|
|
|
*/
|
2018-04-14 01:24:49 +00:00
|
|
|
public static function getPageLanguage( Title $title, $model, $format ) {
|
2012-08-14 05:04:20 +00:00
|
|
|
global $wgCodeEditorEnableCore;
|
|
|
|
|
2016-07-17 05:34:15 +00:00
|
|
|
if ( $wgCodeEditorEnableCore ) {
|
|
|
|
if ( $model === CONTENT_MODEL_JAVASCRIPT ) {
|
2013-12-31 10:20:35 +00:00
|
|
|
return 'javascript';
|
2016-07-17 05:34:15 +00:00
|
|
|
} elseif ( $model === CONTENT_MODEL_CSS ) {
|
2013-12-31 10:20:35 +00:00
|
|
|
return 'css';
|
2016-07-17 05:34:15 +00:00
|
|
|
} elseif ( $model === CONTENT_MODEL_JSON ) {
|
2014-10-07 19:56:16 +00:00
|
|
|
return 'json';
|
2013-12-31 10:20:35 +00:00
|
|
|
}
|
2012-02-06 21:59:13 +00:00
|
|
|
}
|
2014-10-29 17:22:42 +00:00
|
|
|
|
2012-02-06 21:59:13 +00:00
|
|
|
// Give extensions a chance
|
2016-07-17 05:34:15 +00:00
|
|
|
// Note: $model and $format were added around the time of MediaWiki 1.28.
|
2012-02-06 21:59:13 +00:00
|
|
|
$lang = null;
|
2016-07-17 05:34:15 +00:00
|
|
|
Hooks::run( 'CodeEditorGetPageLanguage', [ $title, &$lang, $model, $format ] );
|
2014-10-29 17:22:42 +00:00
|
|
|
|
2012-02-06 21:59:13 +00:00
|
|
|
return $lang;
|
|
|
|
}
|
2014-04-28 13:31:57 +00:00
|
|
|
|
2016-12-13 18:01:34 +00:00
|
|
|
/**
|
2017-10-11 19:01:37 +00:00
|
|
|
* @param User $user
|
|
|
|
* @param array &$defaultPreferences
|
2016-12-13 18:01:34 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-12-16 11:04:49 +00:00
|
|
|
public static function getPreferences( User $user, &$defaultPreferences ) {
|
2016-05-09 23:37:08 +00:00
|
|
|
$defaultPreferences['usecodeeditor'] = [
|
2014-04-28 13:31:57 +00:00
|
|
|
'type' => 'api',
|
|
|
|
'default' => '1',
|
2016-05-09 23:37:08 +00:00
|
|
|
];
|
2014-04-28 13:31:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-10-29 17:22:42 +00:00
|
|
|
|
2016-12-13 18:01:34 +00:00
|
|
|
/**
|
|
|
|
* @param EditPage $editpage
|
|
|
|
* @param OutputPage $output
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-12-16 11:04:49 +00:00
|
|
|
public static function editPageShowEditFormInitial( EditPage $editpage, OutputPage $output ) {
|
2016-07-17 05:34:15 +00:00
|
|
|
$title = $editpage->getContextTitle();
|
|
|
|
$model = $editpage->contentModel;
|
|
|
|
$format = $editpage->contentFormat;
|
|
|
|
|
|
|
|
$lang = self::getPageLanguage( $title, $model, $format );
|
2014-04-28 13:31:57 +00:00
|
|
|
if ( $lang && $output->getUser()->getOption( 'usebetatoolbar' ) ) {
|
2014-02-05 18:30:22 +00:00
|
|
|
$output->addModules( 'ext.codeEditor' );
|
2016-07-17 05:34:15 +00:00
|
|
|
$output->addJsConfigVars( 'wgCodeEditorCurrentLanguage', $lang );
|
2017-02-04 21:35:13 +00:00
|
|
|
} elseif ( !ExtensionRegistry::getInstance()->isLoaded( "WikiEditor" ) ) {
|
|
|
|
throw new ErrorPageError( "codeeditor-error-title", "codeeditor-error-message" );
|
2012-02-06 21:59:13 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2011-06-09 21:50:45 +00:00
|
|
|
}
|