Subversion Repositories eFlore/Projets.eflore-projets

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
460 delphine 1
<?php
2
// Encodage : UTF-8
3
// +-------------------------------------------------------------------------------------------------------------------+
4
/**
5
* Traitement des fichiers de la banque de données SOPHY pour insertion
6
*
7
* Description : classe permettant d'insérer les flores nécessaires à l'étude des données issues de SOPHY.
8
* Avant d'utiliser cette classe nettoyer les fichiers pour ne garder que les lignes à insérer.
9
* Utilisation : php script.php insertionFlore -a test
10
*
11
* @category		PHP 5.3
12
* @package		phytosocio
13
//Auteur original :
14
* @author		Delphine CAUQUIL <delphine@tela-botanica.org>
15
* @copyright	Copyright (c) 2009, Tela Botanica (accueil@tela-botanica.org)
16
* @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3
17
* @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2
18
* @version		$Id$
19
*/
20
// +-------------------------------------------------------------------------------------------------------------------+
21
class Insertionflore extends Script {
22
 
23
	protected $dao;
24
	protected $dossier;
25
	protected $flore;
26
	protected $parametres_autorises = array(
27
		'-n' => array(true, true, 'Nom du fichier ou du dossier à traiter'));
28
 
29
	public function executer() {
30
		include_once dirname(__FILE__).'/bibliotheque/FloreDao.php';
31
		Config::charger(dirname(__FILE__).'/sophy.ini');
32
		$this->dossier = Config::get('dossierDonneesSophy').'FLORE/';
33
		$this->dao = new FloreDao();
34
		// Récupération de paramètres
35
		// Lancement de l'action demandée
36
		$cmd = $this->getParametre('a');
37
		switch ($cmd) {
38
			case 'florenbi' : // fournier
39
				$this->executerFlorenbi();
40
				break;
41
			case 'bdnff' : // bdnff
42
				$this->executerBdnff();
43
				break;
44
			case 'listepla' : // fournier/bdnff
45
				$this->executerListePla();
46
				break;
47
			case 'floeur' : // flora europea
48
				$this->executerFloeur();
49
				break;
50
			case 'bryo' : // bryophyte
51
				$this->executerBryo();
52
				break;
53
			case 'syntri' : //syntri : numéro complémentaire
54
				$this->executerSyntri();
55
				break;
56
			case 'ciff' : // CIFF CIFF/BDNFF
57
				$this->executerCiff();
58
				break;
59
			case 'donneebb' : // Num_nom num_tax bdnff
60
				$this->executerDonneebb();
61
				break;
62
			case 'codefr' : // CODEFR94
63
				$this->executerCodefr();
64
				break;
65
			default :
66
				$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
67
		}
68
    }
69
 
70
// +-------------------------------------------------------------------------------------------------------------------+
71
// Traitement du fichier flore florenbi qui contient tous codes, numéro et nom fournier
72
// /opt/lampp/bin/php cli.php sophy/insertionflore -a florenbi -n ./../donnees/sophy/2010-12-02/FLORE/FLORENBI
73
	private function executerFlorenbi() {
74
		$nomFichier = $this->dossier.'FLORENBI';
75
		if (file_exists($nomFichier) === true) {
76
		    if ( $fichierOuvert = fopen($nomFichier, 'r') ) {
77
		    	$nom = '';
78
		    	while ($ligne = fgets($fichierOuvert)) {
79
					if (preg_match('/^\s{2}[\d\s]{4}\s([\d\s]{3}\d)\s{2}([\d\s]\d\.\d\d\.\d)\s[\d\s]{5}[A-Z\s]\s(\d)\s*(.+)$/', $ligne, $champs)) {
80
						// si le rang taxonomique est inf à 4 (subsp et var)
81
						if ($champs[3] < 4) {
82
							$flore[$champs[1]] = $champs[4];
83
							$nom = trim($champs[4]);
84
						} else {
85
							$flore[$champs[1]] = $nom." ".$champs[4];
86
						}
87
					}
88
		    	}
89
		    	$info = $this->dao->integrerFlore($flore, 'fournier');
90
    			$this->traiterErreur($info);
91
		    }
92
		    fclose($fichierOuvert);
93
		} else {
94
    		$this->traiterErreur("Le fichier {$fichier} n'existe pas.");
95
    	}
96
	}
97
 
98
// +-------------------------------------------------------------------------------------------------------------------+
99
// Traitement du fichier flore florenbi qui contient tous codes, numéro et nom fournier
100
// /opt/lampp/bin/php cli.php sophy/insertionflore -a florenbi -n ./../donnees/sophy/2010-12-02/FLORE/FLORENBI
101
	private function executerBdnff() {
102
		$nomFichier = $this->dossier.'bdnffv5.csv';
103
		if (file_exists($nomFichier) === true) {
104
			$this->dao->chargerDonnees($nomFichier, 'sophy_bdnff');
105
		} else {
106
    		$this->traiterErreur("Le fichier {$nomFichier} n'existe pas.");
107
    	}
108
	}
109
// +-------------------------------------------------------------------------------------------------------------------+
110
// Traitement du fichier flore listePla qui contient numéro de fournier, nom de fournier, numéro bdnff, nom bdnff
111
// /opt/lampp/bin/php -d memory_limit=2048M cli.php insertionFlore -a listepla -n ./../doc/jeux_test/FLORE/LISTEPLA.csv
112
	private function executerListePla() {
113
		// Parcours le fichier .csv et enregistre chaque ligne dans un tableau.
114
		$nomFichier = $this->dossier.'LISTEPLA.csv';
115
    	if ($nomFichier && file_exists($nomFichier) ){
116
    		$extensionFichier = strtolower(strrchr($nomFichier, '.'));
117
    		if ($extensionFichier === ".csv"){
118
    			$file = new SplFileObject($nomFichier);
119
				$file->setFlags(SplFileObject::SKIP_EMPTY);
120
				$i = 0;
121
				echo "Traitement de LISTEPLA : ";
122
				while (!$file->eof()){
123
					$flore = $this->transformerFournierBdnff($file->fgetcsv());
124
				    echo str_repeat(chr(8), ( strlen( $i ) + 1 ))."\t".$i++;
125
				}
126
				echo "\n";
127
				//$info = $this->dao->integrerFlore($this->flore['bdnff'], 'bdnff');
128
    			$info .= $this->dao->integrerFlore($this->flore['correspondance'], 'fournier_bdnff');
129
    			$this->traiterErreur($info);
130
    		} else {
131
    			$this->traiterErreur("Le fichier $nomFichier n'est pas au format csv.");
132
    		}
133
    	} else {
134
    		$this->traiterErreur("Le fichier $nomFichier n'existe pas.");
135
    	}
136
    }
137
 
138
	private function transformerFournierBdnff($ligne_csv){
139
    	//$this->flore['bdnff'][$ligne_csv[5]] = $ligne_csv[6];
140
    	$this->flore['correspondance'][$ligne_csv[3]] = $ligne_csv[5];
141
    }
142
// +-------------------------------------------------------------------------------------------------------------------+
143
// Traitement du fichier flore floeur.bis qui contient numéro et nom de flora europea
144
// /opt/lampp/bin/php -d memory_limit=2048M cli.php insertionFlore -a floeur -n ./../doc/jeux_test/FLORE/FLOEUR.BIS
145
	private function executerFloeur() {
146
		$nomFichier = $this->dossier.'FLOEUR.BIS';
147
		if (file_exists($nomFichier) === true) {
148
		    if ( $fichierOuvert = fopen($nomFichier, 'r') ) {
149
		    	$i = 0;
150
		    	while ($ligne = fgets($fichierOuvert)) {
151
					if (preg_match('/^\s[\d\s]{9}[\d\sA-Z]\s([\d\s]{4}\d)\s(.{71})[\s\d]{2}/', $ligne, $champs)) {
152
						$this->flore[$champs[1]] = trim($champs[2]);
153
					}
154
		    	}
155
		    	$info = $this->dao->integrerFlore($this->flore, 'flora_europea');
156
    			$this->traiterErreur($info);
157
		    }
158
		    fclose($fichierOuvert);
159
		} else {
160
    		$this->traiterErreur("Le fichier {$nomFichier} n'existe pas.");
161
    	}
162
	}
163
 
164
// +-------------------------------------------------------------------------------------------------------------------+
165
// Traitement du fichier flore codebry qui contient numéro et nom des bryophytes
166
// /opt/lampp/bin/php -d memory_limit=2048M cli.php insertionFlore -a bryo -n ./../doc/jeux_test/FLORE/codebry.txt
167
	private function executerBryo() {
168
		$nomFichier = $this->dossier.'codebry.txt';
169
		if (file_exists($nomFichier) === true) {
170
		    if ( $fichierOuvert = fopen($nomFichier, 'r') ) {
171
		    	$i = 0;
172
		    	while ($ligne = fgets($fichierOuvert)) {
173
					if (preg_match('/^\s([\d\s]{4})\s[\d\s][\d\s]{4}\s{2}\d\s*(.*)\s[\d\s]{4}/', $ligne, $champs)) {
174
						if ($champs[1] != 0) {
175
							$this->flore[$champs[1]] = trim($champs[2]);
176
						}
177
					}
178
		    	}
179
		    	$info = $this->dao->integrerFlore($this->flore, 'bryophyte');
180
    			$this->traiterErreur($info);
181
		    }
182
		    fclose($fichierOuvert);
183
		} else {
184
    		$this->traiterErreur("Le fichier {$nomFichier} n'existe pas.");
185
    	}
186
	}
187
// +-------------------------------------------------------------------------------------------------------------------+
188
// Traitement du fichier flore Syntri qui contient numéro de fournier, numéro syntri, nom syntri
189
// /opt/lampp/bin/php -d memory_limit=2048M cli.php insertionFlore -a syntri -n ./../doc/jeux_test/FLORE/SYNTRI.TXT
190
	private function executerSyntri() {
191
		$nomFichier = $this->dossier.'SYNTRI.TXT';
192
		if (file_exists($nomFichier) === true) {
193
		    if ( $fichierOuvert = fopen($nomFichier, 'r') ) {
194
		    	$i = 0;
195
		    	while ($ligne = fgets($fichierOuvert)) {
196
					if (preg_match('/^(\d+)\s{2}\d\s([A-Z,\-\'\.()\s]+[A-Z,\-\'\.()])\s+([\d\s]+)$/', trim($ligne), $champs)) {
197
						$syntri = preg_split('/\s+/', $champs[3]);
198
						if (count($syntri) == 3) {
199
							$num = $syntri[1];
200
						} elseif (count($syntri) == 1){
201
							$num = $syntri[0];
202
						}
203
						if (isset($flore['correspondance'][$num])) {
204
							$flore['syntri'][$num] = $flore['syntri'][$num].$champs[2];
205
						} else {
206
							$flore['correspondance'][$num] = $champs[1];
207
							$flore['syntri'][$num] = $champs[2];
208
						}
209
						$i++;
210
					}
211
		    	}
212
		    	$info = $this->dao->integrerFlore($flore['syntri'], 'syntri');
213
		    	$info .= $this->dao->integrerFlore($flore['correspondance'], 'syntri_fournier');
214
    			$this->traiterErreur($info);
215
		    }
216
		    fclose($fichierOuvert);
217
		} else {
218
    		$this->traiterErreur("Le fichier {$nomFichier} n'existe pas.");
219
    	}
220
	}
221
 
222
 
223
// /opt/lampp/bin/php -d memory_limit=2048M cli.php insertionFlore -a ciff -n ./../doc/jeux_test/FLORE/ciff.txt
224
	private function executerCiff() {
225
		$nomFichier = $this->dossier.'ciff.txt';
226
		if (file_exists($nomFichier) === true) {
227
		    if ( $fichierOuvert = fopen($nomFichier, 'r') ) {
228
		    	$i = 0;
229
		    	while ($ligne = fgets($fichierOuvert)) {
230
					if (preg_match('/^(\d*)\t([A-Za-z].*)\t(\d*)\t\d*/', $ligne, $champs)) {
231
						$flore['ciff'][$champs[1]] = $champs[2];
232
						if ($champs[3] != '') {
233
							$flore['correspondance'][$champs[1]] = $champs[3];
234
						}
235
					}
236
		    	}
237
		    	$info = $this->dao->integrerFlore($flore['ciff'], 'ciff');
238
		    	$info .= $this->dao->integrerFlore($flore['correspondance'], 'ciff_bdnff');
239
    			$this->traiterErreur($info);
240
		    }
241
		    fclose($fichierOuvert);
242
		} else {
243
    		$this->traiterErreur("Le fichier {$nomFichier} n'existe pas.");
244
    	}
245
	}
246
// +-------------------------------------------------------------------------------------------------------------------+
247
// Traitement du fichier flore donneebb.tri qui contient le numero tax t numero nomenclatural de la bdnff
248
// num_tax||num_nom||num_nom_retenu||?||nom
249
// /opt/lampp/bin/php -d memory_limit=2048M cli.php insertionFlore -a donneebb -n ./../doc/jeux_test/FLORE/donneebb.tri
250
	private function executerDonneebb() {
251
		$nomFichier = $this->dossier.'donneebb.tri';
252
		if (file_exists($nomFichier) === true) {
253
			if ( $fichierOuvert = fopen($nomFichier, 'r') ) {
254
				while ($ligne = fgets($fichierOuvert)) {
255
					if (preg_match('/^([\d\s]{4}\d)\|([\d\s]{5})\|([\d\s]{5})\|/', $ligne, $champs)) {
256
						if (!isset($this->flore[$champs[1]])) {
257
							$this->flore[$champs[2]]['num_tax'] = $champs[1];
258
							if (trim($champs[3]) != '') {
259
								$this->flore[$champs[2]]['num_nom_retenu'] = $champs[3];
260
							} else {
261
								$this->flore[$champs[2]]['num_nom_retenu'] = $champs[2];
262
							}
263
						}
264
					}
265
				}
266
				$info = $this->dao->ajouterColonnes($this->flore, 'sophy_bdnff');
267
				$this->traiterErreur($info);
268
			}
269
			fclose($fichierOuvert);
270
		} else {
271
			$this->traiterErreur("Le fichier {$nomFichier} n'existe pas.");
272
		}
273
	}
274
// +-------------------------------------------------------------------------------------------------------------------+
275
// Traitement du fichier flore codefr94 qui contient numéro et nom codefr94
276
// /opt/lampp/bin/php -d memory_limit=2048M cli.php insertionFlore -a ciff -n ./../doc/jeux_test/FLORE/CODEFR94
277
	private function executerCodefr() {
278
		$nomFichier = $this->dossier.'CODEFR94';
279
		if (file_exists($nomFichier) === true) {
280
			if ( $fichierOuvert = fopen($nomFichier, 'r') ) {
281
				$i = 0;
282
				while ($ligne = fgets($fichierOuvert)) {
283
					if (preg_match('/^([\d\s]{5})[\d\s]{7}(.*)/', $ligne, $champs)) {
284
						$flore[$champs[1]] = trim($champs[2]);
285
					}
286
				}
287
				$info = $this->dao->integrerFlore($flore, 'codefr94');
288
				$this->traiterErreur($info);
289
			}
290
			fclose($fichierOuvert);
291
		} else {
292
			$this->traiterErreur("Le fichier {$nomFichier} n'existe pas.");
293
		}
294
	}
295
}