Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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