2011-06-09 21:50:45 +00:00
|
|
|
<?php
|
|
|
|
|
2021-11-25 19:40:49 +00:00
|
|
|
namespace MediaWiki\Extension\CodeEditor;
|
|
|
|
|
|
|
|
use EditPage;
|
|
|
|
use ErrorPageError;
|
|
|
|
use ExtensionRegistry;
|
2021-11-25 20:11:11 +00:00
|
|
|
use MediaWiki\Hook\EditPage__showEditForm_initialHook;
|
|
|
|
use MediaWiki\Hook\EditPage__showReadOnlyForm_initialHook;
|
|
|
|
use MediaWiki\Preferences\Hook\GetPreferencesHook;
|
2021-11-23 14:47:50 +00:00
|
|
|
use MediaWiki\User\UserOptionsLookup;
|
2021-11-25 19:40:49 +00:00
|
|
|
use OutputPage;
|
|
|
|
use Title;
|
|
|
|
use User;
|
|
|
|
|
2021-11-25 20:11:11 +00:00
|
|
|
/**
|
|
|
|
* @phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName
|
|
|
|
*/
|
|
|
|
class Hooks implements
|
|
|
|
GetPreferencesHook,
|
|
|
|
EditPage__showEditForm_initialHook,
|
|
|
|
EditPage__showReadOnlyForm_initialHook
|
|
|
|
{
|
2021-11-23 14:47:50 +00:00
|
|
|
/** @var UserOptionsLookup */
|
|
|
|
private $userOptionsLookup;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param UserOptionsLookup $userOptionsLookup
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
UserOptionsLookup $userOptionsLookup
|
|
|
|
) {
|
|
|
|
$this->userOptionsLookup = $userOptionsLookup;
|
|
|
|
}
|
|
|
|
|
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 ) {
|
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
|
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;
|
2021-11-25 19:40:49 +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
|
|
|
*/
|
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;
|
|
|
|
|
2020-01-01 23:16:28 +00:00
|
|
|
$lang = self::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' );
|
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:' );
|
|
|
|
} 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
|
|
|
}
|