Subversion Repositories eFlore/Projets.eflore-projets

Rev

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