From 99b23782ef871d774e0c4abffc3632ea70ee149a Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 4 Dec 2009 15:30:43 -0500 Subject: let Geonames clients use commercial Web service --- plugins/GeonamesPlugin.php | 59 +++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 22 deletions(-) (limited to 'plugins') diff --git a/plugins/GeonamesPlugin.php b/plugins/GeonamesPlugin.php index 340a6f0bf..fdbf79e41 100644 --- a/plugins/GeonamesPlugin.php +++ b/plugins/GeonamesPlugin.php @@ -51,6 +51,10 @@ class GeonamesPlugin extends Plugin { const LOCATION_NS = 1; + public $host = 'ws.geonames.org'; + public $username = null; + public $token = null; + /** * convert a name into a Location object * @@ -75,12 +79,11 @@ class GeonamesPlugin extends Plugin // XXX: break down a name by commas, narrow by each - $str = http_build_query(array('maxRows' => 1, - 'q' => $name, - 'lang' => $language, - 'type' => 'json')); - - $result = $client->get('http://ws.geonames.org/search?'.$str); + $result = $client->get($this->wsUrl('search', + array('maxRows' => 1, + 'q' => $name, + 'lang' => $language, + 'type' => 'json'))); if ($result->isOk()) { $rj = json_decode($result->getBody()); @@ -135,10 +138,9 @@ class GeonamesPlugin extends Plugin $client = HTTPClient::start(); - $str = http_build_query(array('geonameId' => $id, - 'lang' => $language)); - - $result = $client->get('http://ws.geonames.org/hierarchyJSON?'.$str); + $result = $client->get($this->wsUrl('hierarchyJSON', + array('geonameId' => $id, + 'lang' => $language))); if ($result->isOk()) { @@ -205,12 +207,11 @@ class GeonamesPlugin extends Plugin $client = HTTPClient::start(); - $str = http_build_query(array('lat' => $lat, - 'lng' => $lon, - 'lang' => $language)); - $result = - $client->get('http://ws.geonames.org/findNearbyPlaceNameJSON?'.$str); + $client->get($this->wsUrl('findNearbyPlaceNameJSON', + array('lat' => $lat, + 'lng' => $lon, + 'lang' => $language))); if ($result->isOk()) { @@ -286,10 +287,9 @@ class GeonamesPlugin extends Plugin $client = HTTPClient::start(); - $str = http_build_query(array('geonameId' => $location->location_id, - 'lang' => $language)); - - $result = $client->get('http://ws.geonames.org/hierarchyJSON?'.$str); + $result = $client->get($this->wsUrl('hierarchyJSON', + array('geonameId' => $location->location_id, + 'lang' => $language))); if ($result->isOk()) { @@ -376,7 +376,7 @@ class GeonamesPlugin extends Plugin { $c = common_memcache(); - if (!$c) { + if (empty($c)) { return null; } @@ -387,7 +387,7 @@ class GeonamesPlugin extends Plugin { $c = common_memcache(); - if (!$c) { + if (empty($c)) { return null; } @@ -398,7 +398,7 @@ class GeonamesPlugin extends Plugin { $c = common_memcache(); - if (!$c) { + if (empty($c)) { return null; } @@ -411,4 +411,19 @@ class GeonamesPlugin extends Plugin implode(',', array_keys($attrs)) . ':'. common_keyize(implode(',', array_values($attrs)))); } + + function wsUrl($method, $params) + { + if (!empty($this->username)) { + $params['username'] = $this->username; + } + + if (!empty($this->token)) { + $params['token'] = $this->token; + } + + $str = http_build_query($params); + + return 'http://'.$this->host.'/'.$method.'?'.$str; + } } -- cgit v1.2.3-54-g00ecf From 41f10b742c4c3c21a8783050c6bb55dad17f050d Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 4 Dec 2009 15:34:41 -0500 Subject: remove duplicate log() function from RealtimePlugin --- plugins/Realtime/RealtimePlugin.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'plugins') diff --git a/plugins/Realtime/RealtimePlugin.php b/plugins/Realtime/RealtimePlugin.php index cbfa6bae0..030df405b 100644 --- a/plugins/Realtime/RealtimePlugin.php +++ b/plugins/Realtime/RealtimePlugin.php @@ -120,7 +120,7 @@ class RealtimePlugin extends Plugin function onEndShowStatusNetStyles($action) { - $action->cssLink(common_path('plugins/Realtime/realtimeupdate.css'), + $action->cssLink(common_path('plugins/Realtime/realtimeupdate.css'), null, 'screen, projection, tv'); return true; } @@ -293,13 +293,6 @@ class RealtimePlugin extends Plugin return $tags; } - // Push this up to Plugin - - function log($level, $msg) - { - common_log($level, get_class($this) . ': '.$msg); - } - function _getScripts() { return array('plugins/Realtime/realtimeupdate.js', -- cgit v1.2.3-54-g00ecf From 098426cee901d264332c0ad0dd2155d30487d35f Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 4 Dec 2009 16:25:05 -0500 Subject: configurable expiry for cached Geonames data defaults to 90 days --- plugins/GeonamesPlugin.php | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'plugins') diff --git a/plugins/GeonamesPlugin.php b/plugins/GeonamesPlugin.php index fdbf79e41..a750f1242 100644 --- a/plugins/GeonamesPlugin.php +++ b/plugins/GeonamesPlugin.php @@ -54,6 +54,7 @@ class GeonamesPlugin extends Plugin public $host = 'ws.geonames.org'; public $username = null; public $token = null; + public $expiry = 7776000; // 90-day expiry /** * convert a name into a Location object @@ -197,6 +198,9 @@ class GeonamesPlugin extends Plugin function onLocationFromLatLon($lat, $lon, $language, &$location) { + $lat = rtrim($lat, "0"); + $lon = rtrim($lon, "0"); + $loc = $this->getCache(array('lat' => $lat, 'lon' => $lon)); @@ -380,7 +384,11 @@ class GeonamesPlugin extends Plugin return null; } - return $c->get($this->cacheKey($attrs)); + $key = $this->cacheKey($attrs); + + $value = $c->get($key); + + return $value; } function setCache($attrs, $loc) @@ -391,18 +399,11 @@ class GeonamesPlugin extends Plugin return null; } - $c->set($this->cacheKey($attrs), $loc); - } - - function clearCache($attrs) - { - $c = common_memcache(); + $key = $this->cacheKey($attrs); - if (empty($c)) { - return null; - } + $result = $c->set($key, $loc, 0, time() + $this->expiry); - $c->delete($this->cacheKey($attrs)); + return $result; } function cacheKey($attrs) -- cgit v1.2.3-54-g00ecf