Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 11 | Rev 24 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 jpm 1
<?php
2
/** Exemple lancement:
22 jpm 3
 * /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php nvjfl
4
 * 		-a indexer
5
 * 		-f /home/jpm/eflore/donnees/nvjfl/2007-10-29/nvjfl_v2007-10-29.csv
6
 * 		-table nvjfl_v2007
11 jpm 7
 * Options :
8
 * -f : indiquer le chemin du fichier à analyser
9
*/
10
class Nvjfl extends Script {
11
 
22 jpm 12
	private $bdd = null;
13
	private $nomsIndex = array();
14
	private $numeroIndex = 1;
15
 
11 jpm 16
	protected $parametres_autorises = array(
22 jpm 17
		'-f' => array(true, null, 'Chemin du fichier à analyser'),
18
		'-test' => array(false, 10, 'Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).'),
19
		'-table' => array(true, true, 'Nom de la table où insérer les données.'));
11 jpm 20
 
21
	public function executer() {
22
		// Lancement de l'action demandée
22 jpm 23
		try {
24
			$cmd = $this->getParametre('a');
25
		    switch ($cmd) {
26
				case 'indexer' :
27
					$this->indexer();
28
					break;
29
				default :
30
					$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
31
			}
32
		} catch (Exception $e) {
33
			$this->traiterErreur($e->getMessage());
11 jpm 34
		}
35
    }
22 jpm 36
    /**
37
     * Créer un index pour un nom identique la clé primaire est composée de cet index + num_taxon + langue
38
     */
39
	private function indexer() {
40
		$fichierOuvert = $this->ouvrirFichier($this->getParametre('f'));
41
		$this->bdd = new Bdd();
42
		$donnees = $this->analyserFichier($fichierOuvert);
43
		fclose($fichierOuvert);
44
		foreach ($donnees as $donnee) {
45
			$requete = 'INSERT INTO '.$this->getParametre('table').' VALUES ('.implode(', ', $donnee).')';
46
			$this->bdd->requeter($requete);
11 jpm 47
 
22 jpm 48
			$this->afficherAvancement("Insertion des noms vernaculaires dans la base de données");
49
			if ($this->stopperLaBoucle()) {
50
				break;
51
			}
52
		}
53
		echo "\n";
54
	}
55
 
56
	private function stopperLaBoucle() {
57
		$stop = false;
58
		static $ligneActuelle = 1;
59
		if ($nbreLignesATester = $this->getParametre('test')) {
60
			if ($nbreLignesATester == $ligneActuelle++) {
61
				$stop = true;
62
			}
63
		}
64
		return $stop;
65
	}
66
 
67
	private function analyserFichier($fichierOuvert) {
68
		$donnees = array();
69
		$entetesCsv = fgets($fichierOuvert);
70
		while ($ligneCsv = fgets($fichierOuvert)) {
71
			$champs = explode("\t", trim($ligneCsv));
72
			$nomVernaculaire = $champs[2];
73
			$indexCourrant = $this->getIndexNomVernaculaire($nomVernaculaire);
74
			$champs = array_merge(array($indexCourrant), $champs);
75
			$donnees[] = $this->protegerValeursDesChamps($champs);
76
 
77
			$this->afficherAvancement("Analyse du fichier des noms vernaculaires");
78
			if ($this->stopperLaBoucle()) {
79
				break;
80
			}
81
		}
82
		echo "\n";
83
		return $donnees;
84
	}
85
 
86
	private function getIndexNomVernaculaire($nomVernaculaire) {
87
		$indexCourrant = null;
88
		if (array_key_exists($nomVernaculaire, $this->nomsIndex) == false) {
89
			$this->nomsIndex[$nomVernaculaire] = $this->numeroIndex++;
90
		}
91
		$indexCourrant = $this->nomsIndex[$nomVernaculaire];
92
		return $indexCourrant;
93
	}
94
 
95
	private function ouvrirFichier($chemin) {
96
		$fichierOuvert = false;
97
		if ($chemin) {
98
			if (file_exists($chemin) === true) {
99
				$fichierOuvert = fopen($chemin, 'r');
100
				if ($fichierOuvert == false) {
101
					throw new Exception("Le fichier $chemin n'a pas pu être ouvert.");
11 jpm 102
				}
103
			} else {
22 jpm 104
				throw new Exception("Le fichier $chemin est introuvable.");
11 jpm 105
			}
22 jpm 106
		} else {
107
			throw new Exception("Aucun chemin de fichier n'a été fourni.");
11 jpm 108
		}
22 jpm 109
		return $fichierOuvert;
11 jpm 110
	}
22 jpm 111
 
112
	private function protegerValeursDesChamps($champs) {
113
		$champsProteges = array();
114
		for ($i = 0; $i < 9; $i++) {
115
			$valeur = isset($champs[$i]) ? $champs[$i] : '';
116
			$champsProteges[] = $this->bdd->proteger($valeur);
117
		}
118
		return $champsProteges;
119
	}
11 jpm 120
}
121
?>