function taxonomy_xml_xml_parse in Taxonomy import/export via XML 5
Same name and namespace in other branches
- 5.2 xml_format.inc \taxonomy_xml_xml_parse()
- 6.2 xml_format.inc \taxonomy_xml_xml_parse()
- 6 xml_format.inc \taxonomy_xml_xml_parse()
- 7 formats/xml_format.inc \taxonomy_xml_xml_parse()
Initiate the parser on the custom XML schema.
This uses the XML callback parser with tag callbacks.
See also
taxonomy_xml_element_start
taxonomy_xml_element_end
taxonomy_xml_element_data
File
- ./
xml_format.inc, line 129 - Include routines for the original XML parsing and taxonomy/term creation.
Code
function taxonomy_xml_xml_parse(&$data, $vid = 0, $parent_tid = NULL, $duplicate = 0) {
global $_tx_terms, $_tx_vocabulary;
// Unset the global variables before we use them:
unset($GLOBALS['_tx_element'], $GLOBALS['_tx_term'], $GLOBALS['_tx_tag']);
$_tx_terms = array();
$_tx_vocabulary = array();
$new_terms = array();
// parse the data:
$xml_parser = drupal_xml_parser_create($data);
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
//
xml_set_element_handler($xml_parser, 'taxonomy_xml_element_start', 'taxonomy_xml_element_end');
xml_set_character_data_handler($xml_parser, 'taxonomy_xml_element_data');
if (!xml_parse($xml_parser, $data, 1)) {
watchdog('error', t('Taxonomy_xml: failed to parse file: %error at line %line.', array(
'%error' => xml_error_string(xml_get_error_code($xml_parser)),
'%line' => xml_get_current_line_number($xml_parser),
)));
drupal_set_message(t('Failed to parse file: %error at line %line.', array(
'%error' => xml_error_string(xml_get_error_code($xml_parser)),
'%line' => xml_get_current_line_number($xml_parser),
)), 'error');
}
xml_parser_free($xml_parser);
// If an existing vocabulary has been chosen or has the same name as the vocabulary being added,
// terms should be added to the existing vocabulary. Otherwise a new vocabulary should be created.
if ($vid == 0) {
$name = $_tx_vocabulary['name'];
$_tx_vocabulary = (array) _taxonomy_xml_get_vocabulary_placeholder(trim($name));
}
else {
$_tx_vocabulary = (array) module_invoke('taxonomy', 'get_vocabulary', $vid);
}
// Get the maximum depth of terms
$term_depth = array();
foreach ($_tx_terms as $term) {
$term_depth[] = $term['depth'];
}
// Import terms in order of depth
$new_tid = array();
for ($i = 0; $i <= max($term_depth); $i++) {
foreach ($_tx_terms as $term) {
if ($term['depth'] != $i) {
continue;
}
$term['vid'] = $_tx_vocabulary['vid'];
$term['old_tid'] = $term['tid'];
unset($term['tid']);
if (is_array($term['parent'])) {
foreach ($term['parent'] as $key => $value) {
if ($value) {
$term['parent'][$key] = $new_tid[$value];
}
}
}
$term_exists = false;
if ($duplicate == 0) {
$existing_terms = module_invoke('taxonomy', 'get_term_by_name', $term['name']);
if (count($existing_terms > 0)) {
foreach ($existing_terms as $existing_term) {
if ($existing_term->vid == $term['vid']) {
$term_exists = true;
// Map the term tid from the imported XML file to the tid in term_data database table
$new_tid[$term['old_tid']] = $existing_term->tid;
$skipped_terms[$existing_term->name] = 1;
}
}
}
}
// If the term doesn't already exist in this vocabulary, add it.
if (!$term_exists) {
taxonomy_save_term($term);
// Map the term tid from the imported XML file to the tid in term_data database table
$new_tid[$term['old_tid']] = $term['tid'];
$new_terms[] = $term['name'];
}
}
}
$output .= t('Vocabulary %name: ', array(
'%name' => $_tx_vocabulary['name'],
));
if ($new_terms) {
$output .= t('Added term(s) %terms. ', array(
'%terms' => implode(', ', $new_terms),
));
}
else {
$output .= t('No terms added. ');
}
if ($skipped_terms) {
$output .= t('Ignored duplicate term(s) %terms. ', array(
'%terms' => implode(', ', array_keys($skipped_terms)),
));
}
drupal_set_message($output);
return count($new_terms);
}