+
+ /**
+ * Machine-readable name for a location
+ *
+ * Given a location, we try to retrieve a geonames.org URL.
+ *
+ * @param Location $location Location to get the url for
+ * @param string &$url Place to put the url
+ *
+ * @return boolean whether to continue
+ */
+
+ function onLocationRdfUrl($location, &$url)
+ {
+ if ($location->location_ns != self::LOCATION_NS) {
+ // It's not one of our IDs... keep processing
+ return true;
+ }
+
+ $url = 'http://sw.geonames.org/' . $location->location_id . '/';
+
+ // it's been filled, so don't process further.
+ return false;
+ }
+
+ function getCache($attrs)
+ {
+ $c = common_memcache();
+
+ if (empty($c)) {
+ return null;
+ }
+
+ $key = $this->cacheKey($attrs);
+
+ $value = $c->get($key);
+
+ return $value;
+ }
+
+ function setCache($attrs, $loc)
+ {
+ $c = common_memcache();
+
+ if (empty($c)) {
+ return null;
+ }
+
+ $key = $this->cacheKey($attrs);
+
+ $result = $c->set($key, $loc, 0, time() + $this->expiry);
+
+ return $result;
+ }
+
+ function cacheKey($attrs)
+ {
+ $key = 'geonames:' .
+ implode(',', array_keys($attrs)) . ':'.
+ common_keyize(implode(',', array_values($attrs)));
+ if ($this->cachePrefix) {
+ return $this->cachePrefix . ':' . $key;
+ } else {
+ return common_cache_key($key);
+ }
+ }
+
+ 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)
+ {
+ 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;
+ }