Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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