2014-08-20 17:03:46 +00:00
|
|
|
<?php
|
|
|
|
|
2022-02-06 15:13:05 +00:00
|
|
|
namespace MediaWiki\Extension\CodeMirror;
|
|
|
|
|
2023-10-10 19:23:03 +00:00
|
|
|
use ExtensionRegistry;
|
|
|
|
use InvalidArgumentException;
|
2023-10-03 09:15:14 +00:00
|
|
|
use MediaWiki\Config\Config;
|
2023-10-10 19:23:03 +00:00
|
|
|
use MediaWiki\Extension\Gadgets\GadgetRepo;
|
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-11-29 12:38:30 +00:00
|
|
|
use MediaWiki\User\Options\UserOptionsLookup;
|
2023-10-03 09:15:14 +00:00
|
|
|
use MediaWiki\User\User;
|
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
|
|
|
|
{
|
|
|
|
|
2023-09-19 17:59:29 +00:00
|
|
|
private UserOptionsLookup $userOptionsLookup;
|
2023-10-10 19:23:03 +00:00
|
|
|
private array $conflictingGadgets;
|
2023-09-19 17:59:29 +00:00
|
|
|
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' );
|
2023-10-10 19:23:03 +00:00
|
|
|
$this->conflictingGadgets = $config->get( 'CodeMirrorConflictingGadgets' );
|
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
|
2023-10-10 19:23:03 +00:00
|
|
|
* @param ExtensionRegistry|null $extensionRegistry Overridden in tests.
|
2017-08-30 21:53:55 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2023-10-10 19:23:03 +00:00
|
|
|
public function shouldLoadCodeMirror( OutputPage $out, ?ExtensionRegistry $extensionRegistry = null ): bool {
|
|
|
|
$extensionRegistry = $extensionRegistry ?: ExtensionRegistry::getInstance();
|
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-10-11 02:00:08 +00:00
|
|
|
$isRTL = $out->getTitle()->getPageLanguage()->isRTL();
|
2023-06-06 12:37:54 +00:00
|
|
|
return in_array( $out->getActionName(), [ 'edit', 'submit' ], true ) &&
|
2023-10-10 19:23:03 +00:00
|
|
|
// Disable CodeMirror if we're on an edit page with a conflicting gadget. See T178348.
|
|
|
|
!$this->conflictingGadgetsEnabled( $extensionRegistry, $out->getUser() ) &&
|
2023-10-11 02:00:08 +00:00
|
|
|
// CodeMirror 5 on textarea wikitext editors doesn't support RTL (T170001)
|
|
|
|
( !$isRTL || $this->shouldUseV6( $out ) );
|
2017-08-30 21:53:55 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 19:23:03 +00:00
|
|
|
/**
|
|
|
|
* @param ExtensionRegistry $extensionRegistry
|
|
|
|
* @param User $user
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function conflictingGadgetsEnabled( ExtensionRegistry $extensionRegistry, User $user ): bool {
|
|
|
|
if ( !$extensionRegistry->isLoaded( 'Gadgets' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// @phan-suppress-next-line PhanUndeclaredClassMethod Code path won't be followed if class doesn't exist.
|
|
|
|
$gadgetRepo = GadgetRepo::singleton();
|
|
|
|
$conflictingGadgets = array_intersect( $this->conflictingGadgets, $gadgetRepo->getGadgetIds() );
|
|
|
|
foreach ( $conflictingGadgets as $conflictingGadget ) {
|
|
|
|
try {
|
|
|
|
if ( $gadgetRepo->getGadget( $conflictingGadget )->isEnabled( $user ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch ( InvalidArgumentException $e ) {
|
|
|
|
// Safeguard for an invalid gadget ID; treat as gadget not enabled.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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-10-10 19:23:03 +00:00
|
|
|
if ( !$this->shouldLoadCodeMirror( $out ) ) {
|
2023-09-19 17:59:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-11 02:00:08 +00:00
|
|
|
if ( $this->shouldUseV6( $out ) ) {
|
2024-02-14 01:01:08 +00:00
|
|
|
$out->addModules( 'ext.CodeMirror.v6.WikiEditor' );
|
2023-09-19 17:59:29 +00:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 02:00:08 +00:00
|
|
|
/**
|
|
|
|
* @param OutputPage $out
|
|
|
|
* @return bool
|
|
|
|
* @todo Remove check for cm6enable flag after migration is complete
|
|
|
|
*/
|
|
|
|
private function shouldUseV6( OutputPage $out ): bool {
|
|
|
|
return $this->useV6 || $out->getRequest()->getRawVal( 'cm6enable' );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-11-02 18:46:56 +00:00
|
|
|
// The following messages are generated upstream by the 'section' value
|
|
|
|
// * prefs-accessibility
|
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
|
|
|
}
|