+
+ function getGeonames($method, $params)
+ {
+ if ($this->lastTimeout && (time() - $this->lastTimeout < $this->timeoutWindow)) {
+ throw new Exception("skipping due to recent web service timeout");
+ }
+
+ $client = HTTPClient::start();
+ $client->setConfig('connect_timeout', $this->timeout);
+ $client->setConfig('timeout', $this->timeout);
+
+ try {
+ $result = $client->get($this->wsUrl($method, $params));
+ } catch (Exception $e) {
+ common_log(LOG_ERR, __METHOD__ . ": " . $e->getMessage());
+ $this->lastTimeout = time();
+ throw $e;
+ }
+
+ if (!$result->isOk()) {
+ throw new Exception("HTTP error code " . $result->getStatus());
+ }
+
+ $body = $result->getBody();
+
+ if (empty($body)) {
+ throw new Exception("Empty HTTP body in response");
+ }
+
+ // This will throw an exception if the XML is mal-formed
+
+ $document = new SimpleXMLElement($body);
+
+ // No children, usually no results
+
+ $children = $document->children();
+
+ if (count($children) == 0) {
+ return array();
+ }
+
+ if (isset($document->status)) {
+ throw new Exception("Error #".$document->status['value']." ('".$document->status['message']."')");
+ }
+
+ // Array of elements, >0 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 <a href="http://geonames.org/">Geonames</a> service to get human-readable '.
+ 'names for locations based on user-provided lat/long pairs.'));
+ return true;
+ }
+
+ function canonical($coord)
+ {
+ $coord = rtrim($coord, "0");
+ $coord = rtrim($coord, ".");
+
+ return $coord;
+ }