Subversion Repositories eFlore/Projets.eflore-projets

Rev

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