Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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