2014-08-20 17:03:46 +00:00
|
|
|
<?php
|
|
|
|
|
2022-02-06 15:13:05 +00:00
|
|
|
namespace MediaWiki\Extension\CodeMirror;
|
|
|
|
|
2023-10-03 09:15:14 +00:00
|
|
|
use MediaWiki\Config\Config;
|
2022-09-19 19:01:32 +00:00
|
|
|
use MediaWiki\Hook\BeforePageDisplayHook;
|
2023-10-03 09:15:14 +00:00
|
|
|
use MediaWiki\Output\OutputPage;
|
2022-09-19 19:01:32 +00:00
|
|
|
use MediaWiki\Preferences\Hook\GetPreferencesHook;
|
|
|
|
use MediaWiki\ResourceLoader\Hook\ResourceLoaderGetConfigVarsHook;
|
2023-10-03 09:15:14 +00:00
|
|
|
use MediaWiki\User\User;
|
2022-09-19 19:01:32 +00:00
|
|
|
use MediaWiki\User\UserOptionsLookup;
|
2022-02-06 15:13:05 +00:00
|
|
|
use Skin;
|
2020-12-11 11:30:44 +00:00
|
|
|
|
2022-09-19 19:01:32 +00:00
|
|
|
class Hooks implements
|
|
|
|
BeforePageDisplayHook,
|
|
|
|
ResourceLoaderGetConfigVarsHook,
|
|
|
|
GetPreferencesHook
|
|
|
|
{
|
|
|
|
|
|
|
|
/** @var UserOptionsLookup */
|
2023-09-19 17:59:29 +00:00
|
|
|
private UserOptionsLookup $userOptionsLookup;
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
private bool $useV6;
|
2022-09-19 19:01:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param UserOptionsLookup $userOptionsLookup
|
2023-09-19 17:59:29 +00:00
|
|
|
* @param Config $config
|
2022-09-19 19:01:32 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2023-09-19 17:59:29 +00:00
|
|
|
UserOptionsLookup $userOptionsLookup,
|
|
|
|
Config $config
|
2022-09-19 19:01:32 +00:00
|
|
|
) {
|
|
|
|
$this->userOptionsLookup = $userOptionsLookup;
|
2023-09-19 17:59:29 +00:00
|
|
|
$this->useV6 = $config->get( 'CodeMirrorV6' );
|
2022-09-19 19:01:32 +00:00
|
|
|
}
|
2015-08-07 14:45:17 +00:00
|
|
|
|
2017-08-30 21:53:55 +00:00
|
|
|
/**
|
|
|
|
* Checks if CodeMirror for textarea wikitext editor should be loaded on this page or not.
|
|
|
|
*
|
2019-03-11 06:04:20 +00:00
|
|
|
* @param OutputPage $out
|
2017-08-30 21:53:55 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2023-09-19 17:59:29 +00:00
|
|
|
private function isCodeMirrorOnPage( OutputPage $out ): bool {
|
2019-03-11 06:04:20 +00:00
|
|
|
// Disable CodeMirror when CodeEditor is active on this page
|
2023-05-06 21:19:32 +00:00
|
|
|
// Depends on ext.codeEditor being added by \MediaWiki\EditPage\EditPage::showEditForm:initial
|
2023-06-06 12:37:54 +00:00
|
|
|
if ( in_array( 'ext.codeEditor', $out->getModules(), true ) ) {
|
2019-03-11 06:04:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-03-11 06:17:13 +00:00
|
|
|
// Disable CodeMirror when the WikiEditor toolbar is not enabled in preferences
|
2022-09-19 19:01:32 +00:00
|
|
|
if ( !$this->userOptionsLookup->getOption( $out->getUser(), 'usebetatoolbar' ) ) {
|
2019-03-11 06:17:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-06-06 12:37:54 +00:00
|
|
|
return in_array( $out->getActionName(), [ 'edit', 'submit' ], true ) &&
|
2018-04-18 11:06:37 +00:00
|
|
|
// CodeMirror on textarea wikitext editors doesn't support RTL (T170001)
|
2022-04-15 20:46:08 +00:00
|
|
|
!$out->getTitle()->getPageLanguage()->isRTL();
|
2017-08-30 21:53:55 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 05:00:43 +00:00
|
|
|
/**
|
|
|
|
* BeforePageDisplay hook handler
|
|
|
|
*
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
|
|
|
|
*
|
2019-04-03 23:38:24 +00:00
|
|
|
* @param OutputPage $out
|
|
|
|
* @param Skin $skin
|
2022-09-19 19:01:32 +00:00
|
|
|
* @return void This hook must not abort, it must return no value
|
2015-03-16 05:00:43 +00:00
|
|
|
*/
|
2022-09-19 19:01:32 +00:00
|
|
|
public function onBeforePageDisplay( $out, $skin ): void {
|
2023-09-19 17:59:29 +00:00
|
|
|
if ( !$this->isCodeMirrorOnPage( $out ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove check for cm6enable flag after migration is complete
|
|
|
|
if ( $this->useV6 || $out->getRequest()->getRawVal( 'cm6enable' ) ) {
|
|
|
|
$out->addModules( 'ext.CodeMirror.v6.WikiEditor' );
|
|
|
|
} else {
|
2023-08-17 04:58:59 +00:00
|
|
|
$out->addModules( 'ext.CodeMirror.WikiEditor' );
|
2019-03-09 15:52:49 +00:00
|
|
|
|
2022-09-19 19:01:32 +00:00
|
|
|
if ( $this->userOptionsLookup->getOption( $out->getUser(), 'usecodemirror' ) ) {
|
2019-03-09 15:52:49 +00:00
|
|
|
// These modules are predelivered for performance when needed
|
|
|
|
// keep these modules in sync with ext.CodeMirror.js
|
|
|
|
$out->addModules( [ 'ext.CodeMirror.lib', 'ext.CodeMirror.mode.mediawiki' ] );
|
|
|
|
}
|
2015-03-16 05:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 11:30:44 +00:00
|
|
|
/**
|
|
|
|
* Hook handler for enabling bracket matching.
|
|
|
|
*
|
2021-04-12 09:25:22 +00:00
|
|
|
* TODO: restrict to pages where codemirror might be enabled.
|
|
|
|
*
|
2020-12-11 11:30:44 +00:00
|
|
|
* @param array &$vars Array of variables to be added into the output of the startup module
|
2022-09-19 19:01:32 +00:00
|
|
|
* @param string $skin
|
|
|
|
* @param Config $config
|
|
|
|
* @return void This hook must not abort, it must return no value
|
2020-12-11 11:30:44 +00:00
|
|
|
*/
|
2022-09-19 19:01:32 +00:00
|
|
|
public function onResourceLoaderGetConfigVars( array &$vars, $skin, Config $config ): void {
|
2021-04-12 09:25:22 +00:00
|
|
|
$vars['wgCodeMirrorLineNumberingNamespaces'] = $config->get( 'CodeMirrorLineNumberingNamespaces' );
|
2020-12-11 11:30:44 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 05:00:43 +00:00
|
|
|
/**
|
|
|
|
* GetPreferences hook handler
|
|
|
|
*
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
|
|
|
|
*
|
2015-08-07 14:45:17 +00:00
|
|
|
* @param User $user
|
2017-09-09 16:52:39 +00:00
|
|
|
* @param array &$defaultPreferences
|
2022-09-19 19:01:32 +00:00
|
|
|
* @return bool|void True or no return value to continue or false to abort
|
2015-03-16 05:00:43 +00:00
|
|
|
*/
|
2022-09-19 19:01:32 +00:00
|
|
|
public function onGetPreferences( $user, &$defaultPreferences ) {
|
2021-07-08 00:57:49 +00:00
|
|
|
// CodeMirror is disabled by default for all users. It can enabled for everyone
|
|
|
|
// by default by adding '$wgDefaultUserOptions['usecodemirror'] = 1;' into LocalSettings.php
|
2017-04-01 15:08:36 +00:00
|
|
|
$defaultPreferences['usecodemirror'] = [
|
2015-04-07 09:13:43 +00:00
|
|
|
'type' => 'api',
|
2017-04-01 15:08:36 +00:00
|
|
|
];
|
2022-04-06 10:06:07 +00:00
|
|
|
|
2022-06-13 11:20:24 +00:00
|
|
|
$defaultPreferences['usecodemirror-colorblind'] = [
|
|
|
|
'type' => 'toggle',
|
|
|
|
'label-message' => 'codemirror-prefs-colorblind',
|
|
|
|
'help-message' => 'codemirror-prefs-colorblind-help',
|
|
|
|
'section' => 'editing/accessibility',
|
|
|
|
];
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-04-07 09:13:43 +00:00
|
|
|
|
2014-08-20 17:03:46 +00:00
|
|
|
}
|