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