Skip to content

Commit

Permalink
Fixes issues on database always updating.
Browse files Browse the repository at this point in the history
Progresses on #167: SYSTEM_ADMIN_VARIABLE no longer required for texts reparsing.
Closes #182: MeCab won't for TTS if not necessary.
  • Loading branch information
HugoFara committed Jan 5, 2024
2 parents 015c424 + 4d6b109 commit 12a90a3
Show file tree
Hide file tree
Showing 18 changed files with 407 additions and 114 deletions.
63 changes: 62 additions & 1 deletion api.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require_once __DIR__ . '/inc/ajax_save_text_position.php';
require_once __DIR__ . '/inc/ajax_show_imported_terms.php';
require_once __DIR__ . '/inc/ajax_edit_impr_text.php';
require_once __DIR__ . '/inc/langdefs.php';


/**
Expand Down Expand Up @@ -52,6 +53,9 @@ function endpoint_exits($method, $requestUri)
{
// Set up API endpoints
$endpoints = [
'languages' => [ 'GET' ],
//'languages/(?<lang-id>\d+)/reading-configuration' => [ 'GET' ],

'media-files' => [ 'GET' ],

'phonetic-reading' => [ 'GET' ],
Expand Down Expand Up @@ -153,6 +157,36 @@ function media_files($get_req)
return get_media_paths();
}


/**
* The way text should be read
*/
function readingConfiguration($get_req): array
{
global $tbpref;
// language, voiceAPI, abbr
$req = do_mysqli_query(
"SELECT LgName, LgTTSVoiceAPI, LgRegexpWordCharacters FROM {$tbpref}languages
WHERE LgID = " . $get_req["lang_id"]
);
$record = mysqli_fetch_assoc($req);
$abbr = getLanguageCode($get_req["lang_id"], LWT_LANGUAGES_ARRAY);
if ($record["LgTTSVoiceAPI"] != '') {
$readingMode = "external";
} elseif ($record["LgRegexpWordCharacters"] == "mecab") {
$readingMode = "internal";
} else {
$readingMode = "direct";
}
return array(
"name" => $record["LgName"],
"voiceapi" => $record["LgTTSVoiceAPI"],
"word_parsing" => $record["LgRegexpWordCharacters"],
"abbreviation" => $abbr,
"reading_mode" => $readingMode
);
}

/**
* Get the phonetic reading of a word based on it's language.
*
Expand All @@ -161,10 +195,16 @@ function media_files($get_req)
* @return string[] JSON-encoded result
*
* @psalm-return array{phonetic_reading: string}
*
* @since 2.10.0-fork Can also accept a language ID with "lgid" parameter
*/
function get_phonetic_reading($get_req): array
{
$data = phonetic_reading($get_req['text'], $get_req['lang']);
if (array_key_exists("lang_id", $get_req)) {
$data = phoneticReading($get_req['text'], $get_req['lang_id']);
} else {
$data = phonetic_reading($get_req['text'], $get_req['lang']);
}
return array("phonetic_reading" => $data);
}

