Merge remote branch 'origin/platal-1.0.0'
[platal.git] / modules / fusionax.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 /**
23 * Module to merge data from AX database: this will only be used once on
24 * production site and should be removed afterwards.
25 *
26 * Module to import data from another database of alumni that had
27 * different schemas. The organization that used this db is called AX
28 * hence the name of this module.
29 *
30 * Datas are stored in an export file.
31 */
32 class FusionAxModule extends PLModule
33 {
34 function handlers()
35 {
36 return array(
37 'fusionax' => $this->make_hook('index', AUTH_MDP, 'admin'),
38 'fusionax/import' => $this->make_hook('import', AUTH_MDP, 'admin'),
39 'fusionax/view' => $this->make_hook('view', AUTH_MDP, 'admin'),
40 'fusionax/ids' => $this->make_hook('ids', AUTH_MDP, 'admin'),
41 'fusionax/deceased' => $this->make_hook('deceased', AUTH_MDP, 'admin'),
42 'fusionax/promo' => $this->make_hook('promo', AUTH_MDP, 'admin'),
43 );
44 }
45
46
47 function handler_index(&$page)
48 {
49 $page->changeTpl('fusionax/index.tpl');
50 $page->assign('xorg_title', 'Polytechnique.org - Fusion des annuaires');
51 }
52
53 /** Import de l'annuaire de l'AX depuis l'export situé dans le home de jacou */
54 function handler_import(&$page, $action = 'index', $file = '')
55 {
56 if ($action == 'index') {
57 $page->changeTpl('fusionax/import.tpl');
58 return;
59 }
60
61 // toutes les actions sont faites en ajax en utilisant jquery
62 header('Content-type: text/javascript; charset=utf-8');
63
64 // log des actions
65 $report = array();
66
67 $modulepath = realpath(dirname(__FILE__) . '/fusionax/') . '/';
68 $spoolpath = realpath(dirname(__FILE__) . '/../spool/fusionax/') . '/';
69
70 if ($action == 'launch') {
71 if ($file == '') {
72 $report[] = 'Nom de fichier non renseigné.';
73 } elseif (!file_exists(dirname(__FILE__) . '/../spool/fusionax/' . $file)) {
74 $report[] = 'Le fichier ne se situe pas au bon endroit.';
75 } else {
76 // séparation de l'archive en fichiers par tables
77 $file = $spoolpath . $file;
78 // Removes master and doctorate students
79 exec('grep -v "^[A-Z]\{2\}.[0-9]\{4\}[MD][0-9]\{3\}" ' . $file . ' > ' . $file . '.tmp');
80 exec('mv -f ' . $file . '.tmp ' . $file);
81 // Split export into specialised files
82 exec('grep "^AD" ' . $file . ' > ' . $spoolpath . 'Adresses.txt');
83 exec('grep "^AN" ' . $file . ' > ' . $spoolpath . 'Anciens.txt');
84 exec('grep "^FO" ' . $file . ' > ' . $spoolpath . 'Formations.txt');
85 exec('grep "^AC" ' . $file . ' > ' . $spoolpath . 'Activites.txt');
86 exec('grep "^EN" ' . $file . ' > ' . $spoolpath . 'Entreprises.txt');
87 exec($modulepath . 'formation.pl');
88 exec('mv -f ' . $spoolpath . 'Formations_out.txt ' . $spoolpath . 'Formations.txt');
89 $report[] = 'Fichier parsé.';
90 $report[] = 'Import dans la base en cours...';
91 $next = 'integrateSQL';
92 }
93 } elseif ($action == 'integrateSQL') {
94 // intégration des données dans la base MySQL
95 // liste des fichiers sql à exécuter
96 $filesSQL = array(
97 0 => 'Activites.sql',
98 1 => 'Adresses.sql',
99 2 => 'Anciens.sql',
100 3 => 'Formations.sql',
101 4 => 'Entreprises.sql'
102 );
103 if ($file != '') {
104 // récupère le contenu du fichier sql
105 $queries = explode(';', file_get_contents($modulepath . $filesSQL[$file]));
106 foreach ($queries as $q) {
107 if (trim($q)) {
108 // coupe le fichier en requêtes individuelles
109 if (substr($q, 0, 2) == '--') {
110 // affiche les commentaires dans le report
111 $lines = explode("\n", $q);
112 $l = $lines[0];
113 $report[] = addslashes($l);
114 }
115 // exécute la requête
116 XDB::execute(str_replace('{?}', $spoolpath, $q));
117 }
118 }
119 // trouve le prochain fichier à exécuter
120 $nextfile = $file + 1;
121 } else {
122 $nextfile = 0;
123 }
124 if ($nextfile > 4) {
125 // tous les fichiers ont été exécutés, on passe à l'étape suivante
126 $next = 'adds1920';
127 } else {
128 // on passe au fichier suivant
129 $next = 'integrateSQL/' . $nextfile;
130 }
131 } elseif ($action == 'adds1920') {
132 // Adds promotion 1920 from AX db.
133 $report[] = 'Ajout de la promotion 1920';
134 $res = XDB::iterator('SELECT prenom, Nom_complet, ax_id
135 FROM fusionax_anciens
136 WHERE promotion_etude = 1920;');
137
138 $nameTypes = DirEnum::getOptions(DirEnum::NAMETYPES);
139 $nameTypes = array_flip($nameTypes);
140 $eduSchools = DirEnum::getOptions(DirEnum::EDUSCHOOLS);
141 $eduSchools = array_flip($eduSchools);
142 $eduDegrees = DirEnum::getOptions(DirEnum::EDUDEGREES);
143 $eduDegrees = array_flip($eduDegrees);
144 $degreeid = $eduDegrees[Profile::DEGREE_X];
145 $entry_year = 1920;
146 $grad_year = 1923;
147 $promo = 'X1920';
148 $sex = PlUser::GENDER_MALE;
149 $xorgId = 19200000;
150 $type = 'x';
151
152 while (list($firstname, $lastname, $ax_id) = $res->next()) {
153 $hrid = self::getHrid($firstname, $lastname, $promo);
154 $res1 = XDB::query('SELECT COUNT(*)
155 FROM accounts
156 WHERE hruid = {?}', $hrid);
157 $res2 = XDB::query('SELECT COUNT(*)
158 FROM profiles
159 WHERE hrpid = {?}', $hrid);
160 if (is_null($hrid) || $res1->fetchOneCell() > 0 || $res2->fetchOneCell() > 0) {
161 $report[] = $ax_id . ' non ajouté';
162 }
163 $fullName = $firstname . ' ' . $lastname;
164 $directoryName = $lastname . ' ' . $firstname;
165 ++$xorgId;
166
167 XDB::execute('REPLACE INTO profiles (hrpid, xorg_id, ax_id, sex)
168 VALUES ({?}, {?}, {?}, {?})',
169 $hrid, $xorgId, $ax_id, $sex);
170 $pid = XDB::insertId();
171 XDB::execute('REPLACE INTO profile_name (pid, name, typeid)
172 VALUES ({?}, {?}, {?})',
173 $pid, $lastname, $nameTypes['name_ini']);
174 XDB::execute('REPLACE INTO profile_name (pid, name, typeid)
175 VALUES ({?}, {?}, {?})',
176 $pid, $firstname, $nameTypes['firstname_ini']);
177 XDB::execute('REPLACE INTO profile_display (pid, yourself, public_name, private_name,
178 directory_name, short_name, sort_name, promo)
179 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
180 $pid, $firstname, $fullName, $fullName, $directoryName, $fullName, $directoryName, $promo);
181 XDB::execute('REPLACE INTO profile_education (pid, eduid, degreeid, entry_year, grad_year, flags)
182 VALUES ({?}, {?}, {?}, {?}, {?}, {?})',
183 $pid, $eduSchools[Profile::EDU_X], $degreeid, $entry_year, $grad_year, 'primary');
184 XDB::execute('REPLACE INTO accounts (hruid, type, is_admin, state, full_name, directory_name, display_name, sex)
185 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})',
186 $hrid, $type, 0, 'active', $fullName, $directoryName, $lastname, $sex);
187 $uid = XDB::insertId();
188 XDB::execute('REPLACE INTO account_profiles (uid, pid, perms)
189 VALUES ({?}, {?}, {?})',
190 $uid, $pid, 'owner');
191 }
192 $report[] = 'Promo 1920 ajoutée.';
193 $next = 'view';
194 } elseif ($action == 'view') {
195 XDB::execute('CREATE OR REPLACE ALGORITHM=MERGE VIEW fusionax_xorg_anciens AS
196 SELECT p.pid, p.ax_id, pd.promo, pd.private_name, pd.public_name,
197 pd.sort_name, pd.short_name, pd.directory_name
198 FROM profiles AS p
199 INNER JOIN profile_display AS pd USING(pid)');
200 $next = 'clean';
201 } elseif ($action == 'clean') {
202 // nettoyage du fichier temporaire
203 exec('rm -Rf ' . $spoolpath);
204 $report[] = 'Import finit.';
205 }
206 foreach($report as $t) {
207 // affiche les lignes de report
208 echo "$('#fusionax').append('" . $t . "<br/>');\n";
209 }
210 if (isset($next)) {
211 // lance le prochain script s'il y en a un
212 echo "$.getScript('fusionax/import/" . $next . "');";
213 }
214 // exit pour ne pas afficher la page template par défaut
215 exit;
216 }
217
218 function handler_view(&$page, $action = '')
219 {
220 $page->changeTpl('fusionax/view.tpl');
221 if ($action == 'create') {
222 XDB::execute('DROP VIEW IF EXISTS fusionax_deceased');
223 XDB::execute('CREATE VIEW fusionax_deceased AS
224 SELECT p.pid, a.ax_id, pd.private_name, pd.promo, p.deathdate AS deces_xorg, a.Date_deces AS deces_ax
225 FROM profiles AS p
226 INNER JOIN profile_display AS pd ON (p.pid = pd.pid)
227 INNER JOIN fusionax_anciens AS a ON (a.ax_id = p.ax_id)
228 WHERE p.deathdate != a.Date_deces');
229 XDB::execute('DROP VIEW IF EXISTS fusionax_promo');
230 XDB::execute('CREATE VIEW fusionax_promo AS
231 SELECT p.pid, p.ax_id, pd.private_name, pd.promo, pe.entry_year AS promo_etude_xorg,
232 f.promotion_etude AS promo_etude_ax, pe.grad_year AS promo_sortie_xorg
233 FROM profiles AS p
234 INNER JOIN profile_display AS pd ON (p.pid = pd.pid)
235 INNER JOIN profile_education AS pe ON (p.pid = pe.pid)
236 INNER JOIN fusionax_anciens AS f ON (p.ax_id = f.ax_id)
237 WHERE pd.promo != CONCAT(\'X\', f.promotion_etude)
238 AND !(f.promotion_etude = pe.entry_year + 1 AND pe.grad_year = pe.entry_year + 4)');
239 $page->trigSuccess('Les VIEW ont bien été créées.');
240 }
241 }
242
243 /* Mets à NULL le matricule_ax de ces camarades pour marquer le fait qu'ils ne figurent pas dans l'annuaire de l'AX */
244 private static function clear_wrong_in_xorg($pid)
245 {
246 $res = XDB::execute('UPDATE fusionax_xorg_anciens
247 SET ax_id = NULL
248 WHERE pid = {?}', $pid);
249 if (!$res) {
250 return 0;
251 }
252 return XDB::affectedRows() / 2;
253 }
254
255 /* Cherche les les anciens présents dans Xorg avec un matricule_ax ne correspondant à rien dans la base de l'AX
256 * (mises à part les promo 1921 et 1923 qui ne figurent pas dans les données de l'AX)*/
257 private static function find_wrong_in_xorg($limit = 10)
258 {
259 return XDB::iterator('SELECT u.promo, u.pid, u.private_name
260 FROM fusionax_xorg_anciens AS u
261 WHERE NOT EXISTS (SELECT *
262 FROM fusionax_anciens AS f
263 WHERE f.ax_id = u.ax_id)
264 AND u.ax_id IS NOT NULL AND promo != \'X1921\' AND promo != \'X1923\'');
265 }
266
267 /** Lier les identifiants d'un ancien dans les deux annuaires
268 * @param user_id identifiant dans l'annuaire X.org
269 * @param matricule_ax identifiant dans l'annuaire de l'AX
270 * @return 0 si la liaison a échoué, 1 sinon
271 */
272 private static function link_by_ids($pid, $ax_id)
273 {
274 $res = XDB::execute('UPDATE fusionax_import AS i
275 INNER JOIN fusionax_xorg_anciens AS u
276 SET u.ax_id = i.ax_id,
277 i.pid = u.pid,
278 i.date_match_id = NOW()
279 WHERE i.ax_id = {?} AND u.pid = {?}
280 AND (u.ax_id != {?} OR u.ax_id IS NULL
281 OR i.pid != {?} OR i.pid IS NULL)',
282 $ax_id, $pid, $ax_id, $pid);
283 if (!$res) {
284 return 0;
285 }
286 return XDB::affectedRows() / 2;
287 }
288
289 /** Recherche automatique d'anciens à lier entre les deux annuaires
290 * @param limit nombre d'anciens à trouver au max
291 * @param sure si true, ne trouve que des anciens qui sont quasi sûrs
292 * @return un XOrgDBIterator sur les entrées avec display_name, promo,
293 * pid, ax_id et display_name_ax
294 */
295 private static function find_easy_to_link($limit = 10, $sure = false)
296 {
297 $easy_to_link = XDB::iterator("
298 SELECT u.private_name, u.promo, u.pid, ax.ax_id,
299 CONCAT(ax.prenom, ' ', ax.nom_complet, ' (X', ax.promotion_etude, ')') AS display_name_ax,
300 COUNT(*) AS nbMatches
301 FROM fusionax_anciens AS ax
302 INNER JOIN fusionax_import AS i ON (i.ax_id = ax.ax_id AND i.pid IS NULL)
303 LEFT JOIN fusionax_xorg_anciens AS u ON (u.ax_id IS NULL
304 AND u.promo = CONCAT('X', ax.promotion_etude)
305 AND (CONCAT(ax.prenom, ' ', ax.nom_complet) = u.private_name
306 OR CONCAT(ax.prenom, ' ', ax.nom_complet) = u.public_name
307 OR CONCAT(ax.prenom, ' ', ax.nom_complet) = u.short_name))
308 GROUP BY u.pid
309 HAVING u.pid IS NOT NULL AND nbMatches = 1" . ($limit ? (' LIMIT ' . $limit) : ''));
310 if ($easy_to_link->total() > 0 || $sure) {
311 return $easy_to_link;
312 }
313 return XDB::iterator("
314 SELECT u.private_name, u.promo, u.pid, ax.ax_id,
315 CONCAT(ax.prenom, ' ', ax.nom_complet, ' (X', ax.promotion_etude, ')') AS display_name_ax,
316 COUNT(*) AS nbMatches
317 FROM fusionax_anciens AS ax
318 INNER JOIN fusionax_import AS i ON (i.ax_id = ax.ax_id AND i.pid IS NULL)
319 LEFT JOIN fusionax_xorg_anciens AS u ON (u.ax_id IS NULL
320 AND (CONCAT(ax.prenom, ' ', ax.nom_complet) = u.private_name
321 OR CONCAT(ax.prenom, ' ', ax.nom_complet) = u.public_name
322 OR CONCAT(ax.prenom, ' ', ax.nom_complet) = u.short_name)
323 AND u.promo < CONCAT('X', ax.promotion_etude + 2)
324 AND u.promo > CONCAT('X', ax.promotion_etude - 2))
325 GROUP BY u.pid
326 HAVING u.pid IS NOT NULL AND nbMatches = 1" . ($limit ? (' LIMIT ' . $limit) : ''));
327 }
328
329 /** Module de mise en correspondance les ids */
330 function handler_ids(&$page, $part = 'main', $pid = null, $ax_id = null)
331 {
332 $nbToLink = 100;
333 $page->assign('xorg_title', 'Polytechnique.org - Fusion - Mise en correspondance simple');
334
335 if ($part == 'missingInAX') {
336 // locate all persons from this database that are not in AX's
337 $page->changeTpl('fusionax/idsMissingInAx.tpl');
338 $missingInAX = XDB::iterator('SELECT promo, pid, private_name
339 FROM fusionax_xorg_anciens
340 WHERE ax_id IS NULL');
341 $page->assign('missingInAX', $missingInAX);
342 return;
343 }
344 if ($part == 'missingInXorg') {
345 // locate all persons from AX's database that are not here
346 $page->changeTpl('fusionax/idsMissingInXorg.tpl');
347 $missingInXorg = XDB::iterator("SELECT CONCAT(a.prenom, ' ', a.Nom_usuel) AS private_name,
348 a.promotion_etude AS promo, a.ax_id
349 FROM fusionax_import
350 INNER JOIN fusionax_anciens AS a USING (ax_id)
351 WHERE fusionax_import.pid IS NULL");
352 $page->assign('missingInXorg', $missingInXorg);
353 return;
354 }
355 if ($part == 'wrongInXorg') {
356 // locate all persons from Xorg database that have a bad AX id
357 $page->changeTpl('fusionax/idswrongInXorg.tpl');
358 $wrongInXorg = FusionAxModule::find_wrong_in_xorg($nbToLink);
359 $page->assign('wrongInXorg', $wrongInXorg);
360 return;
361 }
362 if ($part == 'cleanwronginxorg') {
363 $linksToDo = FusionAxModule::find_wrong_in_xorg($nbToLink);
364 while ($l = $linksToDo->next()) {
365 FusionAxModule::clear_wrong_in_xorg($l['pid']);
366 }
367 pl_redirect('fusionax/ids/wrongInXorg');
368 }
369 if ($part == 'lier') {
370 if (Post::has('user_id') && Post::has('matricule_ax')) {
371 FusionAxModule::link_by_ids(Post::i('pid'), Post::v('ax_id'));
372 }
373 }
374 if ($part == 'link') {
375 FusionAxModule::link_by_ids($pid, $ax_id);
376 exit;
377 }
378 if ($part == 'linknext') {
379 $linksToDo = FusionAxModule::find_easy_to_link($nbToLink);
380 while ($l = $linksToDo->next()) {
381 FusionAxModule::link_by_ids($l['pid'], $l['ax_id']);
382 }
383 pl_redirect('fusionax/ids#autolink');
384 }
385 if ($part == 'linkall') {
386 $linksToDo = FusionAxModule::find_easy_to_link(0);
387 while ($l = $linksToDo->next()) {
388 FusionAxModule::link_by_ids($l['pid'], $l['ax_id']);
389 }
390 }
391 {
392 $page->changeTpl('fusionax/ids.tpl');
393 $missingInAX = XDB::query('SELECT COUNT(*)
394 FROM fusionax_xorg_anciens
395 WHERE ax_id IS NULL');
396 if ($missingInAX) {
397 $page->assign('nbMissingInAX', $missingInAX->fetchOneCell());
398 }
399 $missingInXorg = XDB::query('SELECT COUNT(*)
400 FROM fusionax_import
401 WHERE pid IS NULL');
402 if ($missingInXorg) {
403 $page->assign('nbMissingInXorg', $missingInXorg->fetchOneCell());
404 }
405 $wrongInXorg = FusionAxModule::find_wrong_in_xorg($nbToLink);
406 if ($wrongInXorg->total() > 0) {
407 $page->assign('wrongInXorg', $wrongInXorg->total());
408 }
409 $easyToLink = FusionAxModule::find_easy_to_link($nbToLink);
410 if ($easyToLink->total() > 0) {
411 $page->assign('nbMatch', $easyToLink->total());
412 $page->assign('easyToLink', $easyToLink);
413 }
414 }
415 }
416
417 function handler_deceased(&$page, $action = '')
418 {
419 if ($action == 'updateXorg') {
420 XDB::execute('UPDATE fusionax_deceased
421 SET deces_xorg = deces_ax
422 WHERE deces_xorg = "0000-00-00"');
423 }
424 if ($action == 'updateAX') {
425 XDB::execute('UPDATE fusionax_deceased
426 SET deces_ax = deces_xorg
427 WHERE deces_ax = "0000-00-00"');
428 }
429 if ($action == 'update') {
430 if (Post::has('pid') && Post::has('date')) {
431 XDB::execute('UPDATE fusionax_deceased
432 SET deces_ax = {?}, deces_xorg = {?}
433 WHERE pid = {?}',
434 Post::v('date'), Post::v('date'), Post::i('pid'));
435 }
436 }
437 $page->changeTpl('fusionax/deceased.tpl');
438 // deceased
439 $deceasedErrorsSql = XDB::query('SELECT COUNT(*) FROM fusionax_deceased');
440 $page->assign('deceasedErrors', $deceasedErrorsSql->fetchOneCell());
441 $res = XDB::iterator('SELECT pid, ax_id, promo, private_name, deces_ax
442 FROM fusionax_deceased
443 WHERE deces_xorg = "0000-00-00"
444 LIMIT 10');
445 $page->assign('nbDeceasedMissingInXorg', $res->total());
446 $page->assign('deceasedMissingInXorg', $res);
447 $res = XDB::iterator('SELECT pid, ax_id, promo, private_name, deces_xorg
448 FROM fusionax_deceased
449 WHERE deces_ax = "0000-00-00"
450 LIMIT 10');
451 $page->assign('nbDeceasedMissingInAX', $res->total());
452 $page->assign('deceasedMissingInAX', $res);
453 $res = XDB::iterator('SELECT pid, ax_id, promo, private_name, deces_xorg, deces_ax
454 FROM fusionax_deceased
455 WHERE deces_xorg != "0000-00-00" AND deces_ax != "0000-00-00"');
456 $page->assign('nbDeceasedDifferent', $res->total());
457 $page->assign('deceasedDifferent', $res);
458 }
459
460 function handler_promo(&$page, $action = '')
461 {
462 $page->changeTpl('fusionax/promo.tpl');
463 $res = XDB::iterator('SELECT pid, private_name, promo_etude_xorg, promo_sortie_xorg, promo_etude_ax, promo
464 FROM fusionax_promo
465 WHERE !(promo_etude_ax + 1 = promo_etude_xorg AND promo_etude_xorg + 3 = promo_sortie_xorg)');
466 $nbMissmatchingPromos = $res->total();
467 $page->assign('nbMissmatchingPromos1', $res->total());
468 $page->assign('missmatchingPromos1', $res);
469 $res = XDB::iterator('SELECT pid, private_name, promo_etude_xorg, promo_sortie_xorg, promo_etude_ax, promo
470 FROM fusionax_promo
471 WHERE promo_etude_ax + 1 = promo_etude_xorg AND promo_etude_xorg + 3 = promo_sortie_xorg');
472 $nbMissmatchingPromos += $res->total();
473 $page->assign('nbMissmatchingPromos2', $res->total());
474 $page->assign('missmatchingPromos2', $res);
475 $page->assign('nbMissmatchingPromos', $nbMissmatchingPromos);
476 }
477 }
478
479 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:?>