From 850b042d6b200e3bc822fb08bc89c04668077fe9 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Sun, 7 Jul 2019 00:00:32 -0600 Subject: [PATCH] Do reference parsing and orthography translation on backend for public view --- src/js/view/render.js | 9 ++---- src/js/view/wordManagement.js | 54 ----------------------------------- src/php/api/Dictionary.php | 39 ++++++++++++++++++++----- 3 files changed, 35 insertions(+), 67 deletions(-) diff --git a/src/js/view/render.js b/src/js/view/render.js index da0fc5f..8ed7090 100644 --- a/src/js/view/render.js +++ b/src/js/view/render.js @@ -4,7 +4,6 @@ import { getWordsStats, getHomonymnNumber } from './utilities'; import { getMatchingSearchWords, highlightSearchTerm, getSearchFilters, getSearchTerm } from './search'; import { showSection } from './displayToggles'; import { setupSearchFilters, setupInfoModal } from './setupListeners'; -import { parseReferences, translateOrthography } from './wordManagement'; import { renderAd } from '../ads'; import { sortWords } from './wordManagement'; @@ -35,7 +34,7 @@ export function renderName() { } export function renderDescription() { - const descriptionHTML = md(removeTags(window.currentDictionary.description)); + const descriptionHTML = md(window.currentDictionary.description); document.getElementById('detailsPanel').innerHTML = '
' + descriptionHTML + '
'; } @@ -161,7 +160,7 @@ export function renderWords() { pronunciation: removeTags(originalWord.pronunciation), partOfSpeech: removeTags(originalWord.partOfSpeech), definition: removeTags(originalWord.definition), - details: parseReferences(removeTags(originalWord.details)), + details: originalWord.details, wordId: originalWord.wordId, }); @@ -170,11 +169,9 @@ export function renderWords() { wordsHTML += renderAd(displayIndex); - let wordNameDisplay = translateOrthography(word.name); - wordsHTML += `
-

${wordNameDisplay}${homonymnNumber > 0 ? ' ' + homonymnNumber.toString() + '' : ''}

+

${word.name}${homonymnNumber > 0 ? ' ' + homonymnNumber.toString() + '' : ''}

${word.pronunciation} ${word.partOfSpeech} diff --git a/src/js/view/wordManagement.js b/src/js/view/wordManagement.js index 6a61ff0..27de19a 100644 --- a/src/js/view/wordManagement.js +++ b/src/js/view/wordManagement.js @@ -1,4 +1,3 @@ -import { wordExists, getHomonymnIndexes } from "./utilities"; import removeDiacritics from "../StackOverflow/removeDiacritics"; export function sortWords() { @@ -10,56 +9,3 @@ export function sortWords() { return removeDiacritics(wordA[sortBy]).toLowerCase() > removeDiacritics(wordB[sortBy]).toLowerCase() ? 1 : -1; }); } - -export function translateOrthography(word) { - window.currentDictionary.details.orthography.translations.forEach(translation => { - translation = translation.split('=').map(value => value.trim()); - if (translation.length > 1 && translation[0] !== '' && translation[1] !== '') { - word = word.replace(new RegExp(translation[0], 'g'), translation[1]); - } - }); - return word; -} - -export function parseReferences(detailsMarkdown) { - const references = detailsMarkdown.match(/\{\{.+?\}\}/g); - if (references && Array.isArray(references)) { - new Set(references).forEach(reference => { - let wordToFind = reference.replace(/\{\{|\}\}/g, ''); - let homonymn = 0; - - if (wordToFind.includes(':')) { - const separator = wordToFind.indexOf(':'); - homonymn = wordToFind.substr(separator + 1); - wordToFind = wordToFind.substring(0, separator); - if (homonymn && homonymn.trim() - && !isNaN(parseInt(homonymn.trim())) && parseInt(homonymn.trim()) > 0) { - homonymn = parseInt(homonymn.trim()); - } else { - homonymn = false; - } - } - - let existingWordId = false; - const homonymnIndexes = getHomonymnIndexes({ name: wordToFind, wordId: -1 }); - - if (homonymn !== false && homonymn > 0) { - if (typeof homonymnIndexes[homonymn - 1] !== 'undefined') { - existingWordId = window.currentDictionary.words[homonymnIndexes[homonymn - 1]].wordId; - } - } else if (homonymn !== false) { - existingWordId = wordExists(wordToFind, true); - } - - if (existingWordId !== false) { - if (homonymn < 1 && homonymnIndexes.length > 0) { - homonymn = 1; - } - const homonymnSubHTML = homonymn > 0 ? '' + homonymn.toString() + '' : ''; - const wordMarkdownLink = `[${translateOrthography(wordToFind)}${homonymnSubHTML}](#${existingWordId})`; - detailsMarkdown = detailsMarkdown.replace(new RegExp(reference, 'g'), wordMarkdownLink); - } - }); - } - return detailsMarkdown; -} diff --git a/src/php/api/Dictionary.php b/src/php/api/Dictionary.php index 0e9c224..21fb3d4 100644 --- a/src/php/api/Dictionary.php +++ b/src/php/api/Dictionary.php @@ -101,7 +101,7 @@ VALUES ($new_id, ?, ?, ?, ?, ?)"; 'externalID' => $result['id'], 'name' => $result['name'], 'specification' => $result['specification'], - 'description' => $result['description'], + 'description' => $this->parseReferences(strip_tags($result['description']), $result['id']), 'createdBy' => $result['public_name'], 'partsOfSpeech' => explode(',', $partsOfSpeech), 'alphabeticalOrder' => array(), @@ -147,11 +147,11 @@ VALUES ($new_id, ?, ?, ?, ?, ?)"; if ($results) { return array_map(function ($row) use ($dictionary) { return array( - 'name' => $row['name'], + 'name' => $this->translateOrthography($row['name'], $dictionary), 'pronunciation' => $row['pronunciation'], 'partOfSpeech' => $row['part_of_speech'], 'definition' => $row['definition'], - 'details' => $this->parseReferences($row['details'], $dictionary), + 'details' => $this->parseReferences(strip_tags($row['details']), $dictionary), 'lastUpdated' => is_null($row['last_updated']) ? intval($row['created_on']) : intval($row['last_updated']), 'createdOn' => intval($row['created_on']), 'wordId' => intval($row['word_id']), @@ -168,11 +168,11 @@ VALUES ($new_id, ?, ?, ?, ?, ?)"; $result = $this->db->query($query, array($dictionary, $word))->fetch(); if ($result) { return array( - 'name' => $result['name'], + 'name' => $this->translateOrthography($result['name'], $dictionary), 'pronunciation' => $result['pronunciation'], 'partOfSpeech' => $result['part_of_speech'], 'definition' => $result['definition'], - 'details' => $this->parseReferences($result['details'], $dictionary), + 'details' => $this->parseReferences(strip_tags($result['details']), $dictionary), 'lastUpdated' => is_null($result['last_updated']) ? intval($result['created_on']) : intval($result['last_updated']), 'createdOn' => intval($result['created_on']), 'wordId' => intval($result['word_id']), @@ -219,8 +219,9 @@ VALUES ($new_id, ?, ?, ?, ?, ?)"; } $homonymn_sub_html = $homonymn > 0 ? '' . $homonymn . '' : ''; $site_root = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], $dictionary_id)); - $markdown_link = '' - . $word_to_find . $homonymn_sub_html . ''; + $markdown_link = '' + . '' . $this->translateOrthography($word_to_find, $dictionary_id) . '' . $homonymn_sub_html + . ''; $details = str_replace($reference, $markdown_link, $details); } } @@ -243,6 +244,30 @@ VALUES ($new_id, ?, ?, ?, ?, ?)"; return array(); } + private function translateOrthography($word, $dictionary) { + if (!isset($this->translations)) { + $this->translations = $this->getTranslations($dictionary); + } + foreach($this->translations as $translation) { + $translation = array_map('trim', explode('=', $translation)); + if (count($translation) > 1 && $translation[0] !== '' && $translation[1] !== '') { + $word = str_replace($translation[0], $translation[1], $word); + } + }; + return $word; + } + + private function getTranslations($dictionary) { + if (is_numeric($dictionary)) { + $query = "SELECT translations FROM dictionary_linguistics WHERE dictionary=?"; + $result = $this->db->query($query, array($dictionary))->fetch(); + if ($result) { + return explode(PHP_EOL, $result['translations']); + } + } + return array(); + } + public function getDetails ($user, $dictionary) { $query = "SELECT * FROM dictionaries JOIN dictionary_linguistics ON dictionary = id WHERE user=$user AND id=$dictionary"; $result = $this->db->query($query)->fetch();