You are here

function taxonomy_xml_canonicize_predicates in Taxonomy import/export via XML 7

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

Convert aliased predicates into common ones.

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.

This operated by reference on the object it's given. It assumes it's processing a term, but the same logic holds for vocab entities also.

Parameters

object $term: Term object

string $entity_type: type is just a hint to avoid irrelevant warnings if we are 'canonisizing' a vocab definition instead.

4 calls to taxonomy_xml_canonicize_predicates()
taxonomy_xml_absorb_vocabulary_definitions in ./taxonomy_xml.process.inc
Create Vocabulary definitions.
taxonomy_xml_mesh_parse in formats/mesh_format.inc
Reads a XML file and creates the term definitions found in it.
taxonomy_xml_rdf_make_term in formats/rdf_format.inc
Create the placeholder and fill in the values for this term.
taxonomy_xml_tcs_parse in formats/tcs_format.inc
Reads a TCS file and creates the term definitions found in it.

File

./taxonomy_xml.process.inc, line 365
The workhorse processes for importing taxonomies.

Code

function taxonomy_xml_canonicize_predicates(&$term, $entity_type = 'taxonomy_term') {

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

  // Integrity Assertion.
  if (empty($term->vid) && $entity_type == 'taxonomy_term') {
    watchdog('taxonomy_xml', '
      Processing a term with no vocabulary ID, this is unexpected.
      Terms should have a vid set by this point.
      I do not require the vid here, but all term objects should have had it
      set before reaching this point.
      <pre>!term</pre>
    ', array(
      '!term' => print_r($term, 1),
    ), WATCHDOG_WARNING);
  }

  // $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;
    }
    else {

      // Predicates may still be namespaced at this point.
      // Remove the namespace and see if
      // the shortname matches one of our known types.
      $shortname = taxonomy_xml_shortname($predicate);
      if (isset($predicate_synonyms[$shortname]) && ($cannonic = $predicate_synonyms[$shortname])) {
        $predicate = $cannonic;
      }
    }
    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:
        foreach ($values as $target_guid) {
          $term->predicates[$predicate][$target_guid] = $target_guid;
          $strings = array(
            '%predicate' => $predicate,
            '%subject' => isset($term->name) ? $term->name : $term->guid,
            '%target_guid' => $target_guid,
          );
        }
        break;
      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_guid) {
          $term->predicates[$predicate][$target_guid] = $target_guid;
        }
        break;
      case TAXONOMY_XML_HAS_SYNONYM:
        $term->synonyms_array = isset($term->synonyms_array) ? array_merge($term->synonyms_array, $values) : $values;
        $term->synonyms = implode("\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));
        @see taxonomy_xml_absorb_vocabulary_definitions()
        where we make a temporary global note of vocab guid->vid relations for
        this purpose.
        */
        $taxonomy_xml_vocabulary_ids = variable_get('taxonomy_xml_vocabulary_ids', array());
        foreach ($values as $value) {

          // Probably just one...
          // but IF it's named and IF the name is valid
          // and we've seen it before.
          if (isset($taxonomy_xml_vocabulary_ids[$value])) {

            // I know this vocab!
            $term->vid = $taxonomy_xml_vocabulary_ids[$value];
          }
        }
        break;
      case 'type':

      // These are already done. Ignore.
      case TAXONOMY_XML_UNUSED:

        // Explicitly ignore these,
        // Unset and discard them in fact!
        unset($term->predicates[$original_predicate]);
        break;
      case TAXONOMY_XML_OTHER_PREDICATE:

        // These ones we'll try to save as RDF statements,
        // attached to the term URI.
        foreach ($values as $value) {
          $term->rdf[] = array(
            'subject' => NULL,
            'predicate' => $original_predicate,
            'object' => $value,
          );
        }
        watchdog('taxonomy_xml', "\n          Found a useful predicate '<b>%predicate</b> = %value'.\n          Making a note of it for pure-RDF storage.\n          ", array(
          '%predicate' => "{$predicate} ({$original_predicate})",
          '%subject' => isset($term->name) ? $term->name : $term->guid,
          '%value' => $value,
        ), WATCHDOG_DEBUG);
        break;
      default:

        // A valid, but unrecognised statement was found
        // when flattening the input.
        watchdog('taxonomy_xml', "\n          Dunno what to do with '<b>%predicate</b>'.\n          Subject '%subject' has value(s) = <pre>!values</pre>\n          A later content type may absorb this info,\n          but it's not a core term property.", array(
          '%predicate' => $predicate,
          '%subject' => isset($term->name) ? $term->name : $term->guid,
          '!values' => print_r($values, 1),
        ), WATCHDOG_INFO);
    }
  }
  if (!empty($term->guid)) {
    taxonomy_xml_set_term_guid($term, $term->guid);
  }
}