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