function taxonomy_xml_absorb_vocabulary_definitions in Taxonomy import/export via XML 7
Same name and namespace in other branches
- 5.2 taxonomy_xml.module \taxonomy_xml_absorb_vocabulary_definitions()
- 5 taxonomy_xml.module \taxonomy_xml_absorb_vocabulary_definitions()
- 6.2 taxonomy_xml.module \taxonomy_xml_absorb_vocabulary_definitions()
- 6 taxonomy_xml.module \taxonomy_xml_absorb_vocabulary_definitions()
Create Vocabulary definitions.
Use the vocabs defined as resources in the input.
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] = "a list" is flattened to ['#vocabulary-3']['description'] = "a list"
Either input format is fine.
1 call to taxonomy_xml_absorb_vocabulary_definitions()
- taxonomy_xml_rdf_parse in formats/
rdf_format.inc - Read in RDF taxonomies and vocabularies. Create vocabs and terms as needed.
File
- ./
taxonomy_xml.process.inc, line 232 - The workhorse processes for importing taxonomies.
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.
$vocab_ids = array();
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
// Diagnostics:
if (count($vocabularies) > 1) {
drupal_set_message(t("When importing, I found what looked like more than one vocabulary definition in the same resource. This could be confusing in batch jobs, but should be OK if each of the term items include an rdfs:isDefinedBy field. <pre>!object</pre>", array(
'!object' => print_r($vocabularies, 1),
)), 'warning');
}
foreach ($vocabularies as $guid => &$vocab) {
// Relabel any big namespaced predicates into common ones.
// For vocabs this won't do much, but will still help consolidate
// descriptions and labels.
taxonomy_xml_canonicize_predicates($vocab, 'taxonomy_vocabulary');
// 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.
if (!empty($vocab->predicates)) {
taxonomy_xml_merge_predicates_into_attributes($vocab);
}
if (empty($vocab->name)) {
drupal_set_message("We require a NAME to create a vocabulary. Vocabulary definition appeared to have no name. Using a label derived from the URI instead.", 'warning');
// Make up a name based on the URI.
$vocab->name = taxonomy_xml_shortname($guid);
}
$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 {$guid}. 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);
}
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
// There is very little we can import from the given data?
$vocab = _taxonomy_xml_get_vocabulary_placeholder($vocab->name);
$vocab->description = $guid;
// Built a vocabulary from input details. Now save it
$status = taxonomy_vocabulary_save($vocab);
$strings = array(
'%name' => $vocab->name,
'%description' => $vocab->description,
);
if (!empty($vocab->vid)) {
drupal_set_message(t("Made a new Drupal vocabulary definition from data found in the input. Vocab is called: '%name': %description", $strings));
}
else {
drupal_set_message(t("Failed to create a new vocabulary called: '%name' : %description \n This is fatal, aborting.", $strings), 'error');
return FALSE;
}
}
$vocab_ids[$guid] = $vocab->vid;
}
// End looping through found vocabs.
}
else {
drupal_set_message("The document provided no recognisible vocabulary definitions");
}
$taxonomy_xml_vocabulary_ids = variable_get('taxonomy_xml_vocabulary_ids', array()) + $vocab_ids;
variable_set('taxonomy_xml_vocabulary_ids', $taxonomy_xml_vocabulary_ids);
// 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 reset($vocab_ids);
}