Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 28 | Rev 53 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 28 Rev 44
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 chargerTous
4
 * 		-a chargerTous
5
 * Options :
5
 * Options :
6
 * -t : Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).
6
 * -t : Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).
7
*/
7
*/
8
class Nvjfl extends Script {
8
class Nvjfl extends Script {
9
 
9
 
10
	private $bdd = null;
10
	private $bdd = null;
-
 
11
	private $projetNom = 'nvjfl';
11
	private $nomsIndex = array();
12
	private $nomsIndex = array();
12
	private $numeroIndex = 1;
13
	private $numeroIndex = 1;
13
 
14
 
14
	protected $parametres_autorises = array(
15
	protected $parametres_autorises = array(
15
		'-t' => array(false, true, 'Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).'));
16
		'-t' => array(false, true, 'Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).'));
16
 
17
 
17
	public function executer() {
18
	public function executer() {
18
		// Lancement de l'action demandée
19
		// Lancement de l'action demandée
19
		try {
20
		try {
20
			$this->chargerConfigNvjfl();
21
			$this->chargerConfigDuProjet();
21
			$this->bdd = new Bdd();
22
			$this->bdd = new Bdd();
22
 
23
 
23
			$cmd = $this->getParametre('a');
24
			$cmd = $this->getParametre('a');
24
		    switch ($cmd) {
25
		    switch ($cmd) {
25
		    	case 'chargerTous' :
26
		    	case 'chargerTous' :
-
 
27
		    		$this->chargerStructureSql();
26
		    		$this->chargerNvjfl();
28
		    		$this->chargerNvjfl();
27
		    		$this->chargerBiblio();
29
		    		$this->chargerBiblio();
28
		    		$this->chargerBiblioLien();
30
		    		$this->chargerBiblioLien();
29
		    		$this->chargerOntologies();
31
		    		$this->chargerOntologies();
30
		    		break;
32
		    		break;
31
				case 'chargerNvjfl' :
33
				case 'chargerNvjfl' :
32
					$this->chargerNvjfl();
34
					$this->chargerNvjfl();
33
					break;
35
					break;
34
				case 'chargerBiblio' :
36
				case 'chargerBiblio' :
35
					$this->chargerBiblio();
37
					$this->chargerBiblio();
36
					break;
38
					break;
37
				case 'chargerBiblioLien' :
39
				case 'chargerBiblioLien' :
38
					$this->chargerBiblioLien();
40
					$this->chargerBiblioLien();
39
					break;
41
					break;
40
				case 'chargerOntologies' :
42
				case 'chargerOntologies' :
41
					$this->chargerOntologies();
43
					$this->chargerOntologies();
42
					break;
44
					break;
43
				default :
45
				default :
44
					$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
46
					$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
45
			}
47
			}
46
		} catch (Exception $e) {
48
		} catch (Exception $e) {
47
			$this->traiterErreur($e->getMessage());
49
			$this->traiterErreur($e->getMessage());
48
		}
50
		}
49
    }
51
    }
50
 
52
 
51
    private function chargerConfigNvjfl() {
53
    private function chargerConfigDuProjet() {
52
    	$fichierIni = dirname(__FILE__).DS.'nvjfl.ini';
54
    	$fichierIni = dirname(__FILE__).DS.$this->projetNom.'.ini';
53
    	if (file_exists($fichierIni)) {
55
    	if (file_exists($fichierIni)) {
54
    		Config::charger($fichierIni);
56
    		Config::charger($fichierIni);
55
    	} else {
57
    	} else {
56
    		$m = "Veuillez configurer le projet en créant le fichier 'nvjfl.ini' ".
58
    		$m = "Veuillez configurer le projet en créant le fichier '{$this->projetNom}.ini' ".
57
    			"dans le dossier du module de script du projet à partir du fichier 'nvjfl.defaut.ini'.";
59
    			"dans le dossier du module de script du projet à partir du fichier '{$this->projetNom}.defaut.ini'.";
58
    		throw new Exception($m);
60
    		throw new Exception($m);
59
    	}
61
    	}
-
 
62
    }
-
 
63
 
-
 
64
    private function chargerStructureSql() {
-
 
65
    	$chemin = Config::get('chemins.structureSql');
-
 
66
    	$requetes = Outils::extraireRequetes($chemin);
-
 
67
    	foreach ($requetes as $requete) {
-
 
68
    		$this->bdd->requeter($requete);
-
 
69
    	}
60
    }
70
    }
61
 
71
 
62
    /**
72
    /**
63
     * Charge le fichier en créant un id pour chaque nom vernaculaire.
73
     * Charge le fichier en créant un id pour chaque nom vernaculaire.
64
     */
74
     */
65
	private function chargerNvjfl() {
75
	private function chargerNvjfl() {
66
		//Debug::printr(Config::get('fichiers'));
76
		//Debug::printr(Config::get('fichiers'));
67
		$fichierOuvert = $this->ouvrirFichier(Config::get('chemins.nvjfl'));
77
		$fichierOuvert = $this->ouvrirFichier(Config::get('chemins.nvjfl'));
68
		$donnees = $this->analyserFichier($fichierOuvert);
78
		$donnees = $this->analyserFichier($fichierOuvert);
69
		fclose($fichierOuvert);
79
		fclose($fichierOuvert);
70
		foreach ($donnees as $donnee) {
80
		foreach ($donnees as $donnee) {
71
			$requete = 'INSERT INTO '.Config::get('tables.nvjfl').' VALUES ('.implode(', ', $donnee).')';
81
			$requete = 'INSERT INTO '.Config::get('tables.nvjfl').' VALUES ('.implode(', ', $donnee).')';
72
			$this->bdd->requeter($requete);
82
			$this->bdd->requeter($requete);
73
 
83
 
74
			$this->afficherAvancement("Insertion des noms vernaculaires dans la base de données");
84
			$this->afficherAvancement("Insertion des noms vernaculaires dans la base de données");
75
			if ($this->stopperLaBoucle()) {
85
			if ($this->stopperLaBoucle()) {
76
				break;
86
				break;
77
			}
87
			}
78
		}
88
		}
79
		echo "\n";
89
		echo "\n";
80
	}
90
	}
81
 
91
 
82
	private function stopperLaBoucle() {
92
	private function stopperLaBoucle() {
83
		$stop = false;
93
		$stop = false;
84
		static $ligneActuelle = 1;
94
		static $ligneActuelle = 1;
85
		if ($nbreLignesATester = $this->getParametre('t')) {
95
		if ($nbreLignesATester = $this->getParametre('t')) {
86
			if ($nbreLignesATester == $ligneActuelle++) {
96
			if ($nbreLignesATester == $ligneActuelle++) {
87
				$stop = true;
97
				$stop = true;
88
			}
98
			}
89
		}
99
		}
90
		return $stop;
100
		return $stop;
91
	}
101
	}
92
 
102
 
93
	private function analyserFichier($fichierOuvert) {
103
	private function analyserFichier($fichierOuvert) {
94
		$donnees = array();
104
		$donnees = array();
95
		$entetesCsv = fgets($fichierOuvert);
105
		$entetesCsv = fgets($fichierOuvert);
96
		while ($ligneCsv = fgets($fichierOuvert)) {
106
		while ($ligneCsv = fgets($fichierOuvert)) {
97
			$champs = explode("\t", trim($ligneCsv));
107
			$champs = explode("\t", trim($ligneCsv));
98
			if (count($champs) > 0) {
108
			if (count($champs) > 0) {
99
				$nomVernaculaire = $champs[2];
109
				$nomVernaculaire = $champs[2];
100
				$indexCourrant = $this->getIndexNomVernaculaire($nomVernaculaire);
110
				$indexCourrant = $this->getIndexNomVernaculaire($nomVernaculaire);
101
				$champs = array_merge(array($indexCourrant), $champs);
111
				$champs = array_merge(array($indexCourrant), $champs);
102
				$donnees[] = $this->protegerValeursDesChamps($champs);
112
				$donnees[] = $this->protegerValeursDesChamps($champs);
103
			}
113
			}
104
			$this->afficherAvancement("Analyse du fichier des noms vernaculaires");
114
			$this->afficherAvancement("Analyse du fichier des noms vernaculaires");
105
			if ($this->stopperLaBoucle()) {
115
			if ($this->stopperLaBoucle()) {
106
				break;
116
				break;
107
			}
117
			}
108
		}
118
		}
109
		echo "\n";
119
		echo "\n";
110
		return $donnees;
120
		return $donnees;
111
	}
121
	}
112
 
122
 
113
	private function getIndexNomVernaculaire($nomVernaculaire) {
123
	private function getIndexNomVernaculaire($nomVernaculaire) {
114
		$indexCourrant = null;
124
		$indexCourrant = null;
115
		if (array_key_exists($nomVernaculaire, $this->nomsIndex) == false) {
125
		if (array_key_exists($nomVernaculaire, $this->nomsIndex) == false) {
116
			$this->nomsIndex[$nomVernaculaire] = $this->numeroIndex++;
126
			$this->nomsIndex[$nomVernaculaire] = $this->numeroIndex++;
117
		}
127
		}
118
		$indexCourrant = $this->nomsIndex[$nomVernaculaire];
128
		$indexCourrant = $this->nomsIndex[$nomVernaculaire];
119
		return $indexCourrant;
129
		return $indexCourrant;
120
	}
130
	}
121
 
131
 
122
	private function ouvrirFichier($chemin) {
132
	private function ouvrirFichier($chemin) {
123
		$fichierOuvert = false;
133
		$fichierOuvert = false;
124
		if ($chemin) {
134
		if ($chemin) {
125
			if (file_exists($chemin) === true) {
135
			if (file_exists($chemin) === true) {
126
				$fichierOuvert = fopen($chemin, 'r');
136
				$fichierOuvert = fopen($chemin, 'r');
127
				if ($fichierOuvert == false) {
137
				if ($fichierOuvert == false) {
128
					throw new Exception("Le fichier $chemin n'a pas pu être ouvert.");
138
					throw new Exception("Le fichier $chemin n'a pas pu être ouvert.");
129
				}
139
				}
130
			} else {
140
			} else {
131
				throw new Exception("Le fichier $chemin est introuvable.");
141
				throw new Exception("Le fichier $chemin est introuvable.");
132
			}
142
			}
133
		} else {
143
		} else {
134
			throw new Exception("Aucun chemin de fichier n'a été fourni.");
144
			throw new Exception("Aucun chemin de fichier n'a été fourni.");
135
		}
145
		}
136
		return $fichierOuvert;
146
		return $fichierOuvert;
137
	}
147
	}
138
 
148
 
139
	private function protegerValeursDesChamps($champs) {
149
	private function protegerValeursDesChamps($champs) {
140
		$champsProteges = array();
150
		$champsProteges = array();
141
		for ($i = 0; $i < 9; $i++) {
151
		for ($i = 0; $i < 9; $i++) {
142
			$valeur = isset($champs[$i]) ? $champs[$i] : '';
152
			$valeur = isset($champs[$i]) ? $champs[$i] : '';
143
			$champsProteges[] = $this->bdd->proteger($valeur);
153
			$champsProteges[] = $this->bdd->proteger($valeur);
144
		}
154
		}
145
		return $champsProteges;
155
		return $champsProteges;
146
	}
156
	}
147
 
157
 
148
	private function chargerBiblio() {
158
	private function chargerBiblio() {
149
		$cheminsNvjflBiblio = Config::get('chemins.nvjflBiblio');
159
		$cheminsNvjflBiblio = Config::get('chemins.nvjflBiblio');
150
		$tableNvjflBiblio = Config::get('tables.nvjflBiblio');
160
		$tableNvjflBiblio = Config::get('tables.nvjflBiblio');
151
		$requete = "LOAD DATA INFILE '$cheminsNvjflBiblio' ".
161
		$requete = "LOAD DATA INFILE '$cheminsNvjflBiblio' ".
152
			"REPLACE INTO TABLE $tableNvjflBiblio ".
162
			"REPLACE INTO TABLE $tableNvjflBiblio ".
153
			'CHARACTER SET utf8 '.
163
			'CHARACTER SET utf8 '.
154
			'FIELDS '.
164
			'FIELDS '.
155
			"	TERMINATED BY '\t' ".
165
			"	TERMINATED BY '\t' ".
156
			"	ENCLOSED BY '' ".
166
			"	ENCLOSED BY '' ".
157
			"	ESCAPED BY '\\\' ".
167
			"	ESCAPED BY '\\\' ".
158
			'IGNORE 1 LINES';
168
			'IGNORE 1 LINES';
159
		$this->bdd->requeter($requete);
169
		$this->bdd->requeter($requete);
160
	}
170
	}
161
 
171
 
162
	private function chargerBiblioLien() {
172
	private function chargerBiblioLien() {
163
		$cheminNvjflLienBiblio = Config::get('chemins.nvjflLienBiblio');
173
		$cheminNvjflLienBiblio = Config::get('chemins.nvjflLienBiblio');
164
		$tableNvjflLienBiblio = Config::get('tables.nvjflLienBiblio');
174
		$tableNvjflLienBiblio = Config::get('tables.nvjflLienBiblio');
165
		$requete = "LOAD DATA INFILE '$cheminNvjflLienBiblio' ".
175
		$requete = "LOAD DATA INFILE '$cheminNvjflLienBiblio' ".
166
			"REPLACE INTO TABLE $tableNvjflLienBiblio ".
176
			"REPLACE INTO TABLE $tableNvjflLienBiblio ".
167
			'CHARACTER SET utf8 '.
177
			'CHARACTER SET utf8 '.
168
			'FIELDS '.
178
			'FIELDS '.
169
			"	TERMINATED BY '\t' ".
179
			"	TERMINATED BY '\t' ".
170
			"	ENCLOSED BY '' ".
180
			"	ENCLOSED BY '' ".
171
			"	ESCAPED BY '\\\' ".
181
			"	ESCAPED BY '\\\' ".
172
			'IGNORE 1 LINES';
182
			'IGNORE 1 LINES';
173
		$this->bdd->requeter($requete);
183
		$this->bdd->requeter($requete);
174
	}
184
	}
175
 
185
 
176
	private function chargerOntologies() {
186
	private function chargerOntologies() {
177
		$cheminOntologies = Config::get('chemins.ontologies');
187
		$cheminOntologies = Config::get('chemins.ontologies');
178
		$tableOntologies = Config::get('tables.ontologies');
188
		$tableOntologies = Config::get('tables.ontologies');
179
		$requete = "LOAD DATA INFILE '$cheminOntologies' ".
189
		$requete = "LOAD DATA INFILE '$cheminOntologies' ".
180
				"REPLACE INTO TABLE $tableOntologies ".
190
				"REPLACE INTO TABLE $tableOntologies ".
181
				'CHARACTER SET utf8 '.
191
				'CHARACTER SET utf8 '.
182
				'FIELDS '.
192
				'FIELDS '.
183
				"	TERMINATED BY '\t' ".
193
				"	TERMINATED BY '\t' ".
184
				"	ENCLOSED BY '' ".
194
				"	ENCLOSED BY '' ".
185
				"	ESCAPED BY '\\\' ".
195
				"	ESCAPED BY '\\\' ".
186
				'IGNORE 1 LINES';
196
				'IGNORE 1 LINES';
187
		$this->bdd->requeter($requete);
197
		$this->bdd->requeter($requete);
188
	}
198
	}
189
}
199
}
190
?>
200
?>