Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 692 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 692 Rev 1241
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 nvps
3
 * /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php nvps
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 Nvps extends EfloreScript {
8
class Nvps 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, false, '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('nvps');
19
			$this->initialiserProjet('nvps');
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->chargerNvps();
25
					$this->chargerNvps();
26
					break;
26
					break;
27
				case 'chargerStructure' :
27
				case 'chargerStructure' :
28
					$this->chargerStructureSql();
28
					$this->chargerStructureSql();
29
					break;
29
					break;
30
				case 'chargerNvps' :
30
				case 'chargerNvps' :
31
					$this->chargerNvps();
31
					$this->chargerNvps();
32
					break;
32
					break;
-
 
33
				case 'ajouterNumTax' :
-
 
34
					$this->ajouterNumTaxon();
-
 
35
					break;
33
				case 'supprimerTous' :
36
				case 'supprimerTous' :
34
					$this->supprimerTous();
37
					$this->supprimerTous();
35
					break;
38
					break;
36
				default :
39
				default :
37
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
40
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
38
			}
41
			}
39
		} catch (Exception $e) {
42
		} catch (Exception $e) {
40
			$this->traiterErreur($e->getMessage());
43
			$this->traiterErreur($e->getMessage());
41
		}
44
		}
42
	}
45
	}
43
 
46
 
44
	/**
47
	/**
45
	* Charge le fichier en créant un id pour chaque nom vernaculaire.
48
	* Charge le fichier en créant un id pour chaque nom vernaculaire.
46
	*/
49
	*/
47
	private function chargerNvps() {
50
	private function chargerNvps() {
48
		//Debug::printr(Config::get('fichiers'));
51
		//Debug::printr(Config::get('fichiers'));
49
		$fichierOuvert = $this->ouvrirFichier(Config::get('chemins.nvps'));
52
		$fichierOuvert = $this->ouvrirFichier(Config::get('chemins.nvps'));
50
		$donnees = $this->analyserFichier($fichierOuvert);
53
		$donnees = $this->analyserFichier($fichierOuvert);
51
		fclose($fichierOuvert);
54
		fclose($fichierOuvert);
52
		foreach ($donnees as $donnee) {
55
		foreach ($donnees as $donnee) {
53
			$table = Config::get('tables.nvps');
56
			$table = Config::get('tables.nvps');
54
			$fields = implode(', ', array_keys($donnee));
57
			$fields = implode(', ', array_keys($donnee));
55
			$values = implode(', ', $donnee);
58
			$values = implode(', ', $donnee);
56
 
59
 
57
			$requete = "INSERT INTO $table ($fields) VALUES ($values) ";
60
			$requete = "INSERT INTO $table ($fields) VALUES ($values) ";
58
			$this->getBdd()->requeter($requete);
61
			$this->getBdd()->requeter($requete);
59
 
62
 
60
			$this->afficherAvancement("Insertion des noms vernaculaires dans la base de données");
63
			$this->afficherAvancement("Insertion des noms vernaculaires dans la base de données");
61
			if ($this->stopperLaBoucle($this->getParametre('t'))) {
64
			if ($this->stopperLaBoucle($this->getParametre('t'))) {
62
				break;
65
				break;
63
			}
66
			}
64
		}
67
		}
-
 
68
		$requete = "ALTER TABLE `nvps_v2017` ADD `num_taxon` INT(10) NOT NULL AFTER `notes`;";
-
 
69
		
65
		echo "\n";
70
		echo "\n";
66
	}
71
	}
-
 
72
	
-
 
73
	/**
-
 
74
	 * Ajoute le num_taxon
-
 
75
	 */
-
 
76
	private function ajouterNumTaxon() {
-
 
77
	
-
 
78
		$table = Config::get('tables.nvps');
-
 
79
		$referentiel = Config::get('tables.bdtfx');
-
 
80
		
-
 
81
		$requete_struct = "ALTER TABLE ".$table." ADD `num_taxon` INT(10) NOT NULL AFTER `notes`;";
-
 
82
		$this->getBdd()->requeter($requete_struct);
-
 
83
		$requete ="UPDATE ".$table." n left join ".$referentiel." on ".$referentiel.".num_nom = n.num_nom  SET `num_taxon`= num_taxonomique";
-
 
84
		$this->getBdd()->requeter($requete);
-
 
85
	}
67
 
86
 
