Fixes phones' upgrade script.
authorStéphane Jacob <sj@m4x.org>
Fri, 9 Apr 2010 14:04:49 +0000 (16:04 +0200)
committerStéphane Jacob <sj@m4x.org>
Fri, 9 Apr 2010 14:04:49 +0000 (16:04 +0200)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
upgrade/newdirectory-0.0.1/phones.php

index 274c407..9bc08cd 100755 (executable)
@@ -6,7 +6,11 @@ require_once 'profil.func.inc.php';
 $globals->debug = 0; //do not store backtraces
 
 // Convert phone prefixes from varchar to int
-$prefixes = XDB::iterRow("SELECT a2, phoneprf FROM geoloc_pays WHERE phoneprf IS NOT NULL");
+$prefixes = XDB::iterRow('SELECT  iso_3166_1_a2, phonePrefix
+                            FROM  geoloc_countries
+                           WHERE  phonePrefix IS NOT NULL');
+XDB::execute('ALTER TABLE  geoloc_countries
+               ADD COLUMN  tmpPhonePrefix SMALLINT UNSIGNED NULL');
 while (list($id, $pref) = $prefixes->next()) {
     $pref = preg_replace('/[^0-9]/', '', $pref);
     if ($pref[0] == '1') {
@@ -16,22 +20,33 @@ while (list($id, $pref) = $prefixes->next()) {
         $pref = '7';
     }
     if ($pref != '' && strlen($pref) < 4) {
-        XDB::execute("UPDATE geoloc_pays SET tmp_phoneprf = {?} WHERE a2 = {?}", $pref, $id);
+        XDB::execute('UPDATE  geoloc_countries
+                         SET  tmpPhonePrefix = {?}
+                       WHERE  iso_3166_1_a2 = {?}', $pref, $id);
     }
 }
 
 // geoloc_pays post operations
-// Drops old prfix column
-XDB::execute("ALTER TABLE geoloc_pays DROP COLUMN phoneprf");
+// Drops old prefix column
+XDB::execute('ALTER TABLE  geoloc_countries
+              DROP COLUMN  phonePrefix');
 // Renames temporary column
-XDB::execute("ALTER TABLE geoloc_pays CHANGE COLUMN tmp_phoneprf phoneprf smallint unsigned NULL AFTER nat");
-// Adds an index on phoneprf column
-XDB::execute("ALTER TABLE geoloc_pays ADD INDEX (phoneprf)");
+XDB::execute('ALTER TABLE  geoloc_countries
+            CHANGE COLUMN  tmpPhonePrefix phonePrefix SMALLINT UNSIGNED NULL AFTER nationality');
+// Adds an index on phonePrefix column
+XDB::execute('ALTER TABLE  geoloc_countries
+                ADD INDEX  (phonePrefix)');
 // Adds French phone prefix
-XDB::execute("UPDATE geoloc_pays SET phoneprf = '33' WHERE a2 = 'FR'");
+XDB::execute('UPDATE  geoloc_countries
+                 SET  phonePrefix = \'33\'
+               WHERE  iso_3166_1_a2 = \'FR\'');
 // Adds some phone formats
-XDB::execute("UPDATE geoloc_pays SET phoneformat = '0# ## ## ## ##' WHERE phoneprf = '33'");    //France
-XDB::execute("UPDATE geoloc_pays SET phoneformat = '(+p) ### ### ####' WHERE phoneprf = '1'");  //USA and NANP countries
+XDB::execute('UPDATE  geoloc_countries
+                 SET  phoneFormat = \'0# ## ## ## ##\'
+               WHERE  phonePrefix = \'33\'');    //France
+XDB::execute('UPDATE  geoloc_countries
+                 SET  phoneFormat = \'(+p) ### ### ####\'
+               WHERE  phonePrefix = \'1\'');  //USA and NANP countries
 
 
 
@@ -41,15 +56,18 @@ $warnings = 0;
 
 // Import from auth_user_quick
 echo "\nImporting mobile phone numbers from auth_user_quick...\n";
-$phones = XDB::iterRow("SELECT user_id, profile_mobile_pub, profile_mobile FROM auth_user_quick WHERE profile_mobile <> ''");
-while (list($uid, $pub, $phone) = $phones->next()) {
+$phones = XDB::iterRow('SELECT  ap.pid, q.profile_mobile_pub, q.profile_mobile
+                          FROM  #x4dat#.auth_user_quick AS q
+                    INNER JOIN  account_profiles        AS ap ON (q.user_id = ap.uid AND FIND_IN_SET(\'owner\', ap.perms))
+                         WHERE  q.profile_mobile <> \'\'');
+while (list($pid, $pub, $phone) = $phones->next()) {
     $fmt_phone = format_phone_number($phone);
     if ($fmt_phone != '') {
-        $display   = format_display_number($fmt_phone, $error);
-        if (!XDB::execute("INSERT INTO profile_phones (uid, link_type, link_id, tel_id, tel_type, search_tel, display_tel, pub)
-                                VALUES ({?}, 'user', 0, 0, 'mobile', {?}, {?}, {?})", $uid, $fmt_phone, $display, $pub)) {
-            echo "WARNING: insert of profile mobile phone number failed for user $uid.\n";
-            $warnings++;
+        $display = format_display_number($fmt_phone, $error);
+        if (!XDB::execute('INSERT INTO  profile_phones (pid, link_type, link_id, tel_id, tel_type, search_tel, display_tel, pub)
+                                VALUES  ({?}, \'user\', 0, 0, \'mobile\', {?}, {?}, {?})', $pid, $fmt_phone, $display, $pub)) {
+            echo "WARNING: insert of profile mobile phone number failed for profile $pid.\n";
+            ++$warnings;
         }
     }
 }
@@ -57,31 +75,34 @@ while (list($uid, $pub, $phone) = $phones->next()) {
 
 // Import from entreprises
 echo "\nImporting professional phone numbers from entreprises...\n";
-$phones = XDB::iterator("SELECT uid, entrid, tel, fax, mobile, tel_pub FROM entreprises ORDER BY uid");
+$phones = XDB::iterator('SELECT  ap.pid, e.entrid, e.tel, e.fax, e.mobile, e.tel_pub
+                           FROM  #x4dat#.entreprises AS e
+                     INNER JOIN  account_profiles    AS ap ON (e.uid = ap.uid AND FIND_IN_SET(\'owner\', ap.perms))
+                       ORDER BY  ap.pid');
 while ($row = $phones->next()) {
-    $request = "INSERT INTO profile_phones (uid, link_type, link_id, tel_id, tel_type, search_tel, display_tel, pub)
-                     VALUES ({?}, 'pro', {?}, {?}, {?}, {?}, {?}, {?})";
+    $request = 'INSERT INTO  profile_phones (pid, link_type, link_id, tel_id, tel_type, search_tel, display_tel, pub)
+                     VALUES  ({?}, \'pro\', {?}, {?}, {?}, {?}, {?}, {?})';
     $fmt_fixed   = format_phone_number($row['tel']);
     $fmt_mobile  = format_phone_number($row['mobile']);
     $fmt_fax     = format_phone_number($row['fax']);
     if ($fmt_fixed != '') {
         $disp_fixed  = format_display_number($fmt_fixed, $error);
-        if (!XDB::execute($request, $row['uid'], $row['entrid'], 0, 'fixed', $fmt_fixed, $disp_fixed, $row['tel_pub'])) {
-            echo 'WARNING: insert of professional fixed phone number failed for user ' . $row['uid'] . ' and entreprise ' . $row['entrid'] . ".\n";
-            $warnings++;
+        if (!XDB::execute($request, $row['pid'], $row['entrid'], 0, 'fixed', $fmt_fixed, $disp_fixed, $row['tel_pub'])) {
+            echo 'WARNING: insert of professional fixed phone number failed for profile ' . $row['pid'] . ' and entreprise ' . $row['entrid'] . ".\n";
+            ++$warnings;
         }
     }
     if ($fmt_mobile != '') {
         $disp_mobile = format_display_number($fmt_mobile, $error);
-        if (!XDB::execute($request, $row['uid'], $row['entrid'], 1, 'mobile', $fmt_mobile, $disp_mobile, $row['tel_pub'])) {
-            echo 'WARNING: insert of professional mobile number failed for user ' . $row['uid'] . ' and entreprise ' . $row['entrid'] . ".\n";
+        if (!XDB::execute($request, $row['pid'], $row['entrid'], 1, 'mobile', $fmt_mobile, $disp_mobile, $row['tel_pub'])) {
+            echo 'WARNING: insert of professional mobile number failed for profile ' . $row['pid'] . ' and entreprise ' . $row['entrid'] . ".\n";
             $warnings++;
         }
     }
     if ($fmt_fax != '') {
         $disp_fax    = format_display_number($fmt_fax, $error);
-        if (!XDB::execute($request, $row['uid'], $row['entrid'], 2, 'fax', $fmt_fax, $disp_fax, $row['tel_pub'])) {
-            echo 'WARNING: insert of professional fax number failed for user ' . $row['uid'] . ' and entreprise ' . $row['entrid'] . ".\n";
+        if (!XDB::execute($request, $row['pid'], $row['entrid'], 2, 'fax', $fmt_fax, $disp_fax, $row['tel_pub'])) {
+            echo 'WARNING: insert of professional fax number failed for profile ' . $row['pid'] . ' and entreprise ' . $row['entrid'] . ".\n";
             $warnings++;
         }
     }
@@ -90,9 +111,11 @@ while ($row = $phones->next()) {
 
 //import from tels
 echo "\nImporting personnal phone numbers from tels...\n";
-$phones = XDB::iterator("SELECT uid, adrid, telid, tel_type, tel_pub, tel FROM tels");
+$phones = XDB::iterator('SELECT  ap.pid, t.adrid, t.telid, t.tel_type, t.tel_pub, t.tel
+                           FROM  #x4dat#.tels     AS t
+                     INNER JOIN  account_profiles AS ap ON (t.uid = ap.uid AND FIND_IN_SET(\'owner\', ap.perms))');
 $conversions = array();
-$autre_count = 0;
+$other_count = 0;
 while ($row = $phones->next()) {
     $fmt_phone  = format_phone_number($row['tel']);
     if ($fmt_phone != '') {
@@ -103,31 +126,31 @@ while ($row = $phones->next()) {
         case 'fixed':
         case 'fax':
         case 'mobile':
-            if (!XDB::execute("INSERT INTO profile_phones (uid, link_type, link_id, tel_id, tel_type, search_tel, display_tel, pub)
-                                    VALUES ({?}, 'address', {?}, {?}, {?}, {?}, {?}, {?})",
-                              $row['uid'], $row['adrid'], $row['telid'], $guess_type, $fmt_phone, $display, $row['tel_pub'])) {
-                echo  'WARNING: insert of address phone number failed for user ' . $row['uid'] . ', address ' . $row['adrid']
+            if (!XDB::execute('INSERT  INTO profile_phones (pid, link_type, link_id, tel_id, tel_type, search_tel, display_tel, pub)
+                                     VALUES ({?}, \'address\', {?}, {?}, {?}, {?}, {?}, {?})',
+                              $row['pid'], $row['adrid'], $row['telid'], $guess_type, $fmt_phone, $display, $row['tel_pub'])) {
+                echo  'WARNING: insert of address phone number failed for profile ' . $row['pid'] . ', address ' . $row['adrid']
                     . ' and telephone id ' . $row['telid'] . ".\n";
-                $warnings++;
+                ++$warnings;
             } else {
                 if ($row['tel_type'] == 'Autre') {
-                    $autre_count++;
+                    ++$other_count;
                 } else if (!isset($conversions[$row['tel_type']])) {
                     $conversions[$row['tel_type']] = $guess_type;
                 }
             }
             break;
         case 'conflict':
-            echo  'WARNING: conflict for user ' . $row['uid'] . ', address ' . $row['adrid']
+            echo  'WARNING: conflict for profile ' . $row['pid'] . ', address ' . $row['adrid']
                 . ' and telephone id ' . $row['telid'] . ': type = "' . $row['tel_type']
                 . '", number = "' .$fmt_phone . "\"\n";
-            $warnings++;
+            ++$warnings;
             break;
         case 'unknown':
         default:
-            echo  'WARNING: unknown phone type (' . $row['tel_type'] . ') for user ' . $row['uid'] . ', address ' . $row['adrid']
+            echo  'WARNING: unknown phone type (' . $row['tel_type'] . ') for profile ' . $row['pid'] . ', address ' . $row['adrid']
                 . ' and telephone id ' . $row['telid'] . "\n";
-            $warnings++;
+            ++$warnings;
         }
     }
 }
@@ -136,7 +159,7 @@ echo "\nSummary of automatic phone type conversion\n";
 foreach ($conversions as $old => $new) {
     echo "* $old => $new\n";
 }
-echo "There was also $autre_count conversions from old type 'Autre' to a new one determined by the phone number.\n";
+echo "There was also $other_count conversions from old type 'Autre' to a new one determined by the phone number.\n";
 
 
 
@@ -148,13 +171,13 @@ if ($warnings) {
 }
 echo  "\nAfter solving any import problem and checking automatic conversions,\n"
     . "you can drop useless columns and tables by these requests:\n"
-    . "DROP TABLE IF EXISTS `tels`;\n"
-    . "ALTER TABLE `auth_user_quick` DROP COLUMN `profile_mobile`;\n"
-    . "ALTER TABLE `auth_user_quick` DROP COLUMN `profile_mobile_pub`;\n"
-    . "ALTER TABLE `entreprises` DROP COLUMN `tel`;\n"
-    . "ALTER TABLE `entreprises` DROP COLUMN `fax`;\n"
-    . "ALTER TABLE `entreprises` DROP COLUMN `mobile`;\n"
-    . "ALTER TABLE `entreprises` DROP COLUMN `tel_pub`;\n";
+    . "DROP TABLE IF EXISTS tels;\n"
+    . "ALTER TABLE auth_user_quick DROP COLUMN profile_mobile;\n"
+    . "ALTER TABLE auth_user_quick DROP COLUMN profile_mobile_pub;\n"
+    . "ALTER TABLE entreprises DROP COLUMN tel;\n"
+    . "ALTER TABLE entreprises DROP COLUMN fax;\n"
+    . "ALTER TABLE entreprises DROP COLUMN mobile;\n"
+    . "ALTER TABLE entreprises DROP COLUMN tel_pub;\n";
 
 
 // auxilliary functions
@@ -172,7 +195,8 @@ function guess_phone_type($str_type, $phone)
         }
     }
 
-    if ((strpos($str_type, 'mob') !== false) || (strpos($str_type, 'cell') !== false) || (strpos($str_type, 'port') !== false) || (strpos($str_type, 'ptb') !== false)) {
+    if ((strpos($str_type, 'mob') !== false) || (strpos($str_type, 'cell') !== false)
+        || (strpos($str_type, 'port') !== false) || (strpos($str_type, 'ptb') !== false)) {
         if (substr($phone, 3) == '336' || substr($phone, 2) != '33') {
             return 'mobile';      //for France check if number is a mobile one
         } else {
@@ -186,7 +210,8 @@ function guess_phone_type($str_type, $phone)
             return 'fax';
         }
     }
-    if ((strpos($str_type, 'fixe') !== false) || (strpos($str_type, 'tél') !== false) || (strpos($str_type, 'tel') !== false) || (strpos($str_type, 'free') !== false)) {
+    if ((strpos($str_type, 'fixe') !== false) || (strpos($str_type, 'tél') !== false)
+        || (strpos($str_type, 'tel') !== false) || (strpos($str_type, 'free') !== false)) {
         if (substr($phone, 3) == '336') {
             return 'conflict';
         } else {