You are here

function taxonomy_xml_rdf_make_term in Taxonomy import/export via XML 6.2

Same name and namespace in other branches
  1. 7 formats/rdf_format.inc \taxonomy_xml_rdf_make_term()

Create the placeholder and fill in the values for this term - NOT its relationships yet.

1 call to taxonomy_xml_rdf_make_term()
taxonomy_xml_rdf_parse in ./rdf_format.inc
Read in RDF taxonomies and vocabularies. Create vocabs and terms as needed.

File

./rdf_format.inc, line 456
Include routines for RDF parsing and taxonomy/term creation. @author dman http://coders.co.nz

Code

function taxonomy_xml_rdf_make_term(&$term) {
  $identifier = $term->identifier;

  # drupal_set_message(t("Reviewing term %identifier '%name' and analyzing its properties", array('%identifier' => $identifier, '%name' => @$term->name)));

  // When running in batch, children will have a hard time finding their
  // parents if they only know them by source-localized ID (probably a URI)
  // and the destination-taxonomy (here) HASN'T REMEMBERED THAT INFO.
  // Because taxonomy.module just doesn't.
  // We require some other module (taxonomy_enhancer is good) to save that
  // metadata for us so the child can find its target later.
  // This is our 'identifier' - the REMOTE identifier not the local one.
  if (!isset($term->guid)) {
    $term->guid = $identifier;
  }

  // Build term from data
  // Convert all input predicates into attributes on the object
  // the taxonomy.module will understand
  taxonomy_xml_canonicize_predicates($term);

  // Ensure name is valid
  if (empty($term->name)) {

    // which of these approaches is correct?
    // Look, if we don't even have a name, creating a term is a waste of time.
    // RDF feeds commonly consist of a bunch of pointers, we can't invent placeholders until we know a little more.
    // Let's not do this.

    #drupal_set_message(t("Not enough information yet (not even a name) to create a term referred to as %identifier. Not creating it yet.", array('%identifier' => $identifier)));

    #unset($terms[$identifier]);

    #continue;

    // If the parent is trying to link to a child thats not yet made,
    // we probably don't know a proper name or label.
    // Fallback to a name, identifier derived (roughly) from the URI identifier - not always meaningful, but all we have in some contexts.
    $term->name = taxonomy_xml_label_from_uri($identifier);
    watchdog('taxonomy_xml', "\n      We were unable to find a specific label for the term\n      referred to as %identifier.\n      Guessing that %name will be good enough.", array(
      '%identifier' => $identifier,
      '%name' => $term->name,
    ), WATCHDOG_NOTICE);

    // Still, this causes problems if queuing data about terms that are not yet loaded
    // - such as those that are ONLY referenced by URI with no human name (Freenet)
    // Our munged names are temporary until the full data is retrieved.
    if (empty($term->name)) {

      // Still not set?
      // This should be impossible - all subjects must have a URI
      // But who knows what wierdness the input gave us
      drupal_set_message(t("\n        A term called %identifier didn't produce any readable name to use. ", array(
        '%identifier' => $identifier,
      )), 'error');
      continue;
    }
  }
  $force_new = variable_get('taxonomy_xml_duplicate', FALSE);

  // See if a definition matching this terms name already exists in the DB.
  // Build on that.
  $existing_term = taxonomy_xml_get_term_by_guid($term->guid, $term->vid);
  if (!$existing_term) {
    $existing_term = _taxonomy_xml_get_term_placeholder($term->name, $term->vid, $force_new);
  }

  #dpm(array('old term' => $existing_term, 'new term' => $term));

  // Merge the old term objects properties into this one. Really just want its tid, but there may be more info I should not lose.
  // New input takes precedence over older data. Old data just fills in the gaps.
  foreach ((array) $existing_term as $key => $value) {
    if (!isset($term->{$key})) {
      $term->{$key} = $value;
    }
  }

  // The term object is now as tidy as it can be as a self-contained entity.

  # dpm($term);
  if (variable_get('taxonomy_xml_reuseids', FALSE)) {

    // TODO this has not been tested since migration from D5!
    // MAINTAIN IDS
    // Because this is likely to be used with a site-cloning set-up,
    // it would help if we tried to match IDs
    // OTOH, doing so could be very messy for other situations.
    // So,
    //  iff there is no pre-existing term with this id,
    //  create this one as a clone with the old ID.
    // This requires a little DB sneakiness.
    if (isset($term->internal_id) && !taxonomy_term_load($term->internal_id)) {
      $term->tid = $term->internal_id;
      drupal_set_message(t("Doing sneaky import of %term_name re-using the internal id = %term_id", array(
        '%term_name' => $term->name,
        '%term_id' => $term->internal_id,
      )));
      db_query("INSERT INTO {term_data} (tid, name, description, vid, weight) VALUES (%d, '%s', '%s', %d, %d)", $term->tid, $term->name, $term->description, $term->vid, $term->weight);

      # sequences is gone in D6. Will inserting beyond the auto-increment self-correct?
      $current_id = db_last_insert_id('term_data', 'tid');
      if ($current_id < $term->tid) {

        // This is probably now MYSQL specific.
        db_query("ALTER TABLE {term_data} AUTO_INCREMENT = %d;", $term->tid);
      }
    }
  }

  # Here's where last-minute data storage done by other modules gets set up

  // module_invoke_all doesn't do pass-by-reference, so do our own loop.
  foreach (module_implements('taxonomy_xml_term_presave') as $module) {
    $function = $module . '_' . 'taxonomy_xml_term_presave';
    $function($term);
  }

  ////////////////////////////

  // Assist taxonomy_enhancer
  if (module_exists('taxonomy_enhancer')) {
    $fields = taxonomy_enhancer_get_fields_by_vocabulary($term->vid);
    foreach ($fields as $te_field) {
      if (isset($term->predicates[$te_field->title])) {

        // Looks like a predicate of the same name as a te field exists. Set it
        foreach ($term->predicates[$te_field->title] as $delta => $value) {
          $term->fields[$te_field->fid][$delta] = array(
            'value' => $term->predicates[$te_field->title][$delta],
            'format' => 0,
          );
        }
      }
    }
  }

  #dpm($term);

  // finished taxonomy_enhancer (should be delegated to a helper hook)

  /////////////////////////

  #dpm(array("ready to save" => $term));
  $save_term = (array) $term;
  $status = taxonomy_save_term($save_term);

  # Need to ensure the new hook callbacks fire also during that term saving

  // Re-retrieve the new term definition,
  // just in case anything extra happened to it during processing
  $new_term = taxonomy_xml_get_term_by_name_from_vocab($term->name, $term->vid);
  if (!$new_term) {
    drupal_set_message(t("\n      It seems like we failed to create and retrieve a term called %term_name", array(
      '%term_name' => $term->name,
    )), 'error');
  }

  // Merge retrieved values back over our main definition so the handles are up-to-date
  foreach ((array) $new_term as $key => $value) {
    $term->{$key} = $value;
  }
  if ($status == SAVED_NEW) {

    // Just remember this is fresh - for useful feedback messages.
    $term->taxonomy_xml_new_term = TRUE;
  }

  // It's possible that not all the referenced items were available in the current document/loop
  // Add referred items to the import queue for later processing
  taxonomy_xml_add_all_children_to_queue($term);
  $term->taxonomy_xml_presaved = TRUE;

  // A flag to avoid double-processing
  // Allow other hooks to do last-minute processing
  // http://drupal.org/node/791376
  foreach (module_implements('taxonomy_xml_term_postsave') as $module) {
    $function = $module . '_' . 'taxonomy_xml_term_postsave';
    $function($term);
  }
  return $term;

  // end term-construction;
}