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