Add script for filling directory_name for xnet accounts (Closes #1178)
authorRaphaël Barrois <raphael.barrois@polytechnique.org>
Sun, 19 Sep 2010 19:44:26 +0000 (21:44 +0200)
committerRaphaël Barrois <raphael.barrois@polytechnique.org>
Sun, 19 Sep 2010 19:44:26 +0000 (21:44 +0200)
This script tries to guess the directory_name from the full_name.
Any part matching "(...)" or " - ..." will be considered as a comment,
    and added to the 'comment' field of the group this user is a member
    of (only if there is only one such group and no current comment).

All errors are displayed at the end of the run, the 'directory_name'
won't be fixed for them (still 'NULL'); this list can then be accessed
by simply running the script again.

Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
upgrade/1.0.1/xnet_directory_name.php [new file with mode: 0755]

diff --git a/upgrade/1.0.1/xnet_directory_name.php b/upgrade/1.0.1/xnet_directory_name.php
new file mode 100755 (executable)
index 0000000..5bc7526
--- /dev/null
@@ -0,0 +1,64 @@
+#!/usr/bin/php5
+<?php
+
+require_once 'connect.db.inc.php';
+
+// Fetches the list of unregistered users.
+$users = XDB::iterRow(
+    "SELECT  a.uid, a.hruid, a.full_name, a.display_name,
+             COUNT(m.asso_id) AS nb_assos, m.asso_id, m.comm
+       FROM  accounts AS a
+  LEFT JOIN  group_members AS m ON (a.uid = m.uid)
+      WHERE  a.type = 'xnet' AND a.directory_name IS NULL
+   GROUP BY  a.uid");
+
+$errors = array();
+
+// Updates directory_names.
+while (list($uid, $hruid, $full, $display, $nb_assos, $asso, $comm) = $users->next()) {
+    $matches = array();
+    $comment = "";
+    // Match any of the following:
+    // ... - _MATCH_
+    // ... (_MATCH_)
+    // \pL means 'any unicode letter'
+    if (preg_match('/([\pL- ]+)(\((.*)\)| +- +(.*))$/', $full, $matches)) {
+        $full_name = trim($matches[1]);
+        $comment = trim($matches[count($matches) - 1]);
+    } else {
+        $full_name = $full;
+    }
+    $matches = array();
+    if (preg_match('/^([\pL.-]+) +(.*)/u', $full_name, &$matches)) {
+        $dir_name = mb_strtoupper(trim($matches[2])) . " " . ucwords(trim($matches[1]));
+        if ($comment != "") {
+            if ($nb_assos > 1) {
+                $errors[$hruid] = "Can't update comment to |$comment|: more than one asso ($nb_assos).";
+                continue;
+            }
+            if ($comm != null && $comm != "") {
+                $errors[$hruid] = "Can't update comment to |$comment|: comment already set to |$comm|.";
+                continue;
+            }
+        }
+        echo "$hruid: |$full| => |$dir_name| (display: |$display|";
+        if ($comment != "") {
+            echo "; comment: |$comment|";
+        }
+        echo ").\n";
+        XDB::execute('UPDATE accounts SET directory_name = {?} WHERE hruid = {?}', $dir_name, $hruid);
+        if ($comment != "") {
+            XDB::execute('UPDATE group_members SET comm = {?} WHERE uid = {?} AND asso_id = {?}',
+                $comm, $uid, $asso);
+        }
+    } else {
+        $errors[$hruid] = "Unable to match |$full|.";
+    }
+}
+
+echo "== ERRORS ==\n";
+foreach($errors as $hruid => $error) {
+    echo "$hruid\n\t$error\n";
+}
+?>
+