diff options
author | Evan Prodromou <evan@status.net> | 2009-12-23 09:26:43 -0800 |
---|---|---|
committer | Evan Prodromou <evan@status.net> | 2009-12-23 09:26:59 -0800 |
commit | 15b9f61842e899e4463eb6f058228d9ffd631198 (patch) | |
tree | 101911f2a505e64caee66381e9c7ce5b73e6a9e4 | |
parent | 725b2d0475ba3d0dcfafd48a191b4ddee09cae68 (diff) |
Better error notification for Geonames plugin
-rw-r--r-- | plugins/GeonamesPlugin.php | 211 |
1 files changed, 118 insertions, 93 deletions
diff --git a/plugins/GeonamesPlugin.php b/plugins/GeonamesPlugin.php index a750f1242..0d12c1cf7 100644 --- a/plugins/GeonamesPlugin.php +++ b/plugins/GeonamesPlugin.php @@ -86,30 +86,36 @@ class GeonamesPlugin extends Plugin 'lang' => $language, 'type' => 'json'))); - if ($result->isOk()) { - $rj = json_decode($result->getBody()); - if (count($rj->geonames) > 0) { - $n = $rj->geonames[0]; + if (!$result->isOk()) { + $this->log(LOG_WARNING, "Error code " . $result->code . + " from " . $this->host . " for $name"); + return true; + } - $location = new Location(); + $rj = json_decode($result->getBody()); - $location->lat = $n->lat; - $location->lon = $n->lng; - $location->names[$language] = $n->name; - $location->location_id = $n->geonameId; - $location->location_ns = self::LOCATION_NS; + if (count($rj->geonames) <= 0) { + $this->log(LOG_WARNING, "No results in response from " . + $this->host . " for $name"); + return true; + } - $this->setCache(array('name' => $name, - 'language' => $language), - $location); + $n = $rj->geonames[0]; - // handled, don't continue processing! - return false; - } - } + $location = new Location(); + + $location->lat = $n->lat; + $location->lon = $n->lng; + $location->names[$language] = $n->name; + $location->location_id = $n->geonameId; + $location->location_ns = self::LOCATION_NS; + + $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; } /** @@ -143,38 +149,46 @@ class GeonamesPlugin extends Plugin array('geonameId' => $id, 'lang' => $language))); - if ($result->isOk()) { + if (!$result->isOk()) { + $this->log(LOG_WARNING, + "Error code " . $result->code . + " from " . $this->host . " for ID $id"); + return false; + } - $rj = json_decode($result->getBody()); + $rj = json_decode($result->getBody()); - if (count($rj->geonames) > 0) { + if (count($rj->geonames) <= 0) { + $this->log(LOG_WARNING, + "No results in response from " . + $this->host . " for ID $id"); + return false; + } - $parts = array(); + $parts = array(); - foreach ($rj->geonames as $level) { - if (in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) { - $parts[] = $level->name; - } - } + foreach ($rj->geonames as $level) { + if (in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) { + $parts[] = $level->name; + } + } - $last = $rj->geonames[count($rj->geonames)-1]; + $last = $rj->geonames[count($rj->geonames)-1]; - if (!in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) { - $parts[] = $last->name; - } + if (!in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) { + $parts[] = $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 = $last->geonameId; + $location->location_ns = self::LOCATION_NS; + $location->lat = $last->lat; + $location->lon = $last->lng; + $location->names[$language] = implode(', ', array_reverse($parts)); - $this->setCache(array('id' => $last->geonameId), - $location); - } - } + $this->setCache(array('id' => $last->geonameId), + $location); // We're responsible for this NAMESPACE; nobody else // can resolve it @@ -217,48 +231,52 @@ class GeonamesPlugin extends Plugin 'lng' => $lon, 'lang' => $language))); - if ($result->isOk()) { - - $rj = json_decode($result->getBody()); - - if (count($rj->geonames) > 0) { + if (!$result->isOk()) { + $this->log(LOG_WARNING, + "Error code " . $result->code . + " from " . $this->host . " for coords $lat, $lon"); + return true; + } - $n = $rj->geonames[0]; + $rj = json_decode($result->getBody()); - $parts = array(); + if (count($rj->geonames) <= 0) { + $this->log(LOG_WARNING, + "No results in response from " . + $this->host . " for coords $lat, $lon"); + return true; + } - $location = new Location(); + $n = $rj->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[] = $n->name; - $location->location_id = $n->geonameId; - $location->location_ns = self::LOCATION_NS; - $location->lat = $lat; - $location->lon = $lon; + if (!empty($n->adminName1)) { + $parts[] = $n->adminName1; + } - $location->names[$language] = implode(', ', $parts); + if (!empty($n->countryName)) { + $parts[] = $n->countryName; + } - $this->setCache(array('lat' => $lat, - 'lon' => $lon), - $location); + $location->location_id = $n->geonameId; + $location->location_ns = self::LOCATION_NS; + $location->lat = $lat; + $location->lon = $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; } /** @@ -295,37 +313,44 @@ class GeonamesPlugin extends Plugin array('geonameId' => $location->location_id, 'lang' => $language))); - if ($result->isOk()) { + if (!$result->isOk()) { + $this->log(LOG_WARNING, + "Error code " . $result->code . + " from " . $this->host . " for ID " . $location->location_id); + return false; + } - $rj = json_decode($result->getBody()); + $rj = json_decode($result->getBody()); - if (count($rj->geonames) > 0) { + if (count($rj->geonames) <= 0) { + $this->log(LOG_WARNING, + "No results " . + " from " . $this->host . " for ID " . $location->location_id); + return false; + } - $parts = array(); + $parts = array(); - foreach ($rj->geonames as $level) { - if (in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) { - $parts[] = $level->name; - } - } + foreach ($rj->geonames as $level) { + if (in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) { + $parts[] = $level->name; + } + } - $last = $rj->geonames[count($rj->geonames)-1]; + $last = $rj->geonames[count($rj->geonames)-1]; - if (!in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) { - $parts[] = $last->name; - } + if (!in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) { + $parts[] = $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' => $location->location_id, + 'language' => $language), + $name); } - return true; + return false; } /** |