diff --git a/src/Manager/WiktionaryManager.php b/src/Manager/WiktionaryManager.php index f4a4374f12cc3d24b79c46bad59d54df0534eac1..51f4cbd41bfbfa2ae0d9525afa6e2a89c81abe08 100644 --- a/src/Manager/WiktionaryManager.php +++ b/src/Manager/WiktionaryManager.php @@ -12,6 +12,8 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Serializer\SerializerAwareInterface; use Symfony\Component\Serializer\SerializerInterface; +use Symfony\Component\HttpClient\HttpClient; +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; class WiktionaryManager { @@ -64,19 +66,40 @@ class WiktionaryManager // -C, --compact json sans indentation - //$time_start = microtime(true); /*Debug*/ + //$time_start = microtime(true); /*Debug*/ - if ($_ENV['WIKTIONARY_TOOL'] == 'WIKSTRAKTOR') { - $command = $_ENV['WIKSTRAKTOR_COMMAND'] . ' -l ' . $language . ' -w ' . $language . ' -m "' . $word . '" -A -C 2>&1'; - } else { - $command = $_ENV['WIKTEXTRACT_COMMAND'] . ' -l ' . $language . ' -w ' . $language . ' -e "' . $word . '"' . ' -zA 2>&1'; + $url = 'http://localhost:5000/search/' . $language . '/' . $language . '/' . $word . '/a_wikstraktor'; + $client = HttpClient::create(); + + // Make the initial request with a timeout + try { + $response = $client->request('GET', $url, ['timeout' => 2.5]); + + // Check the HTTP status code + if ($response->getStatusCode() === 200) { + // Server is up and returned a successful response + $result = $response->getContent(); + } + } catch (TransportExceptionInterface $exception) { + // Handle timeout for the initial request + $startServerCommand = '/var/www/live-query-wiktextract/venv/bin/python3 /var/www/live-query-wiktextract/src/app.py'; + exec($startServerCommand); + } + + // Stream responses with a timeout + foreach ($client->stream([$client->request('GET', $url)], 1.5) as $response => $chunk) { + if ($chunk->isTimeout()) { + // Handle timeout for streaming responses + $startServerCommand = '/var/www/live-query-wiktextract/venv/bin/python3 /var/www/live-query-wiktextract/src/app.py'; + exec($startServerCommand); + } } - $result = exec($command); - //dump($word);dump($command);dump(microtime(true)-$time_start);dump($result);die();/*Debug*/ + //dump(gettype($result));dump($url);dump(microtime(true)-$time_start);dump($result);/*Debug*/ // Output and error handling $dataArray = json_decode($result, true); + //dump(gettype($dataArray));dump($dataArray);die();/*Debug*/ // On sauvegarde la recherche pour optimiser le nb de requêtes vers le wiktionnaire $this->lastSearch = [ @@ -87,8 +110,7 @@ class WiktionaryManager //dump(json_last_error()); //dump(json_last_error_msg()); -//dump($dataArray);die(); Wiktionary - + //dump($dataArray);die(); return $dataArray; } @@ -135,14 +157,17 @@ class WiktionaryManager $morphologicalLabels = []; $entryData = []; - $word = array_keys($wiktionaryData[0])[2]; + $excludedKeys = ["id", "sources", "translations"]; + $allKeys = array_keys($wiktionaryData[0]); + $single = array_diff($allKeys, $excludedKeys); + $word = reset($single); $entryData['Headword'] = $word; $items = []; foreach ($wiktionaryData as $pos) { - if (array_keys($pos)[2] != $word) { + /*if (array_keys($pos)[3] != $word) { throw new \Exception(sprintf("Items se rapportant à des mots différents dans les données du wiktionnaire pour le mot %s.", $word)); - } + }*/ $posData = $pos[$word]; $item = [];