From 47329fb2e77e635080a232c278d736892354a43e Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sat, 20 Jun 2015 18:40:05 +0200 Subject: [PATCH 1/4] New plugin to add location data for posts that only contain latitude and longitude. --- geocoordinates/geocoordinates.php | 89 ++++++++++++++++++++++++++++++ geocoordinates/templates/admin.tpl | 3 + geocoordinates/test.php | 69 +++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 geocoordinates/geocoordinates.php create mode 100644 geocoordinates/templates/admin.tpl create mode 100644 geocoordinates/test.php diff --git a/geocoordinates/geocoordinates.php b/geocoordinates/geocoordinates.php new file mode 100644 index 00000000..52dd8d75 --- /dev/null +++ b/geocoordinates/geocoordinates.php @@ -0,0 +1,89 @@ + + */ + +function geocoordinates_install() { + register_hook('post_local', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook'); + register_hook('post_remote', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook'); +} + + +function geocoordinates_uninstall() { + unregister_hook('post_local', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook'); + unregister_hook('post_remote', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook'); +} + +function geocoordinates_resolve_item(&$item) { + if((!$item["coord"]) || ($item["location"])) + return; + + $key = get_config("geocoordinates", "api_key"); + if ($key == "") + return; + + $language = get_config("geocoordinates", "language"); + if ($language == "") + $language = "de"; + + $result = Cache::get("geocoordinates:".$language.":".$item["coord"]); + if (!is_null($result)) { + $item["location"] = $result; + return; + } + + $coords = explode(' ',$item["coord"]); + + $s = fetch_url("https://api.opencagedata.com/geocode/v1/json?q=".$coords[0].",".$coords[1]."&key=".$key."&language=".$language); + + if (!$s) { + logger("API could not be queried", LOGGER_DEBUG); + return; + } + + $data = json_decode($s); + + if ($data->status->code != "200") { + logger("API returned error ".$data->status->code." ".$data->status->message, LOGGER_DEBUG); + return; + } + + if (($data->total_results == 0) OR (count($data->results) == 0)) { + logger("No results found", LOGGER_DEBUG); + return; + } + + $item["location"] = $data->results[0]->formatted; + + logger("Got location for coordinates ".$item["coord"].": ".$item["location"], LOGGER_DEBUG); + + if ($item["location"] != "") + Cache::set("geocoordinates:".$language.":".$item["coord"], $item["location"]); +} + +function geocoordinates_post_hook($a, &$item) { + geocoordinates_resolve_item($item); +} + +function geocoordinates_plugin_admin(&$a,&$o) { + + $t = get_markup_template("admin.tpl", "addon/geocoordinates/"); + + $o = replace_macros($t, array( + '$submit' => t('Save Settings'), + '$api_key' => array('api_key', t('API Key'), get_config('geocoordinates', 'api_key' ), ''), + '$language' => array('language', t('Language code (IETF format)'), get_config('geocoordinates', 'language' ), ''), + )); +} + +function geocoordinates_plugin_admin_post(&$a) { + $api_key = ((x($_POST,'api_key')) ? notags(trim($_POST['api_key'])) : ''); + set_config('geocoordinates','api_key',$api_key); + + $language = ((x($_POST,'language')) ? notags(trim($_POST['language'])) : ''); + set_config('geocoordinates','language',$language); + info(t('Settings updated.'). EOL); +} diff --git a/geocoordinates/templates/admin.tpl b/geocoordinates/templates/admin.tpl new file mode 100644 index 00000000..dc173b20 --- /dev/null +++ b/geocoordinates/templates/admin.tpl @@ -0,0 +1,3 @@ +{{include file="field_input.tpl" field=$api_key}} +{{include file="field_input.tpl" field=$language}} +
diff --git a/geocoordinates/test.php b/geocoordinates/test.php new file mode 100644 index 00000000..a215522a --- /dev/null +++ b/geocoordinates/test.php @@ -0,0 +1,69 @@ +set_baseurl(get_config('system','url')); + +require_once("addon/geocoordinates/geocoordinates.php"); + +function geocoordinates_resolve_item2(&$item) { + if((!$item["coord"]) || ($item["location"])) + return; + + $key = get_config("geocoordinates", "api_key"); + if ($key == "") + return; + + $language = get_config("geocoordinates", "language"); + if ($language == "") + $language = "de"; + + $result = Cache::get("geocoordinates:".$language.":".$item["coord"]); + if (!is_null($result)) { + $item["location"] = $result; + return; + } + + $coords = explode(' ',$item["coord"]); + + $s = fetch_url("https://api.opencagedata.com/geocode/v1/json?q=".$coords[0].",".$coords[1]."&key=".$key."&language=".$language); + + if (!$s) { + logger("API could not be queried", LOGGER_DEBUG); + return; + } + + $data = json_decode($s); + + if ($data->status->code != "200") { + logger("API returned error ".$data->status->code." ".$data->status->message, LOGGER_DEBUG); + return; + } + + if (($data->total_results == 0) OR (count($data->results) == 0)) { + logger("No results found", LOGGER_DEBUG); + return; + } + + $item["location"] = $data->results[0]->formatted; + + Cache::set("geocoordinates:".$language.":".$item["coord"], $item["location"]); + +} + +$r = q("SELECT coord, location FROM item WHERE guid='stat1721635584fdaf31b19541063667' LIMIT 1"); +$item = $r[0]; +geocoordinates_resolve_item2($item); +print_r($item); +?> From b9a9c6bde3b6054ca2702bcd276aa01cdf44f5e8 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sat, 20 Jun 2015 22:29:01 +0200 Subject: [PATCH 2/4] Enhanced logger --- geocoordinates/geocoordinates.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/geocoordinates/geocoordinates.php b/geocoordinates/geocoordinates.php index 52dd8d75..d3c7e5af 100644 --- a/geocoordinates/geocoordinates.php +++ b/geocoordinates/geocoordinates.php @@ -52,7 +52,7 @@ function geocoordinates_resolve_item(&$item) { } if (($data->total_results == 0) OR (count($data->results) == 0)) { - logger("No results found", LOGGER_DEBUG); + logger("No results found for coordinates ".$item["coord"], LOGGER_DEBUG); return; } From 1b9fee043c0ad681116c225cb86596c63519735b Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 21 Jun 2015 01:58:19 +0200 Subject: [PATCH 3/4] Round the coordinates --- geocoordinates/geocoordinates.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/geocoordinates/geocoordinates.php b/geocoordinates/geocoordinates.php index d3c7e5af..ff726425 100644 --- a/geocoordinates/geocoordinates.php +++ b/geocoordinates/geocoordinates.php @@ -29,14 +29,20 @@ function geocoordinates_resolve_item(&$item) { if ($language == "") $language = "de"; - $result = Cache::get("geocoordinates:".$language.":".$item["coord"]); + $coords = explode(' ',$item["coord"]); + + if (count($coords) < 2) + return; + + $coords[0] = round($coords[0], 5); + $coords[1] = round($coords[1], 5); + + $result = Cache::get("geocoordinates:".$language.":".$coords[0]."-".$coords[1]); if (!is_null($result)) { $item["location"] = $result; return; } - $coords = explode(' ',$item["coord"]); - $s = fetch_url("https://api.opencagedata.com/geocode/v1/json?q=".$coords[0].",".$coords[1]."&key=".$key."&language=".$language); if (!$s) { @@ -58,10 +64,10 @@ function geocoordinates_resolve_item(&$item) { $item["location"] = $data->results[0]->formatted; - logger("Got location for coordinates ".$item["coord"].": ".$item["location"], LOGGER_DEBUG); + logger("Got location for coordinates ".$coords[0]."-".$coords[1].": ".$item["location"], LOGGER_DEBUG); if ($item["location"] != "") - Cache::set("geocoordinates:".$language.":".$item["coord"], $item["location"]); + Cache::set("geocoordinates:".$language.":".$coords[0]."-".$coords[1], $item["location"]); } function geocoordinates_post_hook($a, &$item) { From 68ca56471a86cb80373634cd697b06ba4579d232 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 21 Jun 2015 02:02:16 +0200 Subject: [PATCH 4/4] delete test file --- geocoordinates/test.php | 69 ----------------------------------------- 1 file changed, 69 deletions(-) delete mode 100644 geocoordinates/test.php diff --git a/geocoordinates/test.php b/geocoordinates/test.php deleted file mode 100644 index a215522a..00000000 --- a/geocoordinates/test.php +++ /dev/null @@ -1,69 +0,0 @@ -set_baseurl(get_config('system','url')); - -require_once("addon/geocoordinates/geocoordinates.php"); - -function geocoordinates_resolve_item2(&$item) { - if((!$item["coord"]) || ($item["location"])) - return; - - $key = get_config("geocoordinates", "api_key"); - if ($key == "") - return; - - $language = get_config("geocoordinates", "language"); - if ($language == "") - $language = "de"; - - $result = Cache::get("geocoordinates:".$language.":".$item["coord"]); - if (!is_null($result)) { - $item["location"] = $result; - return; - } - - $coords = explode(' ',$item["coord"]); - - $s = fetch_url("https://api.opencagedata.com/geocode/v1/json?q=".$coords[0].",".$coords[1]."&key=".$key."&language=".$language); - - if (!$s) { - logger("API could not be queried", LOGGER_DEBUG); - return; - } - - $data = json_decode($s); - - if ($data->status->code != "200") { - logger("API returned error ".$data->status->code." ".$data->status->message, LOGGER_DEBUG); - return; - } - - if (($data->total_results == 0) OR (count($data->results) == 0)) { - logger("No results found", LOGGER_DEBUG); - return; - } - - $item["location"] = $data->results[0]->formatted; - - Cache::set("geocoordinates:".$language.":".$item["coord"], $item["location"]); - -} - -$r = q("SELECT coord, location FROM item WHERE guid='stat1721635584fdaf31b19541063667' LIMIT 1"); -$item = $r[0]; -geocoordinates_resolve_item2($item); -print_r($item); -?>