X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=modules%2Ffusionax.php;h=ff3e33d4418437de52ac186e4ea45078cf573017;hb=043bbacf7d5ceaeba4db582ac0ea7c75d6f13c80;hp=01c702121080393b3de7325efbd16dc0a639770c;hpb=cc8ea8b2cec0dcc0a1511794b572fc52fc9e0678;p=platal.git diff --git a/modules/fusionax.php b/modules/fusionax.php index 01c7021..ff3e33d 100644 --- a/modules/fusionax.php +++ b/modules/fusionax.php @@ -19,25 +19,48 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * ***************************************************************************/ +/** + * @brief Module to merge data from AX database + * + * Module to import data from another database of alumni that had + * different schemas. The organization that used this db is called AX + * hence the name of this module. + * + * Datas are stored in an external server and you need a private key + * to connect to their server. + */ class FusionAxModule extends PLModule{ + /// path ok private key file to connect to AX server + var $ax_xorg_rsa_key; + + function __construct() + { + $this->ax_xorg_rsa_key = + dirname(__FILE__).'/../configs/ax_xorg_rsa.pem'; + } + function handlers() { return array( 'fusionax' => $this->make_hook('index', AUTH_MDP, 'admin'), - 'fusionax/import' => $this->make_hook('import', AUTH_MDP, 'admin'), + 'fusionax/import' => $this->make_hook('import', AUTH_MDP,'admin'), 'fusionax/ids' => $this->make_hook('ids', AUTH_MDP, 'admin'), 'fusionax/misc' => $this->make_hook('misc', AUTH_MDP, 'admin'), ); } - + + function handler_index(&$page) { global $globals; $page->changeTpl('fusionax/index.tpl'); $page->assign('xorg_title','Polytechnique.org - Fusion des annuaires'); - if (isset($globals->fusionax) && isset($globals->fusionax->LastUpdate)) { - $page->assign('lastimport', date("d-m-Y",$globals->fusionax->LastUpdate)); + if (isset($globals->fusionax) && + isset($globals->fusionax->LastUpdate)) { + $page->assign( + 'lastimport', + date("d-m-Y",$globals->fusionax->LastUpdate)); } } @@ -48,11 +71,16 @@ class FusionAxModule extends PLModule{ $page->changeTpl('fusionax/import.tpl'); $page->addJsLink('jquery.js'); global $globals; - if (isset($globals->fusionax) && isset($globals->fusionax->LastUpdate)) { - $page->assign('lastimport', "le ".date("d/m/Y à H:i",$globals->fusionax->LastUpdate)); + if (isset($globals->fusionax) && + isset($globals->fusionax->LastUpdate)) { + $page->assign( + 'lastimport', + "le ".date("d/m/Y à H:i",$globals->fusionax->LastUpdate)); } - if (!file_exists(dirname(__FILE__).'/../configs/ax_xorg_rsa')) { - $page->assign('keymissing', realpath(dirname(__FILE__).'/../configs/').'/ax_xorg_rsa'); + if (!file_exists($this->ax_xorg_rsa_key)) { + $page->assign( + 'keymissing', + $this->ax_xorg_rsa_key); } return; } @@ -71,9 +99,13 @@ class FusionAxModule extends PLModule{ unlink($tmpdir); mkdir($tmpdir); chmod($tmpdir, 0700); - // copie la clef d'authentification (paire de clef RSA dont la partie publique est sur polytechniciens.com) - if (!copy(dirname(__FILE__).'/../configs/ax_xorg_rsa',$tmpdir.'/ax_xorg_rsa')) - $report[] = 'Impossible de copier la clef pour se logger sur le serveur AX'; + // copie la clef d'authentification (paire de clef RSA dont la + // partie publique est sur polytechniciens.com) + if (!copy( + $this->ax_xorg_rsa_key, + $tmpdir.'/ax_xorg_rsa')) + $report[] = 'Impossible de copier la clef pour se logger '. + 'au serveur AX'; chmod($tmpdir.'/ax_xorg_rsa', 0600); } @@ -82,8 +114,9 @@ class FusionAxModule extends PLModule{ chdir($tmpdir); if ($action == 'launch') { - // lancement : connexion en ssh et récupération du fichier depuis polyechniciens.com - // décompression de l'archive et séparation en fichiers par tables + // lancement : connexion en ssh et récupération du fichier depuis + // polyechniciens.com, décompression de l'archive et séparation en + // fichiers par tables exec($modulepath.'import-ax.sh', $report); $report[] = utf8_decode('Récupération du fichier terminé.'); $report[] = 'Import dans la base en cours...'; @@ -118,7 +151,8 @@ class FusionAxModule extends PLModule{ $nextfile = 0; } if (!isset($filesSQL[$nextfile])) { - // tous les fichiers ont été exécutés, on passe à l'étape suivante + // tous les fichiers ont été exécutés, on passe à l'étape + // suivante $next = 'clean'; } else { // on passe au fichier suivant @@ -131,7 +165,9 @@ class FusionAxModule extends PLModule{ $report[] = 'Fin de l\'import'; global $globals; // met à jour la date de dernier import - $globals->change_dynamic_config(array('LastUpdate' => time()), 'FusionAx'); + $globals->change_dynamic_config( + array('LastUpdate' => time()), + 'FusionAx'); } $tmpdir = getcwd(); chdir($olddir); @@ -140,63 +176,116 @@ class FusionAxModule extends PLModule{ echo "$('#fusionax_import').append('".utf8_encode($t)."
');\n"; if (isset($next)) { // lance le prochain script s'il y en a un - echo "$.getScript('fusionax/import/".$next."?tmpdir=".urlencode($tmpdir)."');"; + echo "$.getScript('fusionax/import/".$next."?tmpdir=". + urlencode($tmpdir)."');"; } // exit pour ne pas afficher la page template par défaut exit; } + /** Lier les identifiants d'un ancien dans les deux annuaires + * @param user_id identifiant dans l'annuaire X.org + * @param matricule_ax identifiant dans l'annuaire de l'AX + * @return 0 si la liaison a échoué, 1 sinon + */ private static function link_by_ids($user_id, $matricule_ax) { - if (!XDB::execute("UPDATE fusionax_import AS i INNER JOIN auth_user_md5 AS u - SET u.matricule_ax = i.id_ancien, i.user_id = u.user_id, i.date_match_id = NOW() + if (!XDB::execute(" + UPDATE fusionax_import AS i + INNER JOIN fusionax_xorg_anciens AS u + SET + u.matricule_ax = i.id_ancien, + i.user_id = u.user_id, + i.date_match_id = NOW() WHERE i.id_ancien = {?} AND u.user_id = {?} AND ( u.matricule_ax != {?} OR u.matricule_ax IS NULL OR - i.user_id != {?} OR i.user_id IS NULL)", $matricule_ax, $user_id, $matricule_ax, $user_id)) + i.user_id != {?} OR i.user_id IS NULL)", + $matricule_ax, + $user_id, + $matricule_ax, + $user_id)) { return 0; } return XDB::affectedRows() / 2; } - private static function find_easy_to_link($limit = 10) + /** Recherche automatique d'anciens à lier entre les deux annuaires + * @param limit nombre d'anciens à trouver au max + * @param sure si true, ne trouve que des anciens qui sont quasi sûrs + * @return un XOrgDBIterator sur les entrées avec display_name, promo, + * user_id, id_ancien et display_name_ax + */ + private static function find_easy_to_link($limit = 10, $sure = false) { - return XDB::iterator("SELECT - xorg.prenom, xorg.nom, xorg.promo, xorg.user_id, ax.id_ancien, - CONCAT(ax.prenom,' ',ax.nom_complet,' (X ',ax.promotion_etude,')') AS nom_ax, + $easy_to_link = XDB::iterator(" + SELECT + xorg.display_name, xorg.promo, xorg.user_id, ax.id_ancien, + CONCAT(ax.prenom,' ',ax.nom_complet,' (X ',ax.promotion_etude,')') + AS display_name_ax, COUNT(*) AS nbMatches - FROM fusionax_anciens AS ax - INNER JOIN fusionax_import AS i ON (i.id_ancien = ax.id_ancien AND i.user_id IS NULL) - LEFT JOIN auth_user_md5 AS xorg ON ( - xorg.matricule_ax IS NULL AND - (ax.Nom_complet = xorg.nom - OR ax.Nom_complet LIKE CONCAT(xorg.nom,' %') - OR ax.Nom_complet LIKE CONCAT(xorg.nom,'-%') - OR xorg.nom LIKE CONCAT('%-',ax.Nom_usuel) - OR ax.Nom_usuel = xorg.nom) AND - xorg.promo < ax.promotion_etude + 5 AND - xorg.promo > ax.promotion_etude - 5) - GROUP BY xorg.user_id - HAVING - xorg.user_id IS NOT NULL AND - nbMatches = 1 + FROM fusionax_anciens AS ax + INNER JOIN fusionax_import AS i ON ( + i.id_ancien = ax.id_ancien AND i.user_id IS NULL) + LEFT JOIN fusionax_xorg_anciens AS xorg ON ( + xorg.matricule_ax IS NULL AND + ax.Nom_complet = xorg.nom AND + ax.prenom = xorg.prenom AND + xorg.promo = ax.promotion_etude) + GROUP BY xorg.user_id + HAVING + xorg.user_id IS NOT NULL AND + nbMatches = 1 ".($limit?('LIMIT '.$limit):'')); + if ($easy_to_link->total() > 0 || $sure) { + return $easy_to_link; + } + return XDB::iterator(" + SELECT + xorg.display_name, xorg.promo, xorg.user_id, ax.id_ancien, + CONCAT(ax.prenom,' ',ax.nom_complet,' (X ',ax.promotion_etude,')') + AS display_name_ax, + COUNT(*) AS nbMatches + FROM fusionax_anciens AS ax + INNER JOIN fusionax_import AS i ON ( + i.id_ancien = ax.id_ancien AND i.user_id IS NULL) + LEFT JOIN fusionax_xorg_anciens AS xorg ON ( + xorg.matricule_ax IS NULL AND + (ax.Nom_complet = xorg.nom + OR ax.Nom_complet LIKE CONCAT(xorg.nom,' %') + OR ax.Nom_complet LIKE CONCAT(xorg.nom,'-%') + OR ax.Nom_usuel = xorg.nom + OR xorg.nom LIKE CONCAT('% ',ax.Nom_complet)) AND + xorg.promo < ax.promotion_etude + 2 AND + xorg.promo > ax.promotion_etude - 2) + GROUP BY xorg.user_id + HAVING + xorg.user_id IS NOT NULL AND + nbMatches = 1 + ".($limit?('LIMIT '.$limit):'')); } - function handler_ids(&$page, $part = 'main', $user_id = null, $matricule_ax = null) + /** Module de mise en correspondance les ids */ + function handler_ids( + &$page, + $part = 'main', + $user_id = null, + $matricule_ax = null) { global $globals; - $globals->change_dynamic_config(array('LastUpdate' => time()), 'FusionAX'); + $page->addJsLink('jquery.js'); - $page->assign('xorg_title','Polytechnique.org - Fusion des annuaires - Mise en correspondance simple'); + $page->assign( + 'xorg_title', + 'Polytechnique.org - Fusion - Mise en correspondance simple'); if ($part == 'missingInAX') { // locate all persons from this database that are not in AX's $page->changeTpl('fusionax/idsMissingInAx.tpl'); - $missingInAX = XDB::iterator("SELECT * - FROM auth_user_md5 AS u - LEFT JOIN aliases AS a ON(a.id = u.user_id AND FIND_IN_SET('bestalias', a.flags)) + $missingInAX = XDB::iterator("SELECT + u.promo, u.user_id, u.display_name + FROM fusionax_xorg_anciens AS u WHERE u.matricule_ax IS NULL LIMIT 20"); $page->assign('missingInAX', $missingInAX); @@ -206,7 +295,10 @@ class FusionAxModule extends PLModule{ { // locate all persons from AX's database that are not here $page->changeTpl('fusionax/idsMissingInXorg.tpl'); - $missingInXorg = XDB::iterator("SELECT promotion_etude AS promo, prenom, Nom_usuel AS nom, id_ancien + $missingInXorg = XDB::iterator("SELECT + a.promotion_etude AS promo, + CONCAT(a.prenom, ' ',a.Nom_usuel) AS display_name, + a.id_ancien FROM fusionax_import INNER JOIN fusionax_anciens AS a USING (id_ancien) WHERE fusionax_import.user_id IS NULL @@ -217,6 +309,7 @@ class FusionAxModule extends PLModule{ if ($part == 'link') { FusionAxModule::link_by_ids($user_id,$matricule_ax); + exit; } if ($part == 'linknext') { @@ -225,6 +318,7 @@ class FusionAxModule extends PLModule{ { FusionAxModule::link_by_ids($l['user_id'],$l['id_ancien']); } + pl_redirect('fusionax/ids#autolink'); } if ($part == 'linkall') { @@ -236,15 +330,23 @@ class FusionAxModule extends PLModule{ } { $page->changeTpl('fusionax/ids.tpl'); - $missingInAX = XDB::query("SELECT COUNT(*) FROM auth_user_md5 WHERE matricule_ax IS NULL"); + $missingInAX = XDB::query( + 'SELECT COUNT(*) + FROM fusionax_xorg_anciens AS u + WHERE u.matricule_ax IS NULL'); if ($missingInAX) { $page->assign('nbMissingInAX', $missingInAX->fetchOneCell()); } - $missingInXorg = XDB::query("SELECT COUNT(*) FROM fusionax_import WHERE user_id IS NULL"); + $missingInXorg = XDB::query( + 'SELECT COUNT(*) + FROM fusionax_import AS i + WHERE i.user_id IS NULL'); if ($missingInXorg) { - $page->assign('nbMissingInXorg', $missingInXorg->fetchOneCell()); + $page->assign( + 'nbMissingInXorg', + $missingInXorg->fetchOneCell()); } $easyToLink = FusionAxModule::find_easy_to_link(10); if ($easyToLink->total() > 0) @@ -253,15 +355,37 @@ class FusionAxModule extends PLModule{ } } } + function handler_misc(&$page) { $page->changeTpl('fusionax/misc.tpl'); // deceased - $deceasedErrorsSql = XDB::query('SELECT COUNT(*) FROM fusionax_deceased'); + $deceasedErrorsSql = XDB::query( + 'SELECT COUNT(*) FROM fusionax_deceased'); $page->assign('deceasedErrors',$deceasedErrorsSql->fetchOneCell()); - $page->assign('deceasedMissingInXorg',XDB::iterator('SELECT user_id,id_ancien,nom,prenom,promo,Date_décès FROM fusionax_deceased WHERE deces = "0000-00-00" LIMIT 10')); - $page->assign('deceasedMissingInAX',XDB::iterator('SELECT user_id,id_ancien,nom,prenom,promo,deces FROM fusionax_deceased WHERE Date_décès = "0000-00-00" LIMIT 10')); - $page->assign('deceasedDifferent',XDB::iterator('SELECT user_id,id_ancien,nom,prenom,promo,Date_décès,deces FROM fusionax_deceased WHERE deces != "0000-00-00" AND Date_décès != "0000-00-00" LIMIT 10')); + $page->assign('deceasedMissingInXorg',XDB::iterator( + 'SELECT + d.user_id,d.id_ancien,d.nom,d.prenom,d.promo,d.deces_ax, + CONCAT(d.prenom, " ", d.nom) AS display_name + FROM fusionax_deceased AS d + WHERE d.deces_xorg = "0000-00-00" + LIMIT 10')); + $page->assign('deceasedMissingInAX',XDB::iterator( + 'SELECT + d.user_id,d.id_ancien,d.nom,d.prenom,d.promo,d.deces_xorg, + CONCAT(d.prenom, " ", d.nom) AS display_name + FROM fusionax_deceased AD d + WHERE d.deces_ax = "0000-00-00" + LIMIT 10')); + $page->assign('deceasedDifferent',XDB::iterator( + 'SELECT + d.user_id,d.id_ancien,d.nom,d.prenom,d.promo, + d.deces_ax,d.deces_xorg, + CONCAT(d.prenom, " ", d.nom) AS display_name + FROM fusionax_deceased AS d + WHERE d.deces_xorg != "0000-00-00" AND d.deces_ax != "0000-00-00" + LIMIT 10')); } } + // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:?>