From b8bdd64ee5b86c86039123a592d1e2b89bd6beab Mon Sep 17 00:00:00 2001 From: MusikAnimal Date: Mon, 4 Apr 2022 18:10:11 -0400 Subject: [PATCH] Realtime Preview: add as a Beta Feature For MW installations where BetaFeatures is not available, Realtime Preview is enabled solely with the $wgWikiEditorRealtimePreview configuration setting. Bug: T304596 Change-Id: I880022883bf526a2aef237f4a75e790b0f9bd4e5 --- .phan/config.php | 2 ++ extension.json | 3 ++- i18n/en.json | 4 +++- i18n/qqq.json | 4 +++- includes/Hooks.php | 37 +++++++++++++++++++++++++++-- modules/images/beta-feature-ltr.svg | 1 + modules/images/beta-feature-rtl.svg | 1 + 7 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 modules/images/beta-feature-ltr.svg create mode 100644 modules/images/beta-feature-rtl.svg diff --git a/.phan/config.php b/.phan/config.php index 70a6827c..2b885e1d 100644 --- a/.phan/config.php +++ b/.phan/config.php @@ -8,6 +8,7 @@ $cfg['directory_list'] = array_merge( '../../extensions/EventLogging', '../../extensions/WikimediaEvents', '../../extensions/ConfirmEdit', + '../../extensions/BetaFeatures', ] ); @@ -17,6 +18,7 @@ $cfg['exclude_analysis_directory_list'] = array_merge( '../../extensions/EventLogging', '../../extensions/WikimediaEvents', '../../extensions/ConfirmEdit', + '../../extensions/BetaFeatures', ] ); diff --git a/extension.json b/extension.json index 3e2d33f4..9821e382 100644 --- a/extension.json +++ b/extension.json @@ -29,7 +29,8 @@ "EditPageGetPreviewContent": "WikiEditorHooks", "ListDefinedTags": "WikiEditorHooks", "ChangeTagsListActive": "WikiEditorHooks", - "RecentChange_save": "WikiEditorHooks" + "RecentChange_save": "WikiEditorHooks", + "GetBetaFeaturePreferences": "MediaWiki\\Extension\\WikiEditor\\Hooks::onGetBetaFeaturePreferences" }, "HookHandlers": { "WikiEditorHooks": { diff --git a/i18n/en.json b/i18n/en.json index e1212e08..9835614e 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -200,5 +200,7 @@ "tag-wikieditor-description": "Edit made using [[mw:Special:MyLanguage/Extension:WikiEditor|WikiEditor]] (2010 wikitext editor)", "wikieditor-realtimepreview-preview": "Preview", "wikieditor-realtimepreview-error": "Preview not loading", - "wikieditor-realtimepreview-reload": "Reload" + "wikieditor-realtimepreview-reload": "Reload", + "wikieditor-realtimepreview-beta-label": "Realtime Preview", + "wikieditor-realtimepreview-beta-desc": "See how wikitext changes will appear to readers inside the [[mw:Special:MyLanguage/Extension:WikiEditor|2010 editor]]." } diff --git a/i18n/qqq.json b/i18n/qqq.json index afd8496f..f80ea0c2 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -231,5 +231,7 @@ "tag-wikieditor-description": "Long description of the wikieditor tag ({{msg-mw|Tag-wikieditor}}).\n\nShown on [[Special:Tags]].\n\nSee also:\n* {{msg-mw|Tag-wikieditor}}", "wikieditor-realtimepreview-preview": "Label for the toolbar button to enable/disable realtime preview.", "wikieditor-realtimepreview-error": "Header text for realtime preview errors.", - "wikieditor-realtimepreview-reload": "Button text for the 'reload' button on realtime preview errors." + "wikieditor-realtimepreview-reload": "Button text for the 'reload' button on realtime preview errors.", + "wikieditor-realtimepreview-beta-label": "Used in [[Special:Preferences]].\n\nUsed as label for checkbox to enable Realtime Preview as a Beta Feature.\n\nThe description for this checkbox is: {{msg-mw|wikieditor-realtimepreview-beta-desc}}.", + "wikieditor-realtimepreview-beta-desc": "Used in [[Special:Preferences]].\n\nUsed as description for the checkbox to enable Realtime Preview as a Beta Feature.\n\nThe label for this checkbox is {{msg-mw|wikieditor-realtimepreview-beta-label}}." } diff --git a/includes/Hooks.php b/includes/Hooks.php index 7a72bc1e..46c42161 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -18,6 +18,7 @@ use Html; use MediaWiki\Cache\CacheKeyHelper; use MediaWiki\ChangeTags\Hook\ChangeTagsListActiveHook; use MediaWiki\ChangeTags\Hook\ListDefinedTagsHook; +use MediaWiki\Extension\BetaFeatures\BetaFeatures; use MediaWiki\Extension\EventLogging\EventLogging; use MediaWiki\Hook\EditPage__attemptSave_afterHook; use MediaWiki\Hook\EditPage__attemptSaveHook; @@ -222,8 +223,14 @@ class Hooks implements if ( $this->userOptionsLookup->getBoolOption( $user, 'usebetatoolbar' ) ) { $outputPage->addModuleStyles( 'ext.wikiEditor.styles' ); $outputPage->addModules( 'ext.wikiEditor' ); - // Optionally enable Realtime Preview. - if ( $this->config->get( 'WikiEditorRealtimePreview' ) ) { + // Optionally enable Realtime Preview, and behind a BetaFeature where applicable. + $betaFeaturesInstalled = ExtensionRegistry::getInstance()->isLoaded( 'BetaFeatures' ); + $user = $article->getContext()->getUser(); + $betaFeatureEnabled = $betaFeaturesInstalled && + BetaFeatures::isFeatureEnabled( $user, 'wikieditor-realtime-preview' ); + if ( $this->config->get( 'WikiEditorRealtimePreview' ) && + ( $betaFeatureEnabled || !$betaFeaturesInstalled ) + ) { $outputPage->addModules( 'ext.wikiEditor.realtimepreview' ); } } @@ -568,4 +575,30 @@ class Hooks implements } return true; } + + /** + * @param User $user + * @param array &$prefs + * @return void + */ + public static function onGetBetaFeaturePreferences( $user, &$prefs ) { + $config = MediaWikiServices::getInstance()->getMainConfig(); + if ( !$config->get( 'WikiEditorRealtimePreview' ) ) { + return; + } + $extensionAssetsPath = $config->get( 'ExtensionAssetsPath' ); + $prefs['wikieditor-realtime-preview'] = [ + 'label-message' => 'wikieditor-realtimepreview-beta-label', + 'desc-message' => 'wikieditor-realtimepreview-beta-desc', + 'screenshot' => [ + 'ltr' => "$extensionAssetsPath/WikiEditor/modules/images/beta-feature-ltr.svg", + 'rtl' => "$extensionAssetsPath/WikiEditor/modules/images/beta-feature-rtl.svg", + ], + // @todo Update links once mw:Help:Extension:WikiEditor/Realtime_Preview is written. + 'info-link' => 'https://meta.wikimedia.org/wiki/Special:MyLanguage/' . + 'Community_Wishlist_Survey_2021/Real_Time_Preview_for_Wikitext', + 'discussion-link' => 'https://meta.wikimedia.org/wiki/' . + 'Talk:Community_Wishlist_Survey_2021/Real_Time_Preview_for_Wikitext', + ]; + } } diff --git a/modules/images/beta-feature-ltr.svg b/modules/images/beta-feature-ltr.svg new file mode 100644 index 00000000..479a6e3d --- /dev/null +++ b/modules/images/beta-feature-ltr.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/modules/images/beta-feature-rtl.svg b/modules/images/beta-feature-rtl.svg new file mode 100644 index 00000000..fdc1b386 --- /dev/null +++ b/modules/images/beta-feature-rtl.svg @@ -0,0 +1 @@ + \ No newline at end of file