Subversion Repositories eFlore/Projets.eflore-projets

Compare Revisions

Ignore whitespace Rev 21 → Rev 22

/trunk/scripts/modules/nvjfl/Nvjfl.php
1,52 → 1,121
<?php
/** Exemple lancement:
* /opt/lampp/bin/php -d memory_limit=3500M ~/web/nripts/cli.php nvjfl -a indexer -f /home/delphine/Documents/nvjfl_version_2007-10-29/NomsVernaculaires-2007-10-29/NomsVernaculaires-2007-10-29.csv
* /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php nvjfl
* -a indexer
* -f /home/jpm/eflore/donnees/nvjfl/2007-10-29/nvjfl_v2007-10-29.csv
* -table nvjfl_v2007
* Options :
* -f : indiquer le chemin du fichier à analyser
*/
class Nvjfl extends Script {
 
private $bdd = null;
private $nomsIndex = array();
private $numeroIndex = 1;
 
protected $parametres_autorises = array(
'-f' => array(true, null, 'Chemin du fichier à analyser'));
'-f' => array(true, null, 'Chemin du fichier à analyser'),
'-test' => array(false, 10, 'Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).'),
'-table' => array(true, true, 'Nom de la table où insérer les données.'));
 
public function executer() {
// Lancement de l'action demandée
$cmd = $this->getParametre('a');
switch ($cmd) {
case 'indexer' :
$this->indexer();
break;
default :
$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
try {
$cmd = $this->getParametre('a');
switch ($cmd) {
case 'indexer' :
$this->indexer();
break;
default :
$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
}
} catch (Exception $e) {
$this->traiterErreur($e->getMessage());
}
}
/**
* Créer un index pour un nom identique la clé primaire est composée de cet index + num_taxon + langue
*/
private function indexer() {
$fichierOuvert = $this->ouvrirFichier($this->getParametre('f'));
$this->bdd = new Bdd();
$donnees = $this->analyserFichier($fichierOuvert);
fclose($fichierOuvert);
foreach ($donnees as $donnee) {
$requete = 'INSERT INTO '.$this->getParametre('table').' VALUES ('.implode(', ', $donnee).')';
$this->bdd->requeter($requete);
 
private function indexer() {
// créer un index pour un nom identique la clé primaire est composée de cet index + num_taxon + langue
$nomFichier = $this->getParametre('f');
if ($nomFichier) {
if (file_exists($nomFichier) === true) {
if ( $fichierOuvert = fopen($nomFichier, 'r') ) {
$this->bdd = new Bdd();
$nom_precedent = '';
$index = 0;
while ($ligne = fgets($fichierOuvert)) {
$champs = explode(';', $ligne);
if ($champs[3] != $nom_precedent) {
$index ++;
$nom_precedent = $champs[3];
}
$champs[0] = $index;
$this->bdd->requeter('INSERT INTO '.Config::get('bdd_table').' VALUES ('.implode(', ',$champs).');');
}
fclose($fichierOuvert);
} else {
$this->traiterErreur("Le fichier $nomFichier n'a pas pu être ouvert.");
$this->afficherAvancement("Insertion des noms vernaculaires dans la base de données");
if ($this->stopperLaBoucle()) {
break;
}
}
echo "\n";
}
 
private function stopperLaBoucle() {
$stop = false;
static $ligneActuelle = 1;
if ($nbreLignesATester = $this->getParametre('test')) {
if ($nbreLignesATester == $ligneActuelle++) {
$stop = true;
}
}
return $stop;
}
 
private function analyserFichier($fichierOuvert) {
$donnees = array();
$entetesCsv = fgets($fichierOuvert);
while ($ligneCsv = fgets($fichierOuvert)) {
$champs = explode("\t", trim($ligneCsv));
$nomVernaculaire = $champs[2];
$indexCourrant = $this->getIndexNomVernaculaire($nomVernaculaire);
$champs = array_merge(array($indexCourrant), $champs);
$donnees[] = $this->protegerValeursDesChamps($champs);
 
$this->afficherAvancement("Analyse du fichier des noms vernaculaires");
if ($this->stopperLaBoucle()) {
break;
}
}
echo "\n";
return $donnees;
}
 
private function getIndexNomVernaculaire($nomVernaculaire) {
$indexCourrant = null;
if (array_key_exists($nomVernaculaire, $this->nomsIndex) == false) {
$this->nomsIndex[$nomVernaculaire] = $this->numeroIndex++;
}
$indexCourrant = $this->nomsIndex[$nomVernaculaire];
return $indexCourrant;
}
 
private function ouvrirFichier($chemin) {
$fichierOuvert = false;
if ($chemin) {
if (file_exists($chemin) === true) {
$fichierOuvert = fopen($chemin, 'r');
if ($fichierOuvert == false) {
throw new Exception("Le fichier $chemin n'a pas pu être ouvert.");
}
} else {
$this->traiterErreur("Le fichier $nomFichier est introuvable.");
throw new Exception("Le fichier $chemin est introuvable.");
}
} else {
throw new Exception("Aucun chemin de fichier n'a été fourni.");
}
return $fichierOuvert;
}
 
private function protegerValeursDesChamps($champs) {
$champsProteges = array();
for ($i = 0; $i < 9; $i++) {
$valeur = isset($champs[$i]) ? $champs[$i] : '';
$champsProteges[] = $this->bdd->proteger($valeur);
}
return $champsProteges;
}
}
?>