summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorEvan Prodromou <evan@status.net>2009-12-04 15:30:43 -0500
committerEvan Prodromou <evan@status.net>2009-12-04 15:30:43 -0500
commit99b23782ef871d774e0c4abffc3632ea70ee149a (patch)
tree2a47431a009abde1701eb3f30513839543796f8d /plugins
parente7e9dfceb4fd6db81bf266d71a3ec0b1933fbd02 (diff)
let Geonames clients use commercial Web service
Diffstat (limited to 'plugins')
-rw-r--r--plugins/GeonamesPlugin.php59
1 files changed, 37 insertions, 22 deletions
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;
+ }
}