From 4fc1993c533108c8c456a0389105c7f192d3637c Mon Sep 17 00:00:00 2001
From: Enzo Simonnet <enzosim@laposte.net>
Date: Wed, 31 Jan 2024 11:04:49 -0500
Subject: [PATCH] =?UTF-8?q?gestion=20$word=20fluctuant=20dans=20les=20arra?=
 =?UTF-8?q?y=20+=20gestion=20erreur=20serveur=20=C3=A9teint=20(non=20test?=
 =?UTF-8?q?=C3=A9)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/Manager/WiktionaryManager.php | 49 +++++++++++++++++++++++--------
 1 file changed, 37 insertions(+), 12 deletions(-)

diff --git a/src/Manager/WiktionaryManager.php b/src/Manager/WiktionaryManager.php
index f4a4374..51f4cbd 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 = [];
-- 
GitLab