Subversion Repositories eFlore/Applications.cel

Rev

Rev 3756 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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