2014-08-20 17:03:46 +00:00
|
|
|
<?php
|
|
|
|
|
2022-02-06 15:13:05 +00:00
|
|
|
namespace MediaWiki\Extension\CodeMirror;
|
|
|
|
|
|
|
|
use Config;
|
2022-09-19 19:01:32 +00:00
|
|
|
use MediaWiki\Hook\BeforePageDisplayHook;
|
|
|
|
use MediaWiki\Preferences\Hook\GetPreferencesHook;
|
|
|
|
use MediaWiki\ResourceLoader\Hook\ResourceLoaderGetConfigVarsHook;
|
|
|
|
use MediaWiki\User\UserOptionsLookup;
|
2022-02-06 15:13:05 +00:00
|
|
|
use OutputPage;
|
|
|
|
use Skin;
|
|
|
|
use User;
|
2020-12-11 11:30:44 +00:00
|
|
|
|
2022-09-19 19:01:32 +00:00
|
|
|
class Hooks implements
|
|
|
|
BeforePageDisplayHook,
|
|
|
|
ResourceLoaderGetConfigVarsHook,
|
|
|
|
GetPreferencesHook
|
|
|
|
{
|
|
|
|
|
|
|
|
/** @var UserOptionsLookup */
|
|
|
|
private $userOptionsLookup;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param UserOptionsLookup $userOptionsLookup
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
UserOptionsLookup $userOptionsLookup
|
|
|
|
) {
|
|
|
|
$this->userOptionsLookup = $userOptionsLookup;
|
|
|
|
}
|
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
|
|
|
|
*/
|
2022-09-19 19:01:32 +00:00
|
|
|
private function isCodeMirrorOnPage( OutputPage $out ) {
|
2019-03-11 06:04:20 +00:00
|
|
|
// Disable CodeMirror when CodeEditor is active on this page
|
|
|
|
// Depends on ext.codeEditor being added by EditPage::showEditForm:initial
|
|
|
|
if ( in_array( 'ext.codeEditor', $out->getModules() ) ) {
|
|
|
|
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;
|
|
|
|
}
|
2022-04-15 20:46:08 +00:00
|
|
|
return in_array( $out->getActionName(), [ 'edit', 'submit' ] ) &&
|
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 {
|
|
|
|
if ( $this->isCodeMirrorOnPage( $out ) ) {
|
2017-04-28 13:42:30 +00:00
|
|
|
$out->addModules( 'ext.CodeMirror' );
|
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
|
|
|
}
|