Updates address formating script.
authorStéphane Jacob <sj@m4x.org>
Thu, 12 May 2011 12:09:12 +0000 (14:09 +0200)
committerStéphane Jacob <sj@m4x.org>
Fri, 13 May 2011 12:01:35 +0000 (14:01 +0200)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
bin/formatAddresses.php

index 4ab19d2..6b8a8dd 100755 (executable)
 
 require './connect.db.inc.php';
 require_once '../classes/address.php';
+require_once '../classes/geocoder.php';
+require_once '../classes/gmapsgeocoder.php';
 
 $globals->debug = 0; // Do not store backtraces
 
-print "(Re)Formats postal addresses for all addresses in the database.\n";
+$targets = array(
+    'g' => 'formatted_address',
+    'p' => 'postalText'
+);
+$ranges = array(
+    'f' => ' != \'\'',
+    'e' => ' = \'\'',
+    'a' => 'IS NOT NULL'
+);
+
+$options = getopt('g::t:r:h::', array('geocode::', 'target:', 'range:', 'help::'));
+
+$help_required = isset($options['h']) || isset($options['help']);
+$geocoding_required = isset($options['g']) || isset($options['geocode']);
+$target = '';
+$range = '';
+
+if (isset($options['t'])) {
+    $target = $options['t'];
+} elseif (isset($options['target'])) {
+    $target = $options['target'];
+}
+
+if (isset($options['r'])) {
+    $range = $options['r'];
+} elseif ($options['range']) {
+    $range = $options['range'];
+}
+
+$missing_option = !array_key_exists($target, $targets) || !array_key_exists($range, $ranges);
+
+if ($missing_option || $help_required) {
+    echo <<<EOF
+SYNOPSIS
+    formatAddresses.php [-g] -t [g|p] -r [f|e|a]
+
+DESCRIPTION
+    formatAddresses.php formats addresses. If the addresses need geocoding, this
+    must be specified (-g). The targetted group of addresses must be specified
+    (non formatted addresses, formatted addresses, all addresses).
+
+OPTIONS
+    -g, --geocode
+        Geocodes the adresses. If not required, the address will not be
+        geolocated.
+    -t, --target [ g | p ]
+        The selection will be made either on the geocoding
+        (formatted_address) or the postal address (postalText).
+    -r, --range [ f | e | a ]
+        The selection will include the addresses corresponding to the right
+        target, which are formatted, empty (non formatted) or all addresses.
+    -h, --help
+        displays this help
+
+EOF;
+    exit;
+}
+
+print "Formats addresses addresses.\n";
+
+$where = '';
+if ($range != 'a') {
+    $where = 'WHERE  ' . $targets[$target] . $ranges[$range];
+}
+
+if ($geocoding_required) {
+    // Waiting time is computed as follows: 3600 * 24 / LIMIT,
+    // where LIMIT is google's limit reduced to take into account site geocoding.
+    $wait = ceil(3600 * 24 / 2000);
+    $display_limit = 1;
+} else {
+    $wait = 0;
+    $display_limit = 100;
+}
+
 $it = XDB::rawIterator('SELECT  *
                           FROM  profile_addresses
+                        ' . $where . '
                       ORDER BY  pid, jobid, type, id');
+
 $total = $it->total();
 $i = 0;
 $j = 0;
+$skipped = 0;
 printf("\r%u / %u",  $i, $total);
+
 while ($item = $it->next()) {
     $address = new Address($item);
-    $address->format(array('postalText' => true));
-    $address->delete();
-    $address->save();
+    $address->changed = ($geocoding_required ? 1 : 0);
+    $address->format();
+    if ($address->delete()) {
+        $address->save();
+    } else {
+        ++$skipped;
+    }
 
     ++$i;
-    if ($i == 100) {
+    if ($i == $display_limit) {
         ++$j;
         $i = 0;
-        printf("\r%u / %u",  $i + 100 * $j, $total);
+        printf("\r%u / %u",  $i + $display_limit * $j, $total);
     }
+    sleep($wait * $address->geocoding_calls);
 }
-print "Done.\n";
+printf("\r%u / %u",  $i + $display_limit * $j, $total);
+
+if ($skipped != 0) {
+    printf("\n%u addresses skipped.\n", $skipped);
+}
+
+print "\nDone.\n";
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>