Private GIT

Skip to content
Snippets Groups Projects
Unverified Commit f6be1057 authored by Gary Allan's avatar Gary Allan
Browse files

Bugfix: OpenStreetMap->search_geo_cache doesn't ever expire cached entries

Cache geo lookup results for 1 day.
Fix code formatting.

Fixes #3486
parent c9a8aa63
No related branches found
No related tags found
No related merge requests found
......@@ -23,7 +23,8 @@
* Service can be switched via SQL "UPDATE nominatim SET url='https://newurl/search' WHERE id=1;";
*/
class OpenStreetMap extends Common_functions {
class OpenStreetMap extends Common_functions
{
/**
* List of locations & circuits added to map for de-duplication
......@@ -53,7 +54,8 @@ class OpenStreetMap extends Common_functions {
* @param Database_PDO $database
* @return void
*/
public function __construct (Database_PDO $database) {
public function __construct(Database_PDO $database)
{
parent::__construct();
$this->Database = $database;
......@@ -68,7 +70,8 @@ class OpenStreetMap extends Common_functions {
* @param StdClass $object
* @return bool
*/
private function validate_lat_long($object) {
private function validate_lat_long($object)
{
if (!is_object($object) || !property_exists($object, 'lat') || !property_exists($object, 'long')) {
return false;
}
......@@ -84,7 +87,8 @@ class OpenStreetMap extends Common_functions {
* @param StdClass $location
* @return bool
*/
public function add_location ($location) {
public function add_location($location)
{
return $this->add_object($location, 'locations');
}
......@@ -94,7 +98,8 @@ class OpenStreetMap extends Common_functions {
* @param StdClass $location
* @return bool
*/
public function add_customer ($customer) {
public function add_customer($customer)
{
return $this->add_object($customer, 'customers');
}
......@@ -105,7 +110,8 @@ class OpenStreetMap extends Common_functions {
* @param string $type
* @return bool
*/
private function add_object($object, $type) {
private function add_object($object, $type)
{
if (!$this->validate_lat_long($object)) {
return false;
}
......@@ -133,7 +139,8 @@ class OpenStreetMap extends Common_functions {
$popuptxt .= is_string($desc) ? "<span class=\'text-muted\'>" . $desc . "</span>" : "";
$popuptxt = str_replace(["\r\n", "\n", "\r"], "<br>", $popuptxt);
$this->geodata[] = ["type"=> "Feature",
$this->geodata[] = [
"type" => "Feature",
"properties" => ["name" => $title, "popupContent" => $popuptxt],
"geometry" => ["type" => "Point", "coordinates" => [$object->long, $object->lat]]
];
......@@ -149,7 +156,8 @@ class OpenStreetMap extends Common_functions {
* @param StdClass $type Circuit circuitType object (color & dotted)
* @return bool
*/
public function add_circuit($location1, $location2, $type) {
public function add_circuit($location1, $location2, $type)
{
$this->add_location($location1);
$this->add_location($location2);
......@@ -179,7 +187,8 @@ class OpenStreetMap extends Common_functions {
* @param null|int $height
* @return void
*/
public function map($height=null) {
public function map($height = null)
{
if (sizeof($this->geodata) == 0) {
$this->Result->show("info", _("No Locations with coordinates configured"), false);
return;
......@@ -238,9 +247,14 @@ class OpenStreetMap extends Common_functions {
for (var key in polydata) {
var fmt = key.split('::::');
if (fmt[1] == "Solid") {
L.polyline(polydata[key], {'color': fmt[0]}).addTo(map);
L.polyline(polydata[key], {
'color': fmt[0]
}).addTo(map);
} else {
L.polyline(polydata[key], {'color': fmt[0], dashArray: '20, 10'}).addTo(map);
L.polyline(polydata[key], {
'color': fmt[0],
dashArray: '20, 10'
}).addTo(map);
}
};
......@@ -263,12 +277,13 @@ class OpenStreetMap extends Common_functions {
* @param string $address
* @return string
*/
private function hash_from_address ($address) {
private function hash_from_address($address)
{
$address_min = preg_replace('#\s+#', ' ', mb_strtolower($address));
$hash = openssl_digest(trim($address_min), 'sha256', true);
if (!is_string($hash) || strlen($hash) != 32) {
throw new Exception(_('openssl_digest failure'));
throw new \Exception(_('openssl_digest failure'));
}
return $hash;
......@@ -278,11 +293,19 @@ class OpenStreetMap extends Common_functions {
* Search for cached results
*
* @param string $address
* @param string $only_recent
* @return StdClass|false
*/
private function search_geo_cache ($address) {
private function search_geo_cache($address, $only_recent = true)
{
$hash = $this->hash_from_address($address);
$cached_result = $this->Database->getObjectQuery('SELECT * FROM nominatim_cache WHERE sha256=?;', [$hash]);
if ($only_recent) {
$query = 'SELECT * FROM nominatim_cache WHERE sha256=? AND date > DATE_SUB(NOW(), INTERVAL 1 DAY);';
} else {
$query = 'SELECT * FROM nominatim_cache WHERE sha256=?;';
}
$cached_result = $this->Database->getObjectQuery($query, [$hash]);
return is_object($cached_result) ? $cached_result : false;
}
......@@ -294,16 +317,19 @@ class OpenStreetMap extends Common_functions {
* @param string $json
* @return void
*/
private function update_geo_cache ($address, $json) {
private function update_geo_cache($address, $json)
{
if (!is_string($address) || !is_string($json)) {
return;
}
$values = ['sha256' => $this->hash_from_address($address),
$values = [
'sha256' => $this->hash_from_address($address),
'query' => $address,
'lat_lng' => $json];
'lat_lng' => $json
];
$this->Database->insertObject('nominatim_cache', $values);
$this->Database->insertObject('nominatim_cache', $values, false, true);
}
/**
......@@ -312,7 +338,8 @@ class OpenStreetMap extends Common_functions {
* @param string $address
* @return array
*/
public function get_latlng_from_address ($address) {
public function get_latlng_from_address($address)
{
$results = ['lat' => null, 'lng' => null, 'error' => null];
if (!is_string($address) || strlen($address) == 0) {
......@@ -331,7 +358,8 @@ class OpenStreetMap extends Common_functions {
$elapsed = -microtime(true);
$cached_result = $this->search_geo_cache($address);
// Check cached results from the last 24h
$cached_result = $this->search_geo_cache($address, true);
if ($cached_result) {
$json = json_decode($cached_result->lat_lng, true);
if (is_array($json)) {
......@@ -341,20 +369,31 @@ class OpenStreetMap extends Common_functions {
$url = $Lock->locked_row->url;
$url = $url . "?format=json&q=" . rawurlencode($address);
$headers = ['User-Agent: phpIPAM/'.VERSION_VISIBLE.' (Open source IP address management)',
'Referer: '.$this->createURL().create_link()];
$headers = [
'User-Agent: phpIPAM/' . VERSION_VISIBLE . ' (Open source IP address management)',
'Referer: ' . $this->createURL() . create_link()
];
// fetch geocoding data with proxy settings from config.php
$lookup = $this->curl_fetch_url($url, $headers);
if ($lookup['result_code'] != 200) {
throw new Exception($lookup['error_msg']);
// Lookup failed - Check cache again with no time limit.
$cached_result = $this->search_geo_cache($address, false);
if ($cached_result) {
$json = json_decode($cached_result->lat_lng, true);
if (is_array($json)) {
return $json;
}
}
throw new \Exception($lookup['error_msg']);
}
$geo = json_decode($lookup['result'], true);
if (!is_array($geo)) {
throw new Exception(_('Invalid json response from nominatim'));
throw new \Exception(_('Invalid json response from nominatim'));
}
if (isset($geo['0']['lat']) && isset($geo['0']['lon'])) {
......@@ -363,8 +402,7 @@ class OpenStreetMap extends Common_functions {
}
$this->update_geo_cache($address, json_encode($results));
} catch (Exception $e) {
} catch (\Exception $e) {
$results = ['lat' => null, 'lng' => null, 'error' => $e->getMessage()];
}
......@@ -379,5 +417,4 @@ class OpenStreetMap extends Common_functions {
return $results;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment