You are here

function taxonomy_xml_rdf_add_terms in Taxonomy import/export via XML 7

Append definitions of a list of terms on to a DOM container.

This term dump will directly reflect any rdf_mapping retrieved from the Drupal 'entity' schema, which is based on SKOS.

<skos:Concept rdf:ID="term-1764" rdf:about="http://taxonomy.drupal7. gadget/taxonomy/term/1764"> <rdfs:label>Corporate management (Internal)</rdfs:label> <skos:prefLabel>Corporate management (Internal)</skos:prefLabel> <skos:definition> Managing the organisation's own corporate body </skos:definition> <skos:member>3</skos:member> <skos:broader>1763</skos:broader> </skos:Concept>

Parameters

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

No return, it acts on the DOM document directly.

2 calls to taxonomy_xml_rdf_add_terms()
taxonomy_xml_rdf_create in formats/rdf_format.inc
Return an XML/RDF document representing this vocab
taxonomy_xml_rdf_create_term in formats/rdf_format.inc
Return a term as RDF-XML.

File

formats/rdf_format.inc, line 780

Code

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

  // 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) {

    // dpm($term);
    // rdf_entity_to_xml does a direct mapping from data structure to XML,
    // so picks up most of the default values, using rdf_mapping
    // The term SHOULD have its entity mapping details attached to it by now.
    // didn't module_invoke_all do that?
    // If I have to do it myself : INEFFICIENCY HERE due to the full reload.
    if (empty($term->rdf_mapping)) {
      $term = taxonomy_term_load($term->tid);
    }
    $termnode = rdf_entity_to_xml($term, $dom, $domcontainer);
    if (!$termnode) {
      watchdog('taxonomy_xml', "Failed to create an XML entry for term, <pre>!data</pre>", array(
        '!data' => print_r($term, 1),
      ), WATCHDOG_ERROR);
      continue;
    }

    // Set either the local or (preferably) the cannonic remote URI
    // as the elements 'about' attribute.
    $guid = taxonomy_xml_get_term_guid($term);
    $termnode
      ->setattributens($ns['rdf'], 'rdf:about', $guid);
    $vocab_ref = $dom
      ->createelementns($ns['skos'], 'skos:member');
    $vocabulary = taxonomy_vocabulary_load($term->vid);
    $vocab_guid = taxonomy_xml_get_vocabulary_uri($term->vid);
    $vocab_ref
      ->setattributens($ns['rdf'], 'rdf:resource', $vocab_guid);

    // Looks like setattributens is now safe for xmlentities.
    // Previous PHP did not?
    $vocab_ref
      ->setattributens($ns['rdf'], 'rdf:value', $vocabulary->name);
    $termnode
      ->appendchild($vocab_ref);
    if (!empty($term->parents)) {
      foreach ((array) $term->parents as $parent_id) {
        $parentlist = array();
        if ($parent_id) {
          $parentlist[$parent_id] = $parent = taxonomy_term_load($parent_id);
          $parent_node = $dom
            ->createelementns($ns['skos'], 'skos:broader');
          $parent_node
            ->setattributens($ns['rdf'], 'rdf:resource', taxonomy_xml_get_term_guid($parent));
          $parent_node
            ->setattributens($ns['rdf'], 'rdf:value', $parent->name);
          $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($ns['skos'], 'skos:narrower');
          $child_node
            ->setattributens($ns['rdf'], 'rdf:resource', taxonomy_xml_get_term_guid($child));
          $child_node
            ->setattributens($ns['rdf'], 'rdf:value', $child->name);
          $termnode
            ->appendchild($child_node);
        }
    }

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

  // Done all terms in list.
}