Adds script for phones and addresses formatting after the merge.
authorStéphane Jacob <sj@m4x.org>
Wed, 20 Oct 2010 12:50:48 +0000 (14:50 +0200)
committerStéphane Jacob <sj@m4x.org>
Wed, 20 Oct 2010 13:11:59 +0000 (15:11 +0200)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
upgrade/1.0.1/README
upgrade/1.0.1/merge_issues.php [new file with mode: 0755]

index 1dd917e..65f0c11 100644 (file)
@@ -1,2 +1,3 @@
 99_jobs.sql must be run after php scripts.
-merge.php must not be run during the release. This script is only for merge purpose.
+Both merge.php and merge-issues.php must not be run during the release. They are only for merge purpose.
+On top of that, merge-issues.php requires to be run in a screen as it takes a couple of weeks to complete.
diff --git a/upgrade/1.0.1/merge_issues.php b/upgrade/1.0.1/merge_issues.php
new file mode 100755 (executable)
index 0000000..f924eaf
--- /dev/null
@@ -0,0 +1,137 @@
+#!/usr/bin/php5
+<?php
+// WARNING: this script takes a few weeks to be executed completly, thus run it into a screen.
+
+require_once 'connect.db.inc.php';
+require_once '../../classes/phone.php';
+require_once '../../classes/address.php';
+
+$globals->debug = 0; // Do not store backtraces.
+
+echo "Formats non formated phones.\n";
+$it = XDB::rawIterator("SELECT  search_tel AS search, display_tel AS display, comment, link_id,
+                                tel_type AS type, link_type, tel_id AS id, pid, pub
+                          FROM  profile_phones
+                         WHERE  search_tel = '' OR search_tel IS NULL
+                      ORDER BY  pid, link_id, tel_id");
+while ($phone = new Phone($it->next())) {
+  $phone->delete();
+  $phone->save();
+}
+
+echo "Deletes duplicated phones.\n";
+$it = XDB::rawIterator("SELECT  search_tel AS search, display_tel AS display, comment, link_id,
+                                tel_type AS type, link_type, tel_id AS id, pid, pub
+                          FROM  profile_phones
+                         WHERE  link_type = 'user'
+                      ORDER BY  pid, tel_id");
+$phones = array();
+$duplicates = array();
+$phone = new Phone($it->next());
+$pid = $phone->pid();
+$phones[] = $phone;
+$count = 1;
+while ($phone = new Phone($it->next())) {
+  if ($phone->pid() == $pid) {
+    $phone[] = $phone;
+    ++$count;
+  } else {
+    if ($count != 1) {
+      for ($i = 0; $i < $count; ++$i) {
+        for ($j = $i + 1; $j < $count; ++$j) {
+          if ($phones[$i]->search() == $phones[$j]->search()) {
+            $duplicates[$i] = true;
+          }
+        }
+      }
+      foreach ($duplicates as $key => $bool) {
+        unset($phones[$key]);
+      }
+      if (count($phones) != $count) {
+        Phone::deletePhones($pid, 'user');
+        $id = 0;
+        foreach ($phones as $phone) {
+          $phone->setId($id);
+          $phone->save();
+          ++$id;
+        }
+        XDB::execute('UPDATE  profile_merge_issues
+                         SET  issues = REPLACE(issues, \'phone\', \'\')
+                       WHERE  pid = {?}', $pid);
+      }
+      unset($duplicates);
+    }
+    unset($phones);
+    $pid = $phone->pid();
+    $phones[] = $phone;
+    $count = 1;
+  }
+}
+
+echo "Tries to geocode addresses (due a bug in the previous release, all addresses must run once again).\n";
+$it = XDB::rawIterator('SELECT  *
+                          FROM  profile_addresses
+                      ORDER BY  pid, jobid, type, id');
+$pid = 0;
+$jobid = 0;
+while ($address = $it->next()) {
+  $address->format(array(true, true));
+  $address->delete();
+  $address->save();
+  if (!($pid == $address->pid && $jobid == $address->jobid)) {
+    $pid = $address->pid;
+    $jobid = $address->jobid;
+    sleep(60);
+  }
+}
+
+echo "Deletes duplicated addresses.\n";
+$it = XDB::rawIterator("SELECT  *
+                          FROM  profile_addresses
+                         WHERE  type = 'home'
+                      ORDER BY  pid, id");
+$addresses = array();
+$duplicates = array();
+$address = new Address($it->next());
+$pid = $address->pid;
+$addresses[] = $address;
+$count = 1;
+while ($address = new Address($it->next())) {
+  if ($address->pid == $pid) {
+    $address[] = $address;
+    ++$count;
+  } else {
+    if ($count != 1) {
+      for ($i = 0; $i < $count; ++$i) {
+        for ($j = $i + 1; $j < $count; ++$j) {
+          if ($addresses[$i]->text == $addresses[$j]->text) {
+            $duplicates[$i] = true;
+          }
+        }
+      }
+      foreach ($duplicates as $key => $bool) {
+        unset($addresses[$key]);
+      }
+      if (count($addresses) != $count) {
+        Address::deleteAddresses($pid, 'home');
+        $id = 0;
+        foreach ($addresses as $address) {
+          $address->setId($id);
+          $address->save();
+          ++$id;
+        }
+        XDB::execute('UPDATE  profile_merge_issues
+                         SET  issues = REPLACE(issues, \'address\', \'\')
+                       WHERE  pid = {?}', $pid);
+      }
+      unset($duplicates);
+    }
+    unset($addresses);
+    $pid = $address->pid;
+    $addresses[] = $address;
+    $count = 1;
+  }
+}
+
+/* vim:set et sw=4 sts=4 ts=4: */
+?>