You are here

function taxonomy_xml_get_term_by_guid in Taxonomy import/export via XML 6.2

Same name and namespace in other branches
  1. 7 taxonomy_xml.module \taxonomy_xml_get_term_by_guid()

Special lookup for terms if they are saved with a URI or GUID

Very specific to certain ways of serializing terms, REQUIRES taxonomy_enhancer and a field called field_URI OR rdf.module and an owl:sameAs relation

3 calls to taxonomy_xml_get_term_by_guid()
taxonomy_xml_add_all_children_to_queue in ./taxonomy_xml.module
Queue up an import action.
taxonomy_xml_rdf_make_term in ./rdf_format.inc
Create the placeholder and fill in the values for this term - NOT its relationships yet.
taxonomy_xml_set_term_relations in ./taxonomy_xml.module
Given a list of terms, set the related-terms and structure, and save again

File

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

Code

function taxonomy_xml_get_term_by_guid($guid, $vid = NULL) {
  if (!$guid) {
    return NULL;
  }

  // Our own taxonomy_guid module is the simplest one, check that first.
  if (module_exists('taxonomy_guid')) {
    $terms = taxonomy_guid_get_term($guid, $vid);
    if (count($terms) > 1) {

      // Not sure how it happened, but we may get more than one result.
      watchdog('taxonomy_xml', "This is confusing, apparently there are more than one local match\n        with the GUID '%guid' .\n        <pre>!lookups</pre>", array(
        '%guid' => $guid,
        '!lookups' => print_r($terms, 1),
      ), WATCHDOG_WARNING);
    }
    if (!empty($terms)) {
      return reset($terms);
    }
  }

  // taxonomy_enhancer.module, if available, may have more data about our terms. Hopefully including a GUID.
  if (function_exists('taxonomy_enhancer')) {
    $searchterm = (object) array(
      'field_guid' => $guid,
    );
    $results = taxonomy_enhancer_get_term($searchterm);
    if (!empty($results)) {

      #drupal_set_message(t("Successfully found a known target term indexed by external <a href='!guid'>GUID</a>.", array('!guid' => $guid)));
      $term = array_pop($results);
    }
    else {

      #dpm("Couldn't find a known item with a URI = $guid ");
    }
  }
  if (module_exists('rdf')) {

    // Lookup the RDF values to see if this term has any 'sameAs' matches
    // Note the canononic or remote URL will be on the right of the triple
    $lookups = rdf_normalize(rdf_query(NULL, 'owl:sameAs', $guid));

    // Normalized data is indexed by [subject][predicate][] = object
    // We are looking for the value on the left - the subject.
    $local_term_paths = array_keys($lookups);
    if (count($local_term_paths) > 1) {

      // Not sure how it happened, but we may get more than one result.
      watchdog('taxonomy_xml', "This is confusing, apparently there are more than one local match\n        that are sameAs '%guid' .\n        <pre>!lookups</pre>\n        Possibly the same concept in a different vocabulary.\n        I'm only going to deal with one of them (the one in the current vocab - if any).", array(
        '%guid' => $guid,
        '!lookups' => print_r(array_keys($lookups), 1),
      ), WATCHDOG_DEBUG);
    }
    $term_base_url = url('taxonomy/term/', array(
      'absolute' => TRUE,
    ));

    // There is (almost certainly) only one value all down this tree,
    // but I don't know the keys, just iterate.
    foreach ((array) $lookups as $subject => $predicate_array) {
      foreach ($predicate_array as $predicate => $value_array) {
        foreach ($value_array as $i => $found_value) {

          // Stored in the database was a URI, backtrack to the term ID.
          // Is my term sameAs the URI?
          if (strstr($subject, $term_base_url)) {

            // yep, it's a local term URL
            $tid = intval(drupal_substr($subject, drupal_strlen($term_base_url)));
            if ($found_term = taxonomy_get_term($tid)) {
              watchdog('taxonomy_xml', 'Found <a href="!term_link">an existing term %term_name</a>
                in vocab %vid when looking for %guid', array(
                '%guid' => $guid,
                '%term_name' => $found_term->name,
                '%vid' => $found_term->vid,
                '!term_link' => url('taxonomy/term/' . $found_term->tid),
              ), WATCHDOG_DEBUG);

              // Now I found the term, check it's the right vocab (in case of multiples)
              if ($vid && $found_term->vid == $vid) {
                $term = $found_term;
              }
            }
          }

          // found a string match
        }

        // loop all result values
      }

      // loop all (1) predicates
      // Keep looking if that didn't work
      // (highly rare there's more than one)
    }

    // loop all (1) result
  }

  // RDF.module lookup
  return isset($term) ? $term : NULL;
}