X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=plugins%2FGeonamesPlugin.php;h=52cc9c97f94b48a52c7dcdaae09a3e0eb97b371c;hb=f043bc62a53250eaf383e9f36f95031fcd4e1160;hp=340a6f0bfa916215f10915a3e097a3017b265b76;hpb=d645db38ae4ff4179f7fc31d4501d48054c4b8e6;p=quix0rs-gnu-social.git diff --git a/plugins/GeonamesPlugin.php b/plugins/GeonamesPlugin.php index 340a6f0bfa..52cc9c97f9 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 * @@ -71,41 +76,33 @@ class GeonamesPlugin extends Plugin return false; } - $client = HTTPClient::start(); - - // 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); - - 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; } /** @@ -133,47 +130,41 @@ class GeonamesPlugin extends Plugin return false; } - $client = HTTPClient::start(); - - $str = http_build_query(array('geonameId' => $id, - 'lang' => $language)); - - $result = $client->get('http://ws.geonames.org/hierarchyJSON?'.$str); - - 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; @@ -195,6 +186,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)); @@ -203,57 +197,46 @@ class GeonamesPlugin extends Plugin return false; } - $client = HTTPClient::start(); - - $str = http_build_query(array('lat' => $lat, - 'lng' => $lon, - 'lang' => $language)); - - $result = - $client->get('http://ws.geonames.org/findNearbyPlaceNameJSON?'.$str); - - if ($result->isOk()) { - - $rj = json_decode($result->getBody()); - - if (count($rj->geonames) > 0) { - - $n = $rj->geonames[0]; - - $parts = array(); + 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; + } - $location = new Location(); + $n = $geonames[0]; - $parts[] = $n->name; + $parts = array(); - if (!empty($n->adminName1)) { - $parts[] = $n->adminName1; - } + $location = new Location(); - if (!empty($n->countryName)) { - $parts[] = $n->countryName; - } + $parts[] = (string)$n->name; - $location->location_id = $n->geonameId; - $location->location_ns = self::LOCATION_NS; - $location->lat = $lat; - $location->lon = $lon; + if (!empty($n->adminName1)) { + $parts[] = (string)$n->adminName1; + } - $location->names[$language] = implode(', ', $parts); + if (!empty($n->countryName)) { + $parts[] = (string)$n->countryName; + } - $this->setCache(array('lat' => $lat, - 'lon' => $lon), - $location); + $location->location_id = (string)$n->geonameId; + $location->location_ns = self::LOCATION_NS; + $location->lat = (string)$lat; + $location->lon = (string)$lon; - // Success! We handled it, so no further processing + $location->names[$language] = implode(', ', $parts); - return false; - } - } + $this->setCache(array('lat' => $lat, + 'lon' => $lon), + $location); - // For some reason we don't know, so pass. + // Success! We handled it, so no further processing - return true; + return false; } /** @@ -276,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)) { @@ -284,48 +269,41 @@ class GeonamesPlugin extends Plugin return false; } - $client = HTTPClient::start(); - - $str = http_build_query(array('geonameId' => $location->location_id, - 'lang' => $language)); - - $result = $client->get('http://ws.geonames.org/hierarchyJSON?'.$str); - - 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. * @@ -376,33 +354,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); - } + $key = $this->cacheKey($attrs); - function clearCache($attrs) - { - $c = common_memcache(); + $result = $c->set($key, $loc, 0, time() + $this->expiry); - if (!$c) { - return null; - } - - $c->delete($this->cacheKey($attrs)); + return $result; } function cacheKey($attrs) @@ -411,4 +386,56 @@ 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, 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; + } + + function onPluginVersion(&$versions) + { + $versions[] = array('name' => 'Geonames', + 'version' => STATUSNET_VERSION, + 'author' => 'Evan Prodromou', + 'homepage' => 'http://status.net/wiki/Plugin:Geonames', + 'rawdescription' => + _m('Uses Geonames service to get human-readable '. + 'names for locations based on user-provided lat/long pairs.')); + return true; + } }