| 1 | #!/usr/bin/php5 -q |
| 2 | <?php |
| 3 | /*************************************************************************** |
| 4 | * Copyright (C) 2003-2013 Polytechnique.org * |
| 5 | * http://opensource.polytechnique.org/ * |
| 6 | * * |
| 7 | * This program is free software; you can redistribute it and/or modify * |
| 8 | * it under the terms of the GNU General Public License as published by * |
| 9 | * the Free Software Foundation; either version 2 of the License, or * |
| 10 | * (at your option) any later version. * |
| 11 | * * |
| 12 | * This program is distributed in the hope that it will be useful, * |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 | * GNU General Public License for more details. * |
| 16 | * * |
| 17 | * You should have received a copy of the GNU General Public License * |
| 18 | * along with this program; if not, write to the Free Software * |
| 19 | * Foundation, Inc., * |
| 20 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 21 | ***************************************************************************/ |
| 22 | |
| 23 | require '../../bin/connect.db.inc.php'; |
| 24 | |
| 25 | $globals->debug = 0; // Do not store backtraces |
| 26 | |
| 27 | $res = XDB::rawFetchAllAssoc("SELECT p.pid, p.ax_id, p.hrpid, |
| 28 | f.Nom_patronymique, f.Nom_usuel, f.Nom_complet, |
| 29 | ppn.lastname_initial, ppn.lastname_main, ppn.lastname_marital, ppn.lastname_ordinary |
| 30 | FROM fusionax_anciens AS f |
| 31 | INNER JOIN profiles AS p ON (f.ax_id = p.ax_id) |
| 32 | INNER JOIN profile_public_names AS ppn ON (p.pid = ppn.pid) |
| 33 | WHERE IF(f.partic_patro, CONCAT(f.partic_patro, CONCAT(' ', f.Nom_patronymique)), f.Nom_patronymique) NOT IN (ppn.lastname_initial, ppn.lastname_main, ppn.lastname_marital, ppn.lastname_ordinary) |
| 34 | OR IF(f.partic_nom, CONCAT(f.partic_nom, CONCAT(' ', f.Nom_usuel)), f.Nom_usuel) NOT IN (ppn.lastname_initial, ppn.lastname_main, ppn.lastname_marital, ppn.lastname_ordinary) |
| 35 | OR f.Nom_complet NOT IN (ppn.lastname_initial, ppn.lastname_main, ppn.lastname_marital, ppn.lastname_ordinary)"); |
| 36 | |
| 37 | function fix_ax_particles($ax, $xorg, $item) |
| 38 | { |
| 39 | $count = 0; |
| 40 | foreach ($ax as $ax_key => $ax_name) { |
| 41 | if (!in_array($ax_name, $xorg)) { |
| 42 | $new_name = ''; |
| 43 | foreach ($xorg as $xorg_key => $xorg_name) { |
| 44 | if ($xorg_name && strpos($xorg_name, $ax_name) !== false) { |
| 45 | if ($xorg_name == 'de ' . $ax_name) { |
| 46 | $new_name = 'de ' . $item[$ax_key]; |
| 47 | } elseif ($xorg_name == "d'" . $ax_name) { |
| 48 | $new_name = "d'" . $item[$ax_key]; |
| 49 | } elseif ($xorg_name == 'du ' . $ax_name) { |
| 50 | $new_name = 'du ' . $item[$ax_key]; |
| 51 | } |
| 52 | |
| 53 | if ($new_name) { |
| 54 | XDB::execute("UPDATE fusionax_anciens |
| 55 | SET $ax_key = {?} |
| 56 | WHERE ax_id = {?}", |
| 57 | $new_name, $item['ax_id']); |
| 58 | ++$count; |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | if ($new_name) { |
| 64 | continue; |
| 65 | } |
| 66 | foreach ($ax as $ax_key2 => $ax_name2) { |
| 67 | if ($ax_name2 && strpos($ax_name2, $ax_name) !== false) { |
| 68 | if ($ax_name2 == 'de ' . $ax_name) { |
| 69 | $new_name = 'de ' . $item[$ax_key]; |
| 70 | } elseif ($ax_name2 == "d'" . $ax_name) { |
| 71 | $new_name = "d'" . $item[$ax_key]; |
| 72 | } elseif ($ax_name2 == 'du ' . $ax_name) { |
| 73 | $new_name = 'du ' . $item[$ax_key]; |
| 74 | } |
| 75 | |
| 76 | if ($new_name) { |
| 77 | XDB::execute("UPDATE fusionax_anciens |
| 78 | SET $ax_key = {?} |
| 79 | WHERE ax_id = {?}", |
| 80 | $new_name, $item['ax_id']); |
| 81 | ++$count; |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return $count; |
| 90 | } |
| 91 | |
| 92 | function fix_xorg_particles($ax, $xorg, $item) |
| 93 | { |
| 94 | $count = 0; |
| 95 | foreach ($ax as $ax_key => $ax_name) { |
| 96 | if (!in_array($ax_name, $xorg)) { |
| 97 | foreach ($xorg as $xorg_key => $xorg_name) { |
| 98 | $new_name = ''; |
| 99 | if ($xorg_name && strpos($ax_name, $xorg_name) !== false) { |
| 100 | if ($ax_name == 'de ' . $xorg_name) { |
| 101 | $new_name = 'de ' . $item[$xorg_key]; |
| 102 | } elseif ($ax_name == "d'" . $xorg_name) { |
| 103 | $new_name = "d'" . $item[$xorg_key]; |
| 104 | } elseif ($ax_name == 'du ' . $xorg_name) { |
| 105 | $new_name = 'du ' . $item[$xorg_key]; |
| 106 | } |
| 107 | |
| 108 | if ($new_name) { |
| 109 | XDB::execute("UPDATE profile_public_names |
| 110 | SET $xorg_key = {?} |
| 111 | WHERE pid = {?}", |
| 112 | $new_name, $item['pid']); |
| 113 | ++$count; |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return $count; |
| 122 | } |
| 123 | |
| 124 | function fix_xorg_full_name($ax, $xorg, $item) |
| 125 | { |
| 126 | $new_name = ''; |
| 127 | if (!in_array($ax['Nom_complet'], $xorg) && strpos($ax['Nom_complet'], $xorg['lastname_main']) !== false && $xorg['lastname_ordinary'] == '') { |
| 128 | XDB::execute("UPDATE profile_public_names |
| 129 | SET lastname_main = {?}, lastname_ordinary = {?} |
| 130 | WHERE pid = {?}", |
| 131 | $item['Nom_complet'], $item['lastname_main'], $item['pid']); |
| 132 | return 1; |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | function fix_xorg_ordinary_name($ax, $xorg, $item) |
| 139 | { |
| 140 | $new_name = ''; |
| 141 | if (!in_array($ax['Nom_usuel'], $xorg) && $xorg['lastname_ordinary'] == '') { |
| 142 | XDB::execute("UPDATE profile_public_names |
| 143 | SET lastname_ordinary = {?} |
| 144 | WHERE pid = {?}", |
| 145 | $item['Nom_usuel'], $item['pid']); |
| 146 | return 1; |
| 147 | } |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | function format($string) |
| 153 | { |
| 154 | $string = preg_replace('/\-/', ' ', $string); |
| 155 | return preg_replace('/\s+/', ' ', $string); |
| 156 | } |
| 157 | |
| 158 | $updates_count = 0; |
| 159 | $count = 0; |
| 160 | $total = count($res); |
| 161 | foreach($res as $item) { |
| 162 | array_map('trim', $item); |
| 163 | $ax = array( |
| 164 | 'Nom_patronymique' => format(mb_strtolower(replace_accent($item['Nom_patronymique']))), |
| 165 | 'Nom_usuel' => format(mb_strtolower(replace_accent($item['Nom_usuel']))), |
| 166 | 'Nom_complet' => format(mb_strtolower(replace_accent($item['Nom_complet']))) |
| 167 | ); |
| 168 | $xorg = array( |
| 169 | 'lastname_initial' => format(mb_strtolower(replace_accent($item['lastname_initial']))), |
| 170 | 'lastname_main' => format(mb_strtolower(replace_accent($item['lastname_main']))), |
| 171 | 'lastname_ordinary' => format(mb_strtolower(replace_accent($item['lastname_ordinary']))) |
| 172 | ); |
| 173 | |
| 174 | $updates_count += fix_ax_particles($ax, $xorg, $item); |
| 175 | $updates_count += fix_xorg_particles($ax, $xorg, $item); |
| 176 | $updates_count += fix_xorg_full_name($ax, $xorg, $item); |
| 177 | $updates_count += fix_xorg_ordinary_name($ax, $xorg, $item); |
| 178 | printf("\r%u / %u", $count, $total); |
| 179 | ++$count; |
| 180 | } |
| 181 | printf("\r%u / %u\n\n", $count, $total); |
| 182 | |
| 183 | echo "Nombre de mises à jour effectuées : " . $updates_count . ".\n"; |
| 184 | |
| 185 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 186 | ?> |