5bc75265066991ec504e67341a816e73b134e88a
[platal.git] / upgrade / 1.0.1 / xnet_directory_name.php
1 #!/usr/bin/php5
2 <?php
3
4 require_once 'connect.db.inc.php';
5
6 // Fetches the list of unregistered users.
7 $users = XDB::iterRow(
8 "SELECT a.uid, a.hruid, a.full_name, a.display_name,
9 COUNT(m.asso_id) AS nb_assos, m.asso_id, m.comm
10 FROM accounts AS a
11 LEFT JOIN group_members AS m ON (a.uid = m.uid)
12 WHERE a.type = 'xnet' AND a.directory_name IS NULL
13 GROUP BY a.uid");
14
15 $errors = array();
16
17 // Updates directory_names.
18 while (list($uid, $hruid, $full, $display, $nb_assos, $asso, $comm) = $users->next()) {
19 $matches = array();
20 $comment = "";
21 // Match any of the following:
22 // ... - _MATCH_
23 // ... (_MATCH_)
24 // \pL means 'any unicode letter'
25 if (preg_match('/([\pL- ]+)(\((.*)\)| +- +(.*))$/', $full, $matches)) {
26 $full_name = trim($matches[1]);
27 $comment = trim($matches[count($matches) - 1]);
28 } else {
29 $full_name = $full;
30 }
31 $matches = array();
32 if (preg_match('/^([\pL.-]+) +(.*)/u', $full_name, &$matches)) {
33 $dir_name = mb_strtoupper(trim($matches[2])) . " " . ucwords(trim($matches[1]));
34 if ($comment != "") {
35 if ($nb_assos > 1) {
36 $errors[$hruid] = "Can't update comment to |$comment|: more than one asso ($nb_assos).";
37 continue;
38 }
39 if ($comm != null && $comm != "") {
40 $errors[$hruid] = "Can't update comment to |$comment|: comment already set to |$comm|.";
41 continue;
42 }
43 }
44 echo "$hruid: |$full| => |$dir_name| (display: |$display|";
45 if ($comment != "") {
46 echo "; comment: |$comment|";
47 }
48 echo ").\n";
49 XDB::execute('UPDATE accounts SET directory_name = {?} WHERE hruid = {?}', $dir_name, $hruid);
50 if ($comment != "") {
51 XDB::execute('UPDATE group_members SET comm = {?} WHERE uid = {?} AND asso_id = {?}',
52 $comm, $uid, $asso);
53 }
54 } else {
55 $errors[$hruid] = "Unable to match |$full|.";
56 }
57 }
58
59 echo "== ERRORS ==\n";
60 foreach($errors as $hruid => $error) {
61 echo "$hruid\n\t$error\n";
62 }
63 ?>
64