4 require_once 'connect.db.inc.php';
5 require_once 'name.func.inc.php';
7 $globals->debug
= 0; // Do not store backtraces.
9 $options = getopt('', array('perform-updates::'));
10 if (isset($options['perform-updates']) && $options['perform-updates'] == 'YES') {
11 $perform_updates = true
;
13 $perform_updates = false
;
16 $other_data = XDB
::rawFetchOneCell("SELECT COUNT(*)
17 FROM profile_name AS pn
18 INNER JOIN profile_name_enum AS pne ON (pn.typeid = pne.id)
19 WHERE pne.type = 'name_other' OR pne.type = 'firstname_other'");
21 print "Update this script to take 'name_other' and 'firstname_other' into account.";
24 $aliases = XDB
::fetchAllAssoc('pid', "SELECT pid, name
25 FROM profile_private_names
26 WHERE type = 'nickname'");
29 // This contains a firstname and a lastname, both can be either main or ordinary.
30 function update_main($data, $string, &$update)
32 $matches = explode(' ', $string);
33 $count = count($matches);
36 for (; $i < $count +
1; ++
$i) {
37 $firstname = implode(' ', array_slice($matches, 0, $i +
1));
38 $lastname = implode(' ', array_slice($matches, $i +
1));
39 if ($firstname == $data['firstname_main'] ||
$firstname == $data['firstname_ordinary']) {
40 if ($lastname == $data['lastname_ordinary']) {
43 if ($lastname != $data['lastname_main'] && $lastname != $data['lastname_ordinary']) {
44 $update[] = XDB
::format('lastname_ordinary = {?}', $lastname);
49 if ($lastname == $data['lastname_main'] ||
$lastname == $data['lastname_ordinary']) {
50 if ($firstname != $data['firstname_main'] && $firstname != $data['firstname_ordinary']) {
51 $update[] = XDB
::format('firstname_ordinary = {?}', $firstname);
53 if ($lastname == $data['lastname_ordinary']) {
62 // This is detected by a starting 'M/Mme'. But it can also include a pseudonym.
63 function update_marital($data, $string, &$update)
65 preg_match('/^([^,]+)(?:, (.*))?$/', $string, $matches);
66 if ($matches[1] != $data['lastname_marital']) {
67 $update[] = XDB
::format('lastname_marital = {?}', $matches[1]);
69 if (count($matches) == 3 && $matches[2] != $data['pseudonym']) {
70 $update[] = XDB
::format('pseudonym = {?}', $matches[2]);
74 function update_private($data, $string, $pid, &$aliases, $perform_updates)
76 // We here assume there are no other last/firstnames as there do not seem to be any in the db.
77 $string = substr($string, 1, strlen($string) - 2);
78 $string = substr($string, 6, strlen($string) - 6);
80 if (!isset($aliases[$pid])) {
81 if ($perform_updates) {
82 XDB
::execute("INSERT INTO profile_private_names (pid, type, id, name)
83 VALUES ({?}, 'nickname', 0, {?})",
86 print $string . ' (alias for pid ' . $pid . ")\n";
88 } elseif ($aliases[$pid] != $string) {
89 if ($perform_updates) {
90 XDB
::execute('UPDATE profile_private_names
92 WHERE pid = {?} AND name = {?}',
93 $string, $pid, $aliases[$pid]);
95 print $string . ' (new alias for pid ' . $pid . ' replacing ' . $aliases[$pid] . ")\n";
101 // This can either be a main name or a pseudonym.
102 function update_plain($data, $string, &$update, $has_ordinary)
104 $string = substr($string, 1, strlen($string) - 2);
105 if ($string == $data['lastname_main']) {
109 if ($string != $data['pseudonym']) {
111 $update[] = XDB
::format('pseudonym = {?}', $string);
113 $update[] = XDB
::format('lastname_main = {?}', $string);
120 $res = XDB
::rawIterator('SELECT pd.pid, pd.private_name, pn.lastname_main, pn.lastname_marital, pn.lastname_ordinary,
121 pn.firstname_main, pn.firstname_ordinary, pn.pseudonym
122 FROM profile_display AS pd
123 INNER JOIN profile_public_names AS pn ON (pd.pid = pn.pid)');
125 $pattern = '/^([^\(\)]+)(?: (\([^\(\)]+\)))?(?: (\([^\(\)]+\)))?(?: (\([^\(\)]+\)))?$/';
126 while ($data = $res->next()) {
127 preg_match($pattern, $data['private_name'], $matches);
128 $has_ordinary = false
;
130 $count = count($matches);
132 $has_ordinary = update_main($data, $matches[1], $update);
133 for ($i = 2; $i < $count; ++
$i) {
134 if (preg_match('/^\((?:M|Mme) (.+)\)$/', $matches[$i], $pieces)) {
135 update_marital($data, $pieces[1], $update);
136 } elseif (preg_match('/^\((?:alias|autres prénoms :|autres noms :) .+\)$/', $matches[$i], $pieces)) {
137 update_private($data, $matches[$i], $data['pid'], $aliases, $perform_updates);
139 $has_ordinary = update_plain($data, $matches[$i], $update, $has_ordinary);
143 if (count($update)) {
144 $set = implode(', ', $update);
145 if ($perform_updates) {
146 XDB
::rawExecute('UPDATE profile_public_names
148 WHERE pid = ' . $data['pid']);
150 print $set . ' (for pid ' . $data['pid'] . ")\n";
155 if ($perform_updates) {
156 print "\nUpdates done.\n";
158 print "\nIf this seems correct, relaunch this script with option --perform-updates=YES.\n";
161 /* vim:set et sw=4 sts=4 ts=4: */