Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 838 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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