Sebastian Berlin (WMSE) has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/391006 )

Change subject: Add user preferences
......................................................................

Add user preferences

Created a tab in user preferences for Wikispeech and added options for
voices. The options are read from the config.

Bug: T149302
Change-Id: I91f0956cffcd5cbf12297802b28abb65da33a76f
---
M Hooks.php
M extension.json
M i18n/en.json
M i18n/qqq.json
M modules/ext.wikispeech.js
M tests/qunit/ext.wikispeech.test.js
6 files changed, 94 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikispeech 
refs/changes/06/391006/1

diff --git a/Hooks.php b/Hooks.php
index 7b7e011..6cf22b0 100644
--- a/Hooks.php
+++ b/Hooks.php
@@ -151,4 +151,42 @@
                        $wgWikispeechContentSelector;
                return true;
        }
+
+       /**
+        * Add Wikispeech options to Special:Preferences.
+        *
+        * @param User $user current User object.
+        * @param array &$preferences Preferences array.
+        * @return bool true
+        */
+       static function onGetPreferences( $user, &$preferences ) {
+               self::addVoicePreferences( $preferences );
+               return true;
+       }
+
+       /**
+        * Add preferences for selecting voices per language.
+        *
+        * @param array &$preferences Preferences array.
+        */
+       static function addVoicePreferences( &$preferences ) {
+               global $wgWikispeechVoices;
+               foreach ( $wgWikispeechVoices as $language => $voices ) {
+                       $languageKey = 'wikispeechVoice' . ucfirst( $language );
+                       $languageName = Language::getVariantname( $language );
+                       $options = [ 'Default' => '' ];
+                       foreach ( $voices as $voice ) {
+                               $options[$voice] = $voice;
+                       }
+                       // Set the first voice as default.
+                       $default = reset( $options );
+                       $preferences[$languageKey] = [
+                               'type' => 'select',
+                               'label' => $languageName,
+                               'section' => 'wikispeech/voice',
+                               'options' => $options,
+                               'default' => $default
+                       ];
+               }
+       }
 }
diff --git a/extension.json b/extension.json
index b519ff8..cc12897 100644
--- a/extension.json
+++ b/extension.json
@@ -68,6 +68,9 @@
                ],
                "ResourceLoaderGetConfigVars": [
                        "WikispeechHooks::onResourceLoaderGetConfigVars"
+               ],
+               "GetPreferences": [
+                       "WikispeechHooks::onGetPreferences"
                ]
        },
        "config": {
@@ -115,7 +118,20 @@
                "WikispeechSkipBackRewindsThreshold": 3.0,
                "WikispeechHelpPage": "Help:Wikispeech",
                "WikispeechFeedbackPage": "Wikispeech feedback",
-               "WikispeechContentSelector": "#mw-content-text"
+               "WikispeechContentSelector": "#mw-content-text",
+               "WikispeechVoices": {
+                       "ar": [ "ar-nah-hsmm" ],
+                       "en": [
+                               "dfki-spike-hsmm",
+                               "cmu-slt-flite"
+                       ],
+                       "sv": [ "stts_sv_nst-hsmm" ]
+               }
+       },
+       "DefaultUserOptions": {
+               "wikispeechVoiceAr": "",
+               "wikispeechVoiceEn": "",
+               "wikispeechVoiceSv": ""
        },
        "APIModules": {
                "wikispeech": "ApiWikispeech"
diff --git a/i18n/en.json b/i18n/en.json
index a8d550e..b0b29f3 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -22,5 +22,7 @@
        "apihelp-wikispeech-example-1": "Get segments for the Wikispeech 
extension. The tag <code>sup</code> is removed and so is <code>div</code> when 
it has the class <code>toc</code>. The tags <code>h1</code> and <code>h2</code> 
break segments.",
        "apihelp-wikispeech-example-2": "Get original HTML content and cleaned 
text.",
        "apierror-wikispeech-removetagsinvalidjson": "<var>removetags</var> is 
not a valid JSON string.",
-       "apierror-wikispeech-removetagsinvalid": "<var>removetags</var> is not 
of a valid format. It must be an object with the values being booleans, strings 
or arrays of strings. To not remove any tags, set <var>removetags</var> to 
empty object, <code>{}</code>."
+       "apierror-wikispeech-removetagsinvalid": "<var>removetags</var> is not 
of a valid format. It must be an object with the values being booleans, strings 
or arrays of strings. To not remove any tags, set <var>removetags</var> to 
empty object, <code>{}</code>.",
+       "prefs-wikispeech": "Wikispeech",
+       "prefs-voice": "Voice"
 }
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 711245f..0878dcc 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -23,5 +23,7 @@
        "apihelp-wikispeech-example-1": "{{doc-apihelp-example|wikispeech}}",
        "apihelp-wikispeech-example-2": "{{doc-apihelp-example|wikispeech}}",
        "apierror-wikispeech-removetagsinvalidjson": "{{doc-apierror}}",
