1 |
<?php
|
1 |
<?php
|
2 |
|
2 |
|
3 |
/*
|
3 |
/*
|
4 |
PHP 7
|
4 |
PHP 7
|
5 |
|
5 |
|
6 |
Script used to synchronize PN occ with CEL
|
6 |
Script used to synchronize PN occ with CEL
|
7 |
*/
|
7 |
*/
|
8 |
|
8 |
|
9 |
class PullPlantnet extends Script {
|
9 |
class PullPlantnet extends Script {
|
10 |
|
10 |
|
11 |
protected $bdd;
|
11 |
protected $bdd;
|
12 |
|
12 |
|
13 |
protected $allowedPartners = ['tb', 'pn'];
|
13 |
protected $allowedPartners = ['tb', 'pn'];
|
14 |
|
14 |
|
15 |
// Nom du fichier contenant les correspondances d'ID TB/PN
|
15 |
// Nom du fichier contenant les correspondances d'ID TB/PN
|
16 |
protected $correspondingIdsFilename = '';
|
16 |
protected $correspondingIdsFilename = '';
|
17 |
|
17 |
|
18 |
// Cache d'informations utilisateurs provenant de l'annuaire TB
|
18 |
// Cache d'informations utilisateurs provenant de l'annuaire TB
|
19 |
protected $userInfos = [];
|
19 |
protected $userInfos = [];
|
20 |
|
20 |
|
21 |
// Cache des obs id PN traitées pendant cette run
|
21 |
// Cache des obs id PN traitées pendant cette run
|
22 |
protected $processedObsId = [];
|
22 |
protected $processedObsId = [];
|
23 |
|
23 |
|
24 |
// Paramètre de suivi et de pagination de l'API des obs PN
|
24 |
// Paramètre de suivi et de pagination de l'API des obs PN
|
25 |
protected $currentPage = '';
|
25 |
protected $currentPage = '';
|
26 |
protected $currentProject = '';
|
26 |
protected $currentProject = '';
|
27 |
protected $hasMore = false;
|
27 |
protected $hasMore = false;
|
28 |
protected $count = 0;
|
28 |
protected $count = 0;
|
29 |
|
29 |
|
30 |
// Traduction des organes de PN
|
30 |
// Traduction des organes de PN
|
31 |
protected $tagsImageTraduits = [
|
31 |
protected $tagsImageTraduits = [
|
32 |
'flower' => 'fleur',
|
32 |
'flower' => 'fleur',
|
33 |
'fruit' => 'fruit',
|
33 |
'fruit' => 'fruit',
|
34 |
'leaf' => 'feuille',
|
34 |
'leaf' => 'feuille',
|
35 |
'bark' => 'écorce',
|
35 |
'bark' => 'écorce',
|
36 |
'branch' => 'branche',
|
36 |
'branch' => 'branche',
|
37 |
'habit' => 'port',
|
37 |
'habit' => 'port',
|
38 |
];
|
38 |
];
|
39 |
|
39 |
|
40 |
// Différents types de lifecle possible pour filtrer les obs
|
40 |
// Différents types de lifecle possible pour filtrer les obs
|
41 |
// Modified renvoi les obs en fonction de leur date de modif
|
41 |
// Modified renvoi les obs en fonction de leur date de modif
|
42 |
// Created, en fonction de la date de création
|
42 |
// Created, en fonction de la date de création
|
43 |
// Deleted, well, you already got it
|
43 |
// Deleted, well, you already got it
|
44 |
protected $lifecycles = [
|
44 |
protected $lifecycles = [
|
45 |
'created',
|
45 |
'created',
|
46 |
'modified',
|
46 |
'modified',
|
47 |
'deleted', // deleted peut être très lent
|
47 |
'deleted', // deleted peut être très lent
|
48 |
];
|
48 |
];
|
49 |
|
49 |
|
50 |
/**
|
50 |
/**
|
51 |
* Paramêtres par défaut disponibles pour la ligne de commande
|
51 |
* Paramêtres par défaut disponibles pour la ligne de commande
|
52 |
* le tableau se construit de la forme suivante :
|
52 |
* le tableau se construit de la forme suivante :
|
53 |
* - clé = nom du paramêtre '-foo'
|
53 |
* - clé = nom du paramêtre '-foo'
|
54 |
* - value = contient un nouveau tableau composé de cette façon :
|
54 |
* - value = contient un nouveau tableau composé de cette façon :
|
55 |
* - booléen: true si le paramêtre est obligatoire
|
55 |
* - booléen: true si le paramêtre est obligatoire
|
56 |
* - booléen ou var : true si le paramêtre nécessite un valeur à sa suite ou la valeur par défaut
|
56 |
* - booléen ou var : true si le paramêtre nécessite un valeur à sa suite ou la valeur par défaut
|
57 |
* - string: description du contenu du paramêtre
|
57 |
* - string: description du contenu du paramêtre
|
58 |
* Les paramêtres optionels devraient être déclaré à la fin du tableau.
|
58 |
* Les paramêtres optionels devraient être déclaré à la fin du tableau.
|
59 |
* Le dernier parametre du tableau peut avoir la valeur '...',
|
59 |
* Le dernier parametre du tableau peut avoir la valeur '...',
|
60 |
* il contiendra alors l'ensemble des paramêtres suivant trouvés sur la ligne de commande.
|
60 |
* il contiendra alors l'ensemble des paramêtres suivant trouvés sur la ligne de commande.
|
61 |
* @var array
|
61 |
* @var array
|
62 |
*/
|
62 |
*/
|
63 |
protected $parametres_autorises = array(
|
63 |
protected $parametres_autorises = array(
|
64 |
'-startDate' => array(false, true, 'Date de début (parsée par strtotime). Ex: "YYYY:MM:DD HH:II:SS"'),
|
64 |
'-startDate' => array(false, true, 'Date de début (parsée par strtotime). Ex: "YYYY:MM:DD HH:II:SS"'),
|
65 |
'-lifecycle' => array(false, true, 'Par défaut : modified ; Au choix parmi : created/modified/deleted (deleted est lent)'),
|
65 |
'-lifecycle' => array(false, true, 'Par défaut : modified ; Au choix parmi : created/modified/deleted (deleted est lent)'),
|
66 |
'-pnObsId' => array(false, true, "ID de l'obs chez PlantNet"),
|
66 |
'-pnObsId' => array(false, true, "ID de l'obs chez PlantNet"),
|
67 |
'-project' => array(false, true, "projectId de l'obs chez PlantNet"),
|
67 |
'-project' => array(false, true, "projectId de l'obs chez PlantNet"),
|
68 |
'-tbObsId' => array(false, true, "ID de l'obs chez Tela"),
|
68 |
'-tbObsId' => array(false, true, "ID de l'obs chez Tela"),
|
69 |
);
|
69 |
);
|
70 |
|
70 |
|
71 |
public function __construct($script_nom, $parametres_cli) {
|
71 |
public function __construct($script_nom, $parametres_cli) {
|
72 |
parent::__construct($script_nom, $parametres_cli);
|
72 |
parent::__construct($script_nom, $parametres_cli);
|
73 |
$this->bdd = new Bdd();
|
73 |
$this->bdd = new Bdd();
|
74 |
|
74 |
|
75 |
$this->correspondingIdsFilename = Config::get('correspondingIdsFilename');
|
75 |
$this->correspondingIdsFilename = Config::get('correspondingIdsFilename');
|
76 |
}
|
76 |
}
|
77 |
|
77 |
|
78 |
public function executer() {
|
78 |
public function executer() {
|
79 |
$cmd = $this->getParametre('a');
|
79 |
$cmd = $this->getParametre('a');
|
80 |
|
80 |
|
81 |
switch ($cmd) {
|
81 |
switch ($cmd) {
|
82 |
case 'updateLatest':
|
82 |
case 'updateLatest':
|
83 |
$this->updateLatest($this->getParametre('startDate'));
|
83 |
$this->updateLatest($this->getParametre('startDate'));
|
84 |
break;
|
84 |
break;
|
85 |
case 'updateOnePN':
|
85 |
case 'updateOnePN':
|
86 |
$this->updateOnePN((int)$this->getParametre('pnObsId'), $this->getParametre('project'));
|
86 |
$this->updateOnePN((int)$this->getParametre('pnObsId'), $this->getParametre('project'));
|
87 |
break;
|
87 |
break;
|
88 |
case 'updateOneTB':
|
88 |
case 'updateOneTB':
|
89 |
$this->updateOneTB($this->getParametre('tbObsId'));
|
89 |
$this->updateOneTB($this->getParametre('tbObsId'));
|
90 |
break;
|
90 |
break;
|
91 |
case 'updateCorrespondingIdsTable':
|
91 |
case 'updateCorrespondingIdsTable':
|
92 |
$this->updateCorrespondingIdsTable();
|
92 |
$this->updateCorrespondingIdsTable();
|
93 |
break;
|
93 |
break;
|
94 |
default:
|
94 |
default:
|
95 |
$msg = "Erreur : la commande '$cmd' n'existe pas!\n"
|
95 |
$msg = "Erreur : la commande '$cmd' n'existe pas!\n"
|
96 |
. ', utilisez plutôt :' . "\n"
|
96 |
. ', utilisez plutôt :' . "\n"
|
97 |
. 'php cli.php PullPlantnet -a updateLatest -startDate "YYYY:MM:DD HH:II:SS" [[-lifecycle [modified]/created/deleted]]' . "\n"
|
97 |
. 'php cli.php PullPlantnet -a updateLatest -startDate "YYYY:MM:DD HH:II:SS" [[-lifecycle [modified]/created/deleted]]' . "\n"
|
98 |
. 'php cli.php PullPlantnet -a updateOnePN -pnObsId "xxxxxx" -project "xxxxxx"' . "\n"
|
98 |
. 'php cli.php PullPlantnet -a updateOnePN -pnObsId "xxxxxx" -project "xxxxxx"' . "\n"
|
99 |
. 'php cli.php PullPlantnet -a updateOneTB -tbObsId "xxxxxx"' . "\n"
|
99 |
. 'php cli.php PullPlantnet -a updateOneTB -tbObsId "xxxxxx"' . "\n"
|
100 |
. 'php cli.php PullPlantnet -a updateCorrespondingIdsTable' . "\n"
|
100 |
. 'php cli.php PullPlantnet -a updateCorrespondingIdsTable' . "\n"
|
101 |
;
|
101 |
;
|
102 |
throw new Exception($msg);
|
102 |
throw new Exception($msg);
|
103 |
}
|
103 |
}
|
104 |
}
|
104 |
}
|
105 |
|
105 |
|
106 |
/**
|
106 |
/**
|
107 |
* Considering all modified pn obs since startDate
|
107 |
* Considering all modified pn obs since startDate
|
108 |
* One pn project after an other
|
108 |
* One pn project after an other
|
109 |
* Inserting obs in tb db (if new and matching user email)
|
109 |
* Inserting obs in tb db (if new and matching user email)
|
110 |
*/
|
110 |
*/
|
111 |
private function updateLatest(string $startDate = '1917:03:15 00:00:00', string $lifecycle = 'modified') {
|
111 |
private function updateLatest(string $startDate = '1917:03:15 00:00:00', string $lifecycle = 'modified') {
|
112 |
$startDate = strtotime($startDate) * 1000; // we need a microtimestamp
|
112 |
$startDate = strtotime($startDate) * 1000; // we need a microtimestamp
|
113 |
|
113 |
|
114 |
$pnProjects = $this->getProjects(); // refresh projects list
|
114 |
$pnProjects = $this->getProjects(); // refresh projects list
|
115 |
foreach ($pnProjects as $project) {
|
115 |
foreach ($pnProjects as $project) {
|
116 |
do {
|
116 |
do {
|
117 |
$observations_PN = $this->getProjectLatest($project, $startDate, $lifecycle);
|
117 |
$observations_PN = $this->getProjectLatest($project, $startDate, $lifecycle);
|
118 |
$this->updateObs($observations_PN);
|
118 |
$this->updateObs($observations_PN);
|
119 |
} while ($this->hasMore);
|
119 |
} while ($this->hasMore);
|
120 |
}
|
120 |
}
|
121 |
}
|
121 |
}
|
122 |
|
122 |
|
123 |
private function getProjectLatest(array $project, int $startDate, string $lifecycle): array {
|
123 |
private function getProjectLatest(array $project, int $startDate, string $lifecycle): array {
|
124 |
$this->currentProject = $project;
|
124 |
$this->currentProject = $project;
|
125 |
|
125 |
|
126 |
if (!$this->currentPage) {
|
126 |
if (!$this->currentPage) {
|
127 |
echo 'Projet ' . $project['name'] . "\n";
|
127 |
echo 'Projet ' . $project['name'] . "\n";
|
128 |
|
128 |
|
129 |
// hop, on appelle le service de PN
|
129 |
// hop, on appelle le service de PN
|
130 |
$url_service = str_replace(
|
130 |
$url_service = str_replace(
|
131 |
['{project}', '{token}', '{startDate}', '{lifecycle}'],
|
131 |
['{project}', '{token}', '{startDate}', '{lifecycle}'],
|
132 |
[$project['id'], Config::get('tokenPlantnet'), $startDate, $lifecycle],
|
132 |
[$project['id'], Config::get('tokenPlantnet'), $startDate, $lifecycle],
|
133 |
Config::get('urlPlantnetBase').Config::get('urlPlantnetLatestChanges')
|
133 |
Config::get('urlPlantnetBase').Config::get('urlPlantnetLatestChanges')
|
134 |
);
|
134 |
);
|
135 |
// die(var_dump($url_service));
|
135 |
// die(var_dump($url_service));
|
136 |
$this->currentPage = $url_service;
|
136 |
$this->currentPage = $url_service;
|
137 |
}
|
137 |
}
|
138 |
|
138 |
|
139 |
$ch = curl_init($this->currentPage);
|
139 |
$ch = curl_init($this->currentPage);
|
140 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
140 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
141 |
$reponse = curl_exec($ch);
|
141 |
$reponse = curl_exec($ch);
|
142 |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
142 |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
143 |
curl_close($ch);
|
143 |
curl_close($ch);
|
144 |
|
144 |
|
145 |
// cool, maintenant on va trier ce qu'on a reçu
|
145 |
// cool, maintenant on va trier ce qu'on a reçu
|
146 |
if (!$reponse) {
|
146 |
if (!$reponse) {
|
147 |
throw new Exception("\nPN ne répond rien à l'adresse {$this->currentPage}\n");
|
147 |
throw new Exception("\nPN ne répond rien à l'adresse {$this->currentPage}\n");
|
148 |
} elseif (200 != $code) {
|
148 |
} elseif (200 != $code) {
|
149 |
// l'api répond avec une 404 quand y'a une date dans le futur ou simplement pas de nouvelle obs...
|
149 |
// l'api répond avec une 404 quand y'a une date dans le futur ou simplement pas de nouvelle obs...
|
150 |
if (404 == $code && strpos($reponse, 'No more results')) {
|
150 |
if (404 == $code && strpos($reponse, 'No more results')) {
|
151 |
$this->hasMore = false;
|
151 |
$this->hasMore = false;
|
152 |
$this->currentPage = '';
|
152 |
$this->currentPage = '';
|
153 |
return [];
|
153 |
return [];
|
154 |
}
|
154 |
}
|
155 |
throw new Exception("\nPN répond avec une $code à l'adresse {$this->currentPage} : $reponse\n");
|
155 |
throw new Exception("\nPN répond avec une $code à l'adresse {$this->currentPage} : $reponse\n");
|
156 |
}
|
156 |
}
|
157 |
$responseJson = json_decode($reponse, true);
|
157 |
$responseJson = json_decode($reponse, true);
|
158 |
$observations_PN = $responseJson['data'] ?? [];
|
158 |
$observations_PN = $responseJson['data'] ?? [];
|
159 |
|
159 |
|
160 |
$this->hasMore = $responseJson['hasMore'];
|
160 |
$this->hasMore = $responseJson['hasMore'];
|
161 |
if ($this->hasMore) {
|
161 |
if ($this->hasMore) {
|
162 |
$this->count += count($observations_PN);
|
162 |
$this->count += count($observations_PN);
|
163 |
$this->currentPage = Config::get('urlPlantnetBase').$responseJson['next'];
|
163 |
$this->currentPage = Config::get('urlPlantnetBase').$responseJson['next'];
|
164 |
} else {
|
164 |
} else {
|
165 |
$this->currentPage = '';
|
165 |
$this->currentPage = '';
|
166 |
$this->count = 0;
|
166 |
$this->count = 0;
|
167 |
}
|
167 |
}
|
168 |
|
168 |
|
169 |
return $observations_PN;
|
169 |
return $observations_PN;
|
170 |
}
|
170 |
}
|
171 |
|
171 |
|
172 |
private function getProjects(): array {
|
172 |
private function getProjects(): array {
|
173 |
// get PN projects list
|
173 |
// get PN projects list
|
174 |
$url = str_replace('{token}', Config::get('tokenPlantnet'), Config::get('urlPlantnetBase').Config::get('urlPlantnetProjects'));
|
174 |
$url = str_replace('{token}', Config::get('tokenPlantnet'), Config::get('urlPlantnetBase').Config::get('urlPlantnetProjects'));
|
175 |
|
175 |
|
176 |
$ch = curl_init($url);
|
176 |
$ch = curl_init($url);
|
177 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
177 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
178 |
$reponse = curl_exec($ch);
|
178 |
$reponse = curl_exec($ch);
|
179 |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
179 |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
180 |
curl_close($ch);
|
180 |
curl_close($ch);
|
181 |
|
181 |
|
182 |
if (!$reponse || 200 != $code) {
|
182 |
if (!$reponse || 200 != $code) {
|
183 |
throw new Exception("\nPN répond avec une $code à l'adresse $url : $reponse\n");
|
183 |
throw new Exception("\nPN répond avec une $code à l'adresse $url : $reponse\n");
|
184 |
}
|
184 |
}
|
185 |
$pnProjects = json_decode($reponse, true);
|
185 |
$pnProjects = json_decode($reponse, true);
|
186 |
|
186 |
|
187 |
echo 'Liste de projets récupérée : ' . count($pnProjects) . " projets dans la liste\n";
|
187 |
echo 'Liste de projets récupérée : ' . count($pnProjects) . " projets dans la liste\n";
|
188 |
|
188 |
|
189 |
return $pnProjects;
|
189 |
return $pnProjects;
|
190 |
}
|
190 |
}
|
191 |
|
191 |
|
192 |
/**
|
192 |
/**
|
193 |
* For a given bunch of plantnet obs, post new one threw saisie widget service
|
193 |
* For a given bunch of plantnet obs, post new one threw saisie widget service
|
194 |
*/
|
194 |
*/
|
195 |
private function updateObs(array $observations_PN) {
|
195 |
private function updateObs(array $observations_PN) {
|
196 |
|
196 |
|
197 |
$url_cel_widget_saisie = Config::get('urlCelWidgetSaisie');
|
197 |
$url_cel_widget_saisie = Config::get('urlCelWidgetSaisie');
|
198 |
|
198 |
|
199 |
foreach ($observations_PN as $obs) {
|
199 |
foreach ($observations_PN as $obs) {
|
200 |
// est-ce qu'on a déjà traité cette obs ? (oui une même obs peut être dans plusieurs projects)
|
200 |
// est-ce qu'on a déjà traité cette obs ? (oui une même obs peut être dans plusieurs projects)
|
201 |
if (in_array($obs['id'], $this->processedObsId)) {
|
201 |
if (in_array($obs['id'], $this->processedObsId)) {
|
202 |
continue;
|
202 |
continue;
|
203 |
} else {
|
203 |
} else {
|
204 |
$this->processedObsId[] = $obs['id'];
|
204 |
$this->processedObsId[] = $obs['id'];
|
205 |
}
|
205 |
}
|
206 |
|
206 |
|
207 |
// on ne teste pas la suppression de l'obs ici, faut le faire après avoir vérifié si on l'a déjà synchro
|
207 |
// on ne teste pas la suppression de l'obs ici, faut le faire après avoir vérifié si on l'a déjà synchro
|
208 |
if (!isset($obs['currentName'])) {
|
208 |
if (!isset($obs['currentName'])) {
|
209 |
continue; // pas de nom de taxon, obs inutile
|
209 |
continue; // pas de nom de taxon, obs inutile
|
210 |
}
|
210 |
}
|
211 |
if (!isset($obs['geo'])) {
|
211 |
if (!isset($obs['geo'])) {
|
212 |
continue; // pas de position, obs inutile
|
212 |
continue; // pas de position, obs inutile
|
213 |
}
|
213 |
}
|
214 |
if (!isset($obs['dateObs'])) {
|
214 |
if (!isset($obs['dateObs'])) {
|
215 |
continue; // pas de date, obs inutile
|
215 |
continue; // pas de date, obs inutile
|
216 |
}
|
216 |
}
|
217 |
|
217 |
|
218 |
if (isset($obs['partner']['id']) && 'tela' === $obs['partner']['id']) {
|
218 |
if (isset($obs['partner']['id']) && 'tela' === $obs['partner']['id']) {
|
219 |
// c'est une obs tela
|
219 |
// c'est une obs tela
|
220 |
// si c'est mis à jour récemment coté PN et qu'on l'a pas supprimée chez nous entre temps
|
220 |
// si c'est mis à jour récemment coté PN et qu'on l'a pas supprimée chez nous entre temps
|
221 |
// on update les votes PN
|
221 |
// on update les votes PN
|
222 |
// on update l'identification proposée par PN
|
222 |
// on update l'identification proposée par PN
|
223 |
|
223 |
|
224 |
// @TODO
|
224 |
// @TODO
|
225 |
continue;
|
225 |
continue;
|
226 |
} elseif (!isset($obs['partner']['id'])) {
|
226 |
} elseif (!isset($obs['partner']['id'])) {
|
227 |
// ce n'est pas une obs d'un partenaire, c'est donc une obs PN
|
227 |
// ce n'est pas une obs d'un partenaire, c'est donc une obs PN
|
228 |
// on récupère l'id utilisateur tela via son mail
|
228 |
// on récupère l'id utilisateur tela via son mail
|
229 |
$email = $obs['author']['email'];
|
229 |
$email = $obs['author']['email'];
|
230 |
$infos_utilisateur = $this->findUserInfo($email);
|
230 |
$infos_utilisateur = $this->findUserInfo($email);
|
231 |
if (!$infos_utilisateur) {
|
231 |
if (!$infos_utilisateur) {
|
232 |
continue; // c'est pas un telabotaniste
|
232 |
continue; // c'est pas un telabotaniste
|
233 |
}
|
233 |
}
|
234 |
//echo "Synchro d'une obs d'un telabotaniste : $email";
|
234 |
//echo "Synchro d'une obs d'un telabotaniste : $email";
|
235 |
// echo json_encode($obs);
|
235 |
// echo json_encode($obs);
|
236 |
// die(var_dump($obs));
|
236 |
// die(var_dump($obs));
|
237 |
|
237 |
|
238 |
// vérification que l'obs n'a pas déjà été ajoutée à la BdD de Tela
|
238 |
// vérification que l'obs n'a pas déjà été ajoutée à la BdD de Tela
|
239 |
$sql = "SELECT date_maj, id_observation FROM cel_plantnet WHERE id_plantnet = '{$obs['id']}'"
|
239 |
$sql = "SELECT date_maj, id_observation FROM cel_plantnet WHERE id_plantnet = '{$obs['id']}'"
|
240 |
. ' -- ' . __FILE__ . ':' . __LINE__;
|
240 |
. ' -- ' . __FILE__ . ':' . __LINE__;
|
241 |
$res = $this->bdd->recupererTous($sql);
|
241 |
$res = $this->bdd->recupererTous($sql);
|
242 |
// die(var_dump($res));
|
242 |
// die(var_dump($res));
|
243 |
if ($res) {
|
243 |
if ($res) {
|
244 |
// l'obs existe déjà, on vérifie si il faut màj puis on passe à la suite
|
244 |
// l'obs existe déjà, on vérifie si il faut màj puis on passe à la suite
|
245 |
// la date de l'obs est un microtime, donc on coupe les millièmes
|
245 |
// la date de l'obs est un microtime, donc on coupe les millièmes
|
246 |
// die(var_dump((int)($obs['dateUpdated']/1000), strtotime($res[0]['date_maj'])));
|
246 |
// die(var_dump((int)($obs['dateUpdated']/1000), strtotime($res[0]['date_maj'])));
|
247 |
if ((int)($obs['dateUpdated']/1000) > strtotime($res[0]['date_maj'])) {
|
247 |
if ((int)($obs['dateUpdated']/1000) > strtotime($res[0]['date_maj'])) {
|
248 |
echo "Obs déjà en base, mise à jour : ID PN {$obs['id']} ; ID TB {$res[0]['id_observation']} ; utilisateur_tb $email\n";
|
248 |
echo "Obs déjà en base, mise à jour : ID PN {$obs['id']} ; ID TB {$res[0]['id_observation']} ; utilisateur_tb $email\n";
|
249 |
$this->updateFromPN($obs, $res[0]['id_observation']);
|
249 |
$this->updateFromPN($obs, $res[0]['id_observation']);
|
250 |
} else {
|
250 |
} else {
|
251 |
echo "Obs déjà en base, déjà à jour : ID PN {$obs['id']} ; ID TB {$res[0]['id_observation']} ; utilisateur_tb $email\n";
|
251 |
echo "Obs déjà en base, déjà à jour : ID PN {$obs['id']} ; ID TB {$res[0]['id_observation']} ; utilisateur_tb $email\n";
|
252 |
}
|
252 |
}
|
253 |
continue;
|
253 |
continue;
|
254 |
}
|
254 |
}
|
255 |
|
255 |
|
256 |
if (isset($obs['deleted']) && (true === $obs['deleted'])) {
|
256 |
if (isset($obs['deleted']) && (true === $obs['deleted'])) {
|
257 |
continue; // obs supprimée chez PN sans être passée par nos serveurs
|
257 |
continue; // obs supprimée chez PN sans être passée par nos serveurs
|
258 |
}
|
258 |
}
|
259 |
|
259 |
|
260 |
$images = [];
|
260 |
$images = [];
|
261 |
$tags_images = [];
|
261 |
$tags_images = [];
|
262 |
foreach ($obs['images'] ?? [] as $i => $image) {
|
262 |
foreach ($obs['images'] ?? [] as $i => $image) {
|
263 |
if ($image['deleted']) {
|
263 |
if ($image['deleted']) {
|
264 |
continue;
|
264 |
continue;
|
265 |
}
|
265 |
}
|
266 |
|
266 |
|
267 |
// téléchargement de l'image PN
|
267 |
// téléchargement de l'image PN
|
268 |
$img = false;
|
268 |
$img = false;
|
269 |
$retry = 3;
|
269 |
$retry = 3;
|
270 |
do {
|
270 |
do {
|
271 |
$img = file_get_contents($image['o']);
|
271 |
$img = file_get_contents($image['o']);
|
272 |
$retry--;
|
272 |
$retry--;
|
273 |
} while (!$img && $retry);
|
273 |
} while (!$img && $retry);
|
274 |
|
274 |
|
275 |
if (!$img) {
|
275 |
if (!$img) {
|
276 |
echo "Abandon, image impossible à télécharger : {$image['o']}\n";
|
276 |
echo "Abandon, image impossible à télécharger : {$image['o']}\n";
|
277 |
continue;
|
277 |
continue;
|
278 |
}
|
278 |
}
|
279 |
|
279 |
|
280 |
// Écriture dans un fichier temporaire
|
280 |
// Écriture dans un fichier temporaire
|
281 |
$tempfile = tempnam("/tmp", "PullPN_") . ".jpg";
|
281 |
$tempfile = tempnam("/tmp", "PullPN_") . ".jpg";
|
282 |
$handle = fopen($tempfile, "w");
|
282 |
$handle = fopen($tempfile, "w");
|
283 |
fwrite($handle, $img);
|
283 |
fwrite($handle, $img);
|
284 |
fclose($handle);
|
284 |
fclose($handle);
|
285 |
|
285 |
|
286 |
echo "Image de " . number_format(filesize($tempfile), 0, ',', ' ') . " octets créé : $tempfile\n";
|
286 |
echo "Image de " . number_format(filesize($tempfile), 0, ',', ' ') . " octets créé : $tempfile\n";
|
287 |
|
287 |
|
288 |
$params = [
|
288 |
$params = [
|
289 |
'name' => 'image' . $i,
|
289 |
'name' => 'image' . $i,
|
290 |
'fichier' => new CURLFile($tempfile, 'image/jpg')
|
290 |
'fichier' => new CURLFile($tempfile, 'image/jpg')
|
291 |
];
|
291 |
];
|
292 |
|
292 |
|
293 |
// envoi de l'image PN à l'api de création
|
293 |
// envoi de l'image PN à l'api de création
|
294 |
$ch = curl_init(Config::get('urlCelUploadImageTemp'));
|
294 |
$ch = curl_init(Config::get('urlCelUploadImageTemp'));
|
295 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
295 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
296 |
curl_setopt($ch, CURLOPT_POST, true);
|
296 |
curl_setopt($ch, CURLOPT_POST, true);
|
297 |
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
|
297 |
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
|
298 |
$reponse = curl_exec($ch);
|
298 |
$reponse = curl_exec($ch);
|
299 |
// var_dump($reponse);
|
299 |
// var_dump($reponse);
|
300 |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
300 |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
301 |
curl_close($ch);
|
301 |
curl_close($ch);
|
302 |
|
302 |
|
303 |
unlink($tempfile);
|
303 |
unlink($tempfile);
|
304 |
|
304 |
|
305 |
$reponse = simplexml_load_string($reponse);
|
305 |
$reponse = simplexml_load_string($reponse);
|
306 |
/* Response format if success:
|
306 |
/* Response format if success:
|
307 |
<?xml version="1.0" encoding="UTF-8"?>
|
307 |
<?xml version="1.0" encoding="UTF-8"?>
|
308 |
<root>
|
308 |
<root>
|
309 |
<miniature-url>https://api-test.tela-botanica.org/tmp/cel/imgtmp/chaflipe_min.jpg</miniature-url>
|
309 |
<miniature-url>https://api-test.tela-botanica.org/tmp/cel/imgtmp/chaflipe_min.jpg</miniature-url>
|
310 |
<image-nom>chaflipe.jpg</image-nom>
|
310 |
<image-nom>chaflipe.jpg</image-nom>
|
311 |
<message></message>
|
311 |
<message></message>
|
312 |
<debogage></debogage>
|
312 |
<debogage></debogage>
|
313 |
</root>
|
313 |
</root>
|
314 |
*/
|
314 |
*/
|
315 |
// var_dump($reponse);
|
315 |
// var_dump($reponse);
|
316 |
if ($reponse && '' == $reponse->message /* && '' != $reponse->{'image-nom'} */) {
|
316 |
if ($reponse && '' == $reponse->message /* && '' != $reponse->{'image-nom'} */) {
|
317 |
$images[] = (string)$reponse->{'image-nom'};
|
317 |
$images[] = (string)$reponse->{'image-nom'};
|
- |
|
318 |
|
318 |
$tags_images[] = $this->tagsImageTraduits[$image['organ']] ?? $image['organ'];
|
319 |
$tag = $this->tagsImageTraduits[$image['organ']] ?? $image['organ'];
|
- |
|
320 |
if (trim($tag)) {
|
- |
|
321 |
$tags_images[] = $tag;
|
- |
|
322 |
}
|
319 |
}
|
323 |
}
|
320 |
}
|
324 |
}
|
321 |
// var_dump($images, $tags_images);
|
325 |
// var_dump($images, $tags_images);
|
322 |
// die();
|
326 |
// die();
|
323 |
|
327 |
|
324 |
$geo = $this->getGeoInfo($obs['geo']);
|
328 |
$geo = $this->getGeoInfo($obs['geo']);
|
325 |
|
329 |
|
326 |
// on insère l'obs via le service CelWidgetSaisie
|
330 |
// on insère l'obs via le service CelWidgetSaisie
|
327 |
$infos_obs = [
|
331 |
$infos_obs = [
|
328 |
'obsId1[date]' => date('d/m/Y', intdiv($obs['dateObs'], 1000)),
|
332 |
'obsId1[date]' => date('d/m/Y', intdiv($obs['dateObs'], 1000)),
|
329 |
'obsId1[latitude]' => $geo['lat'] ?? null,
|
333 |
'obsId1[latitude]' => $geo['lat'] ?? null,
|
330 |
'obsId1[longitude]' => $geo['lon'] ?? null,
|
334 |
'obsId1[longitude]' => $geo['lon'] ?? null,
|
331 |
'obsId1[pays]' => $geo['countryCode'] ?? null,
|
335 |
'obsId1[pays]' => $geo['countryCode'] ?? null,
|
332 |
'obsId1[code_postal]' => $geo['postcode'] ?? null,
|
336 |
'obsId1[code_postal]' => $geo['postcode'] ?? null,
|
333 |
'obsId1[commune_nom]' => $geo['city'] ?? null,
|
337 |
'obsId1[commune_nom]' => $geo['city'] ?? null,
|
334 |
// 'obsId1[commune_code_insee]' => '',
|
338 |
// 'obsId1[commune_code_insee]' => '',
|
335 |
// 'obsId1[lieudit]' => '',
|
339 |
// 'obsId1[lieudit]' => '',
|
336 |
// 'obsId1[milieu]' => '',
|
340 |
// 'obsId1[milieu]' => '',
|
337 |
'obsId1[nom_sel]' => $obs['currentName'],
|
341 |
'obsId1[nom_sel]' => $obs['currentName'],
|
338 |
// 'obsId1[nom_ret]' => '',
|
342 |
// 'obsId1[nom_ret]' => '',
|
339 |
// 'obsId1[famille]' => '',
|
343 |
// 'obsId1[famille]' => '',
|
340 |
'obsId1[certitude]' => 'douteux',
|
344 |
'obsId1[certitude]' => 'douteux',
|
341 |
// 'obsId1[notes]' => '',
|
345 |
// 'obsId1[notes]' => '',
|
342 |
// 'obsId1[num_nom_ret]' => '',
|
346 |
// 'obsId1[num_nom_ret]' => '',
|
343 |
// 'obsId1[num_nom_sel]' => '',
|
347 |
// 'obsId1[num_nom_sel]' => '',
|
344 |
// 'obsId1[num_taxon]' => '',
|
348 |
// 'obsId1[num_taxon]' => '',
|
345 |
'obsId1[referentiel]' => $this->findProbableTaxoRepos($this->currentProject['id']),
|
349 |
'obsId1[referentiel]' => $this->findProbableTaxoRepos($this->currentProject['id']),
|
346 |
// 'obsId1[station]' => '',
|
350 |
// 'obsId1[station]' => '',
|
347 |
'obsId1[obs_id]' => $obs['id'],
|
351 |
'obsId1[obs_id]' => $obs['id'],
|
348 |
'projet' => 'PlantNet',
|
352 |
'projet' => 'PlantNet',
|
349 |
'tag-img' => implode(', ', $tags_images ?? []),
|
353 |
'tag-img' => implode(', ', $tags_images ?? []),
|
350 |
'tag-obs' => 'plantnet, plantnet-mobile, pn:referentiel-' . $this->currentProject['id'],
|
354 |
'tag-obs' => 'plantnet, plantnet-mobile, pn:referentiel-' . $this->currentProject['id'],
|
351 |
'utilisateur[courriel]' => $email,
|
355 |
'utilisateur[courriel]' => $email,
|
352 |
'utilisateur[id_utilisateur]' => $infos_utilisateur['id'],
|
356 |
'utilisateur[id_utilisateur]' => $infos_utilisateur['id'],
|
353 |
'utilisateur[nom]' => $infos_utilisateur['nom'],
|
357 |
'utilisateur[nom]' => $infos_utilisateur['nom'],
|
354 |
'utilisateur[prenom]' => $infos_utilisateur['prenom'],
|
358 |
'utilisateur[prenom]' => $infos_utilisateur['prenom'],
|
355 |
'origin' => 'pullPlantnet',
|
359 |
'origin' => 'pullPlantnet',
|
356 |
];
|
360 |
];
|
357 |
|
361 |
|
358 |
foreach ($images as $i => $image) {
|
362 |
foreach ($images as $i => $image) {
|
359 |
$infos_obs["obsId1[image_nom][$i]"] = $image;
|
363 |
$infos_obs["obsId1[image_nom][$i]"] = $image;
|
360 |
}
|
364 |
}
|
361 |
// var_dump($infos_obs);
|
365 |
// var_dump($infos_obs);
|
362 |
|
366 |
|
363 |
// curl post $infos_obs
|
367 |
// curl post $infos_obs
|
364 |
$ch = curl_init($url_cel_widget_saisie);
|
368 |
$ch = curl_init($url_cel_widget_saisie);
|
365 |
curl_setopt($ch, CURLOPT_POST, true);
|
369 |
curl_setopt($ch, CURLOPT_POST, true);
|
366 |
curl_setopt($ch, CURLOPT_POSTFIELDS, $infos_obs);
|
370 |
curl_setopt($ch, CURLOPT_POSTFIELDS, $infos_obs);
|
367 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
371 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
368 |
$reponse = curl_exec($ch);
|
372 |
$reponse = curl_exec($ch);
|
369 |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
373 |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
370 |
curl_close($ch);
|
374 |
curl_close($ch);
|
371 |
|
375 |
|
372 |
if (!$reponse || 200 != $code) {
|
376 |
if (!$reponse || 200 != $code) {
|
373 |
throw new Exception("Ça a pété à l'envoi de l'obs :\n $reponse\n avec l'obs : " . json_encode($infos_obs) . "\n");
|
377 |
throw new Exception("Ça a pété à l'envoi de l'obs :\n $reponse\n avec l'obs : " . json_encode($infos_obs) . "\n");
|
374 |
}
|
378 |
}
|
375 |
|
379 |
|
376 |
// var_dump($reponse);
|
380 |
// var_dump($reponse);
|
377 |
$id_obs_tb = json_decode($reponse, true)['id'];
|
381 |
$id_obs_tb = json_decode($reponse, true)['id'];
|
378 |
$id_obs_pn = $obs['id'];
|
382 |
$id_obs_pn = $obs['id'];
|
379 |
// on insère les ids de correspondance obsPN obsTB dans la table cel_plantnet
|
383 |
// on insère les ids de correspondance obsPN obsTB dans la table cel_plantnet
|
380 |
$sql = 'INSERT INTO cel_plantnet (id_observation, id_plantnet, date_maj)'
|
384 |
$sql = 'INSERT INTO cel_plantnet (id_observation, id_plantnet, date_maj)'
|
381 |
.' VALUES (%s, %s, NOW())'
|
385 |
.' VALUES (%s, %s, NOW())'
|
382 |
. ' -- ' . __FILE__ . ':' . __LINE__;
|
386 |
. ' -- ' . __FILE__ . ':' . __LINE__;
|
383 |
$sql = sprintf($sql, $this->bdd->proteger($id_obs_tb), $this->bdd->proteger($id_obs_pn));
|
387 |
$sql = sprintf($sql, $this->bdd->proteger($id_obs_tb), $this->bdd->proteger($id_obs_pn));
|
384 |
$this->bdd->requeter($sql);
|
388 |
$this->bdd->requeter($sql);
|
385 |
|
389 |
|
386 |
echo "Obs insérée en base : ID PN $id_obs_pn ; ID TB $id_obs_tb ; utilisateur_tb $email\n";
|
390 |
echo "Obs insérée en base : ID PN $id_obs_pn ; ID TB $id_obs_tb ; utilisateur_tb $email\n";
|
387 |
}
|
391 |
}
|
388 |
}
|
392 |
}
|
389 |
}
|
393 |
}
|
390 |
|
394 |
|
391 |
/**
|
395 |
/**
|
392 |
* Return TB taxo repos names for given PN project
|
396 |
* Return TB taxo repos names for given PN project
|
393 |
*
|
397 |
*
|
394 |
* @return string
|
398 |
* @return string
|
395 |
*/
|
399 |
*/
|
396 |
private function findProbableTaxoRepos($pnProjectId) {
|
400 |
private function findProbableTaxoRepos($pnProjectId) {
|
397 |
$selectedRepos = [];
|
401 |
$selectedRepos = [];
|
398 |
// Référentiels tela botanica indexés par référentiels PN (version octobre 2019)
|
402 |
// Référentiels tela botanica indexés par référentiels PN (version octobre 2019)
|
399 |
// @TODO à revoir en fonction des nouvelles mises en prod de nouveaux référentiels
|
403 |
// @TODO à revoir en fonction des nouvelles mises en prod de nouveaux référentiels
|
400 |
$tbTaxoRepos = [
|
404 |
$tbTaxoRepos = [
|
401 |
'afn' => [/*'apd', */'isfan'],
|
405 |
'afn' => [/*'apd', */'isfan'],
|
402 |
'aft' => ['apd'],
|
406 |
'aft' => ['apd'],
|
403 |
'antilles' => ['bdtxa', 'taxref'],
|
407 |
'antilles' => ['bdtxa', 'taxref'],
|
404 |
'canada' => ['vascan'],
|
408 |
'canada' => ['vascan'],
|
405 |
'comores' => ['apd'],
|
409 |
'comores' => ['apd'],
|
406 |
'endemia' => ['florical', 'taxref'],
|
410 |
'endemia' => ['florical', 'taxref'],
|
407 |
'taxref ' => ['bdtfx'],
|
411 |
'taxref ' => ['bdtfx'],
|
408 |
'guyane' => ['aublet', 'taxref'],
|
412 |
'guyane' => ['aublet', 'taxref'],
|
409 |
'hawai' => [],
|
413 |
'hawai' => [],
|
410 |
'lapaz' => ['aublet', 'taxref'],
|
414 |
'lapaz' => ['aublet', 'taxref'],
|
411 |
'martinique' => ['bdtxa', 'taxref'],
|
415 |
'martinique' => ['bdtxa', 'taxref'],
|
412 |
'maurice' => ['apd'],
|
416 |
'maurice' => ['apd'],
|
413 |
'medor' => ['lbf'],
|
417 |
'medor' => ['lbf'],
|
414 |
'namerica' => [],
|
418 |
'namerica' => [],
|
415 |
'polynesiefr' => ['taxref'],
|
419 |
'polynesiefr' => ['taxref'],
|
416 |
'prosea' => ['asia'],
|
420 |
'prosea' => ['asia'],
|
417 |
'prota' => ['isfan', 'apd'],
|
421 |
'prota' => ['isfan', 'apd'],
|
418 |
'provence' => ['bdtfx', 'taxref'],
|
422 |
'provence' => ['bdtfx', 'taxref'],
|
419 |
'reunion' => ['bdtre', 'taxref'],
|
423 |
'reunion' => ['bdtre', 'taxref'],
|
420 |
'salad' => ['bdtfx', 'taxref'],
|
424 |
'salad' => ['bdtfx', 'taxref'],
|
421 |
'useful' => [],
|
425 |
'useful' => [],
|
422 |
'weurope' => ['bdtfx'],
|
426 |
'weurope' => ['bdtfx'],
|
423 |
'the-plant-list' => [],
|
427 |
'the-plant-list' => [],
|
424 |
'iscantree' => ['apd'],
|
428 |
'iscantree' => ['apd'],
|
425 |
'central-america' => [],
|
429 |
'central-america' => [],
|
426 |
'invasion' => ['bdtfx'],
|
430 |
'invasion' => ['bdtfx'],
|
427 |
'weeds' => ['bdtfx'],
|
431 |
'weeds' => ['bdtfx'],
|
428 |
'malaysia' => [],
|
432 |
'malaysia' => [],
|
429 |
];
|
433 |
];
|
430 |
|
434 |
|
431 |
if (array_key_exists($pnProjectId, $tbTaxoRepos)) {
|
435 |
if (array_key_exists($pnProjectId, $tbTaxoRepos)) {
|
432 |
array_map(function($repo) use ($selectedRepos) {
|
436 |
array_map(function($repo) use ($selectedRepos) {
|
433 |
$selectedRepos[] = $repo;
|
437 |
$selectedRepos[] = $repo;
|
434 |
}, $tbTaxoRepos[$pnProjectId]);
|
438 |
}, $tbTaxoRepos[$pnProjectId]);
|
435 |
}
|
439 |
}
|
436 |
|
440 |
|
437 |
// @TODO chercher le nom dans plusieurs référentiels avant d'envoyer l'obs
|
441 |
// @TODO chercher le nom dans plusieurs référentiels avant d'envoyer l'obs
|
438 |
// prendre "enrichirDonneesTaxonomiques" du service de saisie et le coller ici
|
442 |
// prendre "enrichirDonneesTaxonomiques" du service de saisie et le coller ici
|
439 |
// en attendant on envoi juste le premier référentiel trouvé
|
443 |
// en attendant on envoi juste le premier référentiel trouvé
|
440 |
return $selectedRepos[0] ?? '';
|
444 |
return $selectedRepos[0] ?? '';
|
441 |
}
|
445 |
}
|
442 |
|
446 |
|
443 |
/*
|
447 |
/*
|
444 |
* Retrouve les infos d'un utilisateur tela à partir de son email
|
448 |
* Retrouve les infos d'un utilisateur tela à partir de son email
|
445 |
* @param string $email User email
|
449 |
* @param string $email User email
|
446 |
* @return mixed Tableau des infos de l'utilisateur
|
450 |
* @return mixed Tableau des infos de l'utilisateur
|
447 |
* False si l'email n'est pas trouvé dans l'annuaire
|
451 |
* False si l'email n'est pas trouvé dans l'annuaire
|
448 |
*/
|
452 |
*/
|
449 |
private function findUserInfo($email) {
|
453 |
private function findUserInfo($email) {
|
450 |
// cherche dans le cache
|
454 |
// cherche dans le cache
|
451 |
if (array_key_exists($email, $this->userInfos)) {
|
455 |
if (array_key_exists($email, $this->userInfos)) {
|
452 |
$infosUtilisateur = $this->userInfos[$email];
|
456 |
$infosUtilisateur = $this->userInfos[$email];
|
453 |
// echo("Email in cache : $email\n");
|
457 |
// echo("Email in cache : $email\n");
|
454 |
} else {
|
458 |
} else {
|
455 |
$infosUtilisateur = false;
|
459 |
$infosUtilisateur = false;
|
456 |
$urlInfosUtilisateur = str_replace('{email}', $email, Config::get('urlAnnuaireIdParEmail'));
|
460 |
$urlInfosUtilisateur = str_replace('{email}', $email, Config::get('urlAnnuaireIdParEmail'));
|
457 |
|
461 |
|
458 |
$ch = curl_init($urlInfosUtilisateur);
|
462 |
$ch = curl_init($urlInfosUtilisateur);
|
459 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
463 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
460 |
$reponse = curl_exec($ch);
|
464 |
$reponse = curl_exec($ch);
|
461 |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
465 |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
462 |
curl_close($ch);
|
466 |
curl_close($ch);
|
463 |
|
467 |
|
464 |
// on peut pas tester le code de réponse de cette api, si l'utilisateur n'est pas trouvé ça fait une 500 >_<
|
468 |
// on peut pas tester le code de réponse de cette api, si l'utilisateur n'est pas trouvé ça fait une 500 >_<
|
465 |
// une bonne réponse ressemble à ça :
|
469 |
// une bonne réponse ressemble à ça :
|
466 |
// {"killian@tela-botanica.org":{"id":"30489","prenom":"Killian","nom":"Stefanini","pseudo":"Killian Stefanini","pseudoUtilise":true,"intitule":"Killian Stefanini","avatar":"\/\/www.gravatar.com\/avatar\/a9b9b8484076540924c03af816c77fc8?s=50&r=g&d=mm","groupes":{"19226":"adm","18943":"","23152":"","21684":"","21598":"adm","23184":"","23516":""},"permissions":["editor"],"nomWiki":"KillianStefanini"}}
|
470 |
// {"killian@tela-botanica.org":{"id":"30489","prenom":"Killian","nom":"Stefanini","pseudo":"Killian Stefanini","pseudoUtilise":true,"intitule":"Killian Stefanini","avatar":"\/\/www.gravatar.com\/avatar\/a9b9b8484076540924c03af816c77fc8?s=50&r=g&d=mm","groupes":{"19226":"adm","18943":"","23152":"","21684":"","21598":"adm","23184":"","23516":""},"permissions":["editor"],"nomWiki":"KillianStefanini"}}
|
467 |
$reponse = json_decode($reponse, true);
|
471 |
$reponse = json_decode($reponse, true);
|
468 |
if (!isset($reponse)) {
|
472 |
if (!isset($reponse)) {
|
469 |
throw new Exception("\nL'annuaire n'a pas répondu avec du json : code $code à l'adresse $urlInfosUtilisateur : $reponse\n");
|
473 |
throw new Exception("\nL'annuaire n'a pas répondu avec du json : code $code à l'adresse $urlInfosUtilisateur : $reponse\n");
|
470 |
}
|
474 |
}
|
471 |
|
475 |
|
472 |
if (!isset($reponse['error'])) {
|
476 |
if (!isset($reponse['error'])) {
|
473 |
$infosUtilisateur = array_shift($reponse);
|
477 |
$infosUtilisateur = array_shift($reponse);
|
474 |
}
|
478 |
}
|
475 |
|
479 |
|
476 |
// met en cache
|
480 |
// met en cache
|
477 |
$this->userInfos[$email] = $infosUtilisateur;
|
481 |
$this->userInfos[$email] = $infosUtilisateur;
|
478 |
// echo("Email cached : $email\n");
|
482 |
// echo("Email cached : $email\n");
|
479 |
}
|
483 |
}
|
480 |
|
484 |
|
481 |
return $infosUtilisateur;
|
485 |
return $infosUtilisateur;
|
482 |
}
|
486 |
}
|
483 |
|
487 |
|
484 |
private function getGeoInfo($obs) {
|
488 |
private function getGeoInfo($obs) {
|
485 |
$geo = [];
|
489 |
$geo = [];
|
486 |
if (!isset($obs['lat']) && !isset($obs['lon'])) {
|
490 |
if (!isset($obs['lat']) && !isset($obs['lon'])) {
|
487 |
return $geo;
|
491 |
return $geo;
|
488 |
}
|
492 |
}
|
489 |
|
493 |
|
490 |
// $data = [
|
494 |
// $data = [
|
491 |
// 'hitsPerPage' => 1,
|
495 |
// 'hitsPerPage' => 1,
|
492 |
// 'aroundLatLng' => "{$obs['lat']},{$obs['lon']}"
|
496 |
// 'aroundLatLng' => "{$obs['lat']},{$obs['lon']}"
|
493 |
// ];
|
497 |
// ];
|
494 |
$headers = [
|
498 |
$headers = [
|
495 |
'X-Algolia-Application-Id' => Config::get('algoliaApplicationId'),
|
499 |
'X-Algolia-Application-Id' => Config::get('algoliaApplicationId'),
|
496 |
'X-Algolia-API-Key' => Config::get('algoliaAPIKey'),
|
500 |
'X-Algolia-API-Key' => Config::get('algoliaAPIKey'),
|
497 |
];
|
501 |
];
|
498 |
|
502 |
|
499 |
$lat = number_format($obs['lat'], 6, '.', '');
|
503 |
$lat = number_format($obs['lat'], 6, '.', '');
|
500 |
$lon = number_format($obs['lon'], 6, '.', '');
|
504 |
$lon = number_format($obs['lon'], 6, '.', '');
|
501 |
|
505 |
|
502 |
$ch = curl_init(Config::get('urlReverseGeocodingLatLng')."$lat,$lon");
|
506 |
$ch = curl_init(Config::get('urlReverseGeocodingLatLng')."$lat,$lon");
|
503 |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
507 |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
504 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
508 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
505 |
$reponse = curl_exec($ch);
|
509 |
$reponse = curl_exec($ch);
|
506 |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
510 |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
507 |
curl_close($ch);
|
511 |
curl_close($ch);
|
508 |
|
512 |
|
509 |
if ($reponse) {
|
513 |
if ($reponse) {
|
510 |
$reponse = json_decode($reponse, true);
|
514 |
$reponse = json_decode($reponse, true);
|
511 |
// die(var_dump($reponse));
|
515 |
// die(var_dump($reponse));
|
512 |
$infos = $reponse['hits'][0];
|
516 |
$infos = $reponse['hits'][0];
|
513 |
|
517 |
|
514 |
$geo = [
|
518 |
$geo = [
|
515 |
'lat' => (string) $obs['lat'],
|
519 |
'lat' => (string) $obs['lat'],
|
516 |
'lon' => (string) $obs['lon'],
|
520 |
'lon' => (string) $obs['lon'],
|
517 |
'country' => $infos['country']['fr'] ?? $infos['country']['default'],
|
521 |
'country' => $infos['country']['fr'] ?? $infos['country']['default'],
|
518 |
'city' => $infos['city']['default'][0] ?? null,
|
522 |
'city' => $infos['city']['default'][0] ?? null,
|
519 |
'postcode' => $infos['postcode'][0] ?? null,
|
523 |
'postcode' => $infos['postcode'][0] ?? null,
|
520 |
'countryCode' => strtoupper($infos['country_code']),
|
524 |
'countryCode' => strtoupper($infos['country_code']),
|
521 |
];
|
525 |
];
|
522 |
}
|
526 |
}
|
523 |
|
527 |
|
524 |
return $geo;
|
528 |
return $geo;
|
525 |
}
|
529 |
}
|
526 |
|
530 |
|
527 |
private function updateFromPN(array $pnObs, string $tbObsId) {
|
531 |
private function updateFromPN(array $pnObs, string $tbObsId) {
|
528 |
if (!is_array($pnObs) || !is_string($tbObsId)) {
|
532 |
if (!is_array($pnObs) || !is_string($tbObsId)) {
|
529 |
// die(var_dump($pnObs, $tbObsId));
|
533 |
// die(var_dump($pnObs, $tbObsId));
|
530 |
throw new Exception("\nBad params types, give me an array and an integer plz\n");
|
534 |
throw new Exception("\nBad params types, give me an array and an integer plz\n");
|
531 |
}
|
535 |
}
|
532 |
// die(var_dump($pnObs));
|
536 |
// die(var_dump($pnObs));
|
533 |
|
537 |
|
534 |
// log update date to cel_plantnet
|
538 |
// log update date to cel_plantnet
|
535 |
$sql = "UPDATE cel_plantnet SET date_maj = NOW() WHERE id_observation = $tbObsId"
|
539 |
$sql = "UPDATE cel_plantnet SET date_maj = NOW() WHERE id_observation = $tbObsId"
|
536 |
. ' -- ' . __FILE__ . ':' . __LINE__;
|
540 |
. ' -- ' . __FILE__ . ':' . __LINE__;
|
537 |
$this->bdd->requeter($sql);
|
541 |
$this->bdd->requeter($sql);
|
538 |
|
542 |
|
539 |
// check for deleted
|
543 |
// check for deleted
|
540 |
if (isset($pnObs['deleted']) && (true === $pnObs['deleted'])) {
|
544 |
if (isset($pnObs['deleted']) && (true === $pnObs['deleted'])) {
|
541 |
// est-ce une obs issue de PN ?
|
545 |
// est-ce une obs issue de PN ?
|
542 |
//// faut regarder le champ input_source == PlantNet
|
546 |
//// faut regarder le champ input_source == PlantNet
|
543 |
$sql = "SELECT input_source FROM occurrence WHERE id = '$tbObsId'"
|
547 |
$sql = "SELECT input_source FROM occurrence WHERE id = '$tbObsId'"
|
544 |
. ' -- ' . __FILE__ . ':' . __LINE__;
|
548 |
. ' -- ' . __FILE__ . ':' . __LINE__;
|
545 |
$res = $this->bdd->recupererTous($sql);
|
549 |
$res = $this->bdd->recupererTous($sql);
|
546 |
|
550 |
|
547 |
if (isset($res[0]) && ('PlantNet' === $res[0]['input_source'])) {
|
551 |
if (isset($res[0]) && ('PlantNet' === $res[0]['input_source'])) {
|
548 |
// oui ? alors supprimer obs !
|
552 |
// oui ? alors supprimer obs !
|
549 |
echo "Obs supprimée coté PN, suppression : ID PN {$obs['id']} ; ID TB {$res[0]['id_observation']}\n";
|
553 |
echo "Obs supprimée coté PN, suppression : ID PN {$obs['id']} ; ID TB {$res[0]['id_observation']}\n";
|
550 |
|
554 |
|
551 |
$sql = "UPDATE photos SET occurrence_id = NULL WHERE occurrence_id = '$tbObsId'"
|
555 |
$sql = "UPDATE photos SET occurrence_id = NULL WHERE occurrence_id = '$tbObsId'"
|
552 |
. ' -- ' . __FILE__ . ':' . __LINE__;
|
556 |
. ' -- ' . __FILE__ . ':' . __LINE__;
|
553 |
$this->bdd->requeter($sql);
|
557 |
$this->bdd->requeter($sql);
|
554 |
|
558 |
|
555 |
$sql = "DELETE FROM occurrence WHERE id = '$tbObsId'"
|
559 |
$sql = "DELETE FROM occurrence WHERE id = '$tbObsId'"
|
556 |
. ' -- ' . __FILE__ . ':' . __LINE__;
|
560 |
. ' -- ' . __FILE__ . ':' . __LINE__;
|
557 |
$this->bdd->requeter($sql);
|
561 |
$this->bdd->requeter($sql);
|
558 |
}
|
562 |
}
|
559 |
}
|
563 |
}
|
560 |
|
564 |
|
561 |
// check for deleted images
|
565 |
// check for deleted images
|
562 |
foreach ($pnObs['images'] as $image) {
|
566 |
foreach ($pnObs['images'] as $image) {
|
563 |
if ($image['deleted']) {
|
567 |
if ($image['deleted']) {
|
564 |
// no idea
|
568 |
// no idea
|
565 |
// y'a pas encore de lien tangible entre les images PN et les notres
|
569 |
// y'a pas encore de lien tangible entre les images PN et les notres
|
566 |
}
|
570 |
}
|
567 |
}
|
571 |
}
|
568 |
|
572 |
|
569 |
// @TODO : finir la comparaison des proposition entre celles de Tela et celles PN
|
573 |
// @TODO : finir la comparaison des proposition entre celles de Tela et celles PN
|
570 |
// // check for votes (notes et taxon)
|
574 |
// // check for votes (notes et taxon)
|
571 |
// $names = [];
|
575 |
// $names = [];
|
572 |
// foreach ($pnObs['computed']['votes'] as $vote) {
|
576 |
// foreach ($pnObs['computed']['votes'] as $vote) {
|
573 |
// echo($vote['name']);
|
577 |
// echo($vote['name']);
|
574 |
// echo($vote['score']['pn']);
|
578 |
// echo($vote['score']['pn']);
|
575 |
// echo($vote['score']['total']);
|
579 |
// echo($vote['score']['total']);
|
576 |
|
580 |
|
577 |
// $names[$vote['score']['pn']] = $vote['name'];
|
581 |
// $names[$vote['score']['pn']] = $vote['name'];
|
578 |
// }
|
582 |
// }
|
579 |
// ksort($names, SORT_NUMERIC); // votes sorted by score asc
|
583 |
// ksort($names, SORT_NUMERIC); // votes sorted by score asc
|
580 |
// $pn_sorted_votes = array_reverse($names, true); // desc
|
584 |
// $pn_sorted_votes = array_reverse($names, true); // desc
|
581 |
|
585 |
|
582 |
// // get existing votes
|
586 |
// // get existing votes
|
583 |
// $sql = "SELECT id_commentaire, nom_sel, nom_sel_nn, nom_referentiel, nom_referentiel, utilisateur_courriel, proposition_initiale, proposition_retenue"
|
587 |
// $sql = "SELECT id_commentaire, nom_sel, nom_sel_nn, nom_referentiel, nom_referentiel, utilisateur_courriel, proposition_initiale, proposition_retenue"
|
584 |
// . "WHERE ce_observation = '$tbObsId' AND ce_commentaire_parent = 0"
|
588 |
// . "WHERE ce_observation = '$tbObsId' AND ce_commentaire_parent = 0"
|
585 |
// . ' -- ' . __FILE__ . ':' . __LINE__;
|
589 |
// . ' -- ' . __FILE__ . ':' . __LINE__;
|
586 |
// $existing_votes = $this->bdd->recupererTous($sql);
|
590 |
// $existing_votes = $this->bdd->recupererTous($sql);
|
587 |
|
591 |
|
588 |
// // @TODO : compare votes
|
592 |
// // @TODO : compare votes
|
589 |
// // insert_new_votes($pn_sorted_votes, $existing_votes);
|
593 |
// // insert_new_votes($pn_sorted_votes, $existing_votes);
|
590 |
}
|
594 |
}
|
591 |
|
595 |
|
592 |
private function updateOnePN($pnObsId, $pnProjectId) {
|
596 |
private function updateOnePN($pnObsId, $pnProjectId) {
|
593 |
if (!is_int($pnObsId) || !is_string($pnProjectId)) {
|
597 |
if (!is_int($pnObsId) || !is_string($pnProjectId)) {
|
594 |
die(var_dump($pnObsId, $pnProjectId));
|
598 |
die(var_dump($pnObsId, $pnProjectId));
|
595 |
throw new Exception("\nBad params types, give me an integer and a string plz\n");
|
599 |
throw new Exception("\nBad params types, give me an integer and a string plz\n");
|
596 |
}
|
600 |
}
|
597 |
// get PN project list
|
601 |
// get PN project list
|
598 |
$list = [];
|
602 |
$list = [];
|
599 |
$pnProjects = $this->getProjects(); // refresh projects list
|
603 |
$pnProjects = $this->getProjects(); // refresh projects list
|
600 |
foreach ($pnProjects as $project) {
|
604 |
foreach ($pnProjects as $project) {
|
601 |
$list[$project['id']] = $project['name'];
|
605 |
$list[$project['id']] = $project['name'];
|
602 |
}
|
606 |
}
|
603 |
|
607 |
|
604 |
// if project not in list display list
|
608 |
// if project not in list display list
|
605 |
if (!array_key_exists($pnProjectId, $list)) {
|
609 |
if (!array_key_exists($pnProjectId, $list)) {
|
606 |
echo "Available projects:\n";
|
610 |
echo "Available projects:\n";
|
607 |
foreach ($list as $projectId => $projectName) {
|
611 |
foreach ($list as $projectId => $projectName) {
|
608 |
echo " - $projectId ($projectName)\n";
|
612 |
echo " - $projectId ($projectName)\n";
|
609 |
}
|
613 |
}
|
610 |
throw new Exception("Project $pnProjectId does not exist\n");
|
614 |
throw new Exception("Project $pnProjectId does not exist\n");
|
611 |
} else {
|
615 |
} else {
|
612 |
$this->currentProject = [
|
616 |
$this->currentProject = [
|
613 |
'id' => $pnProjectId,
|
617 |
'id' => $pnProjectId,
|
614 |
'name' => $list[$pnProjectId],
|
618 |
'name' => $list[$pnProjectId],
|
615 |
];
|
619 |
];
|
616 |
}
|
620 |
}
|
617 |
|
621 |
|
618 |
// get PN obs
|
622 |
// get PN obs
|
619 |
$urlInfosObs = str_replace(
|
623 |
$urlInfosObs = str_replace(
|
620 |
['{token}', '{project}', '{id}'],
|
624 |
['{token}', '{project}', '{id}'],
|
621 |
[Config::get('tokenPlantnet'), $pnProjectId, $pnObsId],
|
625 |
[Config::get('tokenPlantnet'), $pnProjectId, $pnObsId],
|
622 |
Config::get('urlPlantnetBase').Config::get('urlPlantnetObsById')
|
626 |
Config::get('urlPlantnetBase').Config::get('urlPlantnetObsById')
|
623 |
);
|
627 |
);
|
624 |
$ch = curl_init($urlInfosObs);
|
628 |
$ch = curl_init($urlInfosObs);
|
625 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
629 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
626 |
$response = curl_exec($ch);
|
630 |
$response = curl_exec($ch);
|
627 |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
631 |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
628 |
curl_close($ch);
|
632 |
curl_close($ch);
|
629 |
|
633 |
|
630 |
if (!$response || 200 != $code) {
|
634 |
if (!$response || 200 != $code) {
|
631 |
throw new Exception("\nPlantnet api ($urlInfosObs) replied with code $code : $response\n");
|
635 |
throw new Exception("\nPlantnet api ($urlInfosObs) replied with code $code : $response\n");
|
632 |
}
|
636 |
}
|
633 |
|
637 |
|
634 |
// change last modification date to force update
|
638 |
// change last modification date to force update
|
635 |
$obs = json_decode($response, true);
|
639 |
$obs = json_decode($response, true);
|
636 |
$date = new DateTime();
|
640 |
$date = new DateTime();
|
637 |
$date->setTimestamp(intdiv($obs['dateUpdated'], 1000) - 1);
|
641 |
$date->setTimestamp(intdiv($obs['dateUpdated'], 1000) - 1);
|
638 |
$date = $this->bdd->proteger($date->format('Y-m-d H:i:s'));
|
642 |
$date = $this->bdd->proteger($date->format('Y-m-d H:i:s'));
|
639 |
$sql = "UPDATE cel_plantnet SET date_maj = $date WHERE id_plantnet = '$pnObsId'"
|
643 |
$sql = "UPDATE cel_plantnet SET date_maj = $date WHERE id_plantnet = '$pnObsId'"
|
640 |
. ' -- ' . __FILE__ . ':' . __LINE__;
|
644 |
. ' -- ' . __FILE__ . ':' . __LINE__;
|
641 |
$this->bdd->requeter($sql);
|
645 |
$this->bdd->requeter($sql);
|
642 |
|
646 |
|
643 |
$this->updateObs([$obs]);
|
647 |
$this->updateObs([$obs]);
|
644 |
}
|
648 |
}
|
645 |
|
649 |
|
646 |
private function updateOneTB($id) {
|
650 |
private function updateOneTB($id) {
|
647 |
$corres = $this->findMatchingPartnerId($id, 'tb');
|
651 |
$corres = $this->findMatchingPartnerId($id, 'tb');
|
648 |
echo "L'obs tela avec l'id $id a été trouvée chez plantnet avec l'id : " .$corres['pn'] . "\n";
|
652 |
echo "L'obs tela avec l'id $id a été trouvée chez plantnet avec l'id : " .$corres['pn'] . "\n";
|
649 |
echo "La suite n'a pas été implémentée.\n";
|
653 |
echo "La suite n'a pas été implémentée.\n";
|
650 |
}
|
654 |
}
|
651 |
|
655 |
|
652 |
/*
|
656 |
/*
|
653 |
* Trouve les ids d'une obs chez les partenaires
|
657 |
* Trouve les ids d'une obs chez les partenaires
|
654 |
* Il faut lancer updateMatchingPartnersIds pour obtenir une liste fraiche
|
658 |
* Il faut lancer updateMatchingPartnersIds pour obtenir une liste fraiche
|
655 |
*
|
659 |
*
|
656 |
* @param string $id L'id de l'obs
|
660 |
* @param string $id L'id de l'obs
|
657 |
* @param string $partner Le partenaire correspondant à l'id de l'obs
|
661 |
* @param string $partner Le partenaire correspondant à l'id de l'obs
|
658 |
*
|
662 |
*
|
659 |
* @return mixed Le tableau de correspondances d'ids
|
663 |
* @return mixed Le tableau de correspondances d'ids
|
660 |
* False si l'id n'est pas trouvé
|
664 |
* False si l'id n'est pas trouvé
|
661 |
*/
|
665 |
*/
|
662 |
private function findMatchingPartnerId($id, $partner) {
|
666 |
private function findMatchingPartnerId($id, $partner) {
|
663 |
if (!in_array($partner, $this->allowedPartners)) {
|
667 |
if (!in_array($partner, $this->allowedPartners)) {
|
664 |
throw new Exception("\nUnknown partner : $partner. Available Partners : " . implode(', ', $this->allowedPartners) . "\n");
|
668 |
throw new Exception("\nUnknown partner : $partner. Available Partners : " . implode(', ', $this->allowedPartners) . "\n");
|
665 |
}
|
669 |
}
|
666 |
|
670 |
|
667 |
// le json ressemble à ça :
|
671 |
// le json ressemble à ça :
|
668 |
// {"tb":"2369732","pn":"1001020199"}, {"tb":"2369733","pn":"1001020176"}, ...
|
672 |
// {"tb":"2369732","pn":"1001020199"}, {"tb":"2369733","pn":"1001020176"}, ...
|
669 |
$ids = json_decode(file_get_contents($this->correspondingIdsFilename), true);
|
673 |
$ids = json_decode(file_get_contents($this->correspondingIdsFilename), true);
|
670 |
// extrait la colonne du partenaire recherché du tableau multidimensionnel fraichement décodé
|
674 |
// extrait la colonne du partenaire recherché du tableau multidimensionnel fraichement décodé
|
671 |
$partnerColumn = array_column($ids, $partner);
|
675 |
$partnerColumn = array_column($ids, $partner);
|
672 |
// cherche l'index de l'id recherché
|
676 |
// cherche l'index de l'id recherché
|
673 |
$index = array_search($id, $partnerColumn);
|
677 |
$index = array_search($id, $partnerColumn);
|
674 |
|
678 |
|
675 |
return $index ? $ids[$index] : false;
|
679 |
return $index ? $ids[$index] : false;
|
676 |
}
|
680 |
}
|
677 |
|
681 |
|
678 |
private function updateCorrespondingIdsTable() {
|
682 |
private function updateCorrespondingIdsTable() {
|
679 |
// first update cache file if necessary
|
683 |
// first update cache file if necessary
|
680 |
if (!file_exists($this->correspondingIdsFilename) || time() > (filemtime($this->correspondingIdsFilename) + (24 * 3600))) {
|
684 |
if (!file_exists($this->correspondingIdsFilename) || time() > (filemtime($this->correspondingIdsFilename) + (24 * 3600))) {
|
681 |
$this->updateMatchingPartnersIds();
|
685 |
$this->updateMatchingPartnersIds();
|
682 |
}
|
686 |
}
|
683 |
|
687 |
|
684 |
$data = file_get_contents($this->correspondingIdsFilename);
|
688 |
$data = file_get_contents($this->correspondingIdsFilename);
|
685 |
$data = json_decode($data, true);
|
689 |
$data = json_decode($data, true);
|
686 |
|
690 |
|
687 |
$now = new DateTime;
|
691 |
$now = new DateTime;
|
688 |
$now = $this->bdd->proteger($now->format('Y-m-d H:i:s'));
|
692 |
$now = $this->bdd->proteger($now->format('Y-m-d H:i:s'));
|
689 |
|
693 |
|
690 |
$sqlInsert = 'INSERT INTO cel_plantnet (id_observation, id_plantnet, date_maj)'
|
694 |
$sqlInsert = 'INSERT INTO cel_plantnet (id_observation, id_plantnet, date_maj)'
|
691 |
. ' VALUES %s'
|
695 |
. ' VALUES %s'
|
692 |
. ' ON DUPLICATE KEY UPDATE id_observation=VALUES(id_observation), id_plantnet=VALUES(id_plantnet)'
|
696 |
. ' ON DUPLICATE KEY UPDATE id_observation=VALUES(id_observation), id_plantnet=VALUES(id_plantnet)'
|
693 |
. ' -- ' . __FILE__ . ':' . __LINE__;
|
697 |
. ' -- ' . __FILE__ . ':' . __LINE__;
|
694 |
$values = '';
|
698 |
$values = '';
|
695 |
|
699 |
|
696 |
echo "Updating matching partners ids table\n";
|
700 |
echo "Updating matching partners ids table\n";
|
697 |
foreach ($data as $id => $corres) {
|
701 |
foreach ($data as $id => $corres) {
|
698 |
$id_obs_tb = $this->bdd->proteger($corres['tb']);
|
702 |
$id_obs_tb = $this->bdd->proteger($corres['tb']);
|
699 |
$id_obs_pn = $this->bdd->proteger($corres['pn']);
|
703 |
$id_obs_pn = $this->bdd->proteger($corres['pn']);
|
700 |
// on insère les ids de correspondance obsPN obsTB dans la table cel_plantnet
|
704 |
// on insère les ids de correspondance obsPN obsTB dans la table cel_plantnet
|
701 |
$values .= " ($id_obs_tb, $id_obs_pn, $now),";
|
705 |
$values .= " ($id_obs_tb, $id_obs_pn, $now),";
|
702 |
|
706 |
|
703 |
if (0 === $id % 100) {
|
707 |
if (0 === $id % 100) {
|
704 |
$values = substr($values, 0, -1); // retire la dernière virgule
|
708 |
$values = substr($values, 0, -1); // retire la dernière virgule
|
705 |
$sql = sprintf($sqlInsert, $values);
|
709 |
$sql = sprintf($sqlInsert, $values);
|
706 |
// var_dump($sql);
|
710 |
// var_dump($sql);
|
707 |
$this->bdd->requeter($sql);
|
711 |
$this->bdd->requeter($sql);
|
708 |
$values = '';
|
712 |
$values = '';
|
709 |
}
|
713 |
}
|
710 |
}
|
714 |
}
|
711 |
// Faut envoyer les dernières données qui sont pas passées dans le modulo
|
715 |
// Faut envoyer les dernières données qui sont pas passées dans le modulo
|
712 |
$values = substr($values, 0, -1); // retire la dernière virgule
|
716 |
$values = substr($values, 0, -1); // retire la dernière virgule
|
713 |
$sql = sprintf($sqlInsert, $values);
|
717 |
$sql = sprintf($sqlInsert, $values);
|
714 |
// var_dump($sql);
|
718 |
// var_dump($sql);
|
715 |
$this->bdd->requeter($sql);
|
719 |
$this->bdd->requeter($sql);
|
716 |
|
720 |
|
717 |
$count = count($data);
|
721 |
$count = count($data);
|
718 |
echo "Success: #$count updated\n";
|
722 |
echo "Success: #$count updated\n";
|
719 |
}
|
723 |
}
|
720 |
|
724 |
|
721 |
private function updateMatchingPartnersIds() {
|
725 |
private function updateMatchingPartnersIds() {
|
722 |
// curl -X GET --header 'Accept: application/json' 'https://bourbonnais.cirad.fr:8081/v1/edi/observations/partnerids?token=f0ca433febe320675c24ee2ddfab8b82f65d556b' > partnerids.txt
|
726 |
// curl -X GET --header 'Accept: application/json' 'https://bourbonnais.cirad.fr:8081/v1/edi/observations/partnerids?token=f0ca433febe320675c24ee2ddfab8b82f65d556b' > partnerids.txt
|
723 |
$matchingPartnersIdsServiceUrl = str_replace(
|
727 |
$matchingPartnersIdsServiceUrl = str_replace(
|
724 |
'{token}',
|
728 |
'{token}',
|
725 |
Config::get('tokenPlantnet'),
|
729 |
Config::get('tokenPlantnet'),
|
726 |
Config::get('urlPlantnetBase').Config::get('urlPlantnetMatchingPartner')
|
730 |
Config::get('urlPlantnetBase').Config::get('urlPlantnetMatchingPartner')
|
727 |
);
|
731 |
);
|
728 |
$ch = curl_init($matchingPartnersIdsServiceUrl);
|
732 |
$ch = curl_init($matchingPartnersIdsServiceUrl);
|
729 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
733 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
730 |
|
734 |
|
731 |
echo "Updating matching partners ids...\n";
|
735 |
echo "Updating matching partners ids...\n";
|
732 |
$reponse = curl_exec($ch);
|
736 |
$reponse = curl_exec($ch);
|
733 |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
737 |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
734 |
curl_close($ch);
|
738 |
curl_close($ch);
|
735 |
|
739 |
|
736 |
|
740 |
|
737 |
if (!$reponse || 200 != $code) {
|
741 |
if (!$reponse || 200 != $code) {
|
738 |
throw new Exception("\nError updating partners ids, http error code \"$code\" whith url \"$matchingPartnersIdsServiceUrl\"...\n");
|
742 |
throw new Exception("\nError updating partners ids, http error code \"$code\" whith url \"$matchingPartnersIdsServiceUrl\"...\n");
|
739 |
}
|
743 |
}
|
740 |
echo "Response data from PN api gathered, know computing...\n";
|
744 |
echo "Response data from PN api gathered, know computing...\n";
|
741 |
|
745 |
|
742 |
if (!file_put_contents($this->correspondingIdsFilename, $reponse)) {
|
746 |
if (!file_put_contents($this->correspondingIdsFilename, $reponse)) {
|
743 |
throw new Exception("\nError writing correspondingIdsFilename with path : " . $this->correspondingIdsFilename . "\n");
|
747 |
throw new Exception("\nError writing correspondingIdsFilename with path : " . $this->correspondingIdsFilename . "\n");
|
744 |
}
|
748 |
}
|
745 |
echo "Matching partners ids saved!";
|
749 |
echo "Matching partners ids saved!";
|
746 |
}
|
750 |
}
|
747 |
}
|
751 |
}
|