From 5a8c15f3c223ce29823569fc856987f819d89ce5 Mon Sep 17 00:00:00 2001 From: alistair3149 Date: Sun, 10 Jan 2021 17:38:11 -0500 Subject: [PATCH] refactor: move theme preference underneath skin preferences --- i18n/de.json | 8 +++--- i18n/en.json | 8 +++--- i18n/qqq.json | 8 +++--- includes/CitizenHooks.php | 59 +++++++++++++++++++++++++-------------- 4 files changed, 50 insertions(+), 33 deletions(-) diff --git a/i18n/de.json b/i18n/de.json index a91086cd..570d10a8 100644 --- a/i18n/de.json +++ b/i18n/de.json @@ -10,8 +10,8 @@ "citizen-footer-desc": "Bearbeiten Sie diesen Text auf MediaWiki:Citizen-footer-desc", "citizen-footer-tagline": "Bearbeiten Sie diesen Text auf MediaWiki:Citizen-footer-tagline", - "citizen-upo-style": "Skin Citizen Farbschema", - "citizen-upo-style-auto": "Automatisch", - "citizen-upo-style-light": "Hell", - "citizen-upo-style-dark": "Dunkel" + "prefs-citizen-theme-label": "Thema", + "prefs-citizen-theme-option-auto": "Automatisch", + "prefs-citizen-theme-option-light": "Hell", + "prefs-citizen-theme-option-dark": "Dunkel" } diff --git a/i18n/en.json b/i18n/en.json index 50cab5e3..a01d65de 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -19,8 +19,8 @@ "citizen-search-fulltext": "Search pages containing", - "citizen-upo-style": "Skin Citizen Color Scheme", - "citizen-upo-style-auto": "Auto", - "citizen-upo-style-light": "Light", - "citizen-upo-style-dark": "Dark" + "prefs-citizen-theme-label": "Theme", + "prefs-citizen-theme-option-auto": "Auto", + "prefs-citizen-theme-option-light": "Light", + "prefs-citizen-theme-option-dark": "Dark" } diff --git a/i18n/qqq.json b/i18n/qqq.json index a18d1f24..95199be2 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -16,8 +16,8 @@ "citizen-footer-tagline": "Edit this text on MediaWiki:Citizen-footer-tagline", "citizen-search-fulltext": "Fulltext search suggestion", - "citizen-upo-style": "Skin Citizen Color Scheme", - "citizen-upo-style-auto": "Auto", - "citizen-upo-style-light": "Light", - "citizen-upo-style-dark": "Dark" + "prefs-citizen-theme-label": "Label for the theme dropdown in Special:Preferences", + "prefs-citizen-theme-option-auto": "Auto", + "prefs-citizen-theme-option-light": "Light", + "prefs-citizen-theme-option-dark": "Dark" } diff --git a/includes/CitizenHooks.php b/includes/CitizenHooks.php index e28eb2e1..45840045 100644 --- a/includes/CitizenHooks.php +++ b/includes/CitizenHooks.php @@ -154,29 +154,46 @@ class CitizenHooks { } /** - * @param User $user - * @param array &$preferences + * Add Citizen preferences to the user's Special:Preferences page directly underneath skins. + * Based on Vector's implementation + * + * @param User $user User whose preferences are being modified. + * @param array[] &$prefs Preferences description array, to be fed to a HTMLForm object. */ - public static function onGetPreferences( $user, &$preferences ) { - $options = MediaWikiServices::getInstance() - ->getUserOptionsLookup() - ->getOptions( $user ); - - if ( $options['skin'] !== 'citizen' ) { - return; - } - - // A checkbox - $preferences['CitizenThemeUser'] = [ - 'type' => 'select', - 'label-message' => 'citizen-upo-style', - 'section' => 'rendering/skin', - 'options' => [ - wfMessage( 'citizen-upo-style-auto' )->escaped() => 'auto', - wfMessage( 'citizen-upo-style-light' )->escaped() => 'light', - wfMessage( 'citizen-upo-style-dark' )->escaped() => 'dark', + public static function onGetPreferences( $user, &$prefs ) { + // Preferences to add. + $citizenPrefs = [ + 'CitizenThemeUser' => [ + 'type' => 'select', + // Droptown title + 'label-message' => 'prefs-citizen-theme-label', + // The tab location and title of the section to insert the checkbox. The bit after the slash + // indicates that a prefs-skin-prefs string will be provided. + 'section' => 'rendering/skin/skin-prefs', + 'options' => [ + wfMessage( 'prefs-citizen-theme-option-auto' )->escaped() => 'auto', + wfMessage( 'prefs-citizen-theme-option-light' )->escaped() => 'light', + wfMessage( 'prefs-citizen-theme-option-dark' )->escaped() => 'dark', + ], + 'default' => 'auto', + // Only show this section when the Citizen skin is checked. The JavaScript client also uses + // this state to determine whether to show or hide the whole section. + 'hide-if' => [ '!==', 'wpskin', 'citizen' ], ], - 'default' => $options['CitizenThemeUser'] ?? 'auto' ]; + + // Seek the skin preference section to add Citizen preferences just below it. + $skinSectionIndex = array_search( 'skin', array_keys( $prefs ) ); + if ( $skinSectionIndex !== false ) { + // Skin preference section found. Inject Citizen skin-specific preferences just below it. + // This pattern can be found in Popups too. See T246162. + $citizenSectionIndex = $skinSectionIndex + 1; + $prefs = array_slice( $prefs, 0, $citizenSectionIndex, true ) + + $citizenPrefs + + array_slice( $prefs, $citizenSectionIndex, null, true ); + } else { + // Skin preference section not found. Just append Citizen skin-specific preferences. + $prefs += $citizenPrefs; + } } }