2,10 → 2,8 |
/** Exemple lancement: |
* /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 |
* -t : Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes, par défaut 10). |
*/ |
class Nvjfl extends Script { |
|
14,18 → 12,30 |
private $numeroIndex = 1; |
|
protected $parametres_autorises = array( |
'-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.')); |
'-t' => array(false, true, 'Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).')); |
|
public function executer() { |
// Lancement de l'action demandée |
try { |
$this->chargerConfigNvjfl(); |
$this->bdd = new Bdd(); |
|
$cmd = $this->getParametre('a'); |
switch ($cmd) { |
case 'indexer' : |
$this->indexer(); |
case 'chargerTous' : |
$this->chargerNvjfl(); |
$this->chargerBiblio(); |
$this->chargerBiblioLien(); |
break; |
case 'chargerNvjfl' : |
$this->chargerNvjfl(); |
break; |
case 'chargerBiblio' : |
$this->chargerBiblio(); |
break; |
case 'chargerBiblioLien' : |
$this->chargerBiblioLien(); |
break; |
default : |
$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd)); |
} |
33,16 → 43,28 |
$this->traiterErreur($e->getMessage()); |
} |
} |
|
private function chargerConfigNvjfl() { |
$fichierIni = dirname(__FILE__).DS.'nvjfl.ini'; |
if (file_exists($fichierIni)) { |
Config::charger($fichierIni); |
} else { |
$m = "Veuillez configurer le projet en créant le fichier 'nvjfl.ini' ". |
"dans le dossier du module de script du projet à partir du fichier 'nvjfl.defaut.ini'."; |
throw new Exception($m); |
} |
} |
|
/** |
* Créer un index pour un nom identique la clé primaire est composée de cet index + num_taxon + langue |
* Charge le fichier en créant un id pour chaque nom vernaculaire. |
*/ |
private function indexer() { |
$fichierOuvert = $this->ouvrirFichier($this->getParametre('f')); |
$this->bdd = new Bdd(); |
private function chargerNvjfl() { |
//Debug::printr(Config::get('fichiers')); |
$fichierOuvert = $this->ouvrirFichier(Config::get('chemins.nvjfl')); |
$donnees = $this->analyserFichier($fichierOuvert); |
fclose($fichierOuvert); |
foreach ($donnees as $donnee) { |
$requete = 'INSERT INTO '.$this->getParametre('table').' VALUES ('.implode(', ', $donnee).')'; |
$requete = 'INSERT INTO '.Config::get('tables.nvjfl').' VALUES ('.implode(', ', $donnee).')'; |
$this->bdd->requeter($requete); |
|
$this->afficherAvancement("Insertion des noms vernaculaires dans la base de données"); |
56,7 → 78,7 |
private function stopperLaBoucle() { |
$stop = false; |
static $ligneActuelle = 1; |
if ($nbreLignesATester = $this->getParametre('test')) { |
if ($nbreLignesATester = $this->getParametre('t')) { |
if ($nbreLignesATester == $ligneActuelle++) { |
$stop = true; |
} |
69,11 → 91,12 |
$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); |
|
if (count($champs) > 0) { |
$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; |
117,5 → 140,33 |
} |
return $champsProteges; |
} |
|
private function chargerBiblio() { |
$cheminsNvjflBiblio = Config::get('chemins.nvjflBiblio'); |
$tableNvjflBiblio = Config::get('tables.nvjflBiblio'); |
$requete = "LOAD DATA INFILE '$cheminsNvjflBiblio' ". |
"REPLACE INTO TABLE $tableNvjflBiblio ". |
'CHARACTER SET utf8 '. |
'FIELDS '. |
" TERMINATED BY '\t' ". |
" ENCLOSED BY '' ". |
" ESCAPED BY '\\\' ". |
'IGNORE 1 LINES'; |
$this->bdd->requeter($requete); |
} |
|
private function chargerBiblioLien() { |
$cheminNvjflLienBiblio = Config::get('chemins.nvjflLienBiblio'); |
$tableNvjflLienBiblio = Config::get('tables.nvjflLienBiblio'); |
$requete = "LOAD DATA INFILE '$cheminNvjflLienBiblio' ". |
"REPLACE INTO TABLE $tableNvjflLienBiblio ". |
'CHARACTER SET utf8 '. |
'FIELDS '. |
" TERMINATED BY '\t' ". |
" ENCLOSED BY '' ". |
" ESCAPED BY '\\\' ". |
'IGNORE 1 LINES'; |
$this->bdd->requeter($requete); |
} |
} |
?> |