Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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