Updates merge scripts.
[platal.git] / upgrade / 1.0.1 / merge.php
CommitLineData
6b9e0135
SJ
1#!/usr/bin/php5
2<?php
3require_once 'connect.db.inc.php';
6b9e0135
SJ
4
5$globals->debug = 0; // Do not store backtraces.
f8c2bf68 6XDB::startTransaction();
6b9e0135 7
f8c2bf68 8// Drops temporary tables and views used to checked if the merge was possible.
6b9e0135
SJ
9XDB::rawExecute('DROP VIEW IF EXISTS fusionax_xorg_anciens');
10XDB::rawExecute('DROP VIEW IF EXISTS fusionax_deceased');
11XDB::rawExecute('DROP VIEW IF EXISTS fusionax_promo');
12XDB::rawExecute('DROP TABLE IF EXISTS fusionax_import');
13
a48b23c3 14// Fills pid fields in all table, to avoid to many joins.
0efb08e6 15foreach (array('fusionax_activites', 'fusionax_adresses', 'fusionax_anciens', 'fusionax_formations', 'fusionax_formations_md') as $table) {
a48b23c3
SJ
16 XDB::rawExecute("UPDATE $table AS f
17 INNER JOIN profiles AS p ON (f.ax_id = p.ax_id)
18 SET f.pid = p.pid");
19 XDB::rawExecute("DELETE FROM $table WHERE pid IS NULL");
20}
21
723d4c6b
SJ
22echo "Update timestamp.\n";
23XDB::rawExecute("UPDATE profiles AS p
24 SET p.last_change = GREATEST(COALESCE((SELECT MAX(Date_maj) FROM fusionax_anciens AS f WHERE f.pid = p.pid), '0000-00-00'),
25 COALESCE((SELECT MAX(Date_maj) FROM fusionax_activites AS f WHERE f.pid = p.pid), '0000-00-00'),
26 COALESCE((SELECT MAX(Date_maj) FROM fusionax_adresses AS f WHERE f.pid = p.pid), '0000-00-00'),
27 COALESCE((SELECT MAX(Date_maj) FROM fusionax_formations AS f WHERE f.pid = p.pid), '0000-00-00'),
28 COALESCE(p.last_change, '0000-00-00'))");
29
f8c2bf68
SJ
30// Includes entreprises we do not have into profile_job_enum.
31// We first retrieve AX code, then add missing compagnies.
32echo "Starts jobs inclusions.\n";
a48b23c3
SJ
33XDB::rawExecute('ALTER TABLE profile_job_enum ADD INDEX (name(20))');
34XDB::rawExecute('ALTER TABLE profile_job_enum ADD INDEX (acronym(20))');
35XDB::rawExecute('ALTER TABLE profile_job_enum ADD INDEX (AX_code)');
f8c2bf68
SJ
36XDB::rawExecute("UPDATE profile_job_enum, fusionax_entreprises
37 SET profile_job_enum.AX_code = fusionax_entreprises.Code_etab
38 WHERE (profile_job_enum.name = fusionax_entreprises.Raison_sociale AND profile_job_enum.name != '' AND fusionax_entreprises.Raison_sociale != '')
39 OR (profile_job_enum.name = fusionax_entreprises.Sigle AND profile_job_enum.name != '' AND fusionax_entreprises.Sigle != '')
40 OR (profile_job_enum.acronym = fusionax_entreprises.Sigle AND profile_job_enum.acronym != '' AND fusionax_entreprises.Sigle != '')
41 OR (profile_job_enum.acronym = fusionax_entreprises.Raison_sociale AND profile_job_enum.acronym != '' AND fusionax_entreprises.Raison_sociale != '')");
42XDB::rawExecute("INSERT IGNORE INTO profile_job_enum (name, acronym, AX_code)
43 SELECT f.Raison_sociale, f.Sigle, f.Code_etab
44 FROM fusionax_entreprises AS f
45 WHERE f.Raison_sociale != ''
46 AND NOT EXISTS (SELECT *
47 FROM profile_job_enum AS j
48 WHERE f.Code_etab = j.AX_code)");
6b9e0135 49
f8c2bf68
SJ
50// Includes jobs we do not have into profile_job_enum.
51// There are 3 cases:
52// - the job is incomplete (ie no compagny name) : this is an issue,
53// - the job is complete but the profile already has a job or more : this is an issue,
54// - the job is complete and the the profile has no previous job : there is no issue.
a48b23c3 55// We delete obvious duplicates and avoid multiple joins.
f8c2bf68
SJ
56XDB::rawExecute("DELETE f
57 FROM fusionax_activites AS f
f8c2bf68 58 INNER JOIN profile_job_enum AS pe ON (pe.AX_code = f.Code_etab)
a48b23c3
SJ
59 INNER JOIN profile_job AS pj ON (f.pid = pj.pid AND pj.jobid = pe.id)");
60foreach (array('fusionax_activites', 'fusionax_adresses') as $table) {
61 XDB::rawExecute("UPDATE $table AS f
62 INNER JOIN profile_job_enum AS pe ON (f.Code_etab = pe.AX_code)
63 SET f.jobid = pe.id");
64}
65XDB::rawExecute('ALTER TABLE profile_job_enum DROP INDEX name');
66XDB::rawExecute('ALTER TABLE profile_job_enum DROP INDEX acronym');
67XDB::rawExecute('ALTER TABLE profile_job_enum DROP INDEX AX_code');
68XDB::rawExecute('DROP TABLE IF EXISTS fusionax_entreprises');
f8c2bf68
SJ
69
70// We first update the issues table.
f27cbbb3
SJ
71XDB::rawExecute("INSERT IGNORE INTO profile_merge_issues (pid, issues)
72 SELECT DISTINCT(f.pid), 'job'
73 FROM fusionax_activites AS f
74 WHERE f.jobid IS NULL OR EXISTS (SELECT *
75 FROM profile_job AS pj
76 WHERE pj.pid = f.pid)");
a48b23c3 77// We then add new jobs.
f8c2bf68
SJ
78$id = 0;
79$continue = 1;
80while ($continue > 0) {
81 XDB::rawExecute("INSERT IGNORE INTO profile_job (id, pid, jobid, pub, description)
a48b23c3
SJ
82 SELECT $id, pid, jobid, IF(Annuaire = 1, 'ax', 'private'), description
83 FROM fusionax_activites");
f8c2bf68 84 XDB::rawExecute("DELETE f
a48b23c3 85 FROM fusionax_activites AS f
f27cbbb3
SJ
86 INNER JOIN profile_job AS pj ON (f.pid = pj.pid AND pj.id = $id AND pj.description = f.description)
87 WHERE pj.jobid = f.jobid OR (pj.jobid IS NULL AND f.jobid IS NULL)");
f8c2bf68 88 $continue = XDB::affectedRows();
247b7728 89 ++$id;
6b9e0135 90}
a48b23c3 91XDB::rawExecute('DROP TABLE IF EXISTS fusionax_activites');
f8c2bf68
SJ
92// We also have to add related phones and addresses.
93XDB::rawExecute("INSERT IGNORE INTO profile_addresses (type, pid, id, text)
a48b23c3 94 SELECT 'job', f.pid, pj.id, f.text
f8c2bf68 95 FROM fusionax_adresses AS f
a48b23c3 96 INNER JOIN profile_job AS pj ON (f.pid = pj.pid AND pj.jobid = f.jobid)
f8c2bf68
SJ
97 WHERE f.Type_adr = 'E' AND f.text IS NOT NULL");
98XDB::rawExecute("INSERT IGNORE INTO profile_phones (link_type, link_id, tel_id, tel_type, pid, display_tel)
a48b23c3 99 SELECT 'pro', pj.id, 0, 'fixed', f.pid, f.tel
f8c2bf68 100 FROM fusionax_adresses AS f
a48b23c3 101 INNER JOIN profile_job AS pj ON (f.pid = pj.pid AND pj.jobid = f.jobid)
f8c2bf68
SJ
102 WHERE f.Type_adr = 'E' AND f.tel != ''");
103XDB::rawExecute("INSERT IGNORE INTO profile_phones (link_type, link_id, tel_id, tel_type, pid, display_tel)
a48b23c3 104 SELECT 'pro', pj.id, IF(f.tel = '', 0, 1), 'fax', f.pid, f.fax
f8c2bf68 105 FROM fusionax_adresses AS f
a48b23c3 106 INNER JOIN profile_job AS pj ON (f.pid = pj.pid AND pj.jobid = f.jobid)
f8c2bf68 107 WHERE f.Type_adr = 'E' AND f.fax != ''");
a48b23c3 108XDB::rawExecute("DELETE FROM fusionax_adresses WHERE Type_adr = 'E'");
f8c2bf68 109echo "Jobs inclusions finished.\n";
6b9e0135 110
f8c2bf68
SJ
111// Retrieves information from fusionax_anciens: promo, nationality, corps, email, phone, deathdate.
112// Updates uncertain promotions, but when we are we are right.
113echo "Starts various informations inclusions.\n";
a48b23c3 114XDB::rawExecute("UPDATE profile_merge_issues AS pm, fusionax_anciens AS f, profile_display AS pd, profile_education AS pe
f8c2bf68 115 SET pm.issues = IF(pm.issues, CONCAT(pm.issues, ',', 'promo'), 'promo'), pm.entry_year_ax = f.promotion_etude
a48b23c3
SJ
116 WHERE pm.pid = f.pid AND f.pid NOT IN (18399,21099,40616)
117 AND pd.pid = f.pid AND pe.pid = f.pid AND FIND_IN_SET('primary', pe.flags)
f8c2bf68
SJ
118 AND pd.promo != CONCAT('X', f.promotion_etude)
119 AND !(f.promotion_etude = pe.entry_year + 1 AND pe.grad_year = pe.entry_year + 4)");
a48b23c3 120
f8c2bf68 121XDB::rawExecute("INSERT IGNORE INTO profile_merge_issues (pid, issues, entry_year_ax)
a48b23c3
SJ
122 SELECT f.pid, 'promo', f.promotion_etude
123 FROM fusionax_anciens AS f
124 INNER JOIN profile_display AS pd ON (f.pid = pd.pid)
125 INNER JOIN profile_education AS pe ON (f.pid = pe.pid)
126 WHERE pd.promo != CONCAT('X', f.promotion_etude) AND f.pid NOT IN (18399,21099,40616)
f8c2bf68 127 AND !(f.promotion_etude = pe.entry_year + 1 AND pe.grad_year = pe.entry_year + 4)");
6b9e0135 128
f8c2bf68
SJ
129// Updates nationality.
130XDB::rawExecute('ALTER TABLE geoloc_countries ADD INDEX (licensePlate)');
6b9e0135 131XDB::rawExecute('UPDATE profiles AS p
a48b23c3 132 INNER JOIN fusionax_anciens AS f ON (p.pid = f.pid)
d086dd79 133 INNER JOIN geoloc_countries AS g ON (g.licensePlate = f.Code_nationalite AND g.nationality IS NOT NULL)
f8c2bf68
SJ
134 SET p.nationality1 = g.iso_3166_1_a2
135 WHERE p.nationality1 IS NULL');
6b9e0135 136XDB::rawExecute('UPDATE profiles AS p
a48b23c3 137 INNER JOIN fusionax_anciens AS f ON (p.pid = f.pid)
d086dd79 138 INNER JOIN geoloc_countries AS g ON (g.licensePlate = f.Code_nationalite AND g.nationality IS NOT NULL)
f8c2bf68
SJ
139 SET p.nationality2 = g.iso_3166_1_a2
140 WHERE p.nationality1 != g.iso_3166_1_a2 AND p.nationality2 IS NULL');
6b9e0135 141XDB::rawExecute('UPDATE profiles AS p
a48b23c3 142 INNER JOIN fusionax_anciens AS f ON (p.pid = f.pid)
d086dd79 143 INNER JOIN geoloc_countries AS g ON (g.licensePlate = f.Code_nationalite AND g.nationality IS NOT NULL)
f8c2bf68
SJ
144 SET p.nationality3 = g.iso_3166_1_a2
145 WHERE p.nationality1 != g.iso_3166_1_a2 AND p.nationality2 != g.iso_3166_1_a2 AND p.nationality3 IS NULL');
146XDB::rawExecute('ALTER TABLE geoloc_countries DROP INDEX licensePlate');
6b9e0135 147
f8c2bf68 148// Updates corps.
0b730031 149XDB::rawExecute("INSERT IGNORE INTO profile_corps_enum (name, abbreviation)
02526cb5 150 VALUES ('Ancien élève étranger', 'Z')");
0b730031
SJ
151XDB::rawExecute("INSERT IGNORE INTO profile_corps_rank_enum (name, abbreviation)
152 VALUES ('Ing.ch.P.C.hon.', 'DEL1'), ('Ing.Mil.Air Retr.', 'DEL2'),
153 ('Col.hon.Tra.', 'DEL3'), ('Colonel CR', 'DEL4'),
02526cb5
SJ
154 ('Conseil d\'Etat', 'DEL5'), ('Commiss.Gén. Brigade aérienne', 'DEL6'),
155 ('Off. Mar. dém.', 'DEL7'), ('Maître des Requêtes', 'DEL8'),
c8ab27eb 156 ('', 'DEL9'), ('autres', 'DEL10'), ('Contrôl. d\'Etat hon.', 'DEL11')");
723d4c6b
SJ
157XDB::rawExecute('UPDATE profile_corps AS pc
158 INNER JOIN fusionax_anciens AS f ON (f.pid = pc.pid)
159 INNER JOIN profile_corps_enum AS c ON (f.corps_sortie = c.abbreviation)
160 SET pc.original_corpsid = c.id');
f8c2bf68 161XDB::rawExecute("INSERT IGNORE INTO profile_corps (pid, original_corpsid, current_corpsid, rankid, corps_pub)
a48b23c3
SJ
162 SELECT f.pid, c.id, c.id, r.id, 'ax'
163 FROM fusionax_anciens AS f
f8c2bf68 164 INNER JOIN profile_corps_enum AS c ON (f.corps_sortie = c.abbreviation)
fa84a899 165 INNER JOIN profile_corps_rank_enum AS r ON (f.grade = r.name)
f8c2bf68
SJ
166 WHERE NOT EXISTS (SELECT *
167 FROM profile_corps AS pc
a48b23c3 168 WHERE f.pid = pc.pid AND pc.original_corpsid != 1)");
6b9e0135
SJ
169XDB::rawExecute("UPDATE profile_corps AS c
170 INNER JOIN profile_corps_enum AS e ON (c.original_corpsid = e.id)
171 LEFT JOIN profile_corps_enum AS a ON (a.name = 'Aucun (anc. démissionnaire)')
172 SET c.original_corpsid = a.id
173 WHERE e.name = 'Ancien élève étranger'");
174XDB::rawExecute("UPDATE profile_corps_enum
175 SET name = 'Aucun'
176 WHERE name = 'Aucun (anc. démissionnaire)'");
177XDB::rawExecute("DELETE FROM profile_corps_enum
178 WHERE name = 'Ancien élève étranger'");
0b730031
SJ
179XDB::rawExecute("UPDATE profile_corps AS c
180 INNER JOIN profile_corps_rank_enum AS r ON (c.rankid = r.id)
181 INNER JOIN profile_corps_rank_enum AS a ON (a.name = 'Aucun')
182 SET c.rankid = a.id
183 WHERE r.name LIKE 'DEL%'");
184XDB::rawExecute("DELETE FROM profile_corps_rank_enum
02526cb5 185 WHERE name LIKE 'DEL%'");
6b9e0135 186
89aba52d
SJ
187// Updates title.
188XDB::rawExecute("UPDATE profiles AS p
189 INNER JOIN fusionax_anciens AS f ON (p.pid = f.pid)
190 SET p.title = f.Civilite");
191
f8c2bf68
SJ
192// Updates email_directory.
193XDB::rawExecute("UPDATE profiles AS p
a48b23c3 194 INNER JOIN fusionax_anciens AS f ON (p.pid = f.pid)
f8c2bf68 195 SET p.email_directory = f.Mel_usage
a48b23c3 196 WHERE f.Mel_publiable = 1 AND f.Mel_usage != '' AND p.email_directory IS NULL");
6b9e0135
SJ
197XDB::rawExecute("INSERT IGNORE INTO register_marketing (uid, email, type)
198 SELECT ap.uid, f.Mel_usage, 'ax'
d086dd79
SJ
199 FROM fusionax_anciens AS f
200 INNER JOIN account_profiles AS ap ON (ap.pid = f.pid AND FIND_IN_SET('owner', perms))
201 LEFT JOIN email_redirect_account AS e ON (e.uid = ap.uid AND e.flags = 'active')
6b9e0135
SJ
202 WHERE f.Mel_usage != '' AND f.Mel_usage NOT LIKE '%@polytechnique.edu'
203 AND f.Mel_usage NOT LIKE '%@polytechnique.org' AND f.Mel_usage NOT LIKE '%@m4x.org'
d086dd79 204 AND f.Mel_usage NOT LIKE '%@melix.%' AND e.redirect IS NULL");
6b9e0135 205
f8c2bf68
SJ
206// Retrieves different deathdates.
207XDB::rawExecute("UPDATE profile_merge_issues AS pm
208 INNER JOIN profiles AS p ON (pm.pid = p.pid)
a48b23c3 209 INNER JOIN fusionax_anciens AS f ON (f.pid = p.pid)
f8c2bf68 210 SET pm.issues = IF(pm.issues, CONCAT(pm.issues, ',', 'deathdate'), 'deathdate'), pm.deathdate_ax = f.Date_deces
6b9e0135 211 WHERE p.deathdate != f.Date_deces");
f8c2bf68 212XDB::rawExecute("INSERT IGNORE INTO profile_merge_issues (pid, issues, deathdate_ax)
a48b23c3 213 SELECT f.pid, 'deathdate', f.Date_deces
f8c2bf68 214 FROM fusionax_anciens AS f
a48b23c3 215 INNER JOIN profiles AS p ON (f.pid = p.pid)
f8c2bf68
SJ
216 WHERE p.deathdate != f.Date_deces");
217echo "Various informations inclusions finished.\n";
218
219// Updates phone.
220// We consider there is conflict if a profile has a phone in both databases.
221echo "Starts phones inclusions.\n";
222XDB::rawExecute("UPDATE profile_merge_issues AS pm
a48b23c3
SJ
223 INNER JOIN fusionax_anciens AS f ON (f.pid = pm.pid)
224 INNER JOIN profile_phones AS pp ON (pp.pid = f.pid AND pp.link_type = 'user' AND pp.tel_id = 0)
f8c2bf68
SJ
225 SET pm.issues = IF(pm.issues, CONCAT(pm.issues, ',', 'phone'), 'phone')
226 WHERE f.tel_mobile != ''");
227XDB::rawExecute("INSERT IGNORE INTO profile_merge_issues (pid, issues)
a48b23c3 228 SELECT f.pid, 'phone'
f8c2bf68 229 FROM fusionax_anciens AS f
a48b23c3
SJ
230 INNER JOIN profile_phones AS pp ON (pp.pid = f.pid AND pp.link_type = 'user' AND pp.tel_id = 0)
231 WHERE f.tel_mobile != '' AND f.Mob_publiable = 1");
f8c2bf68
SJ
232
233XDB::rawExecute("INSERT INTO profile_phones (pid, link_type, tel_id, tel_type, display_tel, pub)
a48b23c3 234 SELECT f.pid, 'user', 0, 'mobile', f.tel_mobile, 'ax'
f8c2bf68 235 FROM fusionax_anciens AS f
f8c2bf68
SJ
236 WHERE NOT EXISTS (SELECT *
237 FROM profile_phones AS pp
a48b23c3
SJ
238 WHERE pp.pid = f.pid AND pp.link_type = 'user' AND pp.tel_id = 0)
239 AND f.tel_mobile != '' AND f.Mob_publiable = 1");
f8c2bf68 240XDB::rawExecute("INSERT INTO profile_phones (pid, link_type, tel_id, tel_type, display_tel, pub)
a48b23c3 241 SELECT f.pid, 'user', MAX(pp.tel_id) + 1, 'mobile', f.tel_mobile, 'ax'
f8c2bf68 242 FROM fusionax_anciens AS f
a48b23c3
SJ
243 INNER JOIN profile_phones AS pp ON (pp.pid = f.pid AND pp.link_type = 'user')
244 WHERE f.tel_mobile != '' AND f.Mob_publiable = 1
245 GROUP BY f.pid");
6b9e0135 246XDB::rawExecute('DROP TABLE IF EXISTS fusionax_anciens');
f8c2bf68 247echo "Phones inclusions finished.\n";
6b9e0135 248
f8c2bf68
SJ
249// Retrieves addresses from AX database (one address per preofile maximum).
250echo "Starts addresses inclusions.\n";
251XDB::rawExecute('ALTER TABLE profile_addresses ADD INDEX (text(20))');
252XDB::rawExecute("DELETE f
253 FROM fusionax_adresses AS f
a48b23c3 254 INNER JOIN profile_addresses AS pa ON (pa.pid = f.pid AND pa.type = 'home')
f8c2bf68 255 WHERE pa.text = f.text");
325af12b
SJ
256// Deletes addresses of unknown type.
257XDB::rawExecute("DELETE FROM fusionax_adresses
258 WHERE Type_adr != 'E' AND Type_adr != 'P'");
259
f8c2bf68 260XDB::rawExecute("UPDATE profile_merge_issues AS pm
a48b23c3
SJ
261 INNER JOIN fusionax_adresses AS f ON (f.pid = pm.pid)
262 INNER JOIN profile_addresses AS pa ON (pa.pid = f.pid AND pa.type = 'home' AND pa.id = 0)
f8c2bf68
SJ
263 SET pm.issues = IF(pm.issues, CONCAT(pm.issues, ',', 'address'), 'address')
264 WHERE f.text IS NOT NULL");
265XDB::rawExecute("INSERT IGNORE INTO profile_merge_issues (pid, issues)
a48b23c3 266 SELECT f.pid, 'address'
f8c2bf68 267 FROM fusionax_adresses AS f
a48b23c3 268 INNER JOIN profile_addresses AS pa ON (pa.pid = f.pid AND pa.type = 'home' AND pa.id = 0)
f8c2bf68
SJ
269 WHERE f.text IS NOT NULL");
270
2dc5748d
SJ
271XDB::rawExecute("INSERT INTO profile_addresses (pid, type, id, pub, text, flags)
272 SELECT f.pid, 'home', IF(pa.id IS NULL , 0, MAX(pa.id) + 1), 'ax', f.text, 'mail'
f8c2bf68 273 FROM fusionax_adresses AS f
a48b23c3 274 LEFT JOIN profile_addresses AS pa ON (pa.pid = f.pid AND pa.type = 'home')
f8c2bf68 275 WHERE f.text IS NOT NULL
a48b23c3 276 GROUP BY f.pid");
f8c2bf68 277XDB::rawExecute("INSERT INTO profile_phones (pid, link_type, tel_id, tel_type, display_tel, pub, link_id)
a48b23c3 278 SELECT f.pid, 'address', IF(pp.tel_id IS NULL, 0, MAX(pp.tel_id) + 1), 'fixed', f.tel, 'ax', pa.id
f8c2bf68 279 FROM fusionax_adresses AS f
a48b23c3
SJ
280 INNER JOIN profile_addresses AS pa ON (pa.pid = f.pid AND pa.type = 'home' AND f.text = pa.text)
281 LEFT JOIN profile_phones AS pp ON (pp.pid = f.pid AND pp.link_type = 'address' AND pp.link_id = pa.id)
325af12b 282 WHERE f.tel != ''
a48b23c3 283 GROUP BY f.pid");
f8c2bf68 284XDB::rawExecute("INSERT INTO profile_phones (pid, link_type, tel_id, tel_type, display_tel, pub, link_id)
a48b23c3 285 SELECT f.pid, 'address', IF(pp.tel_id IS NULL, 0, MAX(pp.tel_id) + 1), 'fax', f.fax, 'ax', pa.id
f8c2bf68 286 FROM fusionax_adresses AS f
a48b23c3
SJ
287 INNER JOIN profile_addresses AS pa ON (pa.pid = f.pid AND pa.type = 'home' AND f.text = pa.text)
288 LEFT JOIN profile_phones AS pp ON (pp.pid = f.pid AND pp.link_type = 'address' AND pp.link_id = pa.id)
325af12b 289 WHERE f.fax != ''
a48b23c3 290 GROUP BY f.pid");
f8c2bf68 291XDB::rawExecute('ALTER TABLE profile_addresses DROP INDEX text');
6b9e0135 292XDB::rawExecute('DROP TABLE IF EXISTS fusionax_adresses');
f8c2bf68 293echo "Addresses inclusions finished.\n";
6b9e0135 294
f8c2bf68
SJ
295// Retrieves education from AX database. This is the hardest part since AX only kept education as an unformated string.
296echo "Starts educations inclusions.\n";
0efb08e6
SJ
297// Updates master and doctorate educational fields.
298XDB::rawExecute("UPDATE profile_education AS e
299 INNER JOIN fusionax_formations_md AS f ON (f.pid = e.pid AND FIND_IN_SET('primary', e.flags))
300 SET e.program = f.field, e.fieldid = f.fieldid");
301XDB::rawExecute('DROP TABLE IF EXISTS fusionax_formations_md');
302
323813b3
SJ
303// Deletes empty educations.
304XDB::rawExecute("DELETE FROM fusionax_formations
305 WHERE Intitule_formation = '' AND Intitule_diplome = '' AND Descr_formation = ''");
325af12b 306// Insert ids into fusionax_formations to prevent many joins.
1dd21852
SJ
307XDB::rawExecute('ALTER TABLE profile_education_enum ADD INDEX (name(60))');
308XDB::rawExecute('ALTER TABLE profile_education_degree_enum ADD INDEX (abbreviation(60))');
325af12b
SJ
309XDB::rawExecute('UPDATE fusionax_formations AS f
310 LEFT JOIN profile_education_enum AS pe ON (pe.name = f.Intitule_formation)
311 LEFT JOIN profile_education_degree_enum AS pd ON (pd.abbreviation = f.Intitule_diplome)
312 LEFT JOIN profile_education_field_enum AS pf ON (pf.field = f.Descr_formation)
313 SET f.eduid = pe.id, f.degreeid = pd.id, f.fieldid = pf.id');
1dd21852
SJ
314XDB::rawExecute('ALTER TABLE profile_education_enum DROP INDEX name');
315XDB::rawExecute('ALTER TABLE profile_education_degree_enum DROP INDEX abbreviation');
325af12b
SJ
316// Updates non complete educations.
317XDB::rawExecute("UPDATE profile_education AS e
a48b23c3 318 INNER JOIN fusionax_formations AS f ON (f.pid = e.pid)
325af12b
SJ
319 INNER JOIN profile_education_degree_enum AS pd ON (e.degreeid = pd.id)
320 INNER JOIN profile_education_degree_enum AS fd ON (f.degreeid = fd.id)
321 SET e.eduid = f.eduid
322 WHERE NOT FIND_IN_SET('primary', e.flags) AND e.eduid IS NULL AND pd.level = fd.level");
323XDB::rawExecute("UPDATE profile_education AS e
a48b23c3 324 INNER JOIN fusionax_formations AS f ON (f.pid = e.pid)
325af12b
SJ
325 SET e.degreeid = f.degreeid
326 WHERE NOT FIND_IN_SET('primary', e.flags) AND e.degreeid IS NULL AND e.eduid = f.eduid");
327// Deletes duplicates.
328XDB::rawExecute("DELETE f
329 FROM fusionax_formations AS f
325af12b 330 INNER JOIN profile_education_degree_enum AS fd ON (fd.abbreviation = f.Intitule_diplome)
a48b23c3 331 INNER JOIN profile_education AS e ON (e.pid = f.pid AND NOT FIND_IN_SET('primary', e.flags))
325af12b
SJ
332 INNER JOIN profile_education_degree_enum AS pd ON (pd.id = e.degreeid)
333 WHERE f.eduid = e.eduid AND fd.level = pd.level");
334// Updates merge_issues table.
335XDB::rawExecute("UPDATE profile_merge_issues AS pm
a48b23c3 336 INNER JOIN fusionax_formations AS f ON (f.pid = pm.pid)
325af12b
SJ
337 SET pm.issues = IF(pm.issues, CONCAT(pm.issues, ',', 'education'), 'education')");
338XDB::rawExecute("INSERT IGNORE INTO profile_merge_issues (pid, issues)
a48b23c3
SJ
339 SELECT pid, 'education'
340 FROM fusionax_formations");
325af12b
SJ
341
342$id = 0;
343$continue = 1;
344while ($continue > 0) {
345 XDB::rawExecute("INSERT IGNORE INTO profile_education (id, pid, eduid, degreeid, fieldid, program)
a48b23c3
SJ
346 SELECT $id, pid, eduid, degreeid, fieldid, Descr_formation
347 FROM fusionax_formations");
325af12b
SJ
348 XDB::rawExecute("DELETE f
349 FROM fusionax_formations AS f
1dd21852
SJ
350 INNER JOIN profile_education AS pe ON (pe.pid = f.pid AND pe.id = $id)
351 WHERE (pe.eduid = f.eduid OR (pe.eduid IS NULL AND f.eduid IS NULL))
352 AND (pe.degreeid = f.degreeid OR (pe.degreeid IS NULL AND f.degreeid IS NULL))
353 AND (pe.fieldid = f.fieldid OR (pe.fieldid IS NULL AND f.fieldid IS NULL))
354 AND (pe.program = f.Descr_formation OR (pe.program IS NULL AND f.Descr_formation IS NULL))");
325af12b 355 $continue = XDB::affectedRows();
247b7728 356 ++$id;
6b9e0135 357}
325af12b
SJ
358// Updates merge_issues table (eduid and degreeid should never be empty).
359XDB::rawExecute("UPDATE profile_merge_issues AS pm
360 INNER JOIN profile_education AS pe ON (pe.pid = pm.pid)
361 SET pm.issues = CONCAT(pm.issues, ',', 'education')
362 WHERE NOT FIND_IN_SET('education', pm.issues) AND (pe.eduid = '' OR pe.eduid IS NULL OR pe.degreeid = '' OR pe.degreeid IS NULL)");
363XDB::rawExecute("INSERT IGNORE INTO profile_merge_issues (pid, issues)
364 SELECT pid, 'education'
365 FROM profile_education
366 WHERE eduid = '' OR eduid IS NULL OR degreeid = '' OR degreeid IS NULL");
367
6b9e0135 368XDB::rawExecute('DROP TABLE IF EXISTS fusionax_formations');
f8c2bf68
SJ
369echo "Educations inclusions finished.\n";
370
371echo "All inclusions are done.\n";
372
373XDB::commit();
6b9e0135
SJ
374
375/* vim:set et sw=4 sts=4 ts=4: */
376?>