You are here

function taxonomy_xml_add_all_children_to_queue in Taxonomy import/export via XML 7

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

Queue up an import action.

If the currently processing term refers to other terms by URI, set up a job to retrieve them recursively later.

For all unknown $term->predicates[TAXONOMY_XML_CHILD] URIs, add a job to the batch queue.

Helper function for parser routines

See also

taxonomy_xml_add_term_to_batch_queue()

3 calls to taxonomy_xml_add_all_children_to_queue()
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 544
The workhorse processes for importing taxonomies.

Code

function taxonomy_xml_add_all_children_to_queue($term) {
  if (variable_get('taxonomy_xml_recurse_down', TRUE) && !empty($term->predicates[TAXONOMY_XML_CHILD])) {

    // Add child items to the import queue.
    $children = $term->predicates[TAXONOMY_XML_CHILD];

    // dpm(array('Queuing children' => $children));
    foreach ((array) $children as $child_ref) {

      // Check that it looks like a valid URL we can request.
      $scheme = "unknown";
      if (valid_url($child_ref)) {

        // The ref is a URI.
        // but LSID identifiers pass that test :)
        $url_parts = @parse_url($child_ref);
        $scheme = isset($url_parts['scheme']) ? $url_parts['scheme'] : 'no scheme';
      }
      if (isset($url_parts['host']) && $url_parts['host'] == '_') {

        // BEWARE, RDF bnodes ("_:123") may look like URIs.
        // Ignore them.
        continue;
      }
      if ($scheme == 'http') {

        // Check we don't know it already.
        if ($found_term = taxonomy_xml_get_term_by_guid($child_ref, $term->vid)) {
          watchdog('taxonomy_xml', "\n              While processing %term_name, found an existing local version of it.\n              !ref\n              Therefore NOT queuing it for further recursion again.\n            ", array(
            '%term_name' => $term->name,
            '!ref' => l('#' . $found_term->tid, 'taxonomy/term/' . $found_term->tid),
          ), WATCHDOG_DEBUG);

          // This is cool, we have a handle on this term.
          // Make a note in the global list.
          $terms =& taxonomy_xml_current_terms();
          $terms[$child_ref] = $found_term;
        }
        else {

          // Save the request as a batch job to do later.
          // Our session queue will tuck this away and remember it.
          // Create a placeholder so at least we know where this item
          // is being imported to
          // Beware memory.
          // This should be lightweight, as the batch API seems to be
          // treating it inefficiently.
          $placeholder_term = (object) array(
            'guid' => $child_ref,
            'parent' => array(
              $term->tid => $term->tid,
            ),
            'vid' => $term->vid,
          );

          // Some data sources MAY supply a known name for this child,
          // but that's too hard to extract
          // Trust the named resource will fill in the gaps,
          // and just know it's a URI for now.
          taxonomy_xml_add_term_to_batch_queue($placeholder_term);
          watchdog('taxonomy_xml', "\n            While processing %term_name,\n            Found a reference to child term !child_ref.\n            I did not recognise that URI in this vocab %vid.\n            Queuing it for later retrieval and import", array(
            '%term_name' => $term->name,
            '!child_ref' => l($child_ref, $child_ref),
            '%vid' => $term->vid,
          ), WATCHDOG_NOTICE);
        }
      }
      else {

        // The referred term is not a URI,
        // nor do we recognise its identifier so far.
        // It's a dangling reference. What can we do?
        // Handle URI/GUID lookup services?
        //
        // @todo Should do this with a hook/service-callback rather than built into this module.
        // @todo - this uses a global, should use batch context info
        // Lets see if it fits the pattern that a lookup service expects
        if ($service_id = variable_get('taxonomy_xml_service_id', '')) {
          $services = taxonomy_xml_services();
          $service = $services[$service_id];
          $lookup_guid = taxonomy_xml_sub_placeholders_into_pattern($service['pattern'], array(
            $service['identifier'] => $child_ref,
          ));

          // #drupal_set_message(t('Will use service lookup to find !child_ref', array('!child_ref' => l($child_ref, $lookup_guid), '%servicename' => $service['servicename'])));
          $placeholder_term = (object) array(
            'guid' => $lookup_guid,
            'parent' => array(
              $term->tid => $term->tid,
            ),
          );
          taxonomy_xml_add_term_to_batch_queue($placeholder_term);
        }
        else {
          drupal_set_message(t('Cannot yet resolve non-URI references, and no resolver service is active. %child_ref', array(
            '%child_ref' => $child_ref,
          )));
        }
      }
    }
  }
}