Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 22 | Rev 27 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 22 Rev 24
Line 1... Line 1...
1
<?php
1
<?php
2
/** Exemple lancement:
2
/** Exemple lancement:
3
 * /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php nvjfl
3
 * /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php nvjfl
4
 * 		-a indexer
4
 * 		-a indexer
5
 * 		-f /home/jpm/eflore/donnees/nvjfl/2007-10-29/nvjfl_v2007-10-29.csv
-
 
6
 * 		-table nvjfl_v2007
-
 
7
 * Options :
5
 * Options :
8
 * -f : indiquer le chemin du fichier à analyser
6
 * -t : Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes, par défaut 10).
9
*/
7
*/
10
class Nvjfl extends Script {
8
class Nvjfl extends Script {
Line 11... Line 9...
11
 
9
 
12
	private $bdd = null;
10
	private $bdd = null;
13
	private $nomsIndex = array();
11
	private $nomsIndex = array();
Line 14... Line 12...
14
	private $numeroIndex = 1;
12
	private $numeroIndex = 1;
15
 
-
 
16
	protected $parametres_autorises = array(
13
 
17
		'-f' => array(true, null, 'Chemin du fichier à analyser'),
-
 
Line 18... Line 14...
18
		'-test' => array(false, 10, 'Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).'),
14
	protected $parametres_autorises = array(
19
		'-table' => array(true, true, 'Nom de la table où insérer les données.'));
15
		'-t' => array(false, true, 'Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).'));
20
 
16
 
-
 
17
	public function executer() {
-
 
18
		// Lancement de l'action demandée
-
 
19
		try {
21
	public function executer() {
20
			$this->chargerConfigNvjfl();
22
		// Lancement de l'action demandée
21
			$this->bdd = new Bdd();
-
 
22
 
-
 
23
			$cmd = $this->getParametre('a');
-
 
24
		    switch ($cmd) {
-
 
25
		    	case 'chargerTous' :
-
 
26
		    		$this->chargerNvjfl();
23
		try {
27
		    		$this->chargerBiblio();
24
			$cmd = $this->getParametre('a');
28
		    		$this->chargerBiblioLien();
-
 
29
		    		break;
-
 
30
				case 'chargerNvjfl' :
-
 
31
					$this->chargerNvjfl();
-
 
32
					break;
-
 
33
				case 'chargerBiblio' :
-
 
34
					$this->chargerBiblio();
25
		    switch ($cmd) {
35
					break;
26
				case 'indexer' :
36
				case 'chargerBiblioLien' :
27
					$this->indexer();
37
					$this->chargerBiblioLien();
28
					break;
38
					break;
29
				default :
39
				default :
30
					$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
40
					$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
31
			}
41
			}
32
		} catch (Exception $e) {
42
		} catch (Exception $e) {
-
 
43
			$this->traiterErreur($e->getMessage());
-
 
44
		}
-
 
45
    }
-
 
46
 
-
 
47
    private function chargerConfigNvjfl() {
-
 
48
    	$fichierIni = dirname(__FILE__).DS.'nvjfl.ini';
-
 
49
    	if (file_exists($fichierIni)) {
-
 
50
    		Config::charger($fichierIni);
-
 
51
    	} else {
-
 
52
    		$m = "Veuillez configurer le projet en créant le fichier 'nvjfl.ini' ".
-
 
53
    			"dans le dossier du module de script du projet à partir du fichier 'nvjfl.defaut.ini'.";
-
 
54
    		throw new Exception($m);
33
			$this->traiterErreur($e->getMessage());
55
    	}
34
		}
56
    }
35
    }
57
 
36
    /**
58
    /**
-
 
59
     * Charge le fichier en créant un id pour chaque nom vernaculaire.
37
     * Créer un index pour un nom identique la clé primaire est composée de cet index + num_taxon + langue
60
     */
38
     */
-
 
39
	private function indexer() {
61
	private function chargerNvjfl() {
40
		$fichierOuvert = $this->ouvrirFichier($this->getParametre('f'));
62
		//Debug::printr(Config::get('fichiers'));
41
		$this->bdd = new Bdd();
63
		$fichierOuvert = $this->ouvrirFichier(Config::get('chemins.nvjfl'));
42
		$donnees = $this->analyserFichier($fichierOuvert);
64
		$donnees = $this->analyserFichier($fichierOuvert);
43
		fclose($fichierOuvert);
65
		fclose($fichierOuvert);
Line 44... Line 66...
44
		foreach ($donnees as $donnee) {
66
		foreach ($donnees as $donnee) {
45
			$requete = 'INSERT INTO '.$this->getParametre('table').' VALUES ('.implode(', ', $donnee).')';
67
			$requete = 'INSERT INTO '.Config::get('tables.nvjfl').' VALUES ('.implode(', ', $donnee).')';
46
			$this->bdd->requeter($requete);
68
			$this->bdd->requeter($requete);
Line 54... Line 76...
54
	}
76
	}
Line 55... Line 77...
55
 
77
 
56
	private function stopperLaBoucle() {
78
	private function stopperLaBoucle() {
57
		$stop = false;
79
		$stop = false;
58
		static $ligneActuelle = 1;
80
		static $ligneActuelle = 1;
59
		if ($nbreLignesATester = $this->getParametre('test')) {
81
		if ($nbreLignesATester = $this->getParametre('t')) {
60
			if ($nbreLignesATester == $ligneActuelle++) {
82
			if ($nbreLignesATester == $ligneActuelle++) {
61
				$stop = true;
83
				$stop = true;
62
			}
84
			}
63
		}
85
		}
Line 67... Line 89...
67
	private function analyserFichier($fichierOuvert) {
89
	private function analyserFichier($fichierOuvert) {
68
		$donnees = array();
90
		$donnees = array();
69
		$entetesCsv = fgets($fichierOuvert);
91
		$entetesCsv = fgets($fichierOuvert);
70
		while ($ligneCsv = fgets($fichierOuvert)) {
92
		while ($ligneCsv = fgets($fichierOuvert)) {
71
			$champs = explode("\t", trim($ligneCsv));
93
			$champs = explode("\t", trim($ligneCsv));
-
 
94
			if (count($champs) > 0) {
72
			$nomVernaculaire = $champs[2];
95
				$nomVernaculaire = $champs[2];
73
			$indexCourrant = $this->getIndexNomVernaculaire($nomVernaculaire);
96
				$indexCourrant = $this->getIndexNomVernaculaire($nomVernaculaire);
74
			$champs = array_merge(array($indexCourrant), $champs);
97
				$champs = array_merge(array($indexCourrant), $champs);
75
			$donnees[] = $this->protegerValeursDesChamps($champs);
98
				$donnees[] = $this->protegerValeursDesChamps($champs);
76
 
99
			}
77
			$this->afficherAvancement("Analyse du fichier des noms vernaculaires");
100
			$this->afficherAvancement("Analyse du fichier des noms vernaculaires");
78
			if ($this->stopperLaBoucle()) {
101
			if ($this->stopperLaBoucle()) {
79
				break;
102
				break;
80
			}
103
			}
81
		}
104
		}
Line 115... Line 138...
115
			$valeur = isset($champs[$i]) ? $champs[$i] : '';
138
			$valeur = isset($champs[$i]) ? $champs[$i] : '';
116
			$champsProteges[] = $this->bdd->proteger($valeur);
139
			$champsProteges[] = $this->bdd->proteger($valeur);
117
		}
140
		}
118
		return $champsProteges;
141
		return $champsProteges;
119
	}
142
	}
-
 
143
 
-
 
144
	private function chargerBiblio() {
-
 
145
		$cheminsNvjflBiblio = Config::get('chemins.nvjflBiblio');
-
 
146
		$tableNvjflBiblio = Config::get('tables.nvjflBiblio');
-
 
147
		$requete = "LOAD DATA INFILE '$cheminsNvjflBiblio' ".
-
 
148
			"REPLACE INTO TABLE $tableNvjflBiblio ".
-
 
149
			'CHARACTER SET utf8 '.
-
 
150
			'FIELDS '.
-
 
151
			"	TERMINATED BY '\t' ".
-
 
152
			"	ENCLOSED BY '' ".
-
 
153
			"	ESCAPED BY '\\\' ".
-
 
154
			'IGNORE 1 LINES';
-
 
155
		$this->bdd->requeter($requete);
-
 
156
	}
-
 
157
 
-
 
158
	private function chargerBiblioLien() {
-
 
159
		$cheminNvjflLienBiblio = Config::get('chemins.nvjflLienBiblio');
-
 
160
		$tableNvjflLienBiblio = Config::get('tables.nvjflLienBiblio');
-
 
161
		$requete = "LOAD DATA INFILE '$cheminNvjflLienBiblio' ".
-
 
162
			"REPLACE INTO TABLE $tableNvjflLienBiblio ".
-
 
163
			'CHARACTER SET utf8 '.
-
 
164
			'FIELDS '.
-
 
165
			"	TERMINATED BY '\t' ".
-
 
166
			"	ENCLOSED BY '' ".
-
 
167
			"	ESCAPED BY '\\\' ".
-
 
168
			'IGNORE 1 LINES';
-
 
169
		$this->bdd->requeter($requete);
-
 
170
	}
120
}
171
}
121
?>
172
?>
122
173