-       "apierror-wikispeech-removetagsinvalid": "{{doc-apierror}}"
+       "apierror-wikispeech-removetagsinvalid": "{{doc-apierror}}",
+       "prefs-wikispeech": "Used in user preferences as a tab 
heading.\n{{Identical|wikispeech}}",
+       "prefs-voice": "Used in user preferences as a section heading for voice 
selection."
 }
diff --git a/modules/ext.wikispeech.js b/modules/ext.wikispeech.js
index f8da0c7..68e035a 100644
--- a/modules/ext.wikispeech.js
+++ b/modules/ext.wikispeech.js
@@ -781,16 +781,28 @@
                 */
 
                this.requestTts = function ( text, utterance, callback ) {
-                       var serverUrl = mw.config.get( 'wgWikispeechServerUrl' 
);
+                       var serverUrl, language, voiceKey, voice, data;
+                       serverUrl = mw.config.get( 'wgWikispeechServerUrl' );
+                       language = mw.config.get( 'wgPageContentLanguage' );
+                       // Capitalize first letter in language code.
+                       voiceKey = 'wikispeechVoice' +
+                               language[ 0 ].toUpperCase() +
+                               language.slice( 1 );
+                       voice = mw.user.options.get( voiceKey );
+                       data = {
+                               lang: language,
+                               // eslint-disable-next-line camelcase
+                               input_type: 'text',
+                               input: text
+                       };
+                       if ( voice !== '' ) {
+                               // Set voice if not default.
+                               data.voice = voice;
+                       }
                        $.ajax( {
                                url: serverUrl,
                                method: 'POST',
-                               data: {
-                                       lang: mw.config.get( 
'wgPageContentLanguage' ),
-                                       // eslint-disable-next-line camelcase
-                                       input_type: 'text',
-                                       input: text
-                               },
+                               data: data,
                                dataType: 'json',
                                beforeSend: function ( jqXHR, settings ) {
                                        mw.log(
diff --git a/tests/qunit/ext.wikispeech.test.js 
b/tests/qunit/ext.wikispeech.test.js
index f3a056c..569c7ca 100644
--- a/tests/qunit/ext.wikispeech.test.js
+++ b/tests/qunit/ext.wikispeech.test.js
@@ -260,6 +260,20 @@
                assert.strictEqual( utterances[ 0 ].audio.src, '' );
        } );
 
+       QUnit.test( 'loadAudio(): non default voice', function ( assert ) {
+               assert.expect( 2 );
+               sinon.spy( mw.wikispeech.wikispeech, 'requestTts' );
+               mw.user.options.set( 'wikispeechVoiceEn', 'en-voice' );
+
+               mw.wikispeech.wikispeech.loadAudio( utterances[ 0 ] );
+
+               assert.strictEqual( mw.wikispeech.wikispeech.requestTts.called, 
true );
+               assert.strictEqual(
+                       server.requests[ 0 ].requestBody,
+                       
'lang=en&input_type=text&input=Utterance+zero.&voice=en-voice'
+               );
+       } );
+
        QUnit.test( 'addControlPanel()', function ( assert ) {
                assert.expect( 6 );
                sinon.stub( mw.wikispeech.wikispeech, 
'addStackToPlayStopButton' );

-- 
To view, visit https://gerrit.wikimedia.org/r/391006
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I91f0956cffcd5cbf12297802b28abb65da33a76f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikispeech
Gerrit-Branch: master
Gerrit-Owner: Sebastian Berlin (WMSE) <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to