You are here

function taxonomy_xml_rdf_export_vocabulary in Taxonomy import/export via XML 7

Return an RDF representation of the requested vocab.

Include references to all the top-level child terms.

File

./taxonomy_xml_rdf.inc, line 81

Code

function taxonomy_xml_rdf_export_vocabulary($vocabulary) {

  // Expect an object, but the menu callback may have just provided the id
  $vocabulary = is_numeric($vocabulary) ? taxonomy_vocabulary_load($vocabulary) : $vocabulary;

  // This URI may not resolve unless you are running taxonomy_server, but
  // that's fine - it doesn't have to.
  $subject_uri = url('taxonomy/vocabulary/' . $vocabulary->vid, array(
    'absolute' => TRUE,
  ));
  $subject_curi = 'vocabulary:' . $vocabulary->vid;

  #dpm($vocabulary);
  $XMLDoc = new DOMDocument();
  $XMLRDF = $XMLDoc
    ->createElementNS(TAXONOMY_XML_RDF_NS, "rdf:RDF");
  $XMLDoc
    ->appendChild($XMLRDF);
  $vocab_xml = rdf_entity_to_xml($vocabulary, $XMLDoc);
  $vocab_xml
    ->setAttributeNS(TAXONOMY_XML_RDF_NS, 'rdf:about', $subject_uri);
  $XMLRDF
    ->appendChild($vocab_xml);

  // Now add the terms in this vocabulary
  // They are listed as siblings, not children of the ontology
  // TODO work with max_depth
  $parent = 0;
  $depth = -1;
  $max_depth = NULL;
  $max_depth = 2;
  $tree = taxonomy_get_tree($vocabulary->vid, $parent, $max_depth, $depth);
  foreach ($tree as $term) {

    // need a full term load to retrieve the mapping and some extras
    $term = taxonomy_term_load($term->tid);
    if ($term_xml = rdf_entity_to_xml($term, $XMLDoc)) {
      $term_uri = taxonomy_term_uri($term);
      $path = url($term_uri['path'], array(
        'absolute' => TRUE,
      ));

      // Why does that return an array now?
      $term_xml
        ->setAttributeNS(TAXONOMY_XML_RDF_NS, 'rdf:about', $path);
      $vocab_xml
        ->appendChild($term_xml);
    }
  }
  return '<textarea style="width:100%; height:300px">' . $XMLDoc
    ->saveXML() . '</textarea>';
  $subject = rdf_is_valid_curie($subject_curi) ? rdf_qname_to_uriref($subject_curi) : rdf_uri($subject_curi);
  $data = rdf_query($subject_uri);

  // $data is something called  RDF_QueryIterator extends AppendIterator ??
  // I have no idea how to add things to that. Flatten that (we were about to do that anyway)
  // and add more statements
  $data = rdf_normalize($data);

  #dpm($vocabulary);

  // Get normal internal $term values from Drupal
  // and represent them as RDF statements
  $statements = array(
    'rdf:type' => rdf_qname_to_uriref('owl:Ontology'),
    'rdfs:label' => rdf_literal($vocabulary->name),
  );
  if (!empty($vocabulary->description)) {
    $statements['rdfs:comment'] = rdf_literal($vocabulary->description);
  }
  $data = convert_statements_to_rdf_data($subject_uri, $statements);

  // Now add the terms in this vocabulary
  // They are listed as siblings, not children of the ontology
  // TODO work with max_depth
  $parent = 0;
  $depth = -1;
  $max_depth = NULL;
  $max_depth = 2;
  $tree = taxonomy_get_tree($vocabulary->vid, $parent, $max_depth, $depth);
  foreach ($tree as $term) {
    $term_data = taxonomy_xml_term_as_rdf_data($term);
    $data += $term_data;
  }
  rdf_export($data, 'vocabulary-' . $term->tid);
}