2011-06-09 21:50:45 +00:00
|
|
|
<?php
|
|
|
|
|
2021-11-25 19:40:49 +00:00
|
|
|
namespace MediaWiki\Extension\CodeEditor;
|
|
|
|
|
|
|
|
use ErrorPageError;
|
|
|
|
use ExtensionRegistry;
|
2023-05-06 21:19:29 +00:00
|
|
|
use MediaWiki\EditPage\EditPage;
|
2023-06-03 22:32:28 +00:00
|
|
|
use MediaWiki\Extension\CodeEditor\Hooks\HookRunner;
|
2021-11-25 20:11:11 +00:00
|
|
|
use MediaWiki\Hook\EditPage__showEditForm_initialHook;
|
|
|
|
use MediaWiki\Hook\EditPage__showReadOnlyForm_initialHook;
|
2023-04-27 20:27:56 +00:00
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
2023-10-03 09:11:09 +00:00
|
|
|
use MediaWiki\Output\OutputPage;
|
2021-11-25 20:11:11 +00:00
|
|
|
use MediaWiki\Preferences\Hook\GetPreferencesHook;
|
2023-08-19 04:14:04 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-11-29 12:38:27 +00:00
|
|
|
use MediaWiki\User\Options\UserOptionsLookup;
|
2023-10-03 09:11:09 +00:00
|
|
|
use MediaWiki\User\User;
|
2021-11-25 19:40:49 +00:00
|
|
|
|
2021-11-25 20:11:11 +00:00
|
|
|
/**
|
|
|
|
* @phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName
|
|
|
|
*/
|
|
|
|
class Hooks implements
|
|
|
|
GetPreferencesHook,
|
|
|
|
EditPage__showEditForm_initialHook,
|
|
|
|
EditPage__showReadOnlyForm_initialHook
|
|
|
|
{
|
2024-09-16 07:20:43 +00:00
|
|
|
private UserOptionsLookup $userOptionsLookup;
|
|
|
|
private HookRunner $hookRunner;
|
2021-11-23 14:47:50 +00:00
|
|
|
|
|
|
|
public function __construct(
|
2023-04-27 20:27:56 +00:00
|
|
|
UserOptionsLookup $userOptionsLookup,
|
|
|
|
HookContainer $hookContainer
|
2021-11-23 14:47:50 +00:00
|
|
|
) {
|
|
|
|
$this->userOptionsLookup = $userOptionsLookup;
|
2023-06-03 22:32:28 +00:00
|
|
|
$this->hookRunner = new HookRunner( $hookContainer );
|
2021-11-23 14:47:50 +00:00
|
|
|
}
|
|
|
|
|
2024-09-16 07:20:43 +00:00
|
|
|
private function getPageLanguage( Title $title, string $model, string $format ): ?string {
|
2019-07-12 17:58:50 +00:00
|
|
|
if ( $model === CONTENT_MODEL_JAVASCRIPT ) {
|
|
|
|
return 'javascript';
|
|
|
|
} elseif ( $model === CONTENT_MODEL_CSS ) {
|
|
|
|
return 'css';
|
|
|
|
} elseif ( $model === CONTENT_MODEL_JSON ) {
|
|
|
|
return 'json';
|
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
|
|
|
|
$lang = null;
|
2023-06-03 22:32:28 +00:00
|
|
|
$this->hookRunner->onCodeEditorGetPageLanguage( $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
|
|
|
*/
|
2021-11-25 20:11:11 +00:00
|
|
|
public function onGetPreferences( $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
|
|
|
}
|
2014-10-29 17:22:42 +00:00
|
|
|
|
2016-12-13 18:01:34 +00:00
|
|
|
/**
|
|
|
|
* @param EditPage $editpage
|
|
|
|
* @param OutputPage $output
|
2019-02-02 09:05:09 +00:00
|
|
|
* @throws ErrorPageError
|
2016-12-13 18:01:34 +00:00
|
|
|
*/
|
2021-11-25 20:11:11 +00:00
|
|
|
public function onEditPage__showEditForm_initial( $editpage, $output ) {
|
2020-01-01 23:16:28 +00:00
|
|
|
$title = $editpage->getContextTitle();
|
2016-07-17 05:34:15 +00:00
|
|
|
$model = $editpage->contentModel;
|
|
|
|
$format = $editpage->contentFormat;
|
|
|
|
|
2023-04-27 20:27:56 +00:00
|
|
|
$lang = $this->getPageLanguage( $title, $model, $format );
|
2021-11-23 14:47:50 +00:00
|
|
|
if ( $lang && $this->userOptionsLookup->getOption( $output->getUser(), 'usebetatoolbar' ) ) {
|
2014-02-05 18:30:22 +00:00
|
|
|
$output->addModules( 'ext.codeEditor' );
|
2023-10-14 11:11:23 +00:00
|
|
|
$output->addModuleStyles( 'ext.codeEditor.styles' );
|
2016-07-17 05:34:15 +00:00
|
|
|
$output->addJsConfigVars( 'wgCodeEditorCurrentLanguage', $lang );
|
2019-10-28 09:02:20 +00:00
|
|
|
// Needed because ACE adds a blob: url web-worker.
|
2021-10-30 19:14:37 +00:00
|
|
|
$output->getCSP()->addScriptSrc( 'blob:' );
|
2024-02-11 01:20:41 +00:00
|
|
|
|
|
|
|
if ( $this->userOptionsLookup->getOption( $output->getUser(), 'usecodeeditor' ) ) {
|
|
|
|
$output->addBodyClasses( 'codeeditor-loading' );
|
|
|
|
}
|
2021-10-30 19:14:37 +00:00
|
|
|
} elseif ( !ExtensionRegistry::getInstance()->isLoaded( 'WikiEditor' ) ) {
|
|
|
|
throw new ErrorPageError( 'codeeditor-error-title', 'codeeditor-error-message' );
|
2012-02-06 21:59:13 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-25 20:11:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param EditPage $editpage
|
|
|
|
* @param OutputPage $output
|
|
|
|
* @throws ErrorPageError
|
|
|
|
*/
|
|
|
|
public function onEditPage__showReadOnlyForm_initial( $editpage, $output ) {
|
|
|
|
$this->onEditPage__showEditForm_initial( $editpage, $output );
|
|
|
|
}
|
2011-06-09 21:50:45 +00:00
|
|
|
}
|