From 16bf5e5f4478e71ebbcb70153b9a05f3eef9f618 Mon Sep 17 00:00:00 2001 From: Paul Pound Date: Mon, 28 May 2018 11:35:59 -0300 Subject: [PATCH] added comments and coder fixes --- README.md | 1 - composer.json | 33 +++++---- islandora_herbarium_object.module | 28 ++++---- src/DataParser/DataParser.php | 30 ++++---- src/DataParser/DataParserInterface.php | 54 +++++++++++---- src/DataParser/GbifParser.php | 72 ++++++++++++------- src/DataParser/GeonamesParser.php | 55 ++++++++------- src/DataParser/IpniParser.php | 16 ++--- src/DataParser/NrcanParser.php | 96 ++++++++++++++++---------- src/DataParser/XmlDataParser.php | 26 ++++--- 10 files changed, 246 insertions(+), 165 deletions(-) diff --git a/README.md b/README.md index bd1297c..3568ba6 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,3 @@ ## License - diff --git a/composer.json b/composer.json index 3ec982b..67e1184 100644 --- a/composer.json +++ b/composer.json @@ -1,17 +1,20 @@ { - "name": "islandora/islandora_herbarium_object", - "description": "Islandora Herbarium", - "type": "drupal-module", - "minimum-stability": "dev", - "prefer-stable": true, - "keywords": ["Drupal", "Islandora"], - "license": "GPL-2.0+", - "homepage": "https://github.com/roblib/islandora_herbarium_object", - "support": { - "issues": "https://github.com/islandora-claw/CLAW/issues", - "irc": "irc://irc.freenode.net/islandora" - }, - "require": { - "islandora/islandora_collection": "dev-8.x-1.x" - } + "name": "islandora/islandora_herbarium_object", + "description": "Islandora Herbarium", + "type": "drupal-module", + "minimum-stability": "dev", + "prefer-stable": true, + "keywords": [ + "Drupal", + "Islandora" + ], + "license": "GPL-2.0+", + "homepage": "https://github.com/roblib/islandora_herbarium_object", + "support": { + "issues": "https://github.com/islandora-claw/CLAW/issues", + "irc": "irc://irc.freenode.net/islandora" + }, + "require": { + "islandora/islandora_collection": "dev-8.x-1.x" + } } diff --git a/islandora_herbarium_object.module b/islandora_herbarium_object.module index bfb879b..3c5c197 100644 --- a/islandora_herbarium_object.module +++ b/islandora_herbarium_object.module @@ -5,14 +5,13 @@ * Contains islandora_herbarium.module. */ -use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Routing\RouteMatchInterface; /** * Implements hook_help(). */ -function islandora_herbarium_object_help($route_name, RouteMatchInterface $route_match) -{ +function islandora_herbarium_object_help($route_name, RouteMatchInterface $route_match) { switch ($route_name) { // Main module help for the islandora_pdf module. case 'help.page.islandora_herbarium': @@ -31,7 +30,7 @@ function islandora_herbarium_object_help($route_name, RouteMatchInterface $route function islandora_herbarium_object_rdf_namespaces() { // Yes, it's amazing, rdf is not registered by default! return [ - 'dwc' => 'http://rs.tdwg.org/dwc/terms/', + 'dwc' => 'http://rs.tdwg.org/dwc/terms/', 'dwciri' => 'http://rs.tdwg.org/dwc/iri/', ]; } @@ -39,33 +38,30 @@ function islandora_herbarium_object_rdf_namespaces() { /** * Implements hook_entity_presave(). */ -function islandora_herbarium_object_entity_presave(Drupal\Core\Entity\EntityInterface $entity) -{ +function islandora_herbarium_object_entity_presave(EntityInterface $entity) { if ($entity->bundle() == 'islandora_herbarium_object') { // Get the geo data. try { $geo = \Drupal::service('islandora_herbarium_object.geoparser'); $geo->updateEntity($entity); - } catch (Exception $e) { + } + catch (Exception $e) { drupal_set_message(t('There was an error retrieving Geo data. %msg.', - array('%msg'=> $e->getMessage())), 'error'); + ['%msg' => $e->getMessage()]), 'error'); } // Get the taxonomy data. try { $taxon = \Drupal::service('islandora_herbarium_object.taxonparser'); $taxon->updateEntity($entity); - } catch(Exception $e) { + } + catch (Exception $e) { drupal_set_message(t('There was an error retrieving Taxon data. %msg.', - array('%msg'=> $e->getMessage())), 'error'); + ['%msg' => $e->getMessage()]), 'error'); } - $title = $taxon->getValue('canonicalName') . ' - ' . $geo->getValue('name'); - if($title != ' - ') { + $title = $taxon->getValue('canonicalName') . ' - ' . $geo->getValue('name'); + if ($title != ' - ') { $entity->setTitle($title); } - } } - - - diff --git a/src/DataParser/DataParser.php b/src/DataParser/DataParser.php index bce72c4..cea566f 100644 --- a/src/DataParser/DataParser.php +++ b/src/DataParser/DataParser.php @@ -1,35 +1,39 @@ get(trim($uri)); $data = $request->getBody()->getContents(); - if(empty($data)) { + if (empty($data)) { throw new Exception("Empty dataset"); } - if(!$returnAsVariable) { + if (!$returnAsVariable) { $this->data = $data; - } else { + } + else { return $data; } } + /** + * {@inheritdoc} + */ public function getValue($key) { - return isset($this->data_arr[$key]) ? $this->data_arr[$key] : ""; + return isset($this->dataArray[$key]) ? $this->dataArray[$key] : ""; } } diff --git a/src/DataParser/DataParserInterface.php b/src/DataParser/DataParserInterface.php index 9ad14be..79cc5c7 100644 --- a/src/DataParser/DataParserInterface.php +++ b/src/DataParser/DataParserInterface.php @@ -1,23 +1,53 @@ data, TRUE); - if(empty($arr['results'])) { + $arr = json_decode($this->data, TRUE); + if (empty($arr['results'])) { throw new \Exception('Error parsing json data.'); } - $this->data_arr = reset($arr['results']); + $this->dataArray = reset($arr['results']); } - public function updateEntity($entity) { + /** + * {@inheritdoc} + */ + public function updateEntity(EntityInterface $entity) { try { $taxoniri = $entity->get("field_dwciri_scientificname")->getValue(); $url = ''; - if(empty($url = $taxoniri[0]['value']) && !empty($entity->get('field_dwc_scientificname')->getValue())) { - $value = array($this->getGbifUrl($entity->get('field_dwc_scientificname')->getValue()[0]['value'])); + if (empty($url = $taxoniri[0]['value']) && !empty($entity->get('field_dwc_scientificname') + ->getValue())) { + $value = [ + $this->getGbifUrl($entity->get('field_dwc_scientificname') + ->getValue()[0]['value']), + ]; $taxoniri[] = ['value' => $value[0]]; $url = $taxoniri[0]['value']; - if(empty($url)) { + if (empty($url)) { throw new \Exception("No ScientificName URI or ScientificName text provided, cannot look up taxon data."); } $entity->set('field_dwciri_scientificname', $url); } $this->getRemoteData($url); $this->parseData(); - } catch (Exception $e) { + } + catch (Exception $e) { watchdog_exception('islandora_herbarium_object', $e, 'Error getting dwciri'); drupal_set_message(t('There was an error retrieving Taxon data. %msg. Verify the url is correct %url', - array('%msg'=> $e->getMessage(), '%url' => $taxoniri[0]['value'])),'error'); + [ + '%msg' => $e->getMessage(), + '%url' => $taxoniri[0]['value'], + ]), 'error'); return; } - $entity->set('field_dwc_scientificname', $this->getValue('canonicalName')); + $entity->set('field_dwc_scientificname', $this->getValue('canonicalName')); $name_arr = explode(' ', $this->getValue('canonicalName')); - $entity->set('field_dwc_specificepithet', $name_arr[1]); + $entity->set('field_dwc_specificepithet', $name_arr[1]); $entity->set('field_dwc_kingdom', $this->getValue('kingdom')); - $entity->set('field_dwc_genus', $this->getValue('genus')); - $entity->set('field_dwc_phylum', $this->getValue('phylum')); - $entity->set('field_dwc_order', $this->getValue('order')); - $entity->set('field_dwc_family', $this->getValue('family')); + $entity->set('field_dwc_genus', $this->getValue('genus')); + $entity->set('field_dwc_phylum', $this->getValue('phylum')); + $entity->set('field_dwc_order', $this->getValue('order')); + $entity->set('field_dwc_family', $this->getValue('family')); $entity->set('field_dwc_class', $this->getValue('class')); - $entity->set('field_dwc_vernacularname', $this->getValue('vernacularName')); + $entity->set('field_dwc_vernacularname', $this->getValue('vernacularName')); } - public function getGbifUrl($scientificName){ + /** + * Return a string representation of a URL. + * + * @param string $scientificName + * The scientificName of a herbarium Specimen. + * + * @return string + * A string based on the gbifUrlPrefix and the scientificName. + */ + public function getGbifUrl($scientificName) { return $this->gbifUrlPrefix . $scientificName; } diff --git a/src/DataParser/GeonamesParser.php b/src/DataParser/GeonamesParser.php index a0d3998..7ac2874 100644 --- a/src/DataParser/GeonamesParser.php +++ b/src/DataParser/GeonamesParser.php @@ -1,21 +1,18 @@ loadXML($this->data))) { + if (empty($xml_doc->loadXML($this->data))) { throw new \Exception('Failed loading data as xml'); } $this->getXmlNodeValue($xml_doc, 'toponymName'); @@ -39,32 +36,38 @@ class GeonamesParser extends XmlDataParser $this->getXmlNodeValue($xml_doc, 'south'); } + /** + * {@inheritdoc} + */ public function updateEntity($entity) { try { $dwciri = $entity->get("field_dwciri_municipality")->getValue(); - if(empty($dwciri[0]['value'])) { + if (empty($dwciri[0]['value'])) { throw new Exception("No URI provided, cannot look up location data."); } $this->getRemoteData($dwciri[0]['value']); $this->parseData(); - } catch (Exception $e) { + } + catch (Exception $e) { watchdog_exception('islandora_herbarium_object', $e, 'Error getting dwciri'); drupal_set_message(t('There was an error retrieving Municipality data. %msg. Verify the url is correct %url', - array('%msg'=> $e->getMessage(), '%url' => $dwciri[0]['value'])), 'error'); + ['%msg' => $e->getMessage(), '%url' => $dwciri[0]['value']]), 'error'); return; } $entity->set('field_dwc_municipality', $this->getValue('name')); - $entity->set('field_dwc_countrycode', $this->getValue('countryCode')); - $entity->set('field_dwc_country', $this->getValue('countryName')); - $entity->set('field_dwc_continent', $this->getValue('countinentCode')); - $entity->set('field_dwc_decimallatitude', $this->getValue('lat')); - $entity->set('field_dwc_decimallongitude', $this->getValue('lng')); - $entity->set('field_dwc_stateprovince', $this->getValue('adminName1')); - $point = array( - 'lon' => $this->getValue('lng'), - 'lat' => $this->getValue('lat'), - ); - $geo_field_value = \Drupal::service('geofield.wkt_generator')->WktBuildPoint($point); + $entity->set('field_dwc_countrycode', $this->getValue('countryCode')); + $entity->set('field_dwc_country', $this->getValue('countryName')); + $entity->set('field_dwc_continent', $this->getValue('countinentCode')); + $entity->set('field_dwc_decimallatitude', $this->getValue('lat')); + $entity->set('field_dwc_decimallongitude', $this->getValue('lng')); + $entity->set('field_dwc_stateprovince', $this->getValue('adminName1')); + $point = [ + 'lon' => $this->getValue('lng'), + 'lat' => $this->getValue('lat'), + ]; + $geo_field_value = \Drupal::service('geofield.wkt_generator') + ->WktBuildPoint($point); $entity->set('field_herb_geo_field', $geo_field_value); } + } diff --git a/src/DataParser/IpniParser.php b/src/DataParser/IpniParser.php index 2044464..bf36161 100644 --- a/src/DataParser/IpniParser.php +++ b/src/DataParser/IpniParser.php @@ -1,16 +1,15 @@ loadXML($this->data); @@ -18,4 +17,5 @@ class IpniParser extends XmlDataParser $this->getXmlNodeValue($xml_doc, 'genusPart'); $this->getXmlNodeValue($xml_doc, 'specificEpithet'); } + } diff --git a/src/DataParser/NrcanParser.php b/src/DataParser/NrcanParser.php index 124d9b3..0538b53 100644 --- a/src/DataParser/NrcanParser.php +++ b/src/DataParser/NrcanParser.php @@ -1,38 +1,45 @@ data, TRUE); - if(empty($arr)) { + /** + * {@inheritdoc} + */ + public function parseData() { + $arr = json_decode($this->data, TRUE); + if (empty($arr)) { throw new \Exception('Error parsing geo json data.'); } - $this->data_arr = $arr; + $this->dataArray = $arr; } - public function updateEntity($entity) { + /** + * {@inheritdoc} + */ + public function updateEntity(EntityInterface $entity) { try { $url = ''; $dwciri = $entity->get("field_dwciri_municipality")->getValue(); - if(empty($url = $dwciri[0]['value']) && !empty($entity->get("field_dwc_municipality")->getValue())) { - //throw new Exception("No URI provided, cannot look up location data."); - $value = array($this->getNrcanUrl($entity->get('field_dwc_municipality')->getValue()[0]['value'])); - $dwciri[] = ['value' => $value[0]]; + if (empty($url = $dwciri[0]['value']) && !empty($entity->get("field_dwc_municipality") + ->getValue())) { + $value = [ + $this->getNrcanUrl($entity->get('field_dwc_municipality') + ->getValue()[0]['value']), + ]; + $dwciri[] = ['value' => $value[0]]; $url = $dwciri[0]['value']; - if(empty($url)) { + if (empty($url)) { throw new \Exception("No Municipality URI or Municipality text provided, cannot look up taxon data."); } $entity->set('field_dwciri_municipality', $url); @@ -40,49 +47,62 @@ class NrcanParser extends DataParser $this->getRemoteData($url); $this->parseData(); - } catch (Exception $e) { + } + catch (Exception $e) { watchdog_exception('islandora_herbarium_object', $e, 'Error getting dwciri'); drupal_set_message(t('There was an error retrieving Municipality data. %msg. Verify the url is correct %url', - array('%msg'=> $e->getMessage(), '%url' => $dwciri[0]['value'])), 'error'); + ['%msg' => $e->getMessage(), '%url' => $dwciri[0]['value']]), 'error'); return; } $entity->set('field_dwc_municipality', $this->getValue('name')); - // This parser assumes we are in canada - $entity->set('field_dwc_countrycode', 'CA'); - $entity->set('field_dwc_country', 'Canada'); - $entity->set('field_dwc_continent', 'North America'); - $entity->set('field_dwc_decimallatitude', $this->getValue('latitude')); - $entity->set('field_dwc_decimallongitude', $this->getValue('longitude')); + // This parser assumes we are in canada. + $entity->set('field_dwc_countrycode', 'CA'); + $entity->set('field_dwc_country', 'Canada'); + $entity->set('field_dwc_continent', 'North America'); + $entity->set('field_dwc_decimallatitude', $this->getValue('latitude')); + $entity->set('field_dwc_decimallongitude', $this->getValue('longitude')); $provinceUri = $this->getvalue('province')['links']['self']['href']; try { $provinceData = $this->getRemoteData($provinceUri, TRUE); $p_arr = json_decode($provinceData, TRUE); $province = $p_arr['description']; } - catch(Exception $e) { + catch (Exception $e) { drupal_set_message(t('There was an error retrieving Province data. %msg.', - array('%msg'=> $e->getMessage())), 'error'); + ['%msg' => $e->getMessage()]), 'error'); } - $entity->set('field_dwc_stateprovince', $province); - $point = array( - 'lon' => $this->getValue('longitude'), - 'lat' => $this->getValue('latitude'), - ); - $geo_field_value = \Drupal::service('geofield.wkt_generator')->WktBuildPoint($point); + $entity->set('field_dwc_stateprovince', $province); + $point = [ + 'lon' => $this->getValue('longitude'), + 'lat' => $this->getValue('latitude'), + ]; + $geo_field_value = \Drupal::service('geofield.wkt_generator') + ->WktBuildPoint($point); $entity->set('field_herb_geo_field', $geo_field_value); } - public function getNrcanUrl($municipality){ + /** + * Returns a URL string. + * + * @param string $municipality + * The municipality that we want to lookup. + * + * @return string + * URL String based on the nrcanUrlPrefix class variable plus a key that + * represents the municipality. + */ + public function getNrcanUrl($municipality) { $url = $this->nrcanUrlSearchPrefix . $municipality . "&province=11"; $data = $this->getRemoteData($url, TRUE); $data_arr = json_decode($data, TRUE); $key = ''; - foreach($data_arr['items'] as $item) { - if($item['name'] === $municipality){ + foreach ($data_arr['items'] as $item) { + if ($item['name'] === $municipality) { $key = $item['id']; break; } } return $this->nrcanUrlPrefix . $key; } + } diff --git a/src/DataParser/XmlDataParser.php b/src/DataParser/XmlDataParser.php index 81054ee..74504d8 100644 --- a/src/DataParser/XmlDataParser.php +++ b/src/DataParser/XmlDataParser.php @@ -1,18 +1,22 @@ data_arr[$element_name] = $xml_doc->getElementsByTagName($element_name)[0]->nodeValue; + /** + * Retrieves the value of the first xml node matching element_name. + * + * @param \DOMDocument $xml_doc + * An xml document. + * @param string $element_name + * The xml node to lookup. + */ + public function getXmlNodeValue(\DOMDocument $xml_doc, $element_name) { + $this->dataArray[$element_name] = $xml_doc->getElementsByTagName($element_name)[0]->nodeValue; } + }