summaryrefslogtreecommitdiff
path: root/plugins/GeonamesPlugin.php
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/GeonamesPlugin.php')
-rw-r--r--plugins/GeonamesPlugin.php80
1 files changed, 48 insertions, 32 deletions
diff --git a/plugins/GeonamesPlugin.php b/plugins/GeonamesPlugin.php
index 340a6f0bf..a750f1242 100644
--- a/plugins/GeonamesPlugin.php
+++ b/plugins/GeonamesPlugin.php
@@ -51,6 +51,11 @@ class GeonamesPlugin extends Plugin
{
const LOCATION_NS = 1;
+ public $host = 'ws.geonames.org';
+ public $username = null;
+ public $token = null;
+ public $expiry = 7776000; // 90-day expiry
+
/**
* convert a name into a Location object
*
@@ -75,12 +80,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 +139,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()) {
@@ -195,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));
@@ -205,12 +211,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 +291,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,33 +380,30 @@ class GeonamesPlugin extends Plugin
{
$c = common_memcache();
- if (!$c) {
+ if (empty($c)) {
return null;
}
- return $c->get($this->cacheKey($attrs));
+ $key = $this->cacheKey($attrs);
+
+ $value = $c->get($key);
+
+ return $value;
}
function setCache($attrs, $loc)
{
$c = common_memcache();
- if (!$c) {
+ if (empty($c)) {
return null;
}
- $c->set($this->cacheKey($attrs), $loc);
- }
-
- function clearCache($attrs)
- {
- $c = common_memcache();
+ $key = $this->cacheKey($attrs);
- if (!$c) {
- return null;
- }
+ $result = $c->set($key, $loc, 0, time() + $this->expiry);
- $c->delete($this->cacheKey($attrs));
+ return $result;
}
function cacheKey($attrs)
@@ -411,4 +412,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;
+ }
}