You are here

function taxonomy_xml_absorb_vocabulary_definitions in Taxonomy import/export via XML 5

Same name and namespace in other branches
  1. 5.2 taxonomy_xml.module \taxonomy_xml_absorb_vocabulary_definitions()
  2. 6.2 taxonomy_xml.module \taxonomy_xml_absorb_vocabulary_definitions()
  3. 6 taxonomy_xml.module \taxonomy_xml_absorb_vocabulary_definitions()
  4. 7 taxonomy_xml.process.inc \taxonomy_xml_absorb_vocabulary_definitions()

Use the vocabs defined as resources in the input to find or create vocabulary definitions.

Parameters

$vocabularies An array of vocabulary definition objects, extracted: from the XML. Modified with their deduced or new vid values by reference

$vocabularies = array( '#vocabulary-3' => stdClass Object 'name' => "Countries", 'predicates' => array( 'description' => array( 0 => "A list of countries" ), 'version' => array( 0 => "2008-08-08" ), ) ) )

All 'predicates' will be compressed into properties. EG in the above example, ['#vocabulary-3']['predicates']['description'][0] is flattened to ['#vocabulary-3']['description']

Either input format is fine.

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

File

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

Code

function taxonomy_xml_absorb_vocabulary_definitions(&$vocabularies) {

  // See if we can match this definition against the given vid
  // - then on name.
  // If neither seems to exist, make a new one.
  if (is_array($vocabularies)) {

    # dpm(array("The vocabulary definition(s) found in the input file is ", $vocabularies));

    // There may be more than one vocab def per file, although this is unlikely
    foreach ($vocabularies as $vocabid => &$vocab) {

      // Merge all predicate data into a simpler array, re-tagging the attributes as needed
      // - there's not a lot of metadata about vocabs we know how to import, but try anyway - for expansion
      taxonomy_xml_merge_predicates_into_attributes($vocab);
      $target_vocab = NULL;

      // Deduce the given vocab definitions vid, if given as a value
      if (isset($vocab->vid)) {
        $vocab->internal_id = $vocab->vid;
        drupal_set_message(t("Found a vocabulary definition in the input, called {$vocabid}. vid={$vocab->internal_id}"));

        // Try to maintain old Vocabulary IDs
        // Check if it's a good number to write into
        // If the input defines a vid BUT there is already a non-matching vocab with that number, we need a new number
        // If it DOES seem to match, we are safe
        $target_vocab = taxonomy_vocabulary_load($vocab->internal_id);
      }

      #dpm(array('loaded vocab' => $target_vocab, 'parsed vocab' => $vocab));
      if (!empty($target_vocab) && $target_vocab->name == $vocab->name) {

        // Looks like a great match
        $vocab->vid = $vocab->internal_id;
        drupal_set_message(t("Found matching target vocabulary '%vocab_name' vid=%vocab_vid", array(
          '%vocab_name' => $vocab->name,
          '%vocab_vid' => $vocab->vid,
        )));
      }
      else {
        if ($target_vocab) {
          drupal_set_message(t("The vocab ID given in the input file (%vocab_vid) conflicts with an existing vocabulary. We need a different ID... ", array(
            '%vocab_vid' => $vocab->vid,
          )));
        }
        unset($vocab->vid);

        // Vocab with this id exists, but is called something else - Do not merge with it
        // Look for a match by name instead
        if ($target_vocab = taxonomy_xml_get_vocabulary_by_name($vocab->name)) {

          // Found a local vocab called the same as the input vocab. That's a good enough match for us.
          $vocab->vid = $target_vocab->vid;
          drupal_set_message(t("Found a target vocabulary already in the database, matching by name '%name' vid=%vid . This will be used, but not updated.", array(
            '%name' => $vocab->name,
            '%vid' => $vocab->vid,
          )));
        }
      }

      // Have we found a target vocab yet?
      if (empty($vocab->vid)) {

        // Make a brand new one from the imported definition
        $vocab = _taxonomy_xml_get_vocabulary_placeholder($vocab->name);

        // Built a vocabulary from input details. Now save it
        // dpm($vocab);
        $vocab_array = (array) $vocab;
        taxonomy_save_vocabulary($vocab_array);
        $vocab = taxonomy_vocabulary_load($vocab_array['vid']);
        drupal_set_message(t("Made a new Drupal vocabulary definition from data found in the input. Vocab is called: '%name' : %description ", array(
          '%name' => $vocab->name,
          '%description' => $vocab->description,
        )));

        #dpm($vocab);
      }
    }

    // end looping through found vocabs
  }
  else {
    drupal_set_message("The document provided no recognisible vocabulary definitions");
  }

  // This is the default (last found) vid. Probably should not be used, but we may have to make a guess.
  // Either an input file contains just one vocab - in which case this will be right,
  // or the input file contains multiple vocabularies - in which case the terms damn well ought to be tagged with which vocab to use.
  return isset($vocab->vid) ? $vocab->vid : NULL;
}