public static function split_entry($args)
 {
     $hierarchy_entry_id = @$args['hierarchy_entry_id'];
     $bad_match_hierarchy_entry_id = @$args['bad_match_hierarchy_entry_id'];
     $confirmation = @$args['confirmed'];
     if (!$hierarchy_entry_id || !is_numeric($hierarchy_entry_id) || !$bad_match_hierarchy_entry_id || !is_numeric($bad_match_hierarchy_entry_id)) {
         throw new \Exception("split_entry.php [hierarchy_entry_id] [bad_match_hierarchy_entry_id] [confirmed]");
     }
     \CodeBridge::print_message("Splitting HE# {$hierarchy_entry_id} from {$bad_match_hierarchy_entry_id}");
     $he = HierarchyEntry::find($hierarchy_entry_id);
     $bad_he = HierarchyEntry::find($hierarchy_entry_id);
     if (!$he->id || !$bad_he->id) {
         throw new \Exception("Invalid ID");
     }
     if ($he->taxon_concept_id != $bad_he->taxon_concept_id) {
         throw new \Exception("The bad match ID isn't from the same concept");
     }
     if ($confirmation == 'confirmed') {
         $user_id = 13;
         # 13 is Patrick's user ID - TODO - this should be an argument.  :|
         $new_taxon_concept_id = HierarchyEntry::split_from_concept_static($hierarchy_entry_id);
         $GLOBALS['db_connection']->query("INSERT IGNORE INTO curated_hierarchy_entry_relationships (hierarchy_entry_id_1, hierarchy_entry_id_2, user_id, equivalent)\n                VALUES ({$hierarchy_entry_id}, {$bad_match_hierarchy_entry_id}, {$user_id}, 0)");
         \CodeBridge::print_message("Done. HE# {$hierarchy_entry_id} was split into a new concept # {$new_taxon_concept_id}");
     } else {
         echo "\n\nRemoving:\n";
         print_r($he);
         echo "Name: " . $he->name->string . "\n\nFrom:\n";
         print_r($he->taxon_concept);
         $descendant_objects = TaxonConcept::count_descendants_objects($he->taxon_concept_id);
         echo "\n\nDescendant Objects:  {$descendant_objects}\n\n";
     }
 }
 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);
 }
 function details_tcs($id)
 {
     $entry = HierarchyEntry::find($id);
     if (@(!$entry->id)) {
         return false;
     }
     $nomenclaturalCode = "Zoological";
     $rankCode = "";
     switch (strtolower($entry->rank->translation->label)) {
         case "kingdom":
             $rankCode = "reg";
             break;
         case "phylum":
             $rankCode = "phyl_div";
             break;
         case "class":
             $rankCode = "cl";
             break;
         case "order":
             $rankCode = "ord";
             break;
         case "family":
             $rankCode = "fam";
             break;
         case "genus":
             $rankCode = "gen";
             break;
         case "species":
             $rankCode = "sp";
             break;
     }
     $return = "";
     $return .= "  <TaxonNames>\n";
     $return .= "    <TaxonName id='n" . $entry->name->id . "' nomenclaturalCode='{$nomenclaturalCode}'>\n";
     $return .= "      <Simple>" . htmlspecialchars($entry->name->string) . "</Simple>\n";
     if ($rankCode) {
         $return .= "      <Rank code='{$rankCode}'>" . ucfirst(strtolower($entry->rank->translation->label)) . "</Rank>\n";
     }
     $return .= "      <CanonicalName>\n";
     $return .= "        <Simple>" . htmlspecialchars($entry->name->canonical_form->string) . "</Simple>\n";
     $return .= "      </CanonicalName>\n";
     if ($ahe = $entry->agents_hierarchy_entries) {
         $return .= "      <ProviderSpecificData>\n";
         $return .= "        <NameSources>\n";
         foreach ($ahe as $k => $v) {
             $return .= "          <NameSource>\n";
             $return .= "            <Simple>" . htmlspecialchars($v->agent->full_name) . "</Simple>\n";
             if ($v->agent_role) {
                 $return .= "            <Role>" . htmlspecialchars($v->agent_role->translation->label) . "</Role>\n";
             }
             $return .= "          </NameSource>\n";
         }
         $return .= "        </NameSources>\n";
         $return .= "      </ProviderSpecificData>\n";
     }
     $return .= "    </TaxonName>\n";
     $return .= "  </TaxonNames>\n";
     $return .= "  <TaxonConcepts>\n";
     $return .= "    <TaxonConcept id='c" . $entry->id . "'>\n";
     $return .= "      <Name scientific='true' ref='n" . $entry->name->id . "'>" . htmlspecialchars($entry->name->string) . "</Name>\n";
     if ($rankCode) {
         $return .= "      <Rank code='{$rankCode}'>" . ucfirst(strtolower($entry->rank->translation->label)) . "</Rank>\n";
     }
     $return .= "      <TaxonRelationships>\n";
     if ($parent = $entry->parent()) {
         $return .= "        <TaxonRelationship type='is child taxon of'>\n";
         $return .= "          <ToTaxonConcept ref='{$this->api_url}?function=details_tcs&amp;id=" . $parent->id . "' linkType='external'/>\n";
         $return .= "        </TaxonRelationship>\n";
     }
     if ($children = $entry->children()) {
         foreach ($children as $k => $v) {
             $return .= "        <TaxonRelationship type='is parent taxon of'>\n";
             $return .= "          <ToTaxonConcept ref='{$this->api_url}?function=details_tcs&amp;id=" . $v->id . "' linkType='external'/>\n";
             $return .= "        </TaxonRelationship>\n";
         }
     }
     if ($synonyms = $entry->synonyms()) {
         foreach ($synonyms as $k => $v) {
             $relationship = "has synonym";
             if (strtolower($v->synonym_relation->translation->label) == "common name") {
                 $relationship = "has vernacular";
             }
             $return .= "        <TaxonRelationship type='{$relationship}'>\n";
             $return .= "          <ToTaxonConcept ref='{$this->api_url}?function=details_tcs&amp;sid=" . $v->id . "' linkType='external'/>\n";
             $return .= "        </TaxonRelationship>\n";
         }
     }
     $return .= "      </TaxonRelationships>\n";
     $return .= "    </TaxonConcept>\n";
     $return .= "  </TaxonConcepts>\n";
     return $return;
 }
 static function lookup_existing_entry_and_ancestors($hierarchy_entry, $hierarchy_id)
 {
     $params = array();
     $params["name_id"] = $hierarchy_entry->name_id;
     $params["guid"] = $hierarchy_entry->guid;
     $params["hierarchy_id"] = $hierarchy_id;
     $params["rank_id"] = $hierarchy_entry->rank_id;
     $params["ancestry"] = $hierarchy_entry->ancestry;
     $params["taxon_concept_id"] = $hierarchy_entry->taxon_concept_id;
     $params["parent_id"] = 0;
     // $params["identifier"] = $taxon['identifier'];
     // $params["source_url"] = $taxon['source_url'];
     $params["visibility_id"] = Visibility::preview()->id;
     if ($parent = $hierarchy_entry->parent()) {
         if ($parent_entry = self::lookup_existing_entry_and_ancestors($parent, $hierarchy_id)) {
             $params["parent_id"] = $parent_entry->id;
         } else {
             return false;
         }
     }
     return HierarchyEntry::find_or_create_by_array($params);
 }
 public static function move_entry($args)
 {
     $taxon_concept_id_from = @$args['taxon_concept_id_from'];
     $hierarchy_entry_id = @$args['hierarchy_entry_id'];
     $taxon_concept_id_to = @$args['taxon_concept_id_to'];
     $bad_match_hierarchy_entry_id = @$args['bad_match_hierarchy_entry_id'];
     $confirmation = @$args['confirmed'];
     if (!$taxon_concept_id_from || !is_numeric($taxon_concept_id_from) || !$hierarchy_entry_id || !is_numeric($hierarchy_entry_id) || !$taxon_concept_id_to || !is_numeric($taxon_concept_id_to) || !$bad_match_hierarchy_entry_id || !is_numeric($bad_match_hierarchy_entry_id)) {
         throw new \Exception("split_concept.php [taxon_concept_id_from] [hierarchy_entry_id] [taxon_concept_id_to] [bad_match_hierarchy_entry_id] [confirmed] [reindex?]");
     }
     \CodeBridge::print_message("Moving HE# {$hierarchy_entry_id} from TC# {$taxon_concept_id_from} to TC# " . "{$taxon_concept_id_to} avoiding HE# {$bad_match_hierarchy_entry_id}");
     $tc_from = TaxonConcept::find($taxon_concept_id_from);
     $tc_to = TaxonConcept::find($taxon_concept_id_to);
     $he = HierarchyEntry::find($hierarchy_entry_id);
     $bad_he = HierarchyEntry::find($bad_match_hierarchy_entry_id);
     if (!$he->id || !$tc_from->id || !$tc_to->id || !$bad_he->id) {
         throw new \Exception("Invalid ID");
     }
     if ($he->taxon_concept_id != $tc_from->id) {
         throw new \Exception("This entry is not in the source concept");
     }
     if ($he->taxon_concept_id != $bad_he->taxon_concept_id) {
         throw new \Exception("The bad match ID isn't from the same concept");
     }
     if ($confirmation == 'confirmed' || $confirmation == 'force') {
         if ($confirmation == 'force') {
             $force_move_if_disallowed = true;
         } else {
             $force_move_if_disallowed = false;
         }
         $user_id = 13;
         # 13 is Patrick's user ID
         // TODO Need to look through all the HEs in the TC we're moving *to* and cycle through them to make sure none of
         // them are blocking the move:
         foreach ($tc_to->hierarchy_entries as $tc_he) {
             $GLOBALS['db_connection']->query("DELETE FROM curated_hierarchy_entry_relationships\n                    WHERE hierarchy_entry_id_1={$hierarchy_entry_id} AND hierarchy_entry_id_2=" . $tc_he->id . " AND equivalent=0");
         }
         $moved = HierarchyEntry::move_to_concept_static($hierarchy_entry_id, $taxon_concept_id_to, $force_move_if_disallowed, true);
         if (!$moved) {
             \CodeBridge::print_message("NOT ALLOWED: throwing exception");
             throw new \Exception("This move is not allowed; it would affect other hierarchies");
         }
         $GLOBALS['db_connection']->query("INSERT IGNORE INTO curated_hierarchy_entry_relationships VALUES ({$hierarchy_entry_id}, {$bad_match_hierarchy_entry_id}, {$user_id}, 0)");
         \CodeBridge::print_message("Done. Moved {$hierarchy_entry_id} to {$taxon_concept_id_to}");
     } else {
         echo "\n\nRemoving:\n";
         print_r($he);
         echo "Name: " . $he->name->string . "\n\nFrom:\n";
         print_r($tc_from);
         echo "To:\n";
         print_r($tc_to);
         $descendant_objects = TaxonConcept::count_descendants_objects($tc_from->id);
         $descendants = TaxonConcept::count_descendants($tc_from->id);
         echo "\n\nTaxonConcept1: {$tc_from->id}\n";
         echo "Descendant Objects:  {$descendant_objects}\n";
         echo "Descendant Concepts: {$descendants}\n";
         $descendant_objects = TaxonConcept::count_descendants_objects($tc_to->id);
         $descendants = TaxonConcept::count_descendants($tc_to->id);
         echo "\n\nTaxonConcept1: {$tc_to->id}\n";
         echo "Descendant Objects:  {$descendant_objects}\n";
         echo "Descendant Concepts: {$descendants}\n";
         echo "\n\nDon't forget to solr_update_concept.php\n\n";
     }
 }