| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\CodeEditor; |
| 4 |
|
| 5 |
use ErrorPageError; |
| 6 |
use MediaWiki\EditPage\EditPage; |
| 7 |
use MediaWiki\Extension\CodeEditor\Hooks\HookRunner; |
| 8 |
use MediaWiki\Hook\EditPage__showEditForm_initialHook; |
| 9 |
use MediaWiki\Hook\EditPage__showReadOnlyForm_initialHook; |
| 10 |
use MediaWiki\HookContainer\HookContainer; |
| 11 |
use MediaWiki\Output\OutputPage; |
| 12 |
use MediaWiki\Preferences\Hook\GetPreferencesHook; |
| 13 |
use MediaWiki\Registration\ExtensionRegistry; |
| 14 |
use MediaWiki\Title\Title; |
| 15 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 16 |
use MediaWiki\User\User; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class Hooks implements |
| 22 |
GetPreferencesHook, |
| 23 |
EditPage__showEditForm_initialHook, |
| 24 |
EditPage__showReadOnlyForm_initialHook |
| 25 |
{ |
| 26 |
private UserOptionsLookup $userOptionsLookup; |
| 27 |
private HookRunner $hookRunner; |
| 28 |
|
| 29 |
public function __construct( |
| 30 |
UserOptionsLookup $userOptionsLookup, |
| 31 |
HookContainer $hookContainer |
| 32 |
) { |
| 33 |
$this->userOptionsLookup = $userOptionsLookup; |
| 34 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 35 |
} |
| 36 |
|
| 37 |
private function getPageLanguage( Title $title, string $model, string $format ): ?string { |
| 38 |
if ( $model === CONTENT_MODEL_JAVASCRIPT ) { |
| 39 |
return 'javascript'; |
| 40 |
} elseif ( $model === CONTENT_MODEL_CSS ) { |
| 41 |
return 'css'; |
| 42 |
} elseif ( $model === CONTENT_MODEL_JSON ) { |
| 43 |
return 'json'; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
$lang = null; |
| 48 |
$this->hookRunner->onCodeEditorGetPageLanguage( $title, $lang, $model, $format ); |
| 49 |
|
| 50 |
return $lang; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function onGetPreferences( $user, &$defaultPreferences ) { |
| 58 |
$defaultPreferences['usecodeeditor'] = [ |
| 59 |
'type' => 'api', |
| 60 |
'default' => '1', |
| 61 |
]; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function onEditPage__showEditForm_initial( $editpage, $output ) { |
| 70 |
$title = $editpage->getContextTitle(); |
| 71 |
$model = $editpage->contentModel; |
| 72 |
$format = $editpage->contentFormat; |
| 73 |
|
| 74 |
$lang = $this->getPageLanguage( $title, $model, $format ); |
| 75 |
if ( $lang && $this->userOptionsLookup->getOption( $output->getUser(), 'usebetatoolbar' ) ) { |
| 76 |
$output->addModules( 'ext.codeEditor' ); |
| 77 |
$output->addModuleStyles( 'ext.codeEditor.styles' ); |
| 78 |
$output->addJsConfigVars( 'wgCodeEditorCurrentLanguage', $lang ); |
| 79 |
|
| 80 |
$output->getCSP()->addScriptSrc( 'blob:' ); |
| 81 |
|
| 82 |
if ( $this->userOptionsLookup->getOption( $output->getUser(), 'usecodeeditor' ) ) { |
| 83 |
$output->addBodyClasses( 'codeeditor-loading' ); |
| 84 |
} |
| 85 |
} elseif ( !ExtensionRegistry::getInstance()->isLoaded( 'WikiEditor' ) ) { |
| 86 |
throw new ErrorPageError( 'codeeditor-error-title', 'codeeditor-error-message' ); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public function onEditPage__showReadOnlyForm_initial( $editpage, $output ) { |
| 96 |
$this->onEditPage__showEditForm_initial( $editpage, $output ); |
| 97 |
} |
| 98 |
} |