Hooks: ensure GetPreferences hook and 2017 editor respect beta feature

For users of the 2003 editor (or if WikiEditor is not available), the
user is supposed to still be able to toggle CodeMirror on and off using
the visible preference in the new 'Syntax highlighting' section of
Special:Preferences. This patch fixes that, restoring use of standalone
CodeMirror for those who have the beta feature on but WikiEditor off.

This patch also fixes a bug that prevented 2017 editor users from
getting CM6 via the beta feature.

Bug: T376735
Change-Id: I69562ee1f936050301900ee80ab1aa8eaf92ec0d
This commit is contained in:
MusikAnimal 2024-12-03 18:11:06 -05:00
parent c3689196ad
commit 541741d763
2 changed files with 16 additions and 6 deletions

View file

@ -164,10 +164,17 @@ class Hooks implements
* @todo Remove check for cm6enable flag after migration is complete * @todo Remove check for cm6enable flag after migration is complete
*/ */
private function shouldUseV6( OutputPage $out ): bool { private function shouldUseV6( OutputPage $out ): bool {
return $this->useV6 || $out->getRequest()->getRawVal( 'cm6enable' ) || ( return $this->useV6 || $out->getRequest()->getBool( 'cm6enable' ) ||
ExtensionRegistry::getInstance()->isLoaded( 'BetaFeatures' ) && $this->isBetaFeatureEnabled( $out->getUser() );
BetaFeatures::isFeatureEnabled( $out->getUser(), 'codemirror-beta-feature-enable' ) }
);
/**
* @param User $user
* @return bool
*/
private function isBetaFeatureEnabled( User $user ): bool {
return ExtensionRegistry::getInstance()->isLoaded( 'BetaFeatures' ) &&
BetaFeatures::isFeatureEnabled( $user, 'codemirror-beta-feature-enable' );
} }
/** /**
@ -194,7 +201,7 @@ class Hooks implements
* @return bool|void True or no return value to continue or false to abort * @return bool|void True or no return value to continue or false to abort
*/ */
public function onGetPreferences( $user, &$defaultPreferences ) { public function onGetPreferences( $user, &$defaultPreferences ) {
if ( !$this->useV6 ) { if ( !$this->useV6 && !$this->isBetaFeatureEnabled( $user ) ) {
$defaultPreferences['usecodemirror'] = [ $defaultPreferences['usecodemirror'] = [
'type' => 'api', 'type' => 'api',
]; ];

View file

@ -7,9 +7,12 @@ require( '../ext.CodeMirror.data.js' );
// TODO: Change the PluginModules in extension.json when fully migrated to v6 and drop this. // TODO: Change the PluginModules in extension.json when fully migrated to v6 and drop this.
mw.hook( 've.loadModules' ).add( ( addPlugin ) => { mw.hook( 've.loadModules' ).add( ( addPlugin ) => {
const urlParams = new URLSearchParams( window.location.search ); const urlParams = new URLSearchParams( window.location.search );
const shouldUseV6 = mw.config.get( 'extCodeMirrorConfig' ).useV6 ||
urlParams.get( 'cm6enable' ) ||
mw.user.options.get( 'codemirror-beta-feature-enable' ) === '1';
// VE would wait for plugin callbacks to resolve before initialisation. // VE would wait for plugin callbacks to resolve before initialisation.
if ( mw.config.get( 'extCodeMirrorConfig' ).useV6 || urlParams.get( 'cm6enable' ) ) { if ( shouldUseV6 ) {
addPlugin( () => mw.loader.using( 'ext.CodeMirror.v6.visualEditor' ) ); addPlugin( () => mw.loader.using( 'ext.CodeMirror.v6.visualEditor' ) );
} else { } else {
addPlugin( () => mw.loader.using( 'ext.CodeMirror.visualEditor' ) ); addPlugin( () => mw.loader.using( 'ext.CodeMirror.visualEditor' ) );