Commit | Line | Data |
---|---|---|
b9ad0878 PC |
1 | <?php |
2 | /*************************************************************************** | |
3 | * Copyright (C) 2003-2007 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 | class FusionAxModule extends PLModule{ | |
23 | ||
24 | function handlers() | |
25 | { | |
26 | return array( | |
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'), | |
31 | ); | |
32 | } | |
33 | ||
34 | function handler_index(&$page) | |
35 | { | |
36 | global $globals; | |
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)); | |
41 | } | |
42 | } | |
43 | ||
44 | function handler_import(&$page, $action = 'index', $fileSQL = '') | |
45 | { | |
46 | if ($action == 'index') { | |
47 | $page->changeTpl('fusionax/import.tpl'); | |
48 | $page->addJsLink('jquery.js'); | |
49 | global $globals; | |
50 | if (isset($globals->fusionax) && isset($globals->fusionax->LastUpdate)) { | |
51 | $page->assign('lastimport', "le ".date("d/m/Y à H:i",$globals->fusionax->LastUpdate)); | |
52 | } | |
53 | if (!file_exists(dirname(__FILE__).'/../configs/ax_xorg_rsa')) { | |
54 | $page->assign('keymissing', realpath(dirname(__FILE__).'/../configs/').'/ax_xorg_rsa'); | |
55 | } | |
56 | return; | |
57 | } | |
58 | $report = array(); | |
59 | header("Content-type: text/javascript; charset=utf-8"); | |
60 | if (Env::has('tmpdir')) { | |
61 | $tmpdir = Env::v('tmpdir'); | |
62 | } else { | |
63 | exec('rm /tmp/fusionax* -rf'); | |
64 | $tmpdir = tempnam('/tmp', 'fusionax'); | |
65 | unlink($tmpdir); | |
66 | mkdir($tmpdir); | |
67 | chmod($tmpdir, 0777); | |
68 | if (!copy(dirname(__FILE__).'/../configs/ax_xorg_rsa',$tmpdir.'/ax_xorg_rsa')) | |
69 | $report[] = 'Impossible de copier la clef pour se logger sur le serveur AX'; | |
70 | chmod($tmpdir.'/ax_xorg_rsa', 0600); | |
71 | } | |
72 | $modulepath = realpath(dirname(__FILE__).'/fusionax/').'/'; | |
73 | $olddir = getcwd(); | |
74 | chdir($tmpdir); | |
75 | if ($action == 'launch') { | |
76 | exec($modulepath.'import-ax.sh', $report); | |
77 | $report[] = utf8_decode('Récupération du fichier terminé.'); | |
78 | $report[] = 'Import dans la base en cours...'; | |
79 | $next = 'integrateSQL'; | |
80 | } else if ($action == 'integrateSQL') { | |
81 | $filesSQL = array('Activites.sql', 'Adresses.sql', 'Anciens.sql', 'Formations.sql', 'Entreprises.sql'); | |
82 | if ($fileSQL != '') { | |
83 | $trans = array_flip($filesSQL); | |
84 | $nextfile = $trans[$fileSQL] + 1; | |
85 | $queries = explode(';',file_get_contents($modulepath.$fileSQL)); | |
86 | foreach ($queries as $q) if (trim($q)) { | |
87 | if (substr($q,0,2) == '--') { | |
88 | $lines = explode("\n",$q); | |
89 | $l = $lines[0]; | |
90 | $report[] = addslashes(utf8_decode($l)); | |
91 | } | |
92 | XDB::execute($q); | |
93 | } | |
94 | } else { | |
95 | $nextfile = 0; | |
96 | } | |
97 | if (!isset($filesSQL[$nextfile])) { | |
98 | $next = 'clean'; | |
99 | } else { | |
100 | $next = 'integrateSQL/'.$filesSQL[$nextfile]; | |
101 | } | |
102 | } else if ($action == 'clean') { | |
103 | chdir($olddir); | |
104 | exec("rm -rf $tmpdir", $report); | |
105 | $report[] = 'Fin de l\'import'; | |
106 | global $globals; | |
107 | $globals->change_dynamic_config(array('LastUpdate' => time()), 'FusionAx'); | |
108 | } | |
109 | $tmpdir = getcwd(); | |
110 | chdir($olddir); | |
111 | foreach($report as $t) | |
112 | echo "$('#fusionax_import').append('".utf8_encode($t)."<br/>');\n"; | |
113 | if (isset($next)) { | |
114 | echo "$.getScript('fusionax/import/".$next."?tmpdir=".urlencode($tmpdir)."');"; | |
115 | } | |
116 | exit; | |
117 | } | |
118 | ||
119 | private static function link_by_ids($user_id, $matricule_ax) | |
120 | { | |
121 | if (!XDB::execute("UPDATE fusionax_import AS i INNER JOIN auth_user_md5 AS u | |
122 | SET u.matricule_ax = i.id_ancien, i.user_id = u.user_id, i.date_match_id = NOW() | |
123 | WHERE | |
124 | i.id_ancien = {?} AND u.user_id = {?} AND ( | |
125 | u.matricule_ax != {?} OR u.matricule_ax IS NULL OR | |
126 | i.user_id != {?} OR i.user_id IS NULL)", $matricule_ax, $user_id, $matricule_ax, $user_id)) | |
127 | { | |
128 | return 0; | |
129 | } | |
130 | return XDB::affectedRows() / 2; | |
131 | } | |
132 | ||
133 | private static function find_easy_to_link($limit = 10) | |
134 | { | |
135 | return XDB::iterator("SELECT | |
136 | xorg.prenom, xorg.nom, xorg.promo, xorg.user_id, ax.id_ancien | |
137 | FROM fusionax_anciens AS ax | |
138 | INNER JOIN fusionax_import AS i ON (i.id_ancien = ax.id_ancien AND i.user_id IS NULL) | |
139 | INNER JOIN auth_user_md5 AS xorg | |
140 | WHERE | |
141 | xorg.matricule_ax IS NULL AND | |
142 | xorg.promo = ax.promotion_etude AND | |
143 | xorg.prenom LIKE ax.prenom AND | |
144 | xorg.nom LIKE ax.Nom_complet | |
145 | ".($limit?('LIMIT '.$limit):'')); | |
146 | } | |
147 | ||
148 | function handler_ids(&$page, $part = 'main') | |
149 | { | |
150 | global $globals; | |
151 | $globals->change_dynamic_config(array('LastUpdate' => time()), 'FusionAX'); | |
152 | ||
153 | $page->assign('xorg_title','Polytechnique.org - Fusion des annuaires - Mise en correspondance simple'); | |
154 | if ($part == 'missingInAX') | |
155 | { | |
156 | // locate all persons from this database that are not in AX's | |
157 | $page->changeTpl('fusionax/idsMissingInAx.tpl'); | |
158 | $missingInAX = XDB::iterator("SELECT * | |
159 | FROM auth_user_md5 AS u | |
160 | LEFT JOIN aliases AS a ON(a.id = u.user_id AND FIND_IN_SET('bestalias', a.flags)) | |
161 | WHERE u.matricule_ax IS NULL | |
162 | LIMIT 20"); | |
163 | $page->assign('missingInAX', $missingInAX); | |
164 | return; | |
165 | } | |
166 | if ($part == 'missingInXorg') | |
167 | { | |
168 | // locate all persons from AX's database that are not here | |
169 | $page->changeTpl('fusionax/idsMissingInXorg.tpl'); | |
170 | $missingInXorg = XDB::iterator("SELECT promotion_etude AS promo, prenom, Nom_usuel AS nom, id_ancien | |
171 | FROM fusionax_import | |
172 | INNER JOIN fusionax_anciens AS a USING (id_ancien) | |
173 | WHERE fusionax_import.user_id IS NULL | |
174 | LIMIT 20"); | |
175 | $page->assign('missingInXorg', $missingInXorg); | |
176 | return; | |
177 | } | |
178 | if ($part == 'link') | |
179 | { | |
180 | FusionAxModule::link_by_ids(Env::i('user_id'),Env::v('matricule_ax')); | |
181 | } | |
182 | if ($part == 'linknext') | |
183 | { | |
184 | $linksToDo = FusionAxModule::find_easy_to_link(10); | |
185 | while ($l = $linksToDo->next()) | |
186 | { | |
187 | FusionAxModule::link_by_ids($l['user_id'],$l['id_ancien']); | |
188 | } | |
189 | } | |
190 | if ($part == 'linkall') | |
191 | { | |
192 | $linksToDo = FusionAxModule::find_easy_to_link(0); | |
193 | while ($l = $linksToDo->next()) | |
194 | { | |
195 | FusionAxModule::link_by_ids($l['user_id'],$l['id_ancien']); | |
196 | } | |
197 | } | |
198 | { | |
199 | $page->changeTpl('fusionax/ids.tpl'); | |
200 | $missingInAX = XDB::query("SELECT COUNT(*) FROM auth_user_md5 WHERE matricule_ax IS NULL"); | |
201 | if ($missingInAX) | |
202 | { | |
203 | $page->assign('nbMissingInAX', $missingInAX->fetchOneCell()); | |
204 | } | |
205 | $missingInXorg = XDB::query("SELECT COUNT(*) FROM fusionax_import WHERE user_id IS NULL"); | |
206 | if ($missingInXorg) | |
207 | { | |
208 | $page->assign('nbMissingInXorg', $missingInXorg->fetchOneCell()); | |
209 | } | |
210 | $easyToLink = FusionAxModule::find_easy_to_link(10); | |
211 | if ($easyToLink->total() > 0) | |
212 | { | |
213 | $page->assign('easyToLink', $easyToLink); | |
214 | } | |
215 | } | |
216 | } | |
217 | function handler_misc(&$page) | |
218 | { | |
219 | $page->changeTpl('fusionax/misc.tpl'); | |
220 | // deceased | |
221 | $deceasedErrorsSql = XDB::query('SELECT COUNT(*) FROM fusionax_deceased'); | |
222 | $page->assign('deceasedErrors',$deceasedErrorsSql->fetchOneCell()); | |
223 | $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')); | |
224 | $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')); | |
225 | $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')); | |
226 | } | |
227 | } | |
228 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:?> |