Subversion Repositories eFlore/Applications.cel

Compare Revisions

No changes between revisions

Ignore whitespace Rev 3773 → Rev 3772

/branches/v3.00-serfouette/scripts/modules/pull_plantnet/PullPlantnet.php
3,9 → 3,7
/*
PHP 7
 
Script used to synchronize PN occ with Flora Data
 
Écrit en franglish pour une meilleure compréhension (lol)
Script used to synchronize PN occ with CEL
*/
 
class PullPlantnet extends Script {
12,8 → 10,6
 
protected $bdd;
 
protected $debug = false;
 
protected $allowedPartners = ['tb', 'pn'];
 
// Nom du fichier contenant les correspondances d'ID TB/PN
29,6 → 25,7
protected $currentPage = '';
protected $currentProject = '';
protected $hasMore = false;
protected $count = 0;
 
// Traduction des organes de PN
protected $tagsImageTraduits = [
47,7 → 44,7
protected $lifecycles = [
'created',
'modified',
'deleted', // deleted peut être très lent (pas d'index coté PN pour le moment)
'deleted', // deleted peut être très lent
];
 
/**
69,7 → 66,6
'-pnObsId' => array(false, true, "ID de l'obs chez PlantNet"),
'-project' => array(false, true, "projectId de l'obs chez PlantNet"),
'-tbObsId' => array(false, true, "ID de l'obs chez Tela"),
'-debug' => array(false, false, "Mode débug (ajoute du log) Au choix parmi : debug/log (log est plus verbeux)"),
);
 
public function __construct($script_nom, $parametres_cli) {
77,8 → 73,6
$this->bdd = new Bdd();
 
$this->correspondingIdsFilename = Config::get('correspondingIdsFilename');
 
$this->debug = $this->getParametre('debug');
}
 
public function executer() {
86,13 → 80,13
 
switch ($cmd) {
case 'updateLatest':
$this->updateLatest($this->getParametre('startDate'), $this->getParametre('lifecycle'));
$this->updateLatest($this->getParametre('startDate'));
break;
case 'updateOnePN':
$this->updateOnePN((int)$this->getParametre('pnObsId'), $this->getParametre('project'));
break;
case 'updateOneTB':
$this->updateOneTB((int)$this->getParametre('tbObsId'));
$this->updateOneTB($this->getParametre('tbObsId'));
break;
case 'updateCorrespondingIdsTable':
$this->updateCorrespondingIdsTable();
101,8 → 95,8
$msg = "Erreur : la commande '$cmd' n'existe pas!\n"
. ', utilisez plutôt :' . "\n"
. 'php cli.php PullPlantnet -a updateLatest -startDate "YYYY:MM:DD HH:II:SS" [[-lifecycle [modified]/created/deleted]]' . "\n"
. 'php cli.php PullPlantnet -a updateOnePN -pnObsId "1234567890" -project "xxxxxx"' . "\n"
. 'php cli.php PullPlantnet -a updateOneTB -tbObsId "1234567890"' . "\n"
. 'php cli.php PullPlantnet -a updateOnePN -pnObsId "xxxxxx" -project "xxxxxx"' . "\n"
. 'php cli.php PullPlantnet -a updateOneTB -tbObsId "xxxxxx"' . "\n"
. 'php cli.php PullPlantnet -a updateCorrespondingIdsTable' . "\n"
;
throw new Exception($msg);
138,9 → 132,7
[$project['id'], Config::get('tokenPlantnet'), $startDate, $lifecycle],
Config::get('urlPlantnetBase').Config::get('urlPlantnetLatestChanges')
);
 
$this->debug("URL service derniers changements : $url_service");
 
// die(var_dump($url_service));
$this->currentPage = $url_service;
}
 
156,7 → 148,6
} elseif (200 != $code) {
// l'api répond avec une 404 quand y'a une date dans le futur ou simplement pas de nouvelle obs...
if (404 == $code && strpos($reponse, 'No more results')) {
$this->log("Pas d'autres résultats");
$this->hasMore = false;
$this->currentPage = '';
return [];
168,10 → 159,11
 
$this->hasMore = $responseJson['hasMore'];
if ($this->hasMore) {
$this->count += count($observations_PN);
$this->currentPage = Config::get('urlPlantnetBase').$responseJson['next'];
$this->debug("URL service derniers changements, page suivante : {$this->currentPage}");
} else {
$this->currentPage = '';
$this->count = 0;
}
 
return $observations_PN;
181,8 → 173,6
// get PN projects list
$url = str_replace('{token}', Config::get('tokenPlantnet'), Config::get('urlPlantnetBase').Config::get('urlPlantnetProjects'));
 
$this->debug("URL service liste projets : $url");
 
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$reponse = curl_exec($ch);
196,8 → 186,6
 
echo 'Liste de projets récupérée : ' . count($pnProjects) . " projets dans la liste\n";
 
$this->debug("Liste projets : " . json_encode($pnProjects));
 
return $pnProjects;
}
 
211,24 → 199,19
foreach ($observations_PN as $obs) {
// est-ce qu'on a déjà traité cette obs ? (oui une même obs peut être dans plusieurs projects)
if (in_array($obs['id'], $this->processedObsId)) {
$this->log("Obs {$obs['id']} déjà traitée pendant cette run");
continue;
} else {
$this->processedObsId[] = $obs['id'];
$this->log("Obs {$obs['id']} ajoutée à la liste de cette run");
}
 
// 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
if (!isset($obs['currentName'])) {
$this->log("Obs {$obs['id']} sans nom de taxon, on zap");
continue; // pas de nom de taxon, obs inutile
}
if (!isset($obs['geo'])) {
$this->log("Obs {$obs['id']} sans geom, on zap");
continue; // pas de position, obs inutile
}
if (!isset($obs['dateObs'])) {
$this->log("Obs {$obs['id']} sans date, on zap");
continue; // pas de date, obs inutile
}
 
239,7 → 222,6
// on update l'identification proposée par PN
 
// @TODO
$this->log("Obs {$obs['id']} venant de Tela, mise à jour pas implémentée, on zap");
continue;
} elseif (!isset($obs['partner']['id'])) {
// ce n'est pas une obs d'un partenaire, c'est donc une obs PN
247,11 → 229,10
$email = $obs['author']['email'];
$infos_utilisateur = $this->findUserInfo($email);
if (!$infos_utilisateur) {
$this->log("Obs {$obs['id']} email $email ne provient pas d'un telabotaniste");
continue; // c'est pas un telabotaniste
}
$this->log("Obs {$obs['id']} email $email provient d'un telabotaniste");
$this->log(json_encode($obs));
//echo "Synchro d'une obs d'un telabotaniste : $email";
// echo json_encode($obs);
// die(var_dump($obs));
 
// vérification que l'obs n'a pas déjà été ajoutée à la BdD de Tela
273,7 → 254,6
}
 
if (isset($obs['deleted']) && (true === $obs['deleted'])) {
$this->log("Obs {$obs['id']} supprimée coté PlantNet");
continue; // obs supprimée chez PN sans être passée par nos serveurs
}
 
282,7 → 262,6
$images_size = 0;
foreach ($obs['images'] ?? [] as $i => $image) {
if ($image['deleted']) {
$this->log("Obs {$obs['id']} image {$image['id']} supprimée coté PlantNet");
continue;
}
 
292,7 → 271,6
do {
$img = file_get_contents($image['o']);
$retry--;
$this->log("Obs {$obs['id']} lecture image {$image['id']} tentatives restantes : $retry");
} while (!$img && $retry);
 
if (!$img) {
307,7 → 285,7
fclose($handle);
$images_size += filesize($tempfile);
 
$this->log("Obs {$obs['id']} image {$image['id']} créée " . number_format(filesize($tempfile), 0, ',', ' ') . " octets : $tempfile");
// echo "Image de " . number_format(filesize($tempfile), 0, ',', ' ') . " octets créé : $tempfile\n";
 
$params = [
'name' => 'image' . $i,
320,7 → 298,7
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$reponse = curl_exec($ch);
$this->log("Obs {$obs['id']} image {$image['id']} envoyée à l'api de création. Réponse : $reponse");
// var_dump($reponse);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
 
350,8 → 328,6
// die();
 
$geo = $this->getGeoInfo($obs['geo']);
$this->log("Obs {$obs['id']} décodage du geom : " . json_encode($obs['geo']));
$this->log("Obs {$obs['id']} geom décodé : " . json_encode($geo));
 
// on insère l'obs via le service CelWidgetSaisie
$infos_obs = [
372,7 → 348,7
// 'obsId1[num_nom_ret]' => '',
// 'obsId1[num_nom_sel]' => '',
// 'obsId1[num_taxon]' => '',
'obsId1[referentiel]' => $this->findProbableTaxoRepos($this->currentProject['id']), // @TODO faire une fois et mettre en cache
'obsId1[referentiel]' => $this->findProbableTaxoRepos($this->currentProject['id']),
// 'obsId1[station]' => '',
'obsId1[obs_id]' => $obs['id'],
'projet' => 'PlantNet',
388,9 → 364,8
foreach ($images as $i => $image) {
$infos_obs["obsId1[image_nom][$i]"] = $image;
}
// var_dump($infos_obs);
 
$this->log("Obs {$obs['id']} prête à être insérée : " . json_encode($infos_obs));
 
// curl post $infos_obs
$ch = curl_init($url_cel_widget_saisie);
curl_setopt($ch, CURLOPT_POST, true);
397,7 → 372,6
curl_setopt($ch, CURLOPT_POSTFIELDS, $infos_obs);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$reponse = curl_exec($ch);
$this->log("Obs {$obs['id']} réponse de la requête d'insertion : " . json_encode($reponse));
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
 
405,6 → 379,7
throw new Exception("Ça a pété à l'envoi de l'obs :\n $reponse\n avec l'obs : " . json_encode($infos_obs) . "\n");
}
 
// var_dump($reponse);
$id_obs_tb = json_decode($reponse, true)['id'];
$id_obs_pn = $obs['id'];
// on insère les ids de correspondance obsPN obsTB dans la table cel_plantnet
417,7 → 392,7
$date = date('d/m/Y H:i:s', intdiv($obs['dateObs'], 1000));
$count_img = count($images);
$images_size = number_format($images_size, 0, ',', ' ');
echo "Obs insérée en base : ID PN $id_obs_pn ; ID TB $id_obs_tb ; utilisateur_tb $email ; date_obs $date ; images $count_img ; taille_octets $images_size \n";
echo "Obs insérée en base : ID PN $id_obs_pn ; ID TB $id_obs_tb ; utilisateur_tb $email ; date $date ; images $count_img ; taille_octets $images_size \n";
}
}
}
778,16 → 753,4
}
echo "Matching partners ids saved!";
}
 
private function debug($text) {
if ($this->debug) {
echo 'DEBUG - ' . $text . "\n";
}
}
 
private function log($text) {
if ('log' == $this->debug) {
echo 'LOG - ' . $text . "\n";
}
}
}
/branches/v3.00-serfouette/scripts
Property changes:
Modified: svn:mergeinfo
Reverse-merged /trunk/scripts:r3771-3772
/branches/v3.00-serfouette
Property changes:
Modified: svn:mergeinfo
Reverse-merged /trunk:r3771-3772