Subversion Repositories eFlore/Projets.eflore-projets

Compare Revisions

Ignore whitespace Rev 1173 → Rev 1174

/tags/v5.12-baouque/scripts/modules/nvjfl/Nvjfl.php
New file
0,0 → 1,180
<?php
/** Exemple lancement:
* /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php nvjfl
* -a chargerTous
* Options :
* -t : Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).
*/
class Nvjfl extends EfloreScript {
 
private $nomsIndex = array();
private $numeroIndex = 1;
 
protected $parametres_autorises = array(
'-t' => array(false, false, '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->initialiserProjet('nvjfl');
 
$cmd = $this->getParametre('a');
switch ($cmd) {
case 'chargerTous' :
$this->chargerStructureSql();
$this->chargerNvjfl();
$this->chargerBiblio();
$this->chargerBiblioLien();
$this->chargerOntologies();
break;
case 'chargerStructure' :
$this->chargerStructureSql();
break;
case 'chargerNvjfl' :
$this->chargerNvjfl();
break;
case 'chargerBiblio' :
$this->chargerBiblio();
break;
case 'chargerBiblioLien' :
$this->chargerBiblioLien();
break;
case 'chargerOntologies' :
$this->chargerOntologies();
break;
case 'supprimerTous' :
$this->supprimerTous();
break;
default :
throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
}
} catch (Exception $e) {
$this->traiterErreur($e->getMessage());
}
}
 
/**
* Charge le fichier en créant un id pour chaque nom vernaculaire.
*/
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 '.Config::get('tables.nvjfl').' VALUES ('.implode(', ', $donnee).')';
$this->getBdd()->requeter($requete);
 
$this->afficherAvancement("Insertion des noms vernaculaires dans la base de données");
if ($this->stopperLaBoucle($this->getParametre('t'))) {
break;
}
}
echo "\n";
}
 
private function analyserFichier($fichierOuvert) {
$donnees = array();
$entetesCsv = fgets($fichierOuvert);
while ($ligneCsv = fgets($fichierOuvert)) {
$champs = explode("\t", trim($ligneCsv));
if (count($champs) > 0) {
if (isset($champs[2])) {
$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 {
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->getBdd()->proteger($valeur);
}
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->getBdd()->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->getBdd()->requeter($requete);
}
 
private function chargerOntologies() {
$cheminOntologies = Config::get('chemins.ontologies');
$tableOntologies = Config::get('tables.ontologies');
$requete = "LOAD DATA INFILE '$cheminOntologies' ".
"REPLACE INTO TABLE $tableOntologies ".
'CHARACTER SET utf8 '.
'FIELDS '.
" TERMINATED BY '\t' ".
" ENCLOSED BY '' ".
" ESCAPED BY '\\\' ".
'IGNORE 1 LINES';
$this->getBdd()->requeter($requete);
}
 
private function supprimerTous() {
$requete = "DROP TABLE IF EXISTS nvjfl_biblio_v2007, nvjfl_lien_biblio_v2007, nvjfl_meta, nvjfl_ontologies_v2007, nvjfl_v2007";
$this->getBdd()->requeter($requete);
}
}
?>