Merge remote branch 'origin/xorg/f/geocoding' into xorg/master
[platal.git] / upgrade / 1.1.2 / names.php
1 #!/usr/bin/php5
2 <?php
3
4 require_once 'connect.db.inc.php';
5 require_once 'name.func.inc.php';
6
7 $globals->debug = 0; // Do not store backtraces.
8
9 $options = getopt('', array('perform-updates::'));
10 if (isset($options['perform-updates']) && $options['perform-updates'] == 'YES') {
11 $perform_updates = true;
12 } else {
13 $perform_updates = false;
14 }
15
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'");
20 if ($other_data) {
21 print "Update this script to take 'name_other' and 'firstname_other' into account.";
22 exit();
23 } else {
24 $aliases = XDB::fetchAllAssoc('pid', "SELECT pid, name
25 FROM profile_private_names
26 WHERE type = 'nickname'");
27 }
28
29 // This contains a firstname and a lastname, both can be either main or ordinary.
30 function update_main($data, $string, &$update)
31 {
32 $matches = explode(' ', $string);
33 $count = count($matches);
34 $i = 0;
35
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']) {
41 return true;
42 }
43 if ($lastname != $data['lastname_main'] && $lastname != $data['lastname_ordinary']) {
44 $update[] = XDB::format('lastname_ordinary = {?}', $lastname);
45 return true;
46 }
47 return false;
48 }
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);
52 }
53 if ($lastname == $data['lastname_ordinary']) {
54 return true;
55 }
56 return false;
57 }
58 }
59 return false;
60 }
61
62 // This is detected by a starting 'M/Mme'. But it can also include a pseudonym.
63 function update_marital($data, $string, &$update)
64 {
65 preg_match('/^([^,]+)(?:, (.*))?$/', $string, $matches);
66 if ($matches[1] != $data['lastname_marital']) {
67 $update[] = XDB::format('lastname_marital = {?}', $matches[1]);
68 }
69 if (count($matches) == 3 && $matches[2] != $data['pseudonym']) {
70 $update[] = XDB::format('pseudonym = {?}', $matches[2]);
71 }
72 }
73
74 function update_private($data, $string, $pid, &$aliases, $perform_updates)
75 {
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);
79
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, {?})",
84 $pid, $string);
85 } else {
86 print $string . ' (alias for pid ' . $pid . ")\n";
87 }
88 } elseif ($aliases[$pid] != $string) {
89 if ($perform_updates) {
90 XDB::execute('UPDATE profile_private_names
91 SET name = {?}
92 WHERE pid = {?} AND name = {?}',
93 $string, $pid, $aliases[$pid]);
94 } else {
95 print $string . ' (new alias for pid ' . $pid . ' replacing ' . $aliases[$pid] . ")\n";
96 }
97
98 }
99 }
100
101 // This can either be a main name or a pseudonym.
102 function update_plain($data, $string, &$update, $has_ordinary)
103 {
104 $string = substr($string, 1, strlen($string) - 2);
105 if ($string == $data['lastname_main']) {
106 return true;
107 }
108
109 if ($string != $data['pseudonym']) {
110 if ($has_ordinary) {
111 $update[] = XDB::format('pseudonym = {?}', $string);
112 } else {
113 $update[] = XDB::format('lastname_main = {?}', $string);
114 }
115 return true;
116 }
117 return false;
118 }
119
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)');
124
125 $pattern = '/^([^\(\)]+)(?: (\([^\(\)]+\)))?(?: (\([^\(\)]+\)))?(?: (\([^\(\)]+\)))?$/';
126 while ($data = $res->next()) {
127 preg_match($pattern, $data['private_name'], $matches);
128 $has_ordinary = false;
129
130 $count = count($matches);
131 $update = array();
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);
138 } else {
139 $has_ordinary = update_plain($data, $matches[$i], $update, $has_ordinary);
140 }
141 }
142
143 if (count($update)) {
144 $set = implode(', ', $update);
145 if ($perform_updates) {
146 XDB::rawExecute('UPDATE profile_public_names
147 SET ' . $set . '
148 WHERE pid = ' . $data['pid']);
149 } else {
150 print $set . ' (for pid ' . $data['pid'] . ")\n";
151 }
152 }
153 }
154
155 if ($perform_updates) {
156 print "\nUpdates done.\n";
157 } else {
158 print "\nIf this seems correct, relaunch this script with option --perform-updates=YES.\n";
159 }
160
161 /* vim:set et sw=4 sts=4 ts=4: */
162 ?>