You are here

function taxonomy_xml_rdfs_add_terms in Taxonomy import/export via XML 7

Given a list of terms, append definitions of them to the passed DOM container

Following w3c, SUMO and Wordnet examples (tho not any explicit instructions, taxonomy terms are modelled as rdfs:Class objects structured using rdfs: subClassOf statements.

Sample from Wordnet:

<Class rdf:about="http://xmlns.com/wordnet/1.6/Cat"> <label>Cat [ 1 ]</label> <comment>feline mammal usually having thick soft fur and being unable to roar; domestic cats; wildcats</comment> <subClassOf> <Class rdf:about="http://xmlns.com/wordnet/1.6/Feline" /> </subClassOf> </Class>

I'm copying that syntax.

Parameters

$termlist a FLAT array of all terms, internally cross-referenced to: each other defining the tree stucture

1 call to taxonomy_xml_rdfs_add_terms()
taxonomy_xml_rdfs_create in ./rdfs_format.inc
Return an XML/RDF document representing this vocab

File

./rdfs_format.inc, line 152

Code

function taxonomy_xml_rdfs_add_terms(DOMElement &$domcontainer, $termlist, $recursion_behaviour = TAXONOMY_XML_CHILDREN_REF_ONLY) {
  if (!$termlist) {
    return;
  }
  $dom = $domcontainer->ownerDocument;

  // Allow submission of a single term.
  if (!is_array($termlist)) {
    $termlist = array(
      $termlist,
    );
  }

  // D7 hook_taxonomy_term_load actually takes an array, not a singular.
  module_invoke_all('taxonomy_term_load', $termlist);
  foreach ($termlist as $term) {

    // TODO - rewrite this hunk to use the new rdf entity rdf mappings!
    // eg with
    // $termnode = rdf_entity_to_xml($term, $dom);
    $termnode = $dom
      ->createelementns(TAXONOMY_XML_RDFS_NS, 'rdfs:Class');
    $termnode
      ->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:ID', 'term-' . $term->tid);
    $domcontainer
      ->appendchild($termnode);

    // Set either the local or (preferably)
    // the cannonic remote URI as the elements 'about' attribute.
    if ($uri = taxonomy_xml_get_term_uri($term)) {
      $termnode
        ->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:about', $uri);
    }
    else {
      $term_uri = taxonomy_term_uri($term);
      $path = url($term_uri['path'], array(
        'absolute' => TRUE,
      ));

      // Why does that return an array now?
      $termnode
        ->setAttributeNS(TAXONOMY_XML_RDF_NS, 'rdf:about', $path);
    }
    $termnode
      ->appendchild($dom
      ->createelementns(TAXONOMY_XML_RDFS_NS, 'rdfs:label', xmlentities($term->name)));
    if ($term->description) {
      $termnode
        ->appendchild($dom
        ->createelementns(TAXONOMY_XML_RDFS_NS, 'rdfs:comment', xmlentities($term->description)));
    }
    $vocab_ref = $dom
      ->createelementns(TAXONOMY_XML_RDFS_NS, 'rdfs:isDefinedBy');
    $vocab_ref
      ->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:resource', '#vocabulary-' . $term->vid);
    $termnode
      ->appendchild($vocab_ref);
    if (!empty($term->parents)) {
      foreach ((array) $term->parents as $parentid) {
        $parentlist = array();
        if ($parentid) {
          $parentlist[$parentid] = $parent = taxonomy_term_load($parentid);
          $parent_node = $dom
            ->createelementns(TAXONOMY_XML_RDFS_NS, 'rdfs:subClassOf', xmlentities($parent->name));
          $parent_node
            ->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:resource', '#term-' . $parentid);
          $termnode
            ->appendchild($parent_node);
        }
      }
    }

    // Now add the children also.
    switch ($recursion_behaviour) {
      case TAXONOMY_XML_NO_CHILDREN:
        break;
      case TAXONOMY_XML_CHILDREN_REF_ONLY:
        $max_depth = 1;
        $tree = taxonomy_get_tree($term->vid, $term->tid, $max_depth);
        foreach ($tree as $child) {
          $child_id = $child->tid;
          $child_node = $dom
            ->createelementns(TAXONOMY_XML_RDFS_NS, 'rdfs:superClassOf', xmlentities($child->name));
          $child_node
            ->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:resource', '#term-' . $child_id);
          $termnode
            ->appendchild($child_node);
        }
    }

    // Workaround for large vocabs - extend runtime indefinately.
    drupal_set_time_limit(10);
  }

  // Done all terms in list
}