Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 28 | Rev 53 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 jpm 1
<?php
2
/** Exemple lancement:
22 jpm 3
 * /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php nvjfl
28 jpm 4
 * 		-a chargerTous
11 jpm 5
 * Options :
28 jpm 6
 * -t : Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).
11 jpm 7
*/
8
class Nvjfl extends Script {
9
 
22 jpm 10
	private $bdd = null;
44 jpm 11
	private $projetNom = 'nvjfl';
22 jpm 12
	private $nomsIndex = array();
13
	private $numeroIndex = 1;
14
 
11 jpm 15
	protected $parametres_autorises = array(
24 jpm 16
		'-t' => array(false, true, 'Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).'));
11 jpm 17
 
18
	public function executer() {
19
		// Lancement de l'action demandée
22 jpm 20
		try {
44 jpm 21
			$this->chargerConfigDuProjet();
24 jpm 22
			$this->bdd = new Bdd();
23
 
22 jpm 24
			$cmd = $this->getParametre('a');
25
		    switch ($cmd) {
24 jpm 26
		    	case 'chargerTous' :
44 jpm 27
		    		$this->chargerStructureSql();
24 jpm 28
		    		$this->chargerNvjfl();
29
		    		$this->chargerBiblio();
30
		    		$this->chargerBiblioLien();
27 jpm 31
		    		$this->chargerOntologies();
24 jpm 32
		    		break;
33
				case 'chargerNvjfl' :
34
					$this->chargerNvjfl();
22 jpm 35
					break;
24 jpm 36
				case 'chargerBiblio' :
37
					$this->chargerBiblio();
38
					break;
39
				case 'chargerBiblioLien' :
40
					$this->chargerBiblioLien();
41
					break;
27 jpm 42
				case 'chargerOntologies' :
43
					$this->chargerOntologies();
44
					break;
22 jpm 45
				default :
46
					$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
47
			}
48
		} catch (Exception $e) {
49
			$this->traiterErreur($e->getMessage());
11 jpm 50
		}
51
    }
24 jpm 52
 
44 jpm 53
    private function chargerConfigDuProjet() {
54
    	$fichierIni = dirname(__FILE__).DS.$this->projetNom.'.ini';
24 jpm 55
    	if (file_exists($fichierIni)) {
56
    		Config::charger($fichierIni);
57
    	} else {
44 jpm 58
    		$m = "Veuillez configurer le projet en créant le fichier '{$this->projetNom}.ini' ".
59
    			"dans le dossier du module de script du projet à partir du fichier '{$this->projetNom}.defaut.ini'.";
24 jpm 60
    		throw new Exception($m);
61
    	}
62
    }
63
 
44 jpm 64
    private function chargerStructureSql() {
65
    	$chemin = Config::get('chemins.structureSql');
66
    	$requetes = Outils::extraireRequetes($chemin);
67
    	foreach ($requetes as $requete) {
68
    		$this->bdd->requeter($requete);
69
    	}
70
    }
71
 
22 jpm 72
    /**
24 jpm 73
     * Charge le fichier en créant un id pour chaque nom vernaculaire.
22 jpm 74
     */
24 jpm 75
	private function chargerNvjfl() {
76
		//Debug::printr(Config::get('fichiers'));
77
		$fichierOuvert = $this->ouvrirFichier(Config::get('chemins.nvjfl'));
22 jpm 78
		$donnees = $this->analyserFichier($fichierOuvert);
79
		fclose($fichierOuvert);
80
		foreach ($donnees as $donnee) {
24 jpm 81
			$requete = 'INSERT INTO '.Config::get('tables.nvjfl').' VALUES ('.implode(', ', $donnee).')';
22 jpm 82
			$this->bdd->requeter($requete);
11 jpm 83
 
22 jpm 84
			$this->afficherAvancement("Insertion des noms vernaculaires dans la base de données");
85
			if ($this->stopperLaBoucle()) {
86
				break;
87
			}
88
		}
89
		echo "\n";
90
	}
91
 
92
	private function stopperLaBoucle() {
93
		$stop = false;
94
		static $ligneActuelle = 1;
24 jpm 95
		if ($nbreLignesATester = $this->getParametre('t')) {
22 jpm 96
			if ($nbreLignesATester == $ligneActuelle++) {
97
				$stop = true;
98
			}
99
		}
100
		return $stop;
101
	}
102
 
103
	private function analyserFichier($fichierOuvert) {
104
		$donnees = array();
105
		$entetesCsv = fgets($fichierOuvert);
106
		while ($ligneCsv = fgets($fichierOuvert)) {
107
			$champs = explode("\t", trim($ligneCsv));
24 jpm 108
			if (count($champs) > 0) {
109
				$nomVernaculaire = $champs[2];
110
				$indexCourrant = $this->getIndexNomVernaculaire($nomVernaculaire);
111
				$champs = array_merge(array($indexCourrant), $champs);
112
				$donnees[] = $this->protegerValeursDesChamps($champs);
113
			}
22 jpm 114
			$this->afficherAvancement("Analyse du fichier des noms vernaculaires");
115
			if ($this->stopperLaBoucle()) {
116
				break;
117
			}
118
		}
119
		echo "\n";
120
		return $donnees;
121
	}
122
 
123
	private function getIndexNomVernaculaire($nomVernaculaire) {
124
		$indexCourrant = null;
125
		if (array_key_exists($nomVernaculaire, $this->nomsIndex) == false) {
126
			$this->nomsIndex[$nomVernaculaire] = $this->numeroIndex++;
127
		}
128
		$indexCourrant = $this->nomsIndex[$nomVernaculaire];
129
		return $indexCourrant;
130
	}
131
 
132
	private function ouvrirFichier($chemin) {
133
		$fichierOuvert = false;
134
		if ($chemin) {
135
			if (file_exists($chemin) === true) {
136
				$fichierOuvert = fopen($chemin, 'r');
137
				if ($fichierOuvert == false) {
138
					throw new Exception("Le fichier $chemin n'a pas pu être ouvert.");
11 jpm 139
				}
140
			} else {
22 jpm 141
				throw new Exception("Le fichier $chemin est introuvable.");
11 jpm 142
			}
22 jpm 143
		} else {
144
			throw new Exception("Aucun chemin de fichier n'a été fourni.");
11 jpm 145
		}
22 jpm 146
		return $fichierOuvert;
11 jpm 147
	}
22 jpm 148
 
149
	private function protegerValeursDesChamps($champs) {
150
		$champsProteges = array();
151
		for ($i = 0; $i < 9; $i++) {
152
			$valeur = isset($champs[$i]) ? $champs[$i] : '';
153
			$champsProteges[] = $this->bdd->proteger($valeur);
154
		}
155
		return $champsProteges;
156
	}
24 jpm 157
 
158
	private function chargerBiblio() {
159
		$cheminsNvjflBiblio = Config::get('chemins.nvjflBiblio');
160
		$tableNvjflBiblio = Config::get('tables.nvjflBiblio');
161
		$requete = "LOAD DATA INFILE '$cheminsNvjflBiblio' ".
162
			"REPLACE INTO TABLE $tableNvjflBiblio ".
163
			'CHARACTER SET utf8 '.
164
			'FIELDS '.
165
			"	TERMINATED BY '\t' ".
166
			"	ENCLOSED BY '' ".
167
			"	ESCAPED BY '\\\' ".
168
			'IGNORE 1 LINES';
169
		$this->bdd->requeter($requete);
170
	}
171
 
172
	private function chargerBiblioLien() {
173
		$cheminNvjflLienBiblio = Config::get('chemins.nvjflLienBiblio');
174
		$tableNvjflLienBiblio = Config::get('tables.nvjflLienBiblio');
175
		$requete = "LOAD DATA INFILE '$cheminNvjflLienBiblio' ".
176
			"REPLACE INTO TABLE $tableNvjflLienBiblio ".
177
			'CHARACTER SET utf8 '.
178
			'FIELDS '.
179
			"	TERMINATED BY '\t' ".
180
			"	ENCLOSED BY '' ".
181
			"	ESCAPED BY '\\\' ".
182
			'IGNORE 1 LINES';
183
		$this->bdd->requeter($requete);
184
	}
27 jpm 185
 
186
	private function chargerOntologies() {
187
		$cheminOntologies = Config::get('chemins.ontologies');
188
		$tableOntologies = Config::get('tables.ontologies');
189
		$requete = "LOAD DATA INFILE '$cheminOntologies' ".
190
				"REPLACE INTO TABLE $tableOntologies ".
191
				'CHARACTER SET utf8 '.
192
				'FIELDS '.
193
				"	TERMINATED BY '\t' ".
194
				"	ENCLOSED BY '' ".
195
				"	ESCAPED BY '\\\' ".
196
				'IGNORE 1 LINES';
197
		$this->bdd->requeter($requete);
198
	}
11 jpm 199
}
200
?>