Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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