Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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