Expand Down Expand Up @@ -634,6 +674,27 @@ function request_handler($method, $requestUri, $post_param)
parse_str($uri_query, $req_param);
}
switch ($endpoint_fragments[0]) {
case 'languages':
if (ctype_digit($endpoint_fragments[1])) {
if ($endpoint_fragments[2] == 'reading-configuration') {
$req_param['lang_id'] = (int) $endpoint_fragments[1];
$answer = readingConfiguration($req_param);
send_response(200, $answer);
} else {
send_response(
404,
['error' => 'Expected "reading-configuration", Got ' .
$endpoint_fragments[2]]
);
}
} else {
send_response(
404,
['error' => 'Expected Language ID, found ' .
$endpoint_fragments[1]]
);
}
break;
case 'media-files':
$answer = media_files($req_param);
send_response(200, $answer);
Expand Down
13 changes: 5 additions & 8 deletions do_test_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,8 +722,6 @@ function do_test_test_javascript_clickable($wo_record, $solution)
{
global $tbpref;
$wid = $wo_record['WoID'];
$abbr = getLanguageCode($wo_record['WoLgID'], LWT_LANGUAGES_ARRAY);
$phoneticText = phonetic_reading($wo_record['WoText'], $abbr);
$voiceApi = get_first_value(
"SELECT LgTTSVoiceAPI AS value FROM {$tbpref}languages
WHERE LgID = " . $wo_record['WoLgID']
Expand All @@ -734,14 +732,13 @@ function do_test_test_javascript_clickable($wo_record, $solution)
* Read the word aloud
*/
function read_word() {
if (('speechSynthesis' in window) &&
document.getElementById('utterance-allowed').checked) {
const text = <?php echo json_encode($phoneticText); ?>;
const lang = <?php echo json_encode($abbr); ?>;
readRawTextAloud(text, lang);
}
speechDispatcher(
<?php echo json_encode($wo_record['WoText']); ?>,
<?php echo json_encode($wo_record['WoLgID']); ?>
);
}

LWT_DATA.language.id = <?php echo json_encode($wo_record['WoLgID']); ?>;
LWT_DATA.test.solution = <?php echo prepare_textdata_js($solution); ?>;
LWT_DATA.word.id = <?php echo $wid; ?>;
LWT_DATA.language.ttsVoiceApi = <?php echo json_encode($voiceApi); ?>;
Expand Down
4 changes: 2 additions & 2 deletions do_text_header.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ function browser_tts($text, $languageName): void
WHERE LgName = " . convert_string_to_sqlsyntax($languageName)
);
$languageCode = getLanguageCode($lg_id, LWT_LANGUAGES_ARRAY);
// Phonetic reading for this text
$phoneticText = phonetic_reading($text, $languageCode);
// Phonetic reading for this text. As it can be long do not use AJAX
$phoneticText = phoneticReading($text, $lg_id);
$voiceApi = get_first_value(
"SELECT LgTTSVoiceAPI AS value FROM {$tbpref}languages
WHERE LgID = $lg_id"
Expand Down
21 changes: 11 additions & 10 deletions do_text_text.php
Original file line number Diff line number Diff line change
Expand Up @@ -797,33 +797,34 @@ function do_text_text_content($textid, $only_body = true): void
'LWT_DATA' => array(

'language' => array(
'dict_link1' => $wb1,
'dict_link2' => $wb2,
'id' => $langid,
'dict_link1' => $wb1,
'dict_link2' => $wb2,
'translator_link' => $wb3,
'delimiter' => tohtml(
'delimiter' => tohtml(
str_replace(
array('\\',']','-','^'),
array('\\\\','\\]','\\-','\\^'),
getSettingWithDefault('set-term-translation-delimiters')
)
),
'word_parsing' => $record['LgRegexpWordCharacters'],
'rtl' => $rtlScript
'word_parsing' => $record['LgRegexpWordCharacters'],
'rtl' => $rtlScript
),

'text' => array(
'id' => $textid,
'id' => $textid,
'reading_position' => $pos,
'annotations' => json_decode(annotation_to_json($ann))
'annotations' => json_decode(annotation_to_json($ann))
),

'settings' => array(
'jQuery_tooltip' => (
'jQuery_tooltip' => (
getSettingWithDefault('set-tooltip-mode') == 2 ? 1 : 0
),
'hts' => getSettingWithDefault('set-hts'),
'hts' => getSettingWithDefault('set-hts'),
'word_status_filter' => makeStatusClassFilter((int)$visit_status),
'annotations_mode' => $mode_trans
'annotations_mode' => $mode_trans
),
)
);
Expand Down
10 changes: 6 additions & 4 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Many thanks to [@ProgramComputer](https://github.com/ProgramComputer)!
See [#160](https://github.com/HugoFara/lwt/issues/160).
It adds `README.md` and `UNLICENSE.md`.
* Adds `docs/info.html` and `docs/index.html` from [#146](https://github.com/HugoFara/lwt/pull/146).
* Multi-word creation was reviewed to use a simpler code.
* Multi-word creation was reviewed to use a simpler JS code.
* On word review, status + 1 can be set only after word display. Before, you could
press key up at any time to increase term status, but keydown was effective only
after solution display ([#159](https://github.com/HugoFara/lwt/issues/159)).
Expand All @@ -58,8 +58,10 @@ after solution display ([#159](https://github.com/HugoFara/lwt/issues/159)).
It defines interactions with a text on reading.
* For audio URI, raises the characters length limit from 200 to 2048
([#144](https://github.com/HugoFara/lwt/issues/144)).
* Database creation and migration is now handled directly in SQL, for a safer
* Database:
* Database creation and migration is now handled directly in SQL, for a safer
behavior.
* You do no longer need to give to LWT SYSTEM_VARIABLES_ADMIN for text reparsing. (new function `checkExpressions`) ([#167](https://github.com/HugoFara/lwt/issues/167)). It is still necessary for words import.
* Docker changes:
* By default, the images now runs at localhot/lwt and no longer in the
root folder ([installer #4](https://github.com/HugoFara/lwt-docker-installer/pull/4)
Expand All @@ -75,8 +77,6 @@ behavior.
Solves [#129](https://github.com/HugoFara/lwt/issues/129), thanks to the help of PR
[#168](https://github.com/HugoFara/lwt/pull/168).
* Text reading position was not working consistently when adding several known words.
* Japanese was always requiring MeCab for TTS, even if it was not used
([#155](https://github.com/HugoFara/lwt/pull/155)).
* Multi-words:
* Tooltip was not properly saved ([#170](https://github.com/HugoFara/lwt/pull/170)).
* Translation may be escaped two times ([#170](https://github.com/HugoFara/lwt/pull/170)).
Expand All @@ -90,6 +90,8 @@ Solves [#129](https://github.com/HugoFara/lwt/issues/129), thanks to the help of
* Parsing Japanese texts with MeCab was creating warnings when the text was not
finishing by a punctation mark.
* Multi-words were not saved with MeCab parsing on Japanese.
* Japanese was always requiring MeCab for TTS, even if it was not used
([#155](https://github.com/HugoFara/lwt/pull/155)) and [#182](https://github.com/HugoFara/lwt/pull/182).
* On word review (test):
* the space keyboard shortcut may have been inoperating.
* On `api.php`, tests were always set to "multi-word" due to a missing variable
Expand Down
10 changes: 9 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@ Retrieves the files paths to the audio and video files in the media folder.
- **Method**: GET
- **Arguments**: None

### Get Reading Configuration

Retrieves how word should be read for a specific language.

- **Endpoint**: `/languages/{lang-id}/reading-configuration`
- **Method**: GET
- **Arguments**: None

### Get Phonetic Reading

Retrieves the phonetic reading for any specified term.

- **Endpoint**: `/phonetic-reading`
- **Method**: GET
- **Arguments**: `text` and `lang`.
- **Arguments**: `text` and (`lang` or `lg_id`).


### Get Next Word to Review
Expand Down
29 changes: 23 additions & 6 deletions docs/info.html
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,17 @@ <h4>Get Media Files Paths</h4>
<li>
<strong>Arguments</strong>: None</li>
</ul>
<h4>Get Reading Configuration</h4>
<p>Retrieves how word should be read for a specific language.</p>
<ul>
<li>
<strong>Endpoint</strong>: <code>/languages/{lang-id}/reading-configuration</code>
</li>
<li>
<strong>Method</strong>: GET</li>
<li>
<strong>Arguments</strong>: None</li>
</ul>
<h4>Get Phonetic Reading</h4>
<p>Retrieves the phonetic reading for any specified term.</p>
<ul>
Expand All @@ -1788,7 +1799,7 @@ <h4>Get Phonetic Reading</h4>
<li>
<strong>Method</strong>: GET</li>
<li>
<strong>Arguments</strong>: <code>text</code> and <code>lang</code>.</li>
<strong>Arguments</strong>: <code>text</code> and (<code>lang</code> or <code>lg_id</code>).</li>
</ul>
<h4>Get Next Word to Review</h4>
<p>Retrieves the next word to be reviewed.</p>
Expand Down Expand Up @@ -2254,8 +2265,9 @@ <h4>Added</h4>
<code>db/schema/baseline.sql</code> and no longer in PHP code.</li>
<li>You can choose to add romanization for languages that don't need it in the language
settings (<a href="https://github.com/HugoFara/lwt/issues/119">#119</a>).</li>
<li>Docker images are now built for multiple platforms, see
<a href="https://github.com/HugoFara/lwt/pull/169">#159</a>.
<li>Docker images are now built for multiple platforms, see PR
<a href="https://github.com/HugoFara/lwt/pull/169">#169</a>, closing discussion
<a href="https://github.com/HugoFara/lwt/discussions/141">#141</a>.
Many thanks to <a href="https://github.com/ProgramComputer">@ProgramComputer</a>!</li>
</ul>
<h4>Changed</h4>
Expand All @@ -2268,7 +2280,7 @@ <h4>Changed</h4>
<li>Adds <code>docs/info.html</code> and <code>docs/index.html</code> from <a href="https://github.com/HugoFara/lwt/pull/146">#146</a>.</li>
</ul>
</li>
<li>Multi-word creation was reviewed to use a simpler code.</li>
<li>Multi-word creation was reviewed to use a simpler JS code.</li>
<li>On word review, status + 1 can be set only after word display. Before, you could
press key up at any time to increase term status, but keydown was effective only
after solution display (<a href="https://github.com/HugoFara/lwt/issues/159">#159</a>).</li>
Expand All @@ -2287,8 +2299,13 @@ <h4>Changed</h4>
</li>
<li>For audio URI, raises the characters length limit from 200 to 2048
(<a href="https://github.com/HugoFara/lwt/issues/144">#144</a>).</li>
<li>Database:
<ul>
<li>Database creation and migration is now handled directly in SQL, for a safer
behavior.</li>
<li>You do no longer need to give to LWT SYSTEM_VARIABLES_ADMIN for text reparsing. (new function <code>checkExpressions</code>) (<a href="https://github.com/HugoFara/lwt/issues/167">#167</a>). It is still necessary for words import.</li>
</ul>
</li>
<li>Docker changes:
<ul>
<li>By default, the images now runs at localhot/lwt and no longer in the
Expand All @@ -2307,8 +2324,6 @@ <h4>Fixed</h4>
Solves <a href="https://github.com/HugoFara/lwt/issues/129">#129</a>, thanks to the help of PR
<a href="https://github.com/HugoFara/lwt/pull/168">#168</a>.</li>
<li>Text reading position was not working consistently when adding several known words.</li>
<li>Japanese was always requiring MeCab for TTS, even if it was not used
(<a href="https://github.com/HugoFara/lwt/pull/155">#155</a>).</li>
<li>Multi-words:
<ul>
<li>Tooltip was not properly saved (<a href="https://github.com/HugoFara/lwt/pull/170">#170</a>).</li>
Expand All @@ -2326,6 +2341,8 @@ <h4>Fixed</h4>
<li>Parsing Japanese texts with MeCab was creating warnings when the text was not
finishing by a punctation mark.</li>
<li>Multi-words were not saved with MeCab parsing on Japanese.</li>
<li>Japanese was always requiring MeCab for TTS, even if it was not used
(<a href="https://github.com/HugoFara/lwt/pull/155">#155</a>) and <a href="https://github.com/HugoFara/lwt/pull/182">#182</a>.</li>
</ul>
</li>
<li>On word review (test):
Expand Down
9 changes: 3 additions & 6 deletions edit_languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -692,12 +692,9 @@ function (translation) {

testVoiceAPI: function () {
const api_value = document.forms.lg_form.LgTTSVoiceAPI.value;
const prevApi = LWT_DATA.language.ttsVoiceApi;
LWT_DATA.language.ttsVoiceApi = api_value;
const text = document.forms.lg_form.LgVoiceAPIDemo.value;
const lang = <?php echo json_encode($sourceLg); ?>;
speechDispatcher(text, lang);
LWT_DATA.language.ttsVoiceApi = prevApi;
const term = document.forms.lg_form.LgVoiceAPIDemo.value;
const lang = <?php echo json_encode($language->name); ?>;
readTextWithExternal(term, api_value, lang);
},

fullFormCheck: function () {
Expand Down
6 changes: 3 additions & 3 deletions edit_word.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,16 +418,16 @@ class="textarea-noreturn checklength checkoutsidebmp"
/**
* Sets the romanization of a term.
*/
const autoRomanization = function () {
const autoRomanization = function (lgid) {
const term = $('#wordfield').val();
getPhoneticTextAsync(term, LANG_SHORT)
getPhoneticTextAsync(term, lgid)
.then(function (phonetic) {
newword.WoRomanization.value = phonetic["phonetic_reading"];
});
}

autoTranslate();
autoRomanization();
autoRomanization(<?php echo json_encode($lang); ?>);

</script>
<?php
Expand Down
4 changes: 1 addition & 3 deletions ggl.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,9 @@ function translate_term($text, $file, $sl, $tl): void
</h2>

<script type="text/javascript">
LWT_DATA.language.ttsVoiceApi = <?php echo json_encode($voiceApi); ?>;

$('#textToSpeech').on('click', function () {
const txt = <?php echo json_encode($text); ?>;
speechDispatcher(txt, <?php echo json_encode($sl); ?>);
speechDispatcher(txt, <?php echo json_encode($lg_id); ?>);
});

$(document).ready(function() {
Expand Down
Loading

0 comments on commit 12a90a3

Please sign in to comment.