Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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