You are here

function taxonomy_xml_canonicize_predicates in Taxonomy import/export via XML 6

Same name and namespace in other branches
  1. 6.2 taxonomy_xml.module \taxonomy_xml_canonicize_predicates()
  2. 7 taxonomy_xml.process.inc \taxonomy_xml_canonicize_predicates()

Given a term with a collection of named predicate relations, convert those into canonic (known, defined) terms. This involves some duplication as the original and true names are both packed into the $term->predicates array. Only the true names are looked at later however.

3 calls to taxonomy_xml_canonicize_predicates()
taxonomy_xml_mesh_parse in ./mesh_format.inc
Reads a XML file and creates the term definitions found in it.
taxonomy_xml_rdf_parse in ./rdf_format.inc
Read in RDF taxonomies and vocabularies. Create vocabs and terms as needed.
taxonomy_xml_tcs_parse in ./tcs_format.inc
Reads a TCS file and creates the term definitions found in it.

File

./taxonomy_xml.module, line 680
taxonomy_xml.module This module makes it possible to import and export taxonomies as XML documents.

Code

function taxonomy_xml_canonicize_predicates(&$term) {

  // Translate the predicate statements into what we need
  if (empty($term->predicates)) {
    $term->predicates = array();
  }

  // $predicate_synonyms is a translation array to match synonyms from various syntaxes with Drupal concepts
  $predicate_synonyms = taxonomy_xml_relationship_synonyms();
  foreach ($term->predicates as $predicate => $values) {
    $original_predicate = $predicate;

    // First translate misc terminology synonyms to the cannonic predicate I use everywhere
    // This allows us to interpret several XML dialects at once
    if (isset($predicate_synonyms[$predicate]) && ($cannonic = $predicate_synonyms[$predicate])) {
      $predicate = $cannonic;
    }

    # drupal_set_message(t("Applying '$predicate' ($predicate) value of ". print_r($values, 1) ." found in $uri"));
    switch ($predicate) {
      case TAXONOMY_XML_DESCRIPTION:
        $term->description = taxonomy_xml_get_literal_string($values);
        break;
      case TAXONOMY_XML_NAME:

        // In the (hopefully never) case that a term has, eg, both a 'name'
        // and a 'title' set, and different, we may have conflicts to resolve.
        // Pre-empt this here by noting both, but revisit if we can come up
        // with better logic, eg ordering priority of synonyms.
        $val = taxonomy_xml_get_literal_string($values);
        if (isset($term->name) && $val != $term->name) {
          $term->name .= ' (' . $val . ')';
        }
        else {
          $term->name = $val;
        }
        break;
      case TAXONOMY_XML_PARENT:
      case TAXONOMY_XML_RELATED:
      case TAXONOMY_XML_CHILD:

        // A term relationship.
        // Translate each referred item from URI to its label or handle,
        // and save to be linked in later
        foreach ($values as $i => $target_uri) {
          $term->predicates[$predicate][$i] = $target_uri;
        }
        break;
      case TAXONOMY_XML_HAS_SYNONYM:
        $term->synonyms_array = isset($term->synonyms_array) ? array_merge($term->synonyms_array, $values) : $values;
        $term->synonyms = join("\n", array_unique($term->synonyms_array));
        break;
      case TAXONOMY_XML_IN_VOCABULARY:

        /* currently not used very much - more than one vocab per input file is rare
           // This term need to be in the vocabulary referred to by this URI
           // check our known vocabs to see if they are recognised
           // Do we know a vocab with an ID matching this 'isdefinedby' value?
           dpm(array('looking for vocab' => $values));
           foreach ($values as $value) { // probably just one...
           if (isset($vocabularies[$value])) {
           // I know this vocab!
           $term->vid = $vocabularies[$value]->vid;
           }
           }
           */
        break;
      case 'type':

      // These are already done. Ignore
      case 'subPropertyOf':

      // Useless, ignore also
      case TAXONOMY_XML_UNUSED:

        // Explicitly ignore these
        break;
      default:
    }
  }

  #dpm(array('transformed predicates are' => $term->predicates));
}