Line 1... |
Line 1... |
1 |
<?php
|
1 |
<?php
|
Line 2... |
Line 2... |
2 |
|
2 |
|
3 |
class MigrationMotsCles extends Cel {
|
- |
|
4 |
|
- |
|
5 |
const bdd_cel_migration = 'cel';
|
- |
|
Line -... |
Line 3... |
- |
|
3 |
class MigrationMotsCles extends Cel {
|
- |
|
4 |
|
- |
|
5 |
public static $bdd_cel_migration;
|
- |
|
6 |
public static $bdd_utilisateurs;
|
- |
|
7 |
const SEPARATEUR_MOT_CLE_TEXTE = ',';
|
- |
|
8 |
|
6 |
const bdd_utilisateurs = 'cel_old';
|
9 |
const truncate = true; //Doit on vider les tables de destination ?
|
Line 7... |
Line 10... |
7 |
|
10 |
|
Line 8... |
Line 11... |
8 |
const dry_run = true;
|
11 |
const dry_run = false;
|
9 |
|
12 |
|
Line 18... |
Line 21... |
18 |
/**
|
21 |
/**
|
19 |
* Méthode appelée avec une requête de type GET.
|
22 |
* Méthode appelée avec une requête de type GET.
|
20 |
*/
|
23 |
*/
|
21 |
public function getElement($params) {
|
24 |
public function getElement($params) {
|
Line -... |
Line 25... |
- |
|
25 |
|
- |
|
26 |
if(!isset($this->config['database_cel']['database_migration']) || $this->config['database_cel']['database_migration'] == '') {
|
- |
|
27 |
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";
|
- |
|
28 |
exit;
|
- |
|
29 |
}
|
- |
|
30 |
|
- |
|
31 |
if(!isset($this->config['database_ident']['database']) || $this->config['database_ident']['database'] == '') {
|
- |
|
32 |
echo 'Attention la variable de configuration database dans la section database_ident, contenant la base de données utilisateurs, doit être remplie '."\n";
|
- |
|
33 |
exit;
|
- |
|
34 |
}
|
- |
|
35 |
|
- |
|
36 |
self::$bdd_cel_migration = $this->config['database_cel']['database_migration'];
|
- |
|
37 |
self::$bdd_utilisateurs = $this->config['database_ident']['database'];
|
- |
|
38 |
|
- |
|
39 |
echo "--MIGRATION DES MOTS CLES --------------------------------------\n";
|
- |
|
40 |
//1. TEMPORAIRE : vider les tables de destinations
|
- |
|
41 |
if (self::truncate) {
|
- |
|
42 |
echo "-------------------------------------------------------------------\n\n";
|
- |
|
43 |
echo " ETAPE 0. Vider les tables ... \n\n";
|
- |
|
44 |
echo "-------------------------------------------------------------------\n\n";
|
- |
|
45 |
$nouvellesTables = array('`cel_mots_cles_obs`','`cel_mots_cles_images`', '`cel_images_mots_cles`', '`cel_obs_mots_cles`');
|
- |
|
46 |
foreach ($nouvellesTables as $nomTable) {
|
- |
|
47 |
echo 'Vider la table '.$nomTable.'...';
|
- |
|
48 |
$requeteTruncate = 'TRUNCATE TABLE '.self::$bdd_cel_migration.'.'.$nomTable;
|
- |
|
49 |
$resultatTruncate = $this->executerRequete($requeteTruncate);
|
- |
|
50 |
echo "ok \n";
|
- |
|
51 |
}
|
- |
|
52 |
echo "\n---------------------------------------------------------------- OK\n\n";
|
- |
|
53 |
}
|
22 |
|
54 |
|
23 |
echo "-------------------------------------------------------------------\n\n";
|
55 |
echo "-------------------------------------------------------------------\n\n";
|
24 |
echo " ETAPE 1. Paramétrage ... \n\n";
|
56 |
echo " ETAPE 1. Paramétrage ... \n\n";
|
25 |
echo "-------------------------------------------------------------------\n\n";
|
- |
|
26 |
|
57 |
echo "-------------------------------------------------------------------\n\n";
|
- |
|
58 |
$this->getUtilisateurs();
|
27 |
$this->getUtilisateurs();
|
59 |
echo "\n"."\n"."\n";
|
28 |
echo "-------------------------------------------------------------------\n\n";
|
60 |
echo "-------------------------------------------------------------------\n\n";
|
29 |
echo " ETAPE 2. Migration des mots clés ... \n\n";
|
61 |
echo " ETAPE 2. Migration des mots clés ... \n\n";
|
30 |
echo "-------------------------------------------------------------------\n\n";
|
- |
|
31 |
|
62 |
echo "-------------------------------------------------------------------\n\n";
|
- |
|
63 |
$this->migrerTableMotsClesObs();
|
32 |
$this->migrerTableMotsClesObs();
|
64 |
echo "\n---------------------------------------------------------------- OK\n\n";
|
- |
|
65 |
$this->migrerTableMotsClesImages();
|
- |
|
66 |
echo "\n"."\n"."\n";
|
33 |
$this->migrerTableMotsClesImages();
|
67 |
echo "-------------------------------------------------------------------\n\n";
|
- |
|
68 |
echo " ETAPE 3. Migration des liaisons mots clés ... \n\n";
|
34 |
echo "\n"."\n".'|| Migration des tables de liaison des mots clés ||'."\n";
|
69 |
echo "-------------------------------------------------------------------\n\n";
|
35 |
$this->migrerLiaisonsMotsClesObs();
|
70 |
$this->migrerLiaisonsMotsClesObs();
|
36 |
$this->migrerLiaisonsMotsClesImages();
|
71 |
$this->migrerLiaisonsMotsClesImages();
|
- |
|
72 |
echo "\n"."\n"."\n";
|
- |
|
73 |
|
- |
|
74 |
echo "-------------------------------------------------------------------\n\n";
|
- |
|
75 |
echo " ETAPE 4. Génération des index des mots clés ... \n\n";
|
- |
|
76 |
echo "-------------------------------------------------------------------\n\n";
|
- |
|
77 |
$this->genererIndexTexteMotsClesObs();
|
- |
|
78 |
$this->genererIndexTexteMotsClesImages();
|
37 |
echo "\n"."\n"."\n";
|
79 |
echo "\n"."\n"."\n";
|
Line 38... |
Line 80... |
38 |
}
|
80 |
}
|
39 |
|
81 |
|
40 |
public function executerRequeteSimple($requete) {
|
82 |
public function executerRequeteSimple($requete) {
|
Line 51... |
Line 93... |
51 |
|
93 |
|
52 |
private function getUtilisateurs() {
|
94 |
private function getUtilisateurs() {
|
53 |
echo "\n-------------------------------------------------------------------\n";
|
95 |
echo "\n-------------------------------------------------------------------\n";
|
Line 54... |
Line 96... |
54 |
echo "--SELECTION DES UTILISATEURS---------------------------------------\n\n";
|
96 |
echo "--SELECTION DES UTILISATEURS---------------------------------------\n\n";
|
55 |
|
97 |
|
Line 56... |
Line 98... |
56 |
$requete_selection_utilisateurs = 'SELECT U_ID as id, U_MAIL as mail, U_NAME as nom, U_SURNAME as prenom, U_PASSWD as pass FROM '.self::bdd_utilisateurs.'.annuaire_tela';
|
98 |
$requete_selection_utilisateurs = 'SELECT U_ID as id, U_MAIL as mail, U_NAME as nom, U_SURNAME as prenom, U_PASSWD as pass FROM '.self::$bdd_utilisateurs.'.annuaire_tela';
|
57 |
$tableau_utilisateurs = $this->executerRequete($requete_selection_utilisateurs);
|
99 |
$tableau_utilisateurs = $this->executerRequete($requete_selection_utilisateurs);
|
58 |
|
100 |
|
Line 71... |
Line 113... |
71 |
|
113 |
|
Line 72... |
Line 114... |
72 |
$mots_cles_obs = $this->executerRequete($requete_selection_mots_cles);
|
114 |
$mots_cles_obs = $this->executerRequete($requete_selection_mots_cles);
|
Line 73... |
Line 115... |
73 |
|
115 |
|
74 |
$mots_cles_tableau = array();
|
116 |
$mots_cles_tableau = array();
|
75 |
|
117 |
|
Line 76... |
Line 118... |
76 |
$requete_insertion_mots_cles = 'INSERT INTO '.self::bdd_cel_migration.'.cel_obs_mots_cles '.
|
118 |
$requete_insertion_mots_cles = 'INSERT INTO '.self::$bdd_cel_migration.'.cel_obs_mots_cles '.
|
77 |
'(id_observation, id_mot_cle_utilisateur) '.
|
119 |
'(id_observation, id_mot_cle_obs) '.
|
Line 78... |
Line 120... |
78 |
'VALUES ';
|
120 |
'VALUES ';
|
Line 79... |
Line 121... |
79 |
|
121 |
|
80 |
$sous_requete_insertion_valeurs = $sous_requete_insertion_mots_cles = $this->construireSousRequeteInsertionLiaisons($mots_cles_obs, 'mots_cles','id',';');
|
122 |
$sous_requete_insertion_valeurs = $sous_requete_insertion_mots_cles = $this->construireSousRequeteInsertionLiaisons($mots_cles_obs, 'mots_cles','id',';');
|
81 |
$requete_insertion_mots_cles = $requete_insertion_mots_cles.$sous_requete_insertion_valeurs;
|
123 |
$requete_insertion_mots_cles = $requete_insertion_mots_cles.$sous_requete_insertion_valeurs;
|
82 |
|
124 |
|
83 |
$insertion = $this->executerRequeteSimple($requete_insertion_mots_cles);
|
125 |
$insertion = $this->executerRequeteSimple($requete_insertion_mots_cles);
|
Line 84... |
Line 126... |
84 |
|
126 |
|
85 |
if(!$insertion) {
|
127 |
if(!$insertion) {
|
Line 99... |
Line 141... |
99 |
|
141 |
|
Line 100... |
Line 142... |
100 |
$mots_cles_images = $this->executerRequete($requete_selection_mots_cles);
|
142 |
$mots_cles_images = $this->executerRequete($requete_selection_mots_cles);
|
Line 101... |
Line 143... |
101 |
|
143 |
|
102 |
$mots_cles_tableau = array();
|
144 |
$mots_cles_tableau = array();
|
103 |
|
145 |
|
104 |
$requete_insertion_mots_cles = 'INSERT INTO '.self::bdd_cel_migration.'.cel_images_mots_cles '.
|
146 |
$requete_insertion_mots_cles = 'INSERT INTO '.self::$bdd_cel_migration.'.cel_images_mots_cles '.
|
Line 105... |
Line 147... |
105 |
'(id_image, id_mot_cle_utilisateur) '.
|
147 |
'(id_image, id_mot_cle_image) '.
|
Line 106... |
Line 148... |
106 |
'VALUES ';
|
148 |
'VALUES ';
|
Line 107... |
Line 149... |
107 |
$sous_requete_insertion_mots_cles = $this->construireSousRequeteInsertionLiaisons($mots_cles_images, 'ci_meta_mots_cles','ci_id_image',',');
|
149 |
$sous_requete_insertion_mots_cles = $this->construireSousRequeteInsertionLiaisons($mots_cles_images, 'ci_meta_mots_cles','ci_id_image',',');
|
108 |
|
150 |
|
109 |
$requete_insertion_mots_cles = $requete_insertion_mots_cles.$sous_requete_insertion_mots_cles;
|
151 |
$requete_insertion_mots_cles = $requete_insertion_mots_cles.$sous_requete_insertion_mots_cles;
|
110 |
|
152 |
|
111 |
$insertion = $this->executerRequeteSimple($requete_insertion_mots_cles);
|
153 |
$insertion = $this->executerRequeteSimple($requete_insertion_mots_cles);
|
112 |
|
154 |
|
Line 113... |
Line 155... |
113 |
if(!$insertion) {
|
155 |
if(!$insertion) {
|
114 |
echo 'la migration des mots cles images a échoué ! '."\n";
|
156 |
echo 'la migration des mots cles images a échoué ! '."\n";
|
Line 168... |
Line 210... |
168 |
for($i = 0; $i < $nbMotsCles; $i += $pas) {
|
210 |
for($i = 0; $i < $nbMotsCles; $i += $pas) {
|
169 |
$requete_selection_arbres_mots_cles = 'SELECT * FROM cel_mots_cles_'.$image_ou_obs.' ORDER BY cmc_niveau '.
|
211 |
$requete_selection_arbres_mots_cles = 'SELECT * FROM cel_mots_cles_'.$image_ou_obs.' ORDER BY cmc_niveau '.
|
170 |
'LIMIT '.$i.','.$pas;
|
212 |
'LIMIT '.$i.','.$pas;
|
171 |
$arbres_mots_cles = $this->executerRequete($requete_selection_arbres_mots_cles);
|
213 |
$arbres_mots_cles = $this->executerRequete($requete_selection_arbres_mots_cles);
|
Line -... |
Line 214... |
- |
|
214 |
|
- |
|
215 |
$champ_parent = ($image_ou_obs == "obs") ? 'ce_mot_cle_obs_parent' : 'ce_mot_cle_image_parent';
|
172 |
|
216 |
$champ_id = ($image_ou_obs == "obs") ? 'id_mot_cle_obs' : 'id_mot_cle_image';
|
173 |
$requete_insertion_nouvel_arbre = 'INSERT INTO '.self::bdd_cel_migration.'.cel_mots_cles_'.$image_ou_obs.' '.
|
217 |
$requete_insertion_nouvel_arbre = 'INSERT INTO '.self::$bdd_cel_migration.'.cel_mots_cles_'.$image_ou_obs.' '.
|
174 |
'(id_mot_cle_utilisateur, ce_utilisateur, mot_cle, md5, bg, bd, niveau, ce_mot_cle_utilisateur_parent) '.
|
218 |
'('.$champ_id.', ce_utilisateur, mot_cle, md5, bg, bd, niveau, '.$champ_parent.') '.
|
175 |
'VALUES ';
|
219 |
'VALUES ';
|
Line 176... |
Line 220... |
176 |
$sous_requete_insertion_nouvel_arbre = '';
|
220 |
$sous_requete_insertion_nouvel_arbre = '';
|
177 |
|
221 |
|
Line 181... |
Line 225... |
181 |
}
|
225 |
}
|
Line 182... |
Line 226... |
182 |
|
226 |
|
Line 183... |
Line 227... |
183 |
$sous_requete_insertion_nouvel_arbre = rtrim($sous_requete_insertion_nouvel_arbre,',');
|
227 |
$sous_requete_insertion_nouvel_arbre = rtrim($sous_requete_insertion_nouvel_arbre,',');
|
184 |
|
- |
|
185 |
$requete_insertion_nouvel_arbre = $requete_insertion_nouvel_arbre.$sous_requete_insertion_nouvel_arbre;
|
228 |
|
186 |
//echo var_dump($requete_insertion_nouvel_arbre);
|
229 |
$requete_insertion_nouvel_arbre = $requete_insertion_nouvel_arbre.$sous_requete_insertion_nouvel_arbre;
|
187 |
$migration = $this->executerRequeteSimple($requete_insertion_nouvel_arbre);
|
230 |
$migration = $this->executerRequeteSimple($requete_insertion_nouvel_arbre);
|
188 |
if(!$migration) {
|
231 |
if(!$migration) {
|
189 |
echo 'la migration des mots cles '.$image_ou_obs.' a échoué ! '."\n";
|
232 |
echo 'la migration des mots cles '.$image_ou_obs.' a échoué ! '."\n";
|
190 |
} else {
|
233 |
} else {
|
191 |
$nbTotal ++;
|
234 |
$nbTotal ++;
|
192 |
$this->afficherAvancement('Migration des mots clés (par '.$pas.') :', $nbTotal);
|
235 |
$this->afficherAvancement('Migration des mots clés (par '.$pas.') :', $nbTotal);
|
- |
|
236 |
}
|
193 |
}
|
237 |
} else {
|
194 |
} else {
|
238 |
echo "\n---------------------------------------------------------------- OK\n\n";
|
Line 195... |
Line 239... |
195 |
return;
|
239 |
return;
|
196 |
}
|
240 |
}
|
Line 240... |
Line 284... |
240 |
foreach($tableau_mots_cles as $mot_cle) {
|
284 |
foreach($tableau_mots_cles as $mot_cle) {
|
Line 241... |
Line 285... |
241 |
|
285 |
|
242 |
$mot_cle = str_replace($separateur.$separateur,'',$mot_cle);
|
286 |
$mot_cle = str_replace($separateur.$separateur,'',$mot_cle);
|
Line 243... |
Line 287... |
243 |
$mot_cle = str_replace('null','',$mot_cle);
|
287 |
$mot_cle = str_replace('null','',$mot_cle);
|
244 |
|
- |
|
245 |
if($this->estUnIdentifiantMotCle($mot_cle)) {
|
288 |
|
246 |
|
289 |
if($this->estUnIdentifiantMotCle($mot_cle)) {
|
247 |
// certains mots clés mal formatés contiennent des virgules
|
290 |
// certains mots clés mal formatés contiennent des virgules
|
Line 248... |
Line 291... |
248 |
if(strpos($mot_cle,',') !== false) {
|
291 |
if(strpos($mot_cle,',') !== false) {
|
Line 256... |
Line 299... |
256 |
} else {
|
299 |
} else {
|
257 |
// on met le mot clé dans sa propre case afin d'éviter
|
300 |
// on met le mot clé dans sa propre case afin d'éviter
|
258 |
// facilement les doublons provoqués par de mauvais formatages
|
301 |
// facilement les doublons provoqués par de mauvais formatages
|
259 |
$tableau_mots_cles_formates[$mot_cle] = $mot_cle;
|
302 |
$tableau_mots_cles_formates[$mot_cle] = $mot_cle;
|
260 |
}
|
303 |
}
|
261 |
|
- |
|
262 |
}
|
304 |
}
|
263 |
}
|
305 |
}
|
Line 264... |
Line 306... |
264 |
|
306 |
|
265 |
return $tableau_mots_cles_formates;
|
307 |
return $tableau_mots_cles_formates;
|
Line 266... |
Line 308... |
266 |
}
|
308 |
}
|
267 |
|
309 |
|
268 |
private function estUnIdentifiantMotCle($chaine) {
|
310 |
private function estUnIdentifiantMotCle($chaine) {
|
- |
|
311 |
return trim($chaine) != '' && preg_match('/[0-9A-Z]+\.[0-9A-Z]+/i', $chaine) ;
|
- |
|
312 |
}
|
- |
|
313 |
|
- |
|
314 |
private function genererIndexTexteMotsClesObs() {
|
- |
|
315 |
$requete_selection_obs = 'SELECT DISTINCT id_observation, ce_utilisateur FROM '.self::$bdd_cel_migration.'.cel_obs '.
|
- |
|
316 |
'WHERE id_observation IN (SELECT DISTINCT id_observation FROM '.self::$bdd_cel_migration.'.cel_mots_cles_obs) ';
|
- |
|
317 |
$obs_a_mots_cles = $this->requeter($requete_selection_obs);
|
- |
|
318 |
$nbTotal = 0;
|
- |
|
319 |
|
- |
|
320 |
foreach($obs_a_mots_cles as $obs) {
|
- |
|
321 |
$mots_cles_texte = $this->obtenirMotsClesTexte($obs['id_observation'], $obs['ce_utilisateur'], 'obs');
|
- |
|
322 |
if (is_array($mots_cles_texte) && count($mots_cles_texte) > 0) {
|
- |
|
323 |
$mots_cles_texte_chaine = implode(',', $mots_cles_texte);
|
- |
|
324 |
$mise_a_jour_index = $this->executerRequeteMiseAJourMotCleTexte($mots_cles_texte_chaine,$obs['id_observation'], $obs['ce_utilisateur'], 'obs');
|
- |
|
325 |
$nbTotal ++;
|
- |
|
326 |
$this->afficherAvancement('Génération des index mots clés obs (par 1) :', $nbTotal);
|
- |
|
327 |
}
|
- |
|
328 |
}
|
- |
|
329 |
echo "\n-----------------------------------------------------------------OK\n";
|
- |
|
330 |
}
|
- |
|
331 |
|
- |
|
332 |
private function genererIndexTexteMotsClesImages() {
|
- |
|
333 |
$requete_selection_images = 'SELECT DISTINCT id_image, ce_utilisateur FROM '.self::$bdd_cel_migration.'.cel_images '.
|
- |
|
334 |
'WHERE id_image IN (SELECT DISTINCT id_image FROM '.self::$bdd_cel_migration.'.cel_mots_cles_images) ';
|
- |
|
335 |
$images_a_mots_cles = $this->requeter($requete_selection_images);
|
- |
|
336 |
$nbTotal = 0;
|
- |
|
337 |
|
- |
|
338 |
foreach($images_a_mots_cles as $image) {
|
- |
|
339 |
$mots_cles_texte = $this->obtenirMotsClesTexte($image['id_image'], $image['ce_utilisateur'], 'images');
|
- |
|
340 |
$mots_cles_texte_chaine = '';
|
- |
|
341 |
if (is_array($mots_cles_texte) && count($mots_cles_texte) > 0) {
|
- |
|
342 |
$mots_cles_texte_chaine = implode(',', $mots_cles_texte);
|
- |
|
343 |
$mise_a_jour_index = $this->executerRequeteMiseAJourMotCleTexte($mots_cles_texte_chaine,$image['id_image'], $image['ce_utilisateur'], 'images');
|
- |
|
344 |
$nbTotal ++;
|
- |
|
345 |
$this->afficherAvancement('Génération des index mots clés images (par 1) :', $nbTotal);
|
- |
|
346 |
}
|
- |
|
347 |
}
|
- |
|
348 |
echo "\n-----------------------------------------------------------------OK\n";
|
- |
|
349 |
}
|
- |
|
350 |
|
- |
|
351 |
private function executerRequeteMiseAJourMotCleTexte($mots_cles_texte_chaine, $id_image_ou_obs, $identifiant_utilisateur, $mode_image_ou_obs) {
|
- |
|
352 |
$requete = 'UPDATE '.self::$bdd_cel_migration.'.'.(($mode_image_ou_obs == 'obs') ? 'cel_obs' : 'cel_images').' '.
|
- |
|
353 |
'SET mots_cles_texte = '.$this->proteger($mots_cles_texte_chaine).' '.
|
- |
|
354 |
'WHERE '.(($mode_image_ou_obs == 'obs') ? 'id_observation' : 'id_image').' = '.$this->proteger($id_image_ou_obs).
|
- |
|
355 |
' AND ce_utilisateur = '.$this->proteger($identifiant_utilisateur);
|
- |
|
356 |
|
- |
|
357 |
return $this->executer($requete);
|
- |
|
358 |
}
|
- |
|
359 |
|
- |
|
360 |
private function obtenirMotsClesTexte($id_image_ou_obs, $identifiant_utilisateur, $mode_image_ou_obs) {
|
- |
|
361 |
|
- |
|
362 |
$champ_id = ($mode_image_ou_obs == 'obs') ? 'id_mot_cle_obs' : 'id_mot_cle_image';
|
- |
|
363 |
|
- |
|
364 |
$requete = 'SELECT mot_cle '.
|
- |
|
365 |
'FROM '.self::$bdd_cel_migration.'.cel_mots_cles_'.$mode_image_ou_obs.' '.
|
- |
|
366 |
'WHERE '.$champ_id.' IN '.
|
- |
|
367 |
'('.
|
- |
|
368 |
'SELECT '.$champ_id.' '.
|
- |
|
369 |
'FROM '.self::$bdd_cel_migration.'.cel_'.$mode_image_ou_obs.'_mots_cles '.
|
- |
|
370 |
'WHERE '.(($mode_image_ou_obs == 'obs') ? 'id_observation' : 'id_image').' = '.$this->proteger($id_image_ou_obs).
|
- |
|
371 |
')'.
|
- |
|
372 |
' AND ce_utilisateur = '.$this->proteger($identifiant_utilisateur);
|
- |
|
373 |
|
- |
|
374 |
$resultats = $this->requeter($requete);
|
- |
|
375 |
$mots_cles = array();
|
- |
|
376 |
foreach($resultats as $resultat) {
|
- |
|
377 |
$mots_cles[] = $resultat['mot_cle'];
|
- |
|
378 |
}
|
- |
|
379 |
|
269 |
return trim($chaine) != '' && preg_match('/[0-9A-Z]+\.[0-9A-Z]+/i', $chaine) ;
|
380 |
return $mots_cles;
|
270 |
}
|
381 |
}
|