$city_new_name ]; $updated = updateDataTable('geofence_area_city', 'id=' . $city_id, $data_update_city); } } } $r = getAddressWithEmptyCity(); while ($f = pg_fetch_assoc($r)) { $total++; $city_name = $f['city']; $country_code = $f['country']; $city_id = $f['city_id']; $address = $f['address']; $latitude = $f['latitude']; $longitude = $f['longitude']; $address_info = parseAddressByCountry($address, $country_code); $city_new_name = ""; if (!empty($address_info['city'])) { $city_new_name = $address_info['city']; } else { $city_new_name = Geocode::getGeoInfoBigDataCloud($latitude, $longitude); if (empty($city_new_name)) { $data_reverse = Geocode::reverseGPS($latitude, $longitude); if (!empty($data_reverse['city'])) { $city_new_name = $data_reverse['city']; } } } if (!empty($city_new_name)) { $city_new_id = getCityId($city_new_name, $country_code, $latitude, $longitude); $city_id = $city_new_id; $data_update = [ 'city_id' => $city_new_id, ]; $updated = updateDataTable('address', 'id=' . $f['id'], $data_update); if ($updated) { $ok = true; } else { $ok = false; } } if (empty($city_name)) { $data_update_city = [ 'city' => $city_new_name, 'country' => $country_code, ]; $updated = updateDataTable('geofence_area_city', 'id=' . $city_id, $data_update_city); if ($updated) { $ok = true; } else { $ok = false; } } if (!empty($ok)) { $success++; } else { $failed++; } } //clean duplicated cities $r = getDuplicatedCities(); while ($f = pg_fetch_assoc($r)) { $city_name = $f['city']; $country = $f['country']; $citie_ids = getCities($city_name, $country); $num = count($citie_ids); if ($num > 0) { $total += $num; foreach ($citie_ids as $country => $ids) { $city_id = $ids[0]; $data_update = [ 'city_id' => $city_id, ]; $condition = "country='" . $country . "' AND city_id IN (" . implode(",", $ids) . ")"; $updated = updateDataTable("address", $condition, $data_update); } } } deleteUnusedCities(); echo "Total: " . $total . ".\n"; echo "Success: : " . $success . ".\n"; echo "Failed: : " . $failed . ".\n"; echo "[" . date("Y-m-d H:i:s") . "] {$job_name} job complete.\n"; function getCitiesByCondition($condition) { global $conn, $readOnlyReplicaConn; $q = "SELECT id, latitude,longitude FROM geofence_area_city WHERE ".$condition; $r = pg_query($readOnlyReplicaConn, $q); return $r; } function getAddressWithEmptyCity() { global $conn, $readOnlyReplicaConn; $q = "SELECT a.id, address, a.latitude, a.longitude, city_id, city, a.country as country FROM address a LEFT JOIN geofence_area_city b ON a.city_id=b.id WHERE city_id IS NULL OR b.city='' OR a.country!=b.country;"; $r = pg_query($readOnlyReplicaConn, $q); return $r; } function getDuplicatedCities() { global $conn, $readOnlyReplicaConn; $q = "SELECT city, country, count(*) FROM geofence_area_city GROUP BY city, country HAVING count(*)>1 ORDER BY count(*) DESC"; $r = pg_query($readOnlyReplicaConn, $q); return $r; } function deleteUnusedCities() { global $conn; $q = "DELETE FROM geofence_area_city WHERE id NOT IN (SELECT DISTINCT city_id FROM address);"; $r = pg_query($conn, $q); if ($r && pg_affected_rows($r)) { return true; } return false; } function getCities($city, $country) { global $readOnlyReplicaConn; $q = "SELECT id FROM geofence_area_city WHERE LOWER(city) ILIKE '" . strtolower(pg_escape_string($city)) . "' AND country='" . $country . "' ORDER BY id ASC"; $r = pg_query($readOnlyReplicaConn, $q); $result = []; while ($f = pg_fetch_assoc($r)) { $result[$country][] = $f['id']; } return $result; }