2 /***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class FusionAxModule
extends PLModule
{
27 'fusionax' => $this->make_hook('index', AUTH_MDP
, 'admin'),
28 'fusionax/import' => $this->make_hook('import', AUTH_MDP
, 'admin'),
29 'fusionax/ids' => $this->make_hook('ids', AUTH_MDP
, 'admin'),
30 'fusionax/misc' => $this->make_hook('misc', AUTH_MDP
, 'admin'),
34 function handler_index(&$page)
37 $page->changeTpl('fusionax/index.tpl');
38 $page->assign('xorg_title','Polytechnique.org - Fusion des annuaires');
39 if (isset($globals->fusionax
) && isset($globals->fusionax
->LastUpdate
)) {
40 $page->assign('lastimport', date("d-m-Y",$globals->fusionax
->LastUpdate
));
44 /** Import de l'annuaire de l'AX depuis l'export situé sur leur serveur */
45 function handler_import(&$page, $action = 'index', $fileSQL = '')
47 if ($action == 'index') {
48 $page->changeTpl('fusionax/import.tpl');
49 $page->addJsLink('jquery.js');
51 if (isset($globals->fusionax
) && isset($globals->fusionax
->LastUpdate
)) {
52 $page->assign('lastimport', "le ".date("d/m/Y à H:i",$globals->fusionax
->LastUpdate
));
54 if (!file_exists(dirname(__FILE__
).'/../configs/ax_xorg_rsa')) {
55 $page->assign('keymissing', realpath(dirname(__FILE__
).'/../configs/').'/ax_xorg_rsa');
60 // toutes les actions sont faites en ajax en utilisant jquery
61 header("Content-type: text/javascript; charset=utf-8");
66 // création d'un fichier temporaire si nécessaire
67 if (Env
::has('tmpdir')) {
68 $tmpdir = Env
::v('tmpdir');
70 $tmpdir = tempnam('/tmp', 'fusionax');
74 // copie la clef d'authentification (paire de clef RSA dont la partie publique est sur polytechniciens.com)
75 if (!copy(dirname(__FILE__
).'/../configs/ax_xorg_rsa',$tmpdir.'/ax_xorg_rsa'))
76 $report[] = 'Impossible de copier la clef pour se logger sur le serveur AX';
77 chmod($tmpdir.'/ax_xorg_rsa', 0600);
80 $modulepath = realpath(dirname(__FILE__
).'/fusionax/').'/';
84 if ($action == 'launch') {
85 // lancement : connexion en ssh et récupération du fichier depuis polyechniciens.com
86 // décompression de l'archive et séparation en fichiers par tables
87 exec($modulepath.'import-ax.sh', $report);
88 $report[] = utf8_decode('Récupération du fichier terminé.');
89 $report[] = 'Import dans la base en cours...';
90 $next = 'integrateSQL';
91 } else if ($action == 'integrateSQL') {
92 // intégration des données dans la base MySQL
93 // liste des fichiers sql à exécuter
100 if ($fileSQL != '') {
101 // récupère le contenu du fichier sql
102 $queries = explode(';',file_get_contents($modulepath.$fileSQL));
103 foreach ($queries as $q) if (trim($q)) {
104 // coupe le fichier en requêtes individuelles
105 if (substr($q,0,2) == '--') {
106 // affiche les commentaires dans le report
107 $lines = explode("\n",$q);
109 $report[] = addslashes(utf8_decode($l));
111 // exécute la requête
114 // trouve le prochain fichier à exécuter
115 $trans = array_flip($filesSQL);
116 $nextfile = $trans[$fileSQL] +
1;
120 if (!isset($filesSQL[$nextfile])) {
121 // tous les fichiers ont été exécutés, on passe à l'étape suivante
124 // on passe au fichier suivant
125 $next = 'integrateSQL/'.$filesSQL[$nextfile];
127 } else if ($action == 'clean') {
128 // nettoyage du fichier temporaire
130 exec("rm -rf $tmpdir", $report);
131 $report[] = 'Fin de l\'import';
133 // met à jour la date de dernier import
134 $globals->change_dynamic_config(array('LastUpdate' => time()), 'FusionAx');
138 foreach($report as $t)
139 // affiche les lignes de report
140 echo "$('#fusionax_import').append('".utf8_encode($t)."<br/>');\n";
142 // lance le prochain script s'il y en a un
143 echo "$.getScript('fusionax/import/".$next."?tmpdir=".urlencode($tmpdir)."');";
145 // exit pour ne pas afficher la page template par défaut
149 private static function link_by_ids($user_id, $matricule_ax)
151 if (!XDB
::execute("UPDATE fusionax_import AS i INNER JOIN auth_user_md5 AS u
152 SET u.matricule_ax = i.id_ancien, i.user_id = u.user_id, i.date_match_id = NOW()
154 i.id_ancien = {?} AND u.user_id = {?} AND (
155 u.matricule_ax != {?} OR u.matricule_ax IS NULL OR
156 i.user_id != {?} OR i.user_id IS NULL)", $matricule_ax, $user_id, $matricule_ax, $user_id))
160 return XDB
::affectedRows() / 2;
163 private static function find_easy_to_link($limit = 10)
165 return XDB
::iterator("SELECT
166 xorg.prenom, xorg.nom, xorg.promo, xorg.user_id, ax.id_ancien,
167 CONCAT(ax.prenom,' ',ax.nom_complet,' (X ',ax.promotion_etude,')') AS nom_ax,
168 COUNT(*) AS nbMatches
169 FROM fusionax_anciens AS ax
170 INNER JOIN fusionax_import AS i ON (i.id_ancien = ax.id_ancien AND i.user_id IS NULL)
171 LEFT JOIN auth_user_md5 AS xorg ON (
172 xorg.matricule_ax IS NULL AND
173 (ax.Nom_complet = xorg.nom
174 OR ax.Nom_complet LIKE CONCAT(xorg.nom,' %')
175 OR ax.Nom_complet LIKE CONCAT(xorg.nom,'-%')
176 OR xorg.nom LIKE CONCAT('%-',ax.Nom_usuel)
177 OR ax.Nom_usuel = xorg.nom) AND
178 xorg.promo < ax.promotion_etude + 5 AND
179 xorg.promo > ax.promotion_etude - 5)
180 GROUP BY xorg.user_id
182 xorg.user_id IS NOT NULL AND
184 ".($limit?
('LIMIT '.$limit):''));
187 function handler_ids(&$page, $part = 'main', $user_id = null
, $matricule_ax = null
)
190 $globals->change_dynamic_config(array('LastUpdate' => time()), 'FusionAX');
192 $page->assign('xorg_title','Polytechnique.org - Fusion des annuaires - Mise en correspondance simple');
193 if ($part == 'missingInAX')
195 // locate all persons from this database that are not in AX's
196 $page->changeTpl('fusionax/idsMissingInAx.tpl');
197 $missingInAX = XDB
::iterator("SELECT *
198 FROM auth_user_md5 AS u
199 LEFT JOIN aliases AS a ON(a.id = u.user_id AND FIND_IN_SET('bestalias', a.flags))
200 WHERE u.matricule_ax IS NULL
202 $page->assign('missingInAX', $missingInAX);
205 if ($part == 'missingInXorg')
207 // locate all persons from AX's database that are not here
208 $page->changeTpl('fusionax/idsMissingInXorg.tpl');
209 $missingInXorg = XDB
::iterator("SELECT promotion_etude AS promo, prenom, Nom_usuel AS nom, id_ancien
211 INNER JOIN fusionax_anciens AS a USING (id_ancien)
212 WHERE fusionax_import.user_id IS NULL
214 $page->assign('missingInXorg', $missingInXorg);
219 FusionAxModule
::link_by_ids($user_id,$matricule_ax);
221 if ($part == 'linknext')
223 $linksToDo = FusionAxModule
::find_easy_to_link(10);
224 while ($l = $linksToDo->next())
226 FusionAxModule
::link_by_ids($l['user_id'],$l['id_ancien']);
229 if ($part == 'linkall')
231 $linksToDo = FusionAxModule
::find_easy_to_link(0);
232 while ($l = $linksToDo->next())
234 FusionAxModule
::link_by_ids($l['user_id'],$l['id_ancien']);
238 $page->changeTpl('fusionax/ids.tpl');
239 $missingInAX = XDB
::query("SELECT COUNT(*) FROM auth_user_md5 WHERE matricule_ax IS NULL");
242 $page->assign('nbMissingInAX', $missingInAX->fetchOneCell());
244 $missingInXorg = XDB
::query("SELECT COUNT(*) FROM fusionax_import WHERE user_id IS NULL");
247 $page->assign('nbMissingInXorg', $missingInXorg->fetchOneCell());
249 $easyToLink = FusionAxModule
::find_easy_to_link(10);
250 if ($easyToLink->total() > 0)
252 $page->assign('easyToLink', $easyToLink);
256 function handler_misc(&$page)
258 $page->changeTpl('fusionax/misc.tpl');
260 $deceasedErrorsSql = XDB
::query('SELECT COUNT(*) FROM fusionax_deceased');
261 $page->assign('deceasedErrors',$deceasedErrorsSql->fetchOneCell());
262 $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'));
263 $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'));
264 $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'));
267 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:?>