Fixes bin/formatAddresses.php.
authorStéphane Jacob <sj@m4x.org>
Sun, 23 Oct 2011 21:01:47 +0000 (23:01 +0200)
committerStéphane Jacob <sj@m4x.org>
Sun, 23 Oct 2011 21:10:24 +0000 (23:10 +0200)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
bin/formatAddresses.php
classes/address.php

index 6b8a8dd..d207748 100755 (executable)
@@ -24,17 +24,18 @@ require './connect.db.inc.php';
 require_once '../classes/address.php';
 require_once '../classes/geocoder.php';
 require_once '../classes/gmapsgeocoder.php';
+require_once '../classes/visibility.php';
 
 $globals->debug = 0; // Do not store backtraces
 
 $targets = array(
-    'g' => 'formatted_address',
-    'p' => 'postalText'
+    'g' => 'pa.formatted_address',
+    'p' => 'pa.postalText'
 );
 $ranges = array(
     'f' => ' != \'\'',
     'e' => ' = \'\'',
-    'a' => 'IS NOT NULL'
+    'a' => ' IS NOT NULL'
 );
 
 $options = getopt('g::t:r:h::', array('geocode::', 'target:', 'range:', 'help::'));
@@ -87,9 +88,10 @@ EOF;
 
 print "Formats addresses addresses.\n";
 
-$where = '';
 if ($range != 'a') {
-    $where = 'WHERE  ' . $targets[$target] . $ranges[$range];
+    $where = $targets[$target] . $ranges[$range];
+} else {
+    $where = null;
 }
 
 if ($geocoding_required) {
@@ -102,10 +104,7 @@ if ($geocoding_required) {
     $display_limit = 100;
 }
 
-$it = XDB::rawIterator('SELECT  *
-                          FROM  profile_addresses
-                        ' . $where . '
-                      ORDER BY  pid, jobid, type, id');
+$it = Address::iterate(array(), array(), array(), Visibility::get(Visibility::VIEW_PRIVATE), $where);
 
 $total = $it->total();
 $i = 0;
@@ -113,8 +112,7 @@ $j = 0;
 $skipped = 0;
 printf("\r%u / %u",  $i, $total);
 
-while ($item = $it->next()) {
-    $address = new Address($item);
+while ($address = $it->next()) {
     $address->changed = ($geocoding_required ? 1 : 0);
     $address->format();
     if ($address->delete()) {
index 871526a..915152f 100644 (file)
@@ -878,9 +878,9 @@ class Address
     }
 
     static public function iterate(array $pids = array(), array $types = array(),
-                                   array $jobids = array(), $visibility = null)
+                                   array $jobids = array(), $visibility = null, $where = null)
     {
-        return new AddressIterator($pids, $types, $jobids, $visibility);
+        return new AddressIterator($pids, $types, $jobids, $visibility, $where);
     }
 }
 
@@ -895,9 +895,12 @@ class AddressIterator implements PlIterator
 {
     private $dbiter;
 
-    public function __construct(array $pids, array $types, array $jobids, $visibility)
+    public function __construct(array $pids, array $types, array $jobids, $visibility, $_where)
     {
         $where = array();
+        if (!is_null($_where)) {
+            $where[] = $_where;
+        }
         if (count($pids) != 0) {
             $where[] = XDB::format('(pa.pid IN {?})', $pids);
         }