Subversion Repositories eFlore/Projets.eflore-projets

Rev

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