68
	private function ouvrirFichier($chemin) {
87
	private function ouvrirFichier($chemin) {
69
		$fichierOuvert = false;
88
		$fichierOuvert = false;
70
		if ($chemin) {
89
		if ($chemin) {
71
			if (file_exists($chemin) === true) {
90
			if (file_exists($chemin) === true) {
72
				$fichierOuvert = fopen($chemin, 'r');
91
				$fichierOuvert = fopen($chemin, 'r');
73
				if ($fichierOuvert == false) {
92
				if ($fichierOuvert == false) {
74
					throw new Exception("Le fichier $chemin n'a pas pu être ouvert.");
93
					throw new Exception("Le fichier $chemin n'a pas pu être ouvert.");
75
				}
94
				}
76
			} else {
95
			} else {
77
				throw new Exception("Le fichier $chemin est introuvable.");
96
				throw new Exception("Le fichier $chemin est introuvable.");
78
			}
97
			}
79
		} else {
98
		} else {
80
			throw new Exception("Aucun chemin de fichier n'a été fourni.");
99
			throw new Exception("Aucun chemin de fichier n'a été fourni.");
81
		}
100
		}
82
		return $fichierOuvert;
101
		return $fichierOuvert;
83
	}
102
	}
84
 
103
 
85
	private function analyserFichier($fichierOuvert) {
104
	private function analyserFichier($fichierOuvert) {
86
		$entetesCsv = explode("\t", trim(fgets($fichierOuvert)));
105
		$entetesCsv = explode("\t", trim(fgets($fichierOuvert)));
87
 
106
 
-
 
107
		$donnees = array(); 
88
		$donnees = array();
108
		$i = 0;
89
		while ($ligneCsv = fgets($fichierOuvert)) {
109
		while ($ligneCsv = fgets($fichierOuvert)) {
90
			$champs = explode("\t", trim($ligneCsv));
110
			$champs = explode("\t", trim($ligneCsv));
91
			if (count($champs) > 0) {
111
			if (count($champs) > 0) {
92
				$infos = array();
112
				$infos = array();
93
				foreach ($entetesCsv as $ordre => $champNom) {
113
				foreach ($entetesCsv as $ordre => $champNom) {
94
					$valeur = isset($champs[$ordre]) ? $champs[$ordre] : '';
114
					$valeur = isset($champs[$ordre]) ? $champs[$ordre] : '';
95
					$infos[$champNom] = $valeur;
115
					$infos[$champNom] = $this->getBdd()->proteger($valeur);
96
				}
116
				}
97
				$infos['id'] = $this->getIndexNomVernaculaire($infos['nom_vernaculaire']);
117
				$infos['id'] = $this->getIndexNomVernaculaire($infos['nom_vernaculaire']);
98
				$donnees[] = $this->getBdd()->protegerTableau($infos);
-
 
99
			}
118
			}
100
			$this->afficherAvancement("Analyse du fichier des noms vernaculaires");
119
			$this->afficherAvancement("Analyse du fichier des noms vernaculaires");
-
 
120
			$donnees[$i] = $infos; $i++;
101
			if ($this->stopperLaBoucle()) {
121
			if ($this->stopperLaBoucle()) {
102
				break;
122
				break;
103
			}
123
			}
104
		}
124
		}
105
		echo "\n";
125
		echo "\n";
106
 
-
 
107
		return $donnees;
126
		return $donnees;
108
	}
127
	}
-
 
128
	
109
 
129
 
110
	private function getIndexNomVernaculaire($nomVernaculaire) {
130
	private function getIndexNomVernaculaire($nomVernaculaire) {
111
		$indexCourrant = null;
131
		$indexCourrant = null;
112
		if (array_key_exists($nomVernaculaire, $this->nomsIndex) == false) {
132
		if (array_key_exists($nomVernaculaire, $this->nomsIndex) == false) {
113
			$this->nomsIndex[$nomVernaculaire] = $this->numeroIndex++;
133
			$this->nomsIndex[$nomVernaculaire] = $this->numeroIndex++;
114
		}
134
		}
115
		$indexCourrant = $this->nomsIndex[$nomVernaculaire];
135
		$indexCourrant = $this->nomsIndex[$nomVernaculaire];
116
		return $indexCourrant;
136
		return $indexCourrant;
117
	}
137
	}
118
 
138
 
119
	private function supprimerTous() {
139
	private function supprimerTous() {
120
		$requete = "DROP TABLE IF EXISTS nvps_meta, nvps_v2007, nvps_v2012";
140
		$requete = "DROP TABLE IF EXISTS nvps_meta, nvps_v2007, nvps_v2012";
121
		$this->getBdd()->requeter($requete);
141
		$this->getBdd()->requeter($requete);
122
	}
142
	}
123
}
143
}
124
?>
144
?>