function add_hierarchy_entry(&$row, $parent_hierarchy_entry_id, $ancestry, $branch_kingdom) { self::debug_iterations("Inserting taxon"); self::commit_iterations("Taxa", 500); if ($this->archive_validator->has_error_by_line('http://rs.tdwg.org/dwc/terms/taxon', $row['archive_file_location'], $row['archive_line_number'])) { write_to_resource_harvesting_log("ERROR: add_hierarchy_entry: has_error_by_line" . ",file_location:" . $row['archive_file_location'] . ",line_number:" . $row['archive_line_number']); return false; } // make sure this taxon has a name, otherwise skip this branch $scientific_name = @self::field_decode($row['http://rs.tdwg.org/dwc/terms/scientificName']); $authorship = @self::field_decode($row['http://rs.tdwg.org/dwc/terms/scientificNameAuthorship']); $kingdom = @self::field_decode($row['http://rs.tdwg.org/dwc/terms/kingdom']); $genus = @self::field_decode($row['http://rs.tdwg.org/dwc/terms/genus']); $rank_label = @self::field_decode($row['http://rs.tdwg.org/dwc/terms/taxonRank']); // COL exception if (strtolower($kingdom) == 'viruses') { if (substr($scientific_name, -1) == ":") { $scientific_name = substr($scientific_name, 0, -1); } if (preg_match("/^(.*) ICTV\$/i", $scientific_name, $arr)) { $scientific_name = $arr[1]; } } // COL exception if (strtolower($kingdom) == 'viruses' && $genus && strtolower($rank_label) != 'genus') { if (stripos($scientific_name, $genus) == 0) { $scientific_name = ucfirst(trim(substr($scientific_name, strlen($genus)))); } } else { if ($authorship && stripos($scientific_name, $authorship) === false) { $scientific_name = trim($scientific_name . " " . $authorship); } } if (!$scientific_name) { return false; } $taxon_id = @self::field_decode($row['http://rs.tdwg.org/dwc/terms/taxonID']); if (!$taxon_id) { $taxon_id = @self::field_decode($row['http://purl.org/dc/terms/identifier']); } if (!$taxon_id) { debug("ERROR - no taxon ID for {$scientific_name}, skipping"); return false; } if (isset($this->taxon_ids_inserted[$taxon_id])) { // this taxon_id has already been inserted meaning this tree has a loop in it - so stop debug("ERROR - taxon ID ({$taxon_id}) for {$scientific_name} already inserted; LOOP?"); return false; } $scientific_name = ucfirst($scientific_name); $name = Name::find_or_create_by_string($scientific_name); if (@(!$name->id)) { debug("ERROR - Failed to insert name: {$scientific_name}"); return false; } $phylum = @self::field_decode($row['http://rs.tdwg.org/dwc/terms/phylum']); $class = @self::field_decode($row['http://rs.tdwg.org/dwc/terms/class']); $order = @self::field_decode($row['http://rs.tdwg.org/dwc/terms/order']); $family = @self::field_decode($row['http://rs.tdwg.org/dwc/terms/family']); $rank = Rank::find_or_create_by_translated_label($rank_label); $dataset_id = @self::field_decode($row['http://rs.tdwg.org/dwc/terms/datasetID']); $taxonomic_status = @self::field_decode($row['http://rs.tdwg.org/dwc/terms/taxonomicStatus']); $source_url = @self::field_decode($row['http://rs.tdwg.org/ac/terms/furtherInformationURL']); if (!$source_url) { $source_url = @self::field_decode($row['http://purl.org/dc/terms/source']); } if (!$source_url) { $source_url = @self::field_decode($row['http://purl.org/dc/terms/references']); } if (!$source_url) { $source_url = @self::field_decode($row['http://purl.org/dc/terms/isReferencedBy']); } if (isset($row['http://rs.tdwg.org/dwc/terms/taxonRemarks'])) { $taxon_remarks = @self::field_decode($row['http://rs.tdwg.org/dwc/terms/taxonRemarks']); } else { $taxon_remarks = NULL; } if (!$taxon_remarks && strtolower($taxonomic_status) == 'provisionally accepted name') { $taxon_remarks = "provisionally accepted name"; } // TODO: This block is somewhat confusing. Clearly, it's clearing the // rank that's currently being read, but shouldn't it also clear all of // the ranks below that? if (strtolower($rank_label) == 'kingdom') { $kingdom = null; } if (strtolower($rank_label) == 'phylum') { $phylum = null; } if (strtolower($rank_label) == 'class') { $class = null; } if (strtolower($rank_label) == 'order') { $order = null; } if (strtolower($rank_label) == 'family') { $family = null; } if (strtolower($rank_label) == 'genus') { $genus = null; } // these are the taxa using the adjacency list format if (!$parent_hierarchy_entry_id && ($kingdom || $phylum || $class || $order || $family || $genus)) { $params = array("identifier" => $taxon_id, "source_url" => $source_url, "kingdom" => ucfirst($kingdom), "phylum" => ucfirst($phylum), "class" => ucfirst($class), "order" => ucfirst($order), "family" => ucfirst($family), "genus" => ucfirst($genus), "scientificName" => ucfirst($scientific_name), "name" => $name, "rank" => $rank, "taxon_remarks" => $taxon_remarks); $hierarchy_entry = HierarchyEntry::create_entries_for_taxon($params, $this->harvest_event->resource->hierarchy_id); if (@(!$hierarchy_entry->id)) { debug("ERROR - unable to insert hierarchy entry for {$scientific_name}"); return; } // NOTE: This is NOT adding a hierarchy entry, but a // harvest_event_hierarchy_entry: // TODO: I am not sure this adds entries for ancestors! $this->harvest_event->add_hierarchy_entry($hierarchy_entry, 'inserted'); $this->taxon_ids_inserted[$taxon_id] = array('hierarchy_entry_id' => $hierarchy_entry->id, 'taxon_concept_id' => $hierarchy_entry->taxon_concept_id, 'source_url' => $source_url); self::compress_array($this->taxon_ids_inserted[$taxon_id]); } else { $params = array("identifier" => $taxon_id, "source_url" => $source_url, "name_id" => $name->id, "parent_id" => $parent_hierarchy_entry_id, "hierarchy_id" => $this->harvest_event->resource->hierarchy_id, "rank" => $rank, "ancestry" => $ancestry, "taxon_remarks" => $taxon_remarks); $hierarchy_entry = HierarchyEntry::find_or_create_by_array($params); if (@(!$hierarchy_entry->id)) { return; } $this->harvest_event->add_hierarchy_entry($hierarchy_entry, 'inserted'); $this->taxon_ids_inserted[$taxon_id] = array('hierarchy_entry_id' => $hierarchy_entry->id, 'taxon_concept_id' => $hierarchy_entry->taxon_concept_id, 'source_url' => $source_url); self::compress_array($this->taxon_ids_inserted[$taxon_id]); } if (!isset($this->entry_references_deleted[$hierarchy_entry->id])) { $hierarchy_entry->delete_refs(); $this->entry_references_deleted[$hierarchy_entry->id] = true; } if (!isset($this->entry_vernacular_names_deleted[$hierarchy_entry->id])) { $this->mysqli->delete("DELETE FROM synonyms WHERE hierarchy_entry_id={$hierarchy_entry->id} AND hierarchy_entry_id={$hierarchy_entry->id} AND hierarchy_id=" . $this->harvest_event->resource->hierarchy_id . " AND language_id!=0 AND language_id!=" . Language::find_or_create_for_parser('scientific name')->id); $this->entry_vernacular_names_deleted[$hierarchy_entry->id] = true; } if (!isset($this->entry_synonyms_deleted[$hierarchy_entry->id])) { $hierarchy_entry->delete_synonyms(); $this->entry_synonyms_deleted[$hierarchy_entry->id] = true; } if ($name_published_in = @$row['http://rs.tdwg.org/dwc/terms/namePublishedIn']) { $individual_references = explode("||", $name_published_in); foreach ($individual_references as $reference_string) { $reference = Reference::find_or_create_by_full_reference(trim($reference_string)); if (@$reference->id) { $hierarchy_entry->add_reference($reference->id); $this->mysqli->query("UPDATE refs SET published=1, visibility_id=" . Visibility::visible()->id . " WHERE id={$reference->id}"); } } } // keep track of reference foreign keys self::append_foreign_keys_from_row($row, 'http://eol.org/schema/reference/referenceID', $this->taxon_reference_ids, $hierarchy_entry->id); if (isset($this->synonyms[$taxon_id])) { foreach ($this->synonyms[$taxon_id] as $synonym_row) { self::uncompress_array($synonym_row); $synonym_scientific_name = @self::field_decode($synonym_row['http://rs.tdwg.org/dwc/terms/scientificName']); $synonym_authorship = @self::field_decode($synonym_row['http://rs.tdwg.org/dwc/terms/scientificNameAuthorship']); if ($synonym_authorship && stripos($synonym_scientific_name, $synonym_authorship) === false) { $synonym_scientific_name = trim($synonym_scientific_name . " " . $synonym_authorship); } if (!$synonym_scientific_name) { continue; } $synonym_taxon_id = @self::field_decode($synonym_row['http://rs.tdwg.org/dwc/terms/taxonID']); if (!$synonym_taxon_id) { $taxon_id = @self::field_decode($synonym_row['http://purl.org/dc/terms/identifier']); } if (!$synonym_taxon_id) { continue; } $synonym_name = Name::find_or_create_by_string(ucfirst($synonym_scientific_name)); if (@(!$synonym_name->id)) { continue; } $taxonomic_status = @self::field_decode($synonym_row['http://rs.tdwg.org/dwc/terms/taxonomicStatus']) ?: 'synonym'; if (isset($synonym_row['http://rs.tdwg.org/dwc/terms/taxonRemarks'])) { $taxon_remarks = @self::field_decode($synonym_row['http://rs.tdwg.org/dwc/terms/taxonRemarks']); } else { $taxon_remarks = NULL; } $synonym_relation = SynonymRelation::find_or_create_by_translated_label($taxonomic_status); $hierarchy_entry->add_synonym($synonym_name->id, @$synonym_relation->id ?: 0, 0, 0, 0, 0, $taxon_remarks); } unset($this->synonyms[$taxon_id]); } // COL exception if ($dataset_id && isset($this->dataset_metadata[$dataset_id]) && ($metadata = $this->dataset_metadata[$dataset_id])) { $hierarchy_entry->delete_agents(); $agent_name = $metadata['title']; if ($editors = $metadata['editors']) { $agent_name .= " by {$editors}"; } $params = array("full_name" => $agent_name, "agent_role" => AgentRole::find_or_create_by_translated_label('Source')); $agent = Agent::find_or_create($params); $hierarchy_entry->add_agent($agent->id, @$a['agent_role']->id ?: 0, 0); $reference = Reference::find_or_create(array("full_reference" => $metadata['citation'])); $this->mysqli->insert("INSERT IGNORE INTO hierarchy_entries_refs (hierarchy_entry_id, ref_id) VALUES ({$hierarchy_entry->id}, {$reference->id})"); $this->mysqli->query("UPDATE refs SET published=1, visibility_id=" . Visibility::visible()->id . " WHERE id={$reference->id}"); } $parameters = array('archive_table_definition' => (object) array('row_type' => 'http://rs.tdwg.org/dwc/terms/Taxon')); $this->insert_data($row, $parameters); if (isset($this->children[$taxon_id])) { // set the ancestry for its children if ($ancestry) { $this_ancestry = $ancestry . "|" . $name->id; } else { $this_ancestry = $name->id; } foreach ($this->children[$taxon_id] as &$row) { self::uncompress_array($row); $this->add_hierarchy_entry($row, $hierarchy_entry->id, $this_ancestry, $branch_kingdom); } unset($this->children[$taxon_id]); } unset($hierarchy_entry); unset($row); }
static function force_wikipedia_taxon($t) { $wikipedia_resource = Resource::wikipedia(); $last_wikipedia_harvest = new HarvestEvent($wikipedia_resource->most_recent_published_harvest_event_id()); $content_manager = new ContentManager(); $hierarchy_entry = HierarchyEntry::create_entries_for_taxon($t, $wikipedia_resource->hierarchy_id); if (@(!$hierarchy_entry->id)) { return false; } $last_wikipedia_harvest->add_hierarchy_entry($hierarchy_entry, 'inserted'); foreach ($t['data_objects'] as &$d) { list($data_object, $status) = DataObject::find_and_compare($wikipedia_resource, $d, $content_manager); if (@(!$data_object->id)) { return false; } $hierarchy_entry->add_data_object($data_object->id); $last_wikipedia_harvest->add_data_object($data_object, $status); if (@$d->info_items_ids) { $data_object->delete_info_items(); foreach ($d->info_items_ids as &$id) { $data_object->add_info_item($id); unset($id); } } return array($hierarchy_entry, $data_object); } return false; }