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