Browse Source

added comments and coder fixes

master
Paul Pound 9 years ago
parent
commit
16bf5e5f44
  1. 1
      README.md
  2. 33
      composer.json
  3. 28
      islandora_herbarium_object.module
  4. 30
      src/DataParser/DataParser.php
  5. 54
      src/DataParser/DataParserInterface.php
  6. 72
      src/DataParser/GbifParser.php
  7. 55
      src/DataParser/GeonamesParser.php
  8. 16
      src/DataParser/IpniParser.php
  9. 96
      src/DataParser/NrcanParser.php
  10. 26
      src/DataParser/XmlDataParser.php

1
README.md

@ -4,4 +4,3 @@
## License

33
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"
}
}

28
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);
}
}
}

30
src/DataParser/DataParser.php

@ -1,35 +1,39 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: ppound
* Date: 2018-04-30
* Time: 11:25 AM
*/
namespace Drupal\islandora_herbarium_object\DataParser;
/**
* Abstract implementation of DataParserInterface base on arrays.
*/
abstract class DataParser implements DataParserInterface {
abstract class DataParser implements DataParserInterface
{
public $data;
public $data_arr = array();
public $dataArray = [];
/**
* {@inheritdoc}
*/
public function getRemoteData($uri, $returnAsVariable = FALSE) {
$client = \Drupal::httpClient();
$request = $client->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] : "";
}
}

54
src/DataParser/DataParserInterface.php

@ -1,23 +1,53 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: ppound
* Date: 2018-04-30
* Time: 1:10 PM
*/
namespace Drupal\islandora_herbarium_object\DataParser;
use Drupal\Core\Entity\EntityInterface;
interface DataParserInterface
{
public function getRemoteData($uri);
/**
* Common functions for herbarium taxon and location data.
*/
interface DataParserInterface {
/**
* Retrieve data from a remote URI.
*
* @param string $uri
* The request uri.
* @param bool $returnAsVariable
* If TRUE return the request data, otherwise set a class data variable(s).
*
* @return mixed
* Either NULL or the request data, determined by the value of
* $returnAsVariable.
*/
public function getRemoteData($uri, $returnAsVariable = FALSE);
/**
* Parse the Data stored in a local data structure.
*
* The local data structure is usually populated by the getRemoteData()
* function.
*/
public function parseData();
/**
* Returns a value from a local data structure based on the corresponding key.
*
* @param string $key
* The key to use to lookup the data.
*
* @return string
* The value if the key exists otherwise an empty string.
*/
public function getValue($key);
public function updateEntity($entity);
/**
* Update a Drupal Entity using the local data.
*
* @param Drupal\Core\Entity\EntityInterface $entity
* A Drupal Entity.
*/
public function updateEntity(EntityInterface $entity);
}

72
src/DataParser/GbifParser.php

@ -1,59 +1,81 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: ppound
* Date: 2018-05-04
* Time: 1:19 PM
*/
namespace Drupal\islandora_herbarium_object\DataParser;
use Drupal\Core\Entity\EntityInterface;
/**
* A data structure for GBif data.
*/
class GbifParser extends DataParser {
class GbifParser extends DataParser
{
public $gbifUrlPrefix = 'http://api.gbif.org/v1/species?name=';
/**
* {@inheritdoc}
*/
public function parseData() {
$arr = json_decode($this->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;
}

55
src/DataParser/GeonamesParser.php

@ -1,21 +1,18 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: ppound
* Date: 2018-04-30
* Time: 11:35 AM
*/
namespace Drupal\islandora_herbarium_object\DataParser;
/**
* Provides storage and functions for GeoName data.
*/
class GeonamesParser extends XmlDataParser {
class GeonamesParser extends XmlDataParser
{
public function parseData()
{
/**
* {@inheritdoc}
*/
public function parseData() {
$xml_doc = new \DOMDocument();
if(empty($xml_doc->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);
}
}

16
src/DataParser/IpniParser.php

@ -1,16 +1,15 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: ppound
* Date: 2018-04-30
* Time: 11:23 AM
*/
namespace Drupal\islandora_herbarium_object\DataParser;
/**
* Provides storage and functions for IPNI data.
*/
abstract class IpniParser extends XmlDataParser {
class IpniParser extends XmlDataParser
{
/**
* {@inheritdoc}
*/
public function parseData() {
$xml_doc = new \DOMDocument();
$xml_doc->loadXML($this->data);
@ -18,4 +17,5 @@ class IpniParser extends XmlDataParser
$this->getXmlNodeValue($xml_doc, 'genusPart');
$this->getXmlNodeValue($xml_doc, 'specificEpithet');
}
}

96
src/DataParser/NrcanParser.php

@ -1,38 +1,45 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: ppound
* Date: 2018-05-07
* Time: 10:44 AM
*/
namespace Drupal\islandora_herbarium_object\DataParser;
use Drupal\Core\Entity\EntityInterface;
/**
* Provides storage and functions for NRCAN data.
*/
class NrcanParser extends DataParser {
class NrcanParser extends DataParser
{
public $nrcanUrlSearchPrefix = 'http://geogratis.gc.ca/services/geoname/en/geonames?q=';
public $nrcanUrlPrefix = 'http://geogratis.gc.ca/services/geoname/en/geonames/';
public function parseData()
{
$arr = json_decode($this->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;
}
}

26
src/DataParser/XmlDataParser.php

@ -1,18 +1,22 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: ppound
* Date: 2018-05-04
* Time: 1:16 PM
*/
namespace Drupal\islandora_herbarium_object\DataParser;
/**
* Provides functions for simple XML data.
*/
abstract class XmlDataParser extends DataParser {
abstract class XmlDataParser extends DataParser
{
public function getXmlNodeValue($xml_doc, $element_name)
{
$this->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;
}
}

Loading…
Cancel
Save