Subversion Repositories Sites.obs-saisons.fr

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
207 aurelien 1
<?php
2
 
3
class OdsSpipVersDrupalMigration extends OdsTriple {
4
 
5
	const PREFIXE = 'get';
6
	const BDD_DRUPAL = 'ods_redesign';
7
	const BDD_ANCIEN_ODS = 'ods';
8
	const BDD_NOUVEAU_ODS = 'ods_saisie';
9
 
10
	/**
11
     * Méthode appelée avec une requête de type GET.
12
     *
13
     */
14
    function getElement($param = array()) {
15
 
16
    	$type = $param[0];
17
 
18
        if ($type == '*' || is_numeric($type)) {
19
            $info = $this->getElementParDefaut($param);
20
        } else {
21
            $methode = self::PREFIXE.$type;
22
 
23
            echo $methode;
24
            if (method_exists($this, $methode)) {
25
                array_shift($param);
26
                $info = $this->$methode($param);
27
            } else {
28
                $this->messages[] = "Le type d'information demandé '$type' n'est pas disponible.";
29
            }
30
        }
31
 
32
        // Envoi sur la sortie standard
33
        $this->envoyer($info);
34
    }
35
 
36
    /**
37
     * Méthode appelée pour ajouter un élément.
38
     */
39
    public function createElement($params) {
40
 
41
    	print_r($params);
42
 
43
        $this->envoyer();
44
    }
45
 
46
   	private function getElementParDefaut($param) {
47
 
48
   	}
49
 
50
   	private function getReparationCommunes() {
51
 
52
   		$requete_stations_sans_communes = 'SELECT * FROM ods_saisie.ods_stations WHERE os_ce_commune = "NULL"';
53
    	$stations_sans_communes = $this->executerRequete($requete_stations_sans_communes);
54
 
55
    	$z = 0;
56
 
57
    	$nb_com = count($stations_sans_communes);
58
 
59
    	$ct = 0 ;
60
 
61
    	foreach($stations_sans_communes as $station_sans_commune) {
62
 
63
    		echo $ct.'    ';
64
 
65
    		$nom_station = $station_sans_commune['os_nom'];
66
    		$id_station = $station_sans_commune['os_id_station'];
67
 
68
    		$nom_station_joker = str_replace('-','_',$nom_station);
69
    		$nom_station_joker = str_replace(' ','_',$nom_station);
70
 
71
    		$requete_recherche_nom = 'SELECT oc_code_insee FROM ods_saisie.ods_communes WHERE oc_nom LIKE "'.$nom_station_joker.'"';
72
 
73
    		$recherche_nom = $this->executerRequete($requete_recherche_nom);
74
 
75
    		if(!empty($recherche_nom)) {
76
    			$requete_maj_nom = 'UPDATE ods_stations set os_ce_commune = '.$recherche_nom[0]['oc_code_insee'].' WHERE os_id_station = '.$id_station;
77
 
78
    			echo $requete_maj_nom.'<br />';
79
    			$modif_nom = $this->executerRequeteSimple($requete_maj_nom);
80
    			$z++;
81
    		} else {
82
    			echo "rien trouvé pour la station ".$nom_station.'<br />';
83
    		}
84
 
85
    		$ct++;
86
 
87
    	}
88
 
89
    	echo $z.' stations réparées sur '.$nb_com;
90
 
91
 
92
    	$requete_altitude_communes = 'SELECT * FROM ods_communes_temp';
93
    	$res_alt_communes = $this->executerRequete($requete_altitude_communes);
94
   	}
95
 
96
    private function getMigrationParticipants() {
97
 
98
    	$requete_participants_spip = 'SELECT * FROM '.self::BDD_ANCIEN_ODS.'.PARTICIPANT WHERE PARTICIPANT_ID > 4';
99
 
100
    	$participants_spip = $this->executerRequete($requete_participants_spip);
101
 
102
    	$requete_insertion_participants_drupal = 'INSERT INTO '.self::BDD_DRUPAL.'.drupal_users '.
103
    	'(uid, name, pass, mail, created, access, login, status, timezone, language, init) '.
104
    	'VALUES ';
105
 
106
        foreach($participants_spip as $participant_spip) {
107
 
108
	    	$requete_insertion_participants_drupal .= '('.
109
		    	$this->proteger($participant_spip['PARTICIPANT_ID']).', '.
110
		    	$this->proteger($participant_spip['PARTICIPANT_PSEUDO']).', '.
111
		    	$this->proteger(md5($participant_spip['PARTICIPANT_MOTDEPASSE'])).', '.
112
		    	$this->proteger($participant_spip['PARTICIPANT_EMAIL']).', '.
113
		    	$this->proteger(strtotime($participant_spip['PARTICIPANT_DATE_INSCRIPTION'])).', '.
114
		    	$this->proteger(strtotime($participant_spip['PARTICIPANT_DATE_INSCRIPTION'])).', '.
115
		    	$this->proteger(strtotime($participant_spip['PARTICIPANT_DATE_INSCRIPTION'])).', '.
116
		    	$this->proteger('1').','.
117
		    	$this->proteger('7200').', '.
118
		    	$this->proteger('fr').', '.
119
		    	$this->proteger($participant_spip['PARTICIPANT_EMAIL']).' '.
120
	    	'),';
121
    	}
122
 
123
    	$requete_insertion_participants_drupal = rtrim($requete_insertion_participants_drupal,',');
124
 
125
    	$this->executerRequeteSimple($requete_insertion_participants_drupal);
126
    }
127
 
128
    private function getMigrationStations($liste_evenements) {
129
 
130
    	$this->supprimerDoublonStation();
131
 
132
    	$requete_communes_anciennes_stations = 'SELECT STATION_ID, COMMUNE.COMMUNE_ID, COMMUNE.COMMUNE_NOM, COMMUNE.COMMUNE_CODEPOSTAL '.
133
    										'FROM '.self::BDD_ANCIEN_ODS.'.SEQUENCE '.
134
    										'LEFT JOIN '.self::BDD_ANCIEN_ODS.'.COMMUNE '.
135
    											' ON '.self::BDD_ANCIEN_ODS.'.COMMUNE.COMMUNE_ID = '.self::BDD_ANCIEN_ODS.'.SEQUENCE.COMMUNE_ID '.
136
    										'GROUP BY STATION_ID';
137
 
138
    	$res_communes_anciennes_stations = $this->executerRequete($requete_communes_anciennes_stations);
139
 
140
    	$communes_anciennes_stations = array();
141
 
142
    	foreach($res_communes_anciennes_stations as $com_stat) {
143
 
144
    		$id_station = $com_stat['STATION_ID'];
145
 
146
    		$communes_anciennes_stations[$id_station]['nom_commune'] = $com_stat['COMMUNE_NOM'];
147
    		$communes_anciennes_stations[$id_station]['id_commune'] = $com_stat['COMMUNE_ID'];
148
    		$communes_anciennes_stations[$id_station]['code_postal_commune'] = $com_stat['COMMUNE_CODEPOSTAL'];
149
    	}
150
 
151
    	$requete_selection_station_ancien = 'SELECT * FROM '.self::BDD_ANCIEN_ODS.'.STATION WHERE PARTICIPANT_ID > 5';
152
    	$stations_anciennes = $this->executerRequete($requete_selection_station_ancien);
153
 
154
    	$requete_insertion_station_nouveau = 'INSERT INTO '.self::BDD_NOUVEAU_ODS.'.ods_stations '.
155
    	'(os_id_station, os_ce_participant, os_nom, os_ce_commune, os_latitude, os_longitude, os_altitude, os_ce_environnement, os_commentaire) '.
156
    	'VALUES ';
157
 
158
        foreach($stations_anciennes as $station_ancienne) {
159
 
160
        	$nouvel_id_environnement = $this->getNouvelIdEnvironnement($station_ancienne['STATION_ENVIRONNEMENT_ID']);
161
        	$id_commune = $communes_anciennes_stations[$station_ancienne['STATION_ID']]['id_commune'];
162
        	$nom_commune = $communes_anciennes_stations[$station_ancienne['STATION_ID']]['nom_commune'];
163
        	$cp_commune = $communes_anciennes_stations[$station_ancienne['STATION_ID']]['code_postal_commune'];
164
        	$code_insee_commune = $this->getCodeInseePourNomEtCP($nom_commune,$cp_commune);
165
 
166
        	// Correction des quelques noms de stations vides
167
        	if(trim($station_ancienne['STATION_NOM']) == '') {
168
        		$station_ancienne['STATION_NOM'] = 'station inconnue';
169
        	}
170
 
171
	    	$requete_insertion_station_nouveau .= '('.
172
		    	$this->proteger($station_ancienne['STATION_ID']).', '.
173
		    	$this->proteger($station_ancienne['PARTICIPANT_ID']).', '.
174
		    	$this->proteger($station_ancienne['STATION_NOM']).', '.
175
		    	$this->proteger($code_insee_commune).', '.
176
		    	$this->proteger($station_ancienne['STATION_LATITUDE']).', '.
177
		    	$this->proteger($station_ancienne['STATION_LONGITUDE']).', '.
178
		    	$this->proteger($station_ancienne['STATION_ALTITUDE']).', '.
179
		    	$this->proteger($nouvel_id_environnement).', '.
180
		    	'""'.
181
	    	'),';
182
 
183
    	}
184
 
185
 
186
    	$requete_insertion_station_nouveau = rtrim($requete_insertion_station_nouveau,',');
187
    	$this->executerRequeteSimple($requete_insertion_station_nouveau);
188
    }
189
 
190
    private function getMigrationIndividus() {
191
 
192
    	$anciennes_espece = $this->getAnciennesEspeceGroupeesParNomSci();
193
    	$nouvelles_especes = $this->getEspeceGroupeesParNomSci();
194
 
195
    	$requete_selection_sequence = 'SELECT * FROM '.self::BDD_ANCIEN_ODS.'.SEQUENCE ';
196
    	$sequences = $this->executerRequete($requete_selection_sequence);
197
 
198
    	$requete_insertion_sequence_nouveau = 'INSERT INTO '.self::BDD_NOUVEAU_ODS.'.ods_individus '.
199
    	'(oi_ce_espece, oi_ce_station, oi_nom) '.
200
    	'VALUES ';
201
 
202
    	$compteur_nom = 1;
203
 
204
    	$obs_a_migrer_par_individu = array();
205
 
206
    	foreach($sequences as $sequence) {
207
 
208
    		$nom_sci_espece = $anciennes_espece[$sequence['ESPECE_ID']];
209
    		$id_nouvelle_espece = $nouvelles_especes[$nom_sci_espece];
210
 
211
    		$requete_insertion_sequence_nouveau .= '('.
212
    			$this->proteger($id_nouvelle_espece).', '.
213
		    	$this->proteger($sequence['STATION_ID']).', '.
214
		    	$this->proteger('individu_'.$compteur_nom).' '.
215
		    	'),';
216
 
217
		    $obs_a_migrer_par_individu['individu_'.$compteur_nom]['sequence'] =  $sequence['SEQUENCE_ID'];
218
 
219
		    $compteur_nom++;
220
    	}
221
 
222
    	$requete_insertion_sequence_nouveau = rtrim($requete_insertion_sequence_nouveau,',');
223
    	$this->executerRequeteSimple($requete_insertion_sequence_nouveau);
224
    }
225
 
226
    private function getMigrationObservations() {
227
 
228
    	$anciennes_espece = $this->getAnciennesEspeceGroupeesParNomSci();
229
    	$nouvelles_especes = $this->getEspeceGroupeesParNomSci();
230
 
231
        $requete_selection_mesure = 'SELECT * FROM '.self::BDD_ANCIEN_ODS.'.MESURE ORDER BY MESURE_DATE';
232
    	$mesures = $this->executerRequete($requete_selection_mesure);
233
 
234
    	$requete_selection_sequence = 'SELECT * FROM '.self::BDD_ANCIEN_ODS.'.SEQUENCE';
235
    	$sequences = $this->executerRequete($requete_selection_sequence);
236
 
237
    	$requete_selection_station = 'SELECT * FROM '.self::BDD_ANCIEN_ODS.'.STATION';
238
    	$stations = $this->executerRequete($requete_selection_station);
239
 
240
    	$sequences_en_bordel = array();
241
 
242
    	$mesures_a_debordeliser = array();
243
 
244
    	$stations_a_classer = array();
245
 
246
    	foreach($stations as $station) {
247
    		$stations_a_classer[$station['STATION_ID']] = $station['PARTICIPANT_ID'];
248
    	}
249
 
250
    	foreach($sequences as $sequence) {
251
    		$sequences_en_bordel[$sequence['SEQUENCE_ID']]['station'] = $sequence['STATION_ID'];
252
    		$sequences_en_bordel[$sequence['SEQUENCE_ID']]['espece'] = $sequence['ESPECE_ID'];
253
    	}
254
 
255
    	foreach($mesures as $mesure) {
256
 
257
    		$station_de_mesure =  $sequences_en_bordel[$mesure['SEQUENCE_ID']]['station'];
258
    		$espece_de_mesure = $sequences_en_bordel[$mesure['SEQUENCE_ID']]['espece'];
259
    		$individu_de_mesure =  $mesure['MESURE_INDIVIDU'];
260
    		$evenement = $mesure['EVENEMENT_ID'];
261
    		$date_evenement = $mesure['MESURE_DATE'];
262
    		$id_participant = $stations_a_classer[$sequences_en_bordel[$mesure['SEQUENCE_ID']]['station']];
263
 
264
    		$mesures_a_debordeliser[$id_participant][$station_de_mesure][$espece_de_mesure][$individu_de_mesure][$evenement][] = $date_evenement;
265
    	}
266
 
267
    	$requete_insertion_individus_nouveau = 'INSERT INTO '.self::BDD_NOUVEAU_ODS.'.ods_individus '.
268
    	'(oi_id_individu, oi_ce_espece, oi_ce_station, oi_nom) '.
269
    	'VALUES ';
270
 
271
    	$requete_insertion_observations_nouveau = 'INSERT INTO '.self::BDD_NOUVEAU_ODS.'.ods_observations '.
272
    	'(oo_ce_participant, oo_ordre, oo_ce_individu, oo_ce_evenement,oo_date,oo_commentaire,oo_date_saisie,oo_date_modification) '.
273
    	'VALUES ';
274
 
275
    	$compteur_id_temp = 1;
276
 
277
    	$tableau_ancien_id_individu_nom_individu = array();
278
 
279
    	foreach($mesures_a_debordeliser as $id_participant => $stations_participant) {
280
 
281
    		$compteur_ordre_obs = 1;
282
 
283
		    	foreach($stations_participant as $station => $especes) {
284
 
285
		    		$id_station_individu = $station;
286
 
287
		    		foreach($especes as $id_espece => $individus_espece) {
288
 
289
		    			$nom_sci_espece = $anciennes_espece[$id_espece];
290
		    			$id_nouvelle_espece = $nouvelles_especes[$nom_sci_espece];
291
 
292
		    			foreach($individus_espece as $num_individu => $evenements) {
293
 
294
		    				$nom_individu = 'individu_'.$num_individu;
295
		    				$id_individu_temp = $compteur_id_temp;
296
 
297
				    		foreach($evenements as $id_evenement => $evenement_individu) {
298
 
299
				    			$nouvel_id_evenement = $this->getNouvelIdEvenement($id_evenement);
300
 
301
				    			foreach($evenement_individu as $date_evenement) {
302
 
303
				    			$requete_insertion_observations_nouveau .= '('.$this->proteger($id_participant).', '.
304
						    	$this->proteger($compteur_ordre_obs).', '.
305
						    	$this->proteger($compteur_id_temp).', '.
306
						    	$this->proteger($nouvel_id_evenement).', '.
307
						    	$this->proteger($date_evenement).', '.
308
						    	"'',".
309
						    	$this->proteger($date_evenement).', '.
310
						    	$this->proteger($date_evenement).
311
						    	'),';
312
 
313
						    	$compteur_ordre_obs++;
314
				    		}
315
		    			}
316
 
317
		    			$requete_insertion_individus_nouveau .= '('.
318
		    			$this->proteger($compteur_id).', '.
319
		    			$this->proteger($id_nouvelle_espece).', '.
320
				    	$this->proteger($id_station_individu).', '.
321
				    	$this->proteger($nom_individu).
322
				    	'),';
323
 
324
 
325
				    	$compteur_id_temp++;
326
 
327
		    		}
328
		    	}
329
	    	}
330
    	}
331
 
332
    	$requete_insertion_individus_nouveau = rtrim($requete_insertion_individus_nouveau,',');
333
    	$this->executerRequeteSimple($requete_insertion_individus_nouveau);
334
 
335
    	$requete_insertion_observations_nouveau = rtrim($requete_insertion_observations_nouveau,',');
336
    	$this->executerRequeteSimple($requete_insertion_observations_nouveau);
337
    }
338
 
339
    private function getMigrationCommunes() {
340
 
341
    	$requete_communes = 'SELECT * FROM locations';
342
    	$communes = $this->executerRequete($requete_communes);
343
 
344
    	$requete_altitude_communes = 'SELECT * FROM ods_communes_temp';
345
    	$res_alt_communes = $this->executerRequete($requete_altitude_communes);
346
 
347
    	$altitudes_communes = array();
348
    	foreach($res_alt_communes as $commune_alt) {
349
    		$altitudes_communes[$commune_alt['oc_code_insee']] = $commune_alt['oc_altitude'];
350
    	}
351
 
352
    	$requete_insertion_communes_ods = 'INSERT INTO ods_communes '.
353
    										'(oc_code_insee, oc_nom, oc_secteur, oc_x_utm, oc_y_utm, oc_latitude, oc_longitude, oc_altitude) '.
354
    										'VALUES ';
355
 
356
    	foreach($communes as $commune) {
357
 
358
    		$lat_long = $this->convertirUtmVersLatLong($commune['x_utm'], $commune['y_utm'], $commune['sector']);
359
    		$altitude = $altitudes_communes[$commune['insee_code']];
360
 
361
    		$requete_insertion_communes_ods .= '('.
362
    			$this->proteger($commune['insee_code']).','.
363
    			$this->proteger($commune['name']).','.
364
    			$this->proteger($commune['sector']).','.
365
    			$this->proteger($commune['x_utm']).','.
366
    			$this->proteger($commune['y_utm']).','.
367
    			$this->proteger($lat_long['lat']).','.
368
    			$this->proteger($lat_long['long']).','.
369
    			$this->proteger($altitude).
370
    		'),';
371
    	}
372
 
373
    	$requete_insertion_communes_ods = rtrim($requete_insertion_communes_ods,',');
374
    	$this->executerRequeteSimple($requete_insertion_communes_ods);
375
 
376
    }
377
 
378
    private function convertirUtmVersLatLong($x, $y, $sector) {
379
 
380
    	$lat_long = array();
381
 
382
    	$convertisseur = new gPoint();
383
		$convertisseur->setUTM($x, $y, $sector);
384
		$convertisseur->convertTMtoLL();
385
		$lat_long['lat'] = $convertisseur->Lat();
386
		$lat_long['long'] = $convertisseur->Long();
387
 
388
		return $lat_long;
389
    }
390
 
391
    private function getNouvelIdEnvironnement($ancien_id_environnement) {
392
    	$ids_env = array('1' => '7','2' => '8','3' => '9','4' => '10','6' => '11');
393
    	return $ids_env[$ancien_id_environnement];
394
    }
395
 
396
	private function getNouvelIdEvenement($ancien_id_evenement) {
397
    	$ids_evts = array('1' => '13',
398
    						'2' => '14',
399
    						'3' => '15',
400
    						'4' => '16',
401
    						'5' => '17',
402
    						'6' => '18',
403
    						'7' =>  '19',
404
    						'8' =>  '20');
405
    	return $ids_evts[$ancien_id_evenement];
406
    }
407
 
408
    private function getEspeceGroupeesParNomSci() {
409
 
410
    	$requete_espece = 'SELECT * FROM '.self::BDD_NOUVEAU_ODS.'.ods_especes';
411
 
412
    	$especes = $this->executerRequeteSimple($requete_espece);
413
 
414
    	$especes_ordonnees = array();
415
 
416
    	foreach($especes as $espece) {
417
    		$indice = strtolower(str_replace(' ','_', $espece['oe_nom_scientifique']));
418
    		$especes_ordonnees[$indice] = $espece['oe_id_espece'];
419
    	}
420
 
421
    	return $especes_ordonnees;
422
    }
423
 
424
	private function getAnciennesEspeceGroupeesParNomSci() {
425
 
426
    	$requete_espece = 'SELECT * FROM '.self::BDD_ANCIEN_ODS.'.ESPECE';
427
 
428
    	$especes = $this->executerRequete($requete_espece);
429
 
430
    	$especes_ordonnees = array();
431
 
432
    	foreach($especes as $espece) {
433
    		$nom_sci = strtolower(str_replace(' ','_', $espece['ESPECE_NOM_SCIENTIFIQUE']));
434
    		$especes_ordonnees[$espece['ESPECE_ID']] = $nom_sci;
435
    	}
436
 
437
    	return $especes_ordonnees;
438
    }
439
 
440
    private function getCodeInseePourNomEtCP($nom_commune, $cp) {
441
 
442
    	if(trim($nom_commune) == '') {
443
 
444
    		return 'NULL';
445
    	}
446
 
447
		$limite = 2;
448
 
449
		if(strlen($cp) == 4) {
450
			$limite = 1;
451
		}
452
 
453
		$dpt = substr($cp,0,$limite);
454
 
455
		$requete_code_insee = 'SELECT oc_code_insee '.
456
								'FROM '.self::BDD_NOUVEAU_ODS.'.ods_communes '.
457
								'WHERE oc_nom = '.$this->proteger($nom_commune).' '.
458
								'AND oc_code_insee LIKE "'.$dpt.'___" '.
459
								'LIMIT 1';
460
 
461
		$infos_code_insee_commune = $this->executerRequete($requete_code_insee);
462
 
463
		if(empty($infos_code_insee_commune)) {
464
			// a migrer manuellement
465
			return '_migrer_manu_'.$nom_commune;
466
		}
467
 
468
		return $infos_code_insee_commune[0]['oc_code_insee'];
469
    }
470
 
471
    private function supprimerDoublonStation() {
472
 
473
		$requete = 'SELECT STATION_ID, STATION_NOM, PARTICIPANT_ID FROM '.self::BDD_ANCIEN_ODS.'.STATION ORDER BY PARTICIPANT_ID, STATION_NOM, STATION_ID ';
474
 
475
		$res = $this->executerRequete($requete);
476
		$stations = array();
477
 
478
		foreach($res as $station) {
479
			$stations[$station['PARTICIPANT_ID']][$station['STATION_NOM']][] = $station['STATION_ID'];
480
		}
481
 
482
		foreach($stations as $participant => $stations) {
483
 
484
			foreach($stations as $station_nom => $doublons) {
485
				if(count($doublons) > 1) {
486
 
487
					$id_garde = $doublons[0];
488
 
489
					$ids_a_supprimer = implode(',',array_slice($doublons, 1));
490
 
491
					$requete_fusion_stations = 'DELETE FROM '.self::BDD_ANCIEN_ODS.'.STATION WHERE STATION_ID IN ('.$ids_a_supprimer.') AND PARTICIPANT_ID = '.$participant;
492
					$this->executerRequeteSimple($requete_fusion_stations);
493
 
494
					$requete_fusion_stations_obs = 'UPDATE '.self::BDD_ANCIEN_ODS.'.SEQUENCE
495
							SET STATION_ID = '.$id_garde.' '.
496
							'WHERE STATION_ID IN ('.$ids_a_supprimer.') '.
497
							'AND PARTICIPANT_ID = '.$participant;
498
					$this->executerRequeteSimple($requete_fusion_stations_obs);
499
				}
500
			}
501
		}
502
    }
503
}
504
?>