summaryrefslogtreecommitdiff
path: root/plugins/GeonamesPlugin.php
diff options
context:
space:
mode:
authorBrenda Wallace <shiny@cpan.org>2009-12-28 01:57:51 +0000
committerBrenda Wallace <shiny@cpan.org>2009-12-28 01:57:51 +0000
commitf4459dfedcdea2f2a6078bedf9530deb45b6d52c (patch)
tree942db6159710f5dd17e0c43a5659e1eec027a9e6 /plugins/GeonamesPlugin.php
parent9d3829df9d29581c1d0281e57fda8ba4452ce2c2 (diff)
parentce8c69a49fc6668a19886ebc354ea5bef808d12a (diff)
Merge commit 'origin/0.9.x' into 0.9.x
Diffstat (limited to 'plugins/GeonamesPlugin.php')
-rw-r--r--plugins/GeonamesPlugin.php265
1 files changed, 132 insertions, 133 deletions
diff --git a/plugins/GeonamesPlugin.php b/plugins/GeonamesPlugin.php
index a750f1242..805166eaa 100644
--- a/plugins/GeonamesPlugin.php
+++ b/plugins/GeonamesPlugin.php
@@ -76,40 +76,33 @@ class GeonamesPlugin extends Plugin
return false;
}
- $client = HTTPClient::start();
-
- // XXX: break down a name by commas, narrow by each
-
- $result = $client->get($this->wsUrl('search',
- array('maxRows' => 1,
- 'q' => $name,
- 'lang' => $language,
- 'type' => 'json')));
-
- if ($result->isOk()) {
- $rj = json_decode($result->getBody());
- if (count($rj->geonames) > 0) {
- $n = $rj->geonames[0];
+ try {
+ $geonames = $this->getGeonames('search',
+ array('maxRows' => 1,
+ 'q' => $name,
+ 'lang' => $language,
+ 'type' => 'xml'));
+ } catch (Exception $e) {
+ $this->log(LOG_WARNING, "Error for $name: " . $e->getMessage());
+ return true;
+ }
- $location = new Location();
+ $n = $geonames[0];
- $location->lat = $n->lat;
- $location->lon = $n->lng;
- $location->names[$language] = $n->name;
- $location->location_id = $n->geonameId;
- $location->location_ns = self::LOCATION_NS;
+ $location = new Location();
- $this->setCache(array('name' => $name,
- 'language' => $language),
- $location);
+ $location->lat = (string)$n->lat;
+ $location->lon = (string)$n->lng;
+ $location->names[$language] = (string)$n->name;
+ $location->location_id = (string)$n->geonameId;
+ $location->location_ns = self::LOCATION_NS;
- // handled, don't continue processing!
- return false;
- }
- }
+ $this->setCache(array('name' => $name,
+ 'language' => $language),
+ $location);
- // Continue processing; we don't have the answer
- return true;
+ // handled, don't continue processing!
+ return false;
}
/**
@@ -137,46 +130,41 @@ class GeonamesPlugin extends Plugin
return false;
}
- $client = HTTPClient::start();
-
- $result = $client->get($this->wsUrl('hierarchyJSON',
- array('geonameId' => $id,
- 'lang' => $language)));
-
- if ($result->isOk()) {
-
- $rj = json_decode($result->getBody());
-
- if (count($rj->geonames) > 0) {
+ try {
+ $geonames = $this->getGeonames('hierarchy',
+ array('geonameId' => $id,
+ 'lang' => $language));
+ } catch (Exception $e) {
+ $this->log(LOG_WARNING, "Error for ID $id: " . $e->getMessage());
+ return false;
+ }
- $parts = array();
+ $parts = array();
- foreach ($rj->geonames as $level) {
- if (in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
- $parts[] = $level->name;
- }
- }
+ foreach ($geonames as $level) {
+ if (in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
+ $parts[] = (string)$level->name;
+ }
+ }
- $last = $rj->geonames[count($rj->geonames)-1];
+ $last = $geonames[count($geonames)-1];
- if (!in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
- $parts[] = $last->name;
- }
+ if (!in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
+ $parts[] = (string)$last->name;
+ }
- $location = new Location();
+ $location = new Location();
- $location->location_id = $last->geonameId;
- $location->location_ns = self::LOCATION_NS;
- $location->lat = $last->lat;
- $location->lon = $last->lng;
- $location->names[$language] = implode(', ', array_reverse($parts));
+ $location->location_id = (string)$last->geonameId;
+ $location->location_ns = self::LOCATION_NS;
+ $location->lat = (string)$last->lat;
+ $location->lon = (string)$last->lng;
+ $location->names[$language] = implode(', ', array_reverse($parts));
- $this->setCache(array('id' => $last->geonameId),
- $location);
- }
- }
+ $this->setCache(array('id' => (string)$last->geonameId),
+ $location);
- // We're responsible for this NAMESPACE; nobody else
+ // We're responsible for this namespace; nobody else
// can resolve it
return false;
@@ -209,56 +197,46 @@ class GeonamesPlugin extends Plugin
return false;
}
- $client = HTTPClient::start();
-
- $result =
- $client->get($this->wsUrl('findNearbyPlaceNameJSON',
- array('lat' => $lat,
- 'lng' => $lon,
- 'lang' => $language)));
-
- if ($result->isOk()) {
-
- $rj = json_decode($result->getBody());
-
- if (count($rj->geonames) > 0) {
+ try {
+ $geonames = $this->getGeonames('findNearbyPlaceName',
+ array('lat' => $lat,
+ 'lng' => $lon,
+ 'lang' => $language));
+ } catch (Exception $e) {
+ $this->log(LOG_WARNING, "Error for coords $lat, $lon: " . $e->getMessage());
+ return true;
+ }
- $n = $rj->geonames[0];
+ $n = $geonames[0];
- $parts = array();
+ $parts = array();
- $location = new Location();
+ $location = new Location();
- $parts[] = $n->name;
+ $parts[] = (string)$n->name;
- if (!empty($n->adminName1)) {
- $parts[] = $n->adminName1;
- }
+ if (!empty($n->adminName1)) {
+ $parts[] = (string)$n->adminName1;
+ }
- if (!empty($n->countryName)) {
- $parts[] = $n->countryName;
- }
+ if (!empty($n->countryName)) {
+ $parts[] = (string)$n->countryName;
+ }
- $location->location_id = $n->geonameId;
- $location->location_ns = self::LOCATION_NS;
- $location->lat = $lat;
- $location->lon = $lon;
+ $location->location_id = (string)$n->geonameId;
+ $location->location_ns = self::LOCATION_NS;
+ $location->lat = (string)$lat;
+ $location->lon = (string)$lon;
- $location->names[$language] = implode(', ', $parts);
+ $location->names[$language] = implode(', ', $parts);
- $this->setCache(array('lat' => $lat,
- 'lon' => $lon),
- $location);
+ $this->setCache(array('lat' => $lat,
+ 'lon' => $lon),
+ $location);
- // Success! We handled it, so no further processing
+ // Success! We handled it, so no further processing
- return false;
- }
- }
-
- // For some reason we don't know, so pass.
-
- return true;
+ return false;
}
/**
@@ -281,7 +259,9 @@ class GeonamesPlugin extends Plugin
return true;
}
- $n = $this->getCache(array('id' => $location->location_id,
+ $id = $location->location_id;
+
+ $n = $this->getCache(array('id' => $id,
'language' => $language));
if (!empty($n)) {
@@ -289,47 +269,41 @@ class GeonamesPlugin extends Plugin
return false;
}
- $client = HTTPClient::start();
-
- $result = $client->get($this->wsUrl('hierarchyJSON',
- array('geonameId' => $location->location_id,
- 'lang' => $language)));
-
- if ($result->isOk()) {
-
- $rj = json_decode($result->getBody());
-
- if (count($rj->geonames) > 0) {
+ try {
+ $geonames = $this->getGeonames('hierarchy',
+ array('geonameId' => $id,
+ 'lang' => $language));
+ } catch (Exception $e) {
+ $this->log(LOG_WARNING, "Error for ID $id: " . $e->getMessage());
+ return false;
+ }
- $parts = array();
+ $parts = array();
- foreach ($rj->geonames as $level) {
- if (in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
- $parts[] = $level->name;
- }
- }
+ foreach ($geonames as $level) {
+ if (in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
+ $parts[] = (string)$level->name;
+ }
+ }
- $last = $rj->geonames[count($rj->geonames)-1];
+ $last = $geonames[count($geonames)-1];
- if (!in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
- $parts[] = $last->name;
- }
+ if (!in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
+ $parts[] = (string)$last->name;
+ }
- if (count($parts)) {
- $name = implode(', ', array_reverse($parts));
- $this->setCache(array('id' => $location->location_id,
- 'language' => $language),
- $name);
- return false;
- }
- }
+ if (count($parts)) {
+ $name = implode(', ', array_reverse($parts));
+ $this->setCache(array('id' => $id,
+ 'language' => $language),
+ $name);
}
- return true;
+ return false;
}
/**
- * Human-readable name for a location
+ * Human-readable URL for a location
*
* Given a location, we try to retrieve a geonames.org URL.
*
@@ -423,8 +397,33 @@ class GeonamesPlugin extends Plugin
$params['token'] = $this->token;
}
- $str = http_build_query($params);
+ $str = http_build_query($params, null, '&');
return 'http://'.$this->host.'/'.$method.'?'.$str;
}
+
+ function getGeonames($method, $params)
+ {
+ $client = HTTPClient::start();
+
+ $result = $client->get($this->wsUrl($method, $params));
+
+ if (!$result->isOk()) {
+ throw new Exception("HTTP error code " . $result->code);
+ }
+
+ $document = new SimpleXMLElement($result->getBody());
+
+ if (empty($document)) {
+ throw new Exception("No results in response");
+ }
+
+ if (isset($document->status)) {
+ throw new Exception("Error #".$document->status['value']." ('".$document->status['message']."')");
+ }
+
+ // Array of elements
+
+ return $document->geoname;
+ }
}