1149 |
gduche |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
class MigrationObs extends Cel {
|
1184 |
jpm |
4 |
|
1189 |
jpm |
5 |
const truncate = true; //Doit on vider les tables de destination ?
|
|
|
6 |
const dry_run = false;
|
|
|
7 |
const PATTERN_LAT = '/^[+-]?(?:[1-9][0-9]|[0-9])(?:[.][0-9]+|)$/';
|
|
|
8 |
const PATTERN_LNG = '/^[+-]?(?:1[0-8][0-9]|[1-9][0-9]|[0-9])(?:[.][0-9]+|)$/';
|
|
|
9 |
|
1158 |
aurelien |
10 |
public static $bdd_cel_migration;
|
|
|
11 |
public static $bdd_utilisateurs;
|
1189 |
jpm |
12 |
private $communesOubliees = array();
|
|
|
13 |
private $tableau_utilisateurs = array();
|
|
|
14 |
private $tableau_mots_cles = array();
|
|
|
15 |
private $tableau_zones_geo = array();
|
1184 |
jpm |
16 |
|
1149 |
gduche |
17 |
/** Tableau associatif permettant de stocker l'avancement dans une boucle.
|
|
|
18 |
* La clé est un md5 du message à afficher au démarrage de la boucle.
|
|
|
19 |
* @var array
|
|
|
20 |
*/
|
|
|
21 |
private static $avancement = array();
|
1184 |
jpm |
22 |
|
1149 |
gduche |
23 |
private $tableau_nouveau_ancien = array(
|
|
|
24 |
'id_observation' => 'id',
|
|
|
25 |
'ordre' => 'ordre',
|
|
|
26 |
'ce_utilisateur' => 'traiterIdentifiantUtilisateur',
|
|
|
27 |
'prenom_utilisateur' => 'traiterPrenomUtilisateur',
|
|
|
28 |
'nom_utilisateur' => 'traiterNomUtilisateur',
|
|
|
29 |
'courriel_utilisateur' => 'identifiant',
|
|
|
30 |
'nom_sel' => 'nom_sel',
|
|
|
31 |
'nom_sel_nn' => 'num_nom_sel',
|
|
|
32 |
'nom_ret' => 'nom_ret',
|
|
|
33 |
'nom_ret_nn' => 'num_nom_ret',
|
|
|
34 |
'nt' => 'num_taxon',
|
|
|
35 |
'famille' => 'famille',
|
|
|
36 |
'nom_referentiel' => 'traiterReferentiel',
|
|
|
37 |
'ce_zone_geo' => 'traiterIdentifiantZoneGeo',
|
|
|
38 |
'zone_geo' => 'location',
|
|
|
39 |
'lieudit' => 'lieudit',
|
|
|
40 |
'station' => 'station',
|
|
|
41 |
'milieu' => 'milieu',
|
1189 |
jpm |
42 |
'latitude' => 'traiterLat',
|
|
|
43 |
'longitude' => 'traiterLng',
|
1186 |
jpm |
44 |
'geodatum' => 'traiterGeodatum',
|
1149 |
gduche |
45 |
'date_observation' => 'date_observation',
|
|
|
46 |
'mots_cles_texte' => 'traiterMotsClesTexte',
|
|
|
47 |
'commentaire' => 'commentaire',
|
|
|
48 |
'transmission' => 'transmission',
|
|
|
49 |
'date_creation' => 'date_creation',
|
|
|
50 |
'date_modification' => 'date_modification',
|
|
|
51 |
'date_transmission' => 'date_transmission'
|
|
|
52 |
);
|
1184 |
jpm |
53 |
|
1149 |
gduche |
54 |
private $tableau_ancien_nouveau = array(
|
|
|
55 |
'id' => 'id_observation',
|
|
|
56 |
'identifiant' => '',
|
|
|
57 |
'prenom_utilisateur' => 'prenom_utilisateur',
|
|
|
58 |
'nom_utilisateur' => 'nom_utilisateur',
|
|
|
59 |
'ordre' => 'ordre',
|
|
|
60 |
'nom_sel' => 'nom_sel',
|
|
|
61 |
'num_nom_sel' => 'nom_sel_nn',
|
|
|
62 |
'nom_ret' => 'nom_ret',
|
|
|
63 |
'num_nom_ret' => 'nom_ret_nn',
|
|
|
64 |
'num_taxon' => 'nt',
|
|
|
65 |
'famille' => 'famille',
|
|
|
66 |
'location' => '',
|
|
|
67 |
'id_location' => '',
|
|
|
68 |
'date_observation' => 'date_observation',
|
|
|
69 |
'lieu_dit' => 'lieudit',
|
|
|
70 |
'station' => 'station',
|
|
|
71 |
'milieu' => 'milieu',
|
|
|
72 |
'commentaire' => 'commentaire',
|
|
|
73 |
'transmission' => 'transmission',
|
|
|
74 |
'date_creation' => 'date_creation',
|
|
|
75 |
'date_modification' => 'date_modification',
|
|
|
76 |
'date_transmission' => 'date_transmission',
|
|
|
77 |
'mots_cles' => '',
|
|
|
78 |
'coord_x' => 'latitude',
|
|
|
79 |
'coord_y' => 'longitude',
|
|
|
80 |
'ref_geo' => 'geodatum'
|
|
|
81 |
);
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Méthode appelée avec une requête de type GET.
|
|
|
85 |
*/
|
|
|
86 |
public function getElement($params) {
|
1184 |
jpm |
87 |
|
1158 |
aurelien |
88 |
if(!isset($this->config['database_cel']['database_migration']) || $this->config['database_cel']['database_migration'] == '') {
|
|
|
89 |
echo 'Attention la variable de configuration database_migration dans la section database_cel, contenant la base de données d\'arrivée, doit être remplie '."\n";
|
|
|
90 |
exit;
|
|
|
91 |
}
|
1184 |
jpm |
92 |
|
1158 |
aurelien |
93 |
if(!isset($this->config['database_ident']['database']) || $this->config['database_ident']['database'] == '') {
|
|
|
94 |
echo 'Attention la variable de configuration database dans la section database_ident, contenant la base de données utilisateurs, doit être remplie '."\n";
|
|
|
95 |
exit;
|
|
|
96 |
}
|
1184 |
jpm |
97 |
|
1158 |
aurelien |
98 |
self::$bdd_cel_migration = $this->config['database_cel']['database_migration'];
|
|
|
99 |
self::$bdd_utilisateurs = $this->config['database_ident']['database'];
|
1184 |
jpm |
100 |
|
1149 |
gduche |
101 |
echo "--MIGRATION DES OBSERVATIONS --------------------------------------\n";
|
|
|
102 |
//1. TEMPORAIRE : vider les tables de destinations
|
|
|
103 |
if (self::truncate) {
|
|
|
104 |
echo "-------------------------------------------------------------------\n\n";
|
|
|
105 |
echo " ETAPE 0. Vider les tables ... \n\n";
|
|
|
106 |
echo "-------------------------------------------------------------------\n\n";
|
1189 |
jpm |
107 |
$nouvellesTables = array('cel_obs', 'cel_utilisateurs_infos', 'cel_zones_geo');
|
1149 |
gduche |
108 |
foreach ($nouvellesTables as $nomTable) {
|
|
|
109 |
echo 'Vider la table '.$nomTable.'...';
|
1158 |
aurelien |
110 |
$requeteTruncate = 'TRUNCATE TABLE '.self::$bdd_cel_migration.'.'.$nomTable;
|
1149 |
gduche |
111 |
$resultatTruncate = $this->executerRequete($requeteTruncate);
|
|
|
112 |
echo "ok \n";
|
|
|
113 |
}
|
1184 |
jpm |
114 |
|
1149 |
gduche |
115 |
echo "\n---------------------------------------------------------------- OK\n\n";
|
|
|
116 |
}
|
1184 |
jpm |
117 |
|
1149 |
gduche |
118 |
echo "-------------------------------------------------------------------\n\n";
|
|
|
119 |
echo " ETAPE 1. Paramétrage ... \n\n";
|
|
|
120 |
echo "-------------------------------------------------------------------\n\n";
|
|
|
121 |
$this->getUtilisateurs();
|
|
|
122 |
$this->getMotsCles();
|
1184 |
jpm |
123 |
|
1149 |
gduche |
124 |
echo "-------------------------------------------------------------------\n\n";
|
|
|
125 |
echo " ETAPE 2. Migration des utilisateurs ... \n\n";
|
|
|
126 |
echo "-------------------------------------------------------------------\n\n";
|
|
|
127 |
$this->migrerUtilisateurs();
|
1184 |
jpm |
128 |
|
1149 |
gduche |
129 |
echo "-------------------------------------------------------------------\n\n";
|
|
|
130 |
echo " ETAPE 3. Migration des zone géographiques ... \n\n";
|
|
|
131 |
echo "-------------------------------------------------------------------\n\n";
|
|
|
132 |
$this->migrerZonesGeo();
|
1184 |
jpm |
133 |
|
1149 |
gduche |
134 |
echo "-------------------------------------------------------------------\n\n";
|
|
|
135 |
echo " ETAPE 4. Migration des observations ... \n\n";
|
|
|
136 |
echo "-------------------------------------------------------------------\n\n";
|
|
|
137 |
$this->migrerObs();
|
1184 |
jpm |
138 |
$this->ordonnerObs();
|
1149 |
gduche |
139 |
echo "\n"."\n"."\n";
|
|
|
140 |
}
|
1184 |
jpm |
141 |
|
1149 |
gduche |
142 |
public function executerRequeteSimple($requete) {
|
|
|
143 |
// Fonction de commodité pour afficher les requetes au lieu de les executer
|
|
|
144 |
if (self::dry_run) {
|
1184 |
jpm |
145 |
echo str_replace('),','),'."\n", $requete);
|
1149 |
gduche |
146 |
return true;
|
|
|
147 |
} else {
|
|
|
148 |
return parent::executerRequeteSimple($requete);
|
|
|
149 |
}
|
|
|
150 |
}
|
1184 |
jpm |
151 |
|
1149 |
gduche |
152 |
private function getUtilisateurs() {
|
|
|
153 |
echo "\n-------------------------------------------------------------------\n";
|
|
|
154 |
echo "--SELECTION DES UTILISATEURS---------------------------------------\n\n";
|
1184 |
jpm |
155 |
|
1189 |
jpm |
156 |
$requete = 'SELECT DISTINCT u_id AS id, u_mail AS mail, u_name AS nom, u_surname AS prenom, u_passwd AS pass '.
|
|
|
157 |
'FROM cel_inventory INNER JOIN '.self::$bdd_utilisateurs.'.annuaire_tela ON (u_mail = identifiant) ';
|
1184 |
jpm |
158 |
$tableau_utilisateurs = $this->executerRequete($requete);
|
|
|
159 |
|
1149 |
gduche |
160 |
foreach( $tableau_utilisateurs as &$utilisateur) {
|
|
|
161 |
$this->tableau_utilisateurs[$utilisateur['mail']] = $utilisateur;
|
|
|
162 |
}
|
1184 |
jpm |
163 |
|
1149 |
gduche |
164 |
echo sizeof($this->tableau_utilisateurs)." utilisateurs sélectionnés";
|
|
|
165 |
echo "\n-----------------------------------------------------------------OK\n";
|
|
|
166 |
}
|
1184 |
jpm |
167 |
|
1149 |
gduche |
168 |
private function getMotsCles() {
|
|
|
169 |
echo "\n-------------------------------------------------------------------\n";
|
|
|
170 |
echo "--SELECTION DES MOTS-CLES -----------------------------------------\n\n";
|
|
|
171 |
|
1184 |
jpm |
172 |
$requete = 'SELECT cmc_id_proprietaire as id_utilisateur, cmc_id_mot_cle_utilisateur as id_mot_cle, '.
|
|
|
173 |
'cmc_mot_cle as mot_cle '.
|
|
|
174 |
'FROM cel_mots_cles_obs ';
|
|
|
175 |
$tableau_mots_cles = $this->executerRequete($requete);
|
|
|
176 |
|
1149 |
gduche |
177 |
foreach( $tableau_mots_cles as &$mot_cle) {
|
|
|
178 |
$this->tableau_mots_cles[$mot_cle['id_utilisateur']][$mot_cle['id_mot_cle']] = $mot_cle;
|
|
|
179 |
}
|
1184 |
jpm |
180 |
|
1149 |
gduche |
181 |
echo sizeof($this->tableau_mots_cles)." mots-clés sélectionnés";
|
|
|
182 |
echo "\n-----------------------------------------------------------------OK\n";
|
1184 |
jpm |
183 |
|
1149 |
gduche |
184 |
}
|
1184 |
jpm |
185 |
|
|
|
186 |
/**
|
1149 |
gduche |
187 |
* Utiliser cette méthode dans une boucle pour afficher un message suivi du nombre de tour de boucle effectué.
|
1184 |
jpm |
188 |
* Vous devrez vous même gérer le retour à la ligne à la sortie de la boucle.
|
|
|
189 |
*
|
1149 |
gduche |
190 |
* @param string le message d'information.
|
|
|
191 |
* @param int le nombre de départ à afficher.
|
|
|
192 |
* @return void le message est affiché dans la console.
|
|
|
193 |
*/
|
|
|
194 |
protected function afficherAvancement($message, $depart = 0) {
|
|
|
195 |
if (! isset(self::$avancement[$message])) {
|
|
|
196 |
self::$avancement[$message] = $depart;
|
|
|
197 |
echo "$message : ";
|
1184 |
jpm |
198 |
|
1149 |
gduche |
199 |
$actuel =& self::$avancement[$message];
|
|
|
200 |
echo $actuel++;
|
|
|
201 |
} else {
|
|
|
202 |
$actuel =& self::$avancement[$message];
|
1184 |
jpm |
203 |
|
1149 |
gduche |
204 |
// Cas du passage de 99 (= 2 caractères) à 100 (= 3 caractères)
|
|
|
205 |
$passage = 0;
|
|
|
206 |
if (strlen((string) ($actuel - 1)) < strlen((string) ($actuel))) {
|
1184 |
jpm |
207 |
$passage = 1;
|
1149 |
gduche |
208 |
}
|
1184 |
jpm |
209 |
|
1149 |
gduche |
210 |
echo str_repeat(chr(8), (strlen((string) $actuel) - $passage));
|
|
|
211 |
echo $actuel++;
|
|
|
212 |
}
|
|
|
213 |
}
|
1184 |
jpm |
214 |
|
1149 |
gduche |
215 |
private function migrerUtilisateurs() {
|
1189 |
jpm |
216 |
$requete = 'INSERT INTO '.self::$bdd_cel_migration.'.cel_utilisateurs_infos '.
|
|
|
217 |
'(id_utilisateur) '.
|
|
|
218 |
'VALUES ';
|
1184 |
jpm |
219 |
|
1189 |
jpm |
220 |
$sous_requete = array();
|
|
|
221 |
foreach ($this->tableau_utilisateurs as $id => &$utilisateur) {
|
|
|
222 |
$sous_requete[] = '('.$this->proteger($utilisateur['id']).')';
|
|
|
223 |
}
|
|
|
224 |
$requete .= implode(',', $sous_requete);
|
1184 |
jpm |
225 |
|
1189 |
jpm |
226 |
$migration_utilisateurs = $this->executerRequeteSimple($requete);
|
|
|
227 |
|
|
|
228 |
if ($migration_utilisateurs) {
|
|
|
229 |
echo "Migration utilisateurs : ".count($sous_requete);
|
|
|
230 |
} else {
|
|
|
231 |
exit('Erreur lors de la migration des utilisateurs '."\n");
|
1149 |
gduche |
232 |
}
|
|
|
233 |
echo "\n---------------------------------------------------------------- OK\n\n";
|
1184 |
jpm |
234 |
|
1149 |
gduche |
235 |
}
|
1184 |
jpm |
236 |
|
1149 |
gduche |
237 |
private function migrerZonesGeo() {
|
1184 |
jpm |
238 |
|
1149 |
gduche |
239 |
$pas = 5000;
|
1184 |
jpm |
240 |
|
1149 |
gduche |
241 |
//SELECTIONNER LE NOMBRE DE ZONE GEO
|
|
|
242 |
$requete_nombreZonesGeo = 'SELECT count(*) as nb FROM locations';
|
|
|
243 |
$resultatNbZonesGeo = $this->executerRequete($requete_nombreZonesGeo);
|
|
|
244 |
$nbZones = (int) $resultatNbZonesGeo[0]['nb'];
|
1184 |
jpm |
245 |
|
1149 |
gduche |
246 |
$nbTotal = 0;
|
|
|
247 |
for($i = 0; $i <= $nbZones ; $i += $pas) {
|
1184 |
jpm |
248 |
|
1149 |
gduche |
249 |
$requete_selection_zones_geo = 'SELECT * FROM locations LIMIT '.$i.', '.$pas;
|
1184 |
jpm |
250 |
|
1149 |
gduche |
251 |
$zones_geo = $this->executerRequete($requete_selection_zones_geo);
|
1184 |
jpm |
252 |
|
1158 |
aurelien |
253 |
$requete_insertion_nouvelles_zones_geo = 'INSERT INTO '.self::$bdd_cel_migration.'.cel_zones_geo '.
|
1149 |
gduche |
254 |
'(id_zone_geo, code, nom, utm_secteur, utm_x, utm_y, wgs84_latitude, wgs84_longitude, date_modification) '.
|
|
|
255 |
'VALUES ';
|
1184 |
jpm |
256 |
|
1149 |
gduche |
257 |
$sous_requete_insertion_valeurs = '';
|
1184 |
jpm |
258 |
|
|
|
259 |
if(count($zones_geo) > 0) {
|
1149 |
gduche |
260 |
foreach($zones_geo as $zone_geo) {
|
1184 |
jpm |
261 |
|
1149 |
gduche |
262 |
$zone_geo['nouveau_code_geo'] = 'INSEE-C:'.$zone_geo['insee_code'];
|
|
|
263 |
$lat_long = $this->convertirUtmVersLatLong($zone_geo['x_utm'],$zone_geo['y_utm'],$zone_geo['sector']);
|
1184 |
jpm |
264 |
|
1149 |
gduche |
265 |
$indice_tableau_localites = $this->construireIndiceTableauLocalites($zone_geo['name'], $zone_geo['insee_code']);
|
|
|
266 |
$this->tableau_zones_geo[$indice_tableau_localites] = $zone_geo;
|
1184 |
jpm |
267 |
|
1149 |
gduche |
268 |
$sous_requete_insertion_valeurs .= '('.$this->proteger($zone_geo['nouveau_code_geo']).','.
|
|
|
269 |
$this->proteger($zone_geo['insee_code']).','.
|
|
|
270 |
$this->proteger($zone_geo['name']).','.
|
|
|
271 |
$this->proteger($zone_geo['sector']).','.
|
|
|
272 |
$this->proteger($zone_geo['x_utm']).','.
|
|
|
273 |
$this->proteger($zone_geo['y_utm']).','.
|
|
|
274 |
$this->proteger($lat_long['lat']).','.
|
|
|
275 |
$this->proteger($lat_long['long']).','.
|
|
|
276 |
$this->proteger($zone_geo['update_date']).
|
|
|
277 |
'),';
|
|
|
278 |
}
|
1184 |
jpm |
279 |
|
1149 |
gduche |
280 |
$sous_requete_insertion_valeurs = rtrim($sous_requete_insertion_valeurs,',');
|
1184 |
jpm |
281 |
|
1149 |
gduche |
282 |
$requete_insertion_nouvelles_zones_geo .= $sous_requete_insertion_valeurs;
|
1184 |
jpm |
283 |
|
1149 |
gduche |
284 |
$migration_zones_geo = $this->executerRequeteSimple($requete_insertion_nouvelles_zones_geo);
|
|
|
285 |
} else {
|
|
|
286 |
echo 'Fin de migration des zones géo '."\n";
|
1184 |
jpm |
287 |
return;
|
1149 |
gduche |
288 |
}
|
1184 |
jpm |
289 |
|
1149 |
gduche |
290 |
if ($migration_zones_geo) {
|
1184 |
jpm |
291 |
$nbTotal ++;
|
1149 |
gduche |
292 |
$this->afficherAvancement('Migration des zones (par '.$pas.')', $nbTotal);
|
|
|
293 |
} else {
|
|
|
294 |
exit('Erreur lors de la migration des zones géo '.$i.' à '.($i+$pas)."\n");
|
|
|
295 |
}
|
|
|
296 |
}
|
|
|
297 |
echo "\n---------------------------------------------------------------- OK\n\n";
|
|
|
298 |
}
|
1184 |
jpm |
299 |
|
1149 |
gduche |
300 |
private function convertirUtmVersLatLong($x, $y, $sector) {
|
|
|
301 |
$lat_long = array();
|
1184 |
jpm |
302 |
|
1149 |
gduche |
303 |
$convertisseur = new gPoint();
|
|
|
304 |
$convertisseur->setUTM($x, $y, $sector);
|
|
|
305 |
$convertisseur->convertTMtoLL();
|
|
|
306 |
$lat_long['lat'] = str_replace(',','.',$convertisseur->Lat());
|
|
|
307 |
$lat_long['long'] = str_replace(',','.',$convertisseur->Long());
|
1184 |
jpm |
308 |
|
1149 |
gduche |
309 |
return $lat_long;
|
|
|
310 |
}
|
1184 |
jpm |
311 |
|
1149 |
gduche |
312 |
private function migrerObs() {
|
|
|
313 |
$debut = 0;
|
|
|
314 |
$pas = 1000;
|
|
|
315 |
$nbTotal = 0;
|
1184 |
jpm |
316 |
|
1149 |
gduche |
317 |
//Selectionner le nombre d'observations
|
|
|
318 |
$requeteNbObs = "SELECT COUNT(*) as nb FROM cel_inventory";
|
|
|
319 |
$resultatNbObs = $this->executerRequete($requeteNbObs);
|
|
|
320 |
$fin = $resultatNbObs[0]['nb'];
|
1184 |
jpm |
321 |
|
1149 |
gduche |
322 |
for ($i = $debut; $i < $fin ; $i += $pas) {
|
|
|
323 |
$requete_selection_obs = 'SELECT * FROM cel_inventory '.
|
|
|
324 |
'ORDER BY identifiant LIMIT '.$i.','.$pas;
|
|
|
325 |
$observations = $this->requeter($requete_selection_obs);
|
1184 |
jpm |
326 |
|
1158 |
aurelien |
327 |
$requete_insertion_observations = 'INSERT IGNORE INTO '.self::$bdd_cel_migration.'.cel_obs (';
|
1184 |
jpm |
328 |
|
1149 |
gduche |
329 |
foreach ($this->tableau_nouveau_ancien as $nouveau_champ => $ancien_champ) {
|
|
|
330 |
$requete_insertion_observations .= $nouveau_champ.',';
|
1184 |
jpm |
331 |
}
|
|
|
332 |
|
1149 |
gduche |
333 |
$requete_insertion_observations = rtrim($requete_insertion_observations, ',');
|
|
|
334 |
$requete_insertion_observations = $requete_insertion_observations.') VALUES ';
|
1184 |
jpm |
335 |
|
|
|
336 |
if (count($observations) > 0) {
|
1149 |
gduche |
337 |
foreach($observations as $observation) {
|
|
|
338 |
$nouvelle_observation = $this->traiterLigneObservation($observation);
|
1184 |
jpm |
339 |
|
1189 |
jpm |
340 |
$nouvelle_observation = array_map(array($this, 'protegerSiNonNull'), $nouvelle_observation);
|
|
|
341 |
$requete_insertion_observations .= '('.join(',', array_values($nouvelle_observation)).'),';
|
1149 |
gduche |
342 |
}
|
1184 |
jpm |
343 |
|
1149 |
gduche |
344 |
$requete_insertion_observations = rtrim($requete_insertion_observations, ',');
|
1184 |
jpm |
345 |
|
1149 |
gduche |
346 |
$migration_observations = $this->executerRequeteSimple($requete_insertion_observations);
|
|
|
347 |
} else {
|
|
|
348 |
echo 'Fin de migration des observations '."\n"."\n";
|
1184 |
jpm |
349 |
return;
|
1149 |
gduche |
350 |
}
|
1184 |
jpm |
351 |
|
1149 |
gduche |
352 |
if ($migration_observations) {
|
1184 |
jpm |
353 |
$nbTotal ++;
|
1149 |
gduche |
354 |
$this->afficherAvancement('Migration des observations (par '.$pas.')', $nbTotal);
|
|
|
355 |
} else {
|
|
|
356 |
exit('Erreur lors de la migration des observation de '.$i.' à '.($i+$pas)."\n");
|
|
|
357 |
}
|
|
|
358 |
}
|
|
|
359 |
if (sizeof($this->communesOubliees) > 0) {
|
1158 |
aurelien |
360 |
echo "\nxxxxxxxxx communes oubliées : ".sizeof($this->communesOubliees)." xxxxxxxxx \\n";
|
1149 |
gduche |
361 |
}
|
|
|
362 |
echo "\n---------------------------------------------------------------- OK\n\n";
|
|
|
363 |
}
|
1184 |
jpm |
364 |
|
|
|
365 |
private function ordonnerObs() {
|
1186 |
jpm |
366 |
$requete = 'ALTER TABLE '.self::$bdd_cel_migration.'.cel_obs ORDER BY id_observation';
|
1184 |
jpm |
367 |
$this->executerRequeteSimple($requete);
|
|
|
368 |
}
|
|
|
369 |
|
1149 |
gduche |
370 |
private function traiterLigneObservation($obs) {
|
|
|
371 |
$nouvelle_obs = array();
|
1184 |
jpm |
372 |
foreach($this->tableau_nouveau_ancien as $nouveau_champ_obs => $ancien_champ_obs) {
|
1149 |
gduche |
373 |
if ($this->estUnChampATraiter($ancien_champ_obs)) {
|
1186 |
jpm |
374 |
if (method_exists($this, $ancien_champ_obs)) {
|
1149 |
gduche |
375 |
$nouvelle_obs[$nouveau_champ_obs] = $this->$ancien_champ_obs($obs);
|
|
|
376 |
} else {
|
|
|
377 |
$nouvelle_obs[$nouveau_champ_obs] = '';
|
|
|
378 |
}
|
|
|
379 |
} else {
|
1186 |
jpm |
380 |
if ($obs[$ancien_champ_obs] == '000null' || $obs[$ancien_champ_obs] == 'null' || trim($obs[$ancien_champ_obs]) == '') {
|
1158 |
aurelien |
381 |
$obs[$ancien_champ_obs] = 'NULL';
|
|
|
382 |
}
|
1184 |
jpm |
383 |
|
1186 |
jpm |
384 |
if (($ancien_champ_obs == 'coord_x' || $ancien_champ_obs == 'coord_y') && ($obs[$ancien_champ_obs] == '0' || $obs[$ancien_champ_obs] == 0)) {
|
1158 |
aurelien |
385 |
$obs[$ancien_champ_obs] = 'NULL';
|
|
|
386 |
}
|
1184 |
jpm |
387 |
|
1149 |
gduche |
388 |
$nouvelle_obs[$nouveau_champ_obs] = $obs[$ancien_champ_obs];
|
|
|
389 |
}
|
|
|
390 |
}
|
|
|
391 |
return $nouvelle_obs;
|
|
|
392 |
}
|
1184 |
jpm |
393 |
|
1158 |
aurelien |
394 |
private function protegerSiNonNull($valeur) {
|
1186 |
jpm |
395 |
if ($valeur != 'NULL') {
|
1158 |
aurelien |
396 |
$valeur = $this->proteger($valeur);
|
|
|
397 |
}
|
|
|
398 |
return $valeur;
|
|
|
399 |
}
|
1184 |
jpm |
400 |
|
1149 |
gduche |
401 |
private function estUnChampATraiter($champ) {
|
|
|
402 |
return strpos($champ,'traiter') !== false;
|
|
|
403 |
}
|
1184 |
jpm |
404 |
|
1149 |
gduche |
405 |
private function traiterReferentiel($observation) {
|
1186 |
jpm |
406 |
$retour = 'NULL';
|
|
|
407 |
if ($observation['num_nom_sel'] != '' && $observation['num_nom_sel'] != '0') {
|
|
|
408 |
$retour = 'bdnff:4.02';
|
|
|
409 |
}
|
|
|
410 |
return $retour;
|
1149 |
gduche |
411 |
}
|
1184 |
jpm |
412 |
|
1189 |
jpm |
413 |
private function traiterLat(&$observation) {
|
|
|
414 |
if ($this->etreNull($observation['coord_x'])) {
|
|
|
415 |
$observation['coord_x'] = 'NULL';
|
|
|
416 |
} else if (preg_match(self::PATTERN_LAT, $observation['coord_x']) == false) {
|
|
|
417 |
$latNote = 'Latitude éronnée : '.$observation['coord_x'];
|
|
|
418 |
if ($this->etreNull($observation['commentaire'])) {
|
|
|
419 |
$observation['commentaire'] = $latNote;
|
|
|
420 |
} else {
|
|
|
421 |
$observation['commentaire'] .= "\n".$latNote;
|
|
|
422 |
}
|
|
|
423 |
$observation['coord_x'] = 'NULL';
|
|
|
424 |
}
|
|
|
425 |
$retour = $observation['coord_x'];
|
|
|
426 |
return $retour;
|
|
|
427 |
}
|
|
|
428 |
|
|
|
429 |
private function traiterLng(&$observation) {
|
|
|
430 |
if ($this->etreNull($observation['coord_y'])) {
|
|
|
431 |
$observation['coord_y'] = 'NULL';
|
|
|
432 |
} else if (preg_match(self::PATTERN_LNG, $observation['coord_y']) == false) {
|
|
|
433 |
$lngNote = 'Longitude éronnée : '.$observation['coord_y'];
|
|
|
434 |
if ($this->etreNull($observation['commentaire'])) {
|
|
|
435 |
$observation['commentaire'] = $lngNote;
|
|
|
436 |
} else {
|
|
|
437 |
$observation['commentaire'] .= "\n".$lngNote;
|
|
|
438 |
}
|
|
|
439 |
$observation['coord_y'] = 'NULL';
|
|
|
440 |
}
|
|
|
441 |
$retour = $observation['coord_y'];
|
|
|
442 |
return $retour;
|
|
|
443 |
}
|
|
|
444 |
|
1186 |
jpm |
445 |
private function traiterGeodatum($observation) {
|
|
|
446 |
$retour = 'NULL';
|
|
|
447 |
if ($observation['coord_x'] != 'NULL' && $observation['coord_y'] != 'NULL') {
|
|
|
448 |
$retour = 'WGS84';
|
|
|
449 |
}
|
|
|
450 |
return $retour;
|
|
|
451 |
}
|
|
|
452 |
|
1149 |
gduche |
453 |
private function traiterMotsClesTexte($ligne_observation) {
|
|
|
454 |
$mail_observation = $ligne_observation['identifiant'];
|
1184 |
jpm |
455 |
$retour = $ligne_observation['mots_cles'];
|
1149 |
gduche |
456 |
if (isset($this->tableau_mots_cles[$mail_observation])) {
|
|
|
457 |
$mots_cles_tableau = $this->parserMotsCles($mail_observation, $ligne_observation['mots_cles'], ';');
|
1186 |
jpm |
458 |
$retour = join(',', $mots_cles_tableau);
|
|
|
459 |
$retour = ltrim($retour, ',,') ;
|
1149 |
gduche |
460 |
}
|
1184 |
jpm |
461 |
|
1149 |
gduche |
462 |
return $retour;
|
1184 |
jpm |
463 |
}
|
|
|
464 |
|
1149 |
gduche |
465 |
private function parserMotsCles($utilisateur, $mot_cles, $separateur = ',') {
|
|
|
466 |
$tableau_mots_cles = explode($separateur,$mot_cles);
|
|
|
467 |
$tableau_mots_cles_formates = array();
|
1184 |
jpm |
468 |
|
1186 |
jpm |
469 |
foreach ($tableau_mots_cles as $mot_cle) {
|
1184 |
jpm |
470 |
|
1149 |
gduche |
471 |
$mot_cle = str_replace($separateur.$separateur,'',$mot_cle);
|
|
|
472 |
$mot_cle = str_replace('null','',$mot_cle);
|
1184 |
jpm |
473 |
|
1149 |
gduche |
474 |
if ($this->estUnIdentifiantMotCle($mot_cle)) {
|
1184 |
jpm |
475 |
|
1149 |
gduche |
476 |
// certains mots clés mal formatés contiennent des virgules
|
|
|
477 |
if (strpos($mot_cle,',') !== false) {
|
|
|
478 |
$tab_mot_cle_mal_formate = explode(',',$mot_cle);
|
1184 |
jpm |
479 |
|
1149 |
gduche |
480 |
foreach ($tab_mot_cle_mal_formate as $mot_cle_mal_formate) {
|
|
|
481 |
if ($this->estUnIdentifiantMotCle($mot_cle_mal_formate)) {
|
|
|
482 |
$tableau_mots_cles_formates[$mot_cle_mal_formate] = $this->tableau_mots_cles[$utilisateur][$mot_cle_mal_formate]['mot_cle'];
|
|
|
483 |
}
|
1184 |
jpm |
484 |
}
|
1149 |
gduche |
485 |
} else {
|
|
|
486 |
// on met le mot clé dans sa propre case afin d'éviter
|
1184 |
jpm |
487 |
// facilement les doublons provoqués par de mauvais formatages
|
|
|
488 |
if (isset($this->tableau_mots_cles[$utilisateur][$mot_cle]) && trim($this->tableau_mots_cles[$utilisateur][$mot_cle]['mot_cle']) != '') {
|
1149 |
gduche |
489 |
$tableau_mots_cles_formates[$mot_cle] = $this->tableau_mots_cles[$utilisateur][$mot_cle]['mot_cle'];
|
|
|
490 |
}
|
|
|
491 |
}
|
|
|
492 |
|
|
|
493 |
}
|
|
|
494 |
}
|
1184 |
jpm |
495 |
|
1149 |
gduche |
496 |
return $tableau_mots_cles_formates;
|
|
|
497 |
}
|
1184 |
jpm |
498 |
|
1149 |
gduche |
499 |
private function estUnIdentifiantMotCle($chaine) {
|
|
|
500 |
return trim($chaine) != '' && preg_match('/[0-9A-Z]+\.[0-9A-Z]+/i', $chaine) ;
|
|
|
501 |
}
|
1184 |
jpm |
502 |
|
1149 |
gduche |
503 |
private function traiterIdentifiantUtilisateur($ligne_observation) {
|
|
|
504 |
$mail_observation = $ligne_observation['identifiant'];
|
1181 |
aurelien |
505 |
$retour = $this->renvoyerIdPourMigration($mail_observation);
|
1149 |
gduche |
506 |
return $retour;
|
|
|
507 |
}
|
1184 |
jpm |
508 |
|
1149 |
gduche |
509 |
private function traiterPrenomUtilisateur($ligne_observation) {
|
|
|
510 |
$mail_observation = $ligne_observation['identifiant'];
|
1184 |
jpm |
511 |
|
|
|
512 |
$retour = '';
|
1149 |
gduche |
513 |
if (isset($this->tableau_utilisateurs[$mail_observation])) {
|
1184 |
jpm |
514 |
$prenom = $this->tableau_utilisateurs[$mail_observation]['prenom'];
|
|
|
515 |
$retour = self::formaterMotPremiereLettreChaqueMotEnMajuscule($prenom);
|
1149 |
gduche |
516 |
}
|
|
|
517 |
return $retour;
|
|
|
518 |
}
|
1184 |
jpm |
519 |
|
1149 |
gduche |
520 |
private function traiterNomUtilisateur($ligne_observation) {
|
|
|
521 |
$mail_observation = $ligne_observation['identifiant'];
|
1184 |
jpm |
522 |
|
|
|
523 |
$retour = '';
|
1149 |
gduche |
524 |
if (isset($this->tableau_utilisateurs[$mail_observation])) {
|
1184 |
jpm |
525 |
$nom = $this->tableau_utilisateurs[$mail_observation]['nom'];
|
|
|
526 |
$retour = self::formaterMotEnMajuscule($nom);
|
1149 |
gduche |
527 |
}
|
|
|
528 |
return $retour;
|
|
|
529 |
}
|
1184 |
jpm |
530 |
|
|
|
531 |
public static function formaterMotPremiereLettreChaqueMotEnMajuscule($chaine, $encodage= 'UTF-8') {
|
|
|
532 |
$chaine = str_replace('-', ' - ', $chaine);
|
|
|
533 |
$chaine = mb_strtolower($chaine, $encodage);
|
|
|
534 |
$chaine = mb_convert_case($chaine, MB_CASE_TITLE, $encodage);
|
|
|
535 |
$chaine = str_replace(' - ', '-', $chaine);
|
|
|
536 |
return $chaine;
|
|
|
537 |
}
|
|
|
538 |
|
|
|
539 |
public static function formaterMotEnMajuscule($chaine, $encodage= 'UTF-8') {
|
|
|
540 |
return mb_convert_case($chaine, MB_CASE_UPPER, $encodage);
|
|
|
541 |
}
|
|
|
542 |
|
1149 |
gduche |
543 |
private function traiterZoneGeo($ligne_observation) {
|
1184 |
jpm |
544 |
|
1149 |
gduche |
545 |
$zone_geo = $ligne_observation['location'];
|
1184 |
jpm |
546 |
|
|
|
547 |
if ($ligne_observation['id_location'] != null && !is_numeric($ligne_observation['id_location']) && $ligne_observation['id_location'] != '000null') {
|
1149 |
gduche |
548 |
$id_zone_geo_ancienne = $ligne_observation['id_location'];
|
1184 |
jpm |
549 |
if ($zone_geo != '') {
|
1149 |
gduche |
550 |
$id_zone_geo_ancienne = '('.$id_zone_geo_ancienne.')';
|
|
|
551 |
}
|
1184 |
jpm |
552 |
|
1149 |
gduche |
553 |
$zone_geo .= $id_zone_geo_ancienne;
|
1184 |
jpm |
554 |
} else if ($ligne_observation['location'] == null || $ligne_observation['location'] == "" || $ligne_observation['location'] == "000null") {
|
|
|
555 |
|
|
|
556 |
if ($ligne_observation['id_location'] != '' && $ligne_observation['id_location'] != '000null') {
|
1158 |
aurelien |
557 |
$id_zone_geo_ancienne = $ligne_observation['id_location'];
|
|
|
558 |
$id_zone_geo_ancienne = $id_zone_geo_ancienne;
|
|
|
559 |
$zone_geo = $id_zone_geo_ancienne;
|
|
|
560 |
} else {
|
|
|
561 |
$zones_geo = 'NULL';
|
|
|
562 |
}
|
1149 |
gduche |
563 |
}
|
1184 |
jpm |
564 |
|
1149 |
gduche |
565 |
return $zone_geo;
|
|
|
566 |
}
|
1184 |
jpm |
567 |
|
1149 |
gduche |
568 |
private function traiterIdentifiantZoneGeo($ligne_observation) {
|
|
|
569 |
$id_zone_geo = '';
|
1184 |
jpm |
570 |
|
|
|
571 |
if ($ligne_observation['id_location'] != '' && $ligne_observation['id_location'] != '000null') {
|
1149 |
gduche |
572 |
$indice = $this->construireIndiceTableauLocalites($ligne_observation['location'], $ligne_observation['id_location']);
|
1184 |
jpm |
573 |
if (isset($this->tableau_zones_geo[$indice])) {
|
1149 |
gduche |
574 |
$id_zone_geo = $this->tableau_zones_geo[$indice]['nouveau_code_geo'];
|
|
|
575 |
} else {
|
1184 |
jpm |
576 |
if ($ligne_observation['location'] != "000null") {
|
1149 |
gduche |
577 |
$this->communesOubliees[$indice] = false;
|
|
|
578 |
}
|
|
|
579 |
}
|
1158 |
aurelien |
580 |
} else {
|
|
|
581 |
$id_zone_geo = 'NULL';
|
1149 |
gduche |
582 |
}
|
1184 |
jpm |
583 |
|
1149 |
gduche |
584 |
return $id_zone_geo;
|
|
|
585 |
}
|
1184 |
jpm |
586 |
|
1149 |
gduche |
587 |
private function construireIndiceTableauLocalites($nom, $id) {
|
|
|
588 |
$nom = htmlentities($nom, ENT_NOQUOTES, 'UTF-8');
|
1184 |
jpm |
589 |
|
1149 |
gduche |
590 |
$nom = preg_replace('#&([A-za-z])(?:acute|cedil|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $nom);
|
|
|
591 |
$nom = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $nom); // pour les ligatures e.g. 'œ'
|
|
|
592 |
$nom = preg_replace('#&[^;]+;#', '', $nom); // supprime les autres caractères
|
1184 |
jpm |
593 |
|
1149 |
gduche |
594 |
$nom = str_replace("'",'_',$nom);
|
|
|
595 |
$nom = str_replace(' ','_',$nom);
|
|
|
596 |
$nom = str_replace('-','_',$nom);
|
|
|
597 |
$nom = str_replace(' ','_',$nom);
|
|
|
598 |
$indice = strtolower($nom).substr($id,0,2);
|
1184 |
jpm |
599 |
|
1149 |
gduche |
600 |
return $indice;
|
1184 |
jpm |
601 |
}
|
|
|
602 |
|
|
|
603 |
// Par défaut, on garde l'utilisateur tel quel (cas de la chaine de session des utilisateur anonymes)
|
1181 |
aurelien |
604 |
private function renvoyerIdPourMigration($utilisateur) {
|
|
|
605 |
$retour = $utilisateur;
|
|
|
606 |
// si le mail correspond a un utilisateur de la bdd
|
|
|
607 |
if (isset($this->tableau_utilisateurs[$utilisateur])) {
|
1184 |
jpm |
608 |
// on renvoie son id
|
|
|
609 |
$retour = $this->tableau_utilisateurs[$utilisateur]['id'];
|
|
|
610 |
} else if ($utilisateur != '') {
|
|
|
611 |
// sinon si c'est un mail inconnu, on garde le md5
|
|
|
612 |
if ($this->mailValide($utilisateur)) {
|
|
|
613 |
$retour = md5($utilisateur);
|
|
|
614 |
}
|
1181 |
aurelien |
615 |
}
|
|
|
616 |
return $retour;
|
|
|
617 |
}
|
1184 |
jpm |
618 |
|
1181 |
aurelien |
619 |
public function mailValide($mail) {
|
|
|
620 |
// vérification bidon mais ça suffit pour ici
|
1184 |
jpm |
621 |
return !(strpos('@', $mail) === false);
|
1181 |
aurelien |
622 |
}
|
1149 |
gduche |
623 |
}
|