From 4389c9263b7e95d133be7946487c8b044b818186 Mon Sep 17 00:00:00 2001
From: Chris Skene <chris@xtfer.com>
Date: Wed, 4 Jan 2012 13:30:19 +1100
Subject: [PATCH] Fix an issue where RDF output would not validate due to ID
 and about attributes both being present

---
 rdf_format.inc |   73 ++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 48 insertions(+), 25 deletions(-)

diff --git a/rdf_format.inc b/rdf_format.inc
index 8cf243a..37080e6 100755
--- a/rdf_format.inc
+++ b/rdf_format.inc
@@ -1060,11 +1060,22 @@ function taxonomy_xml_add_terms_as_rdf(&$domcontainer, $termlist, $vocabulary) {
     $term->uri = taxonomy_xml_taxonomy_term_uri($term);
     // List child terms, this will help if breaking the XML into lumps
     $term->child = taxonomy_get_children($term->tid, $term->vid);
-    
+
+    // If a GUID is available, it should be used instead of the ID to avoid validation errors
+    $guid = taxonomy_xml_get_term_guid($term);    
+    if (isset($guid) && !empty($guid)) {
+      $term->uri['guid'] = $guid;
+    }
+
     $termnode = taxonomy_xml_entity_to_rdf($term, 'taxonomy_term', $domcontainer);
     // That has already added it to the document - required to prevent it adding dummy namespaces
   
-    $termnode->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:ID', $term->uri['id'] );
+    if (isset($term->guid) && !empty($term->guid)) {
+      $termnode->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:about', $guid );
+    }
+    else {
+      $termnode->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:ID', $term->uri['id'] );
+    }
     
     // Add this because it helps visualizations
     if (empty($term->parent)) {
@@ -1073,10 +1084,6 @@ function taxonomy_xml_add_terms_as_rdf(&$domcontainer, $termlist, $vocabulary) {
       $rel_node->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:resource', '#' . $vocabulary->uri['id'] );
       $termnode->appendchild($rel_node);
     }
-    
-    if ($guid = taxonomy_xml_get_term_guid($term)) {
-      $termnode->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:about', $guid );
-    }
 
     // Additional module support
     // eg taxonomy_image, geotaxonomy, path
@@ -1214,7 +1221,10 @@ function taxonomy_xml_entity_to_rdf($object, $object_type, $domcontainer) {
           if (isset($attribute_mapping['type']) && $attribute_mapping['type'] == 'rel') {
             $rel_node = $dom->createelementns($predicate_full['uri'], $predicate_full['id']);
             if (is_array($datum)) {
-              if (isset($datum['id'])) {
+              if (isset($datum['guid'])) {
+                $rel_node->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:resource', $datum['guid'] );
+              }
+              elseif (isset($datum['id'])) {
                 $rel_node->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:resource', '#' . $datum['id'] );
               }
               else if (isset($datum['path'])) {
@@ -1363,11 +1373,17 @@ function taxonomy_xml_taxonomy_term_uri($term) {
   if (is_numeric($term)) {
     $term = taxonomy_get_term($term);
   }
-  return array(
-    'path' => 'taxonomy/term/' . $term->tid,
-    'title' => $term->name,
-    'id' => 'term-' . $term->tid,
-  );
+
+  if (is_object($term)) {
+    $term = (array) $term;
+    if (!empty($term)) {
+      $term['path'] = 'taxonomy/term/' . $term['tid'];
+      $term['title'] = $term['name'];
+      $term['id'] = 'term-' . $term['tid'];
+      return $term;
+    }
+  }
+  return FALSE;
 }
 
 /**
@@ -1377,20 +1393,27 @@ function taxonomy_xml_taxonomy_vocabulary_uri($vocabulary) {
   if (is_numeric($vocabulary)) {
     $vocabulary = taxonomy_vocabulary_load($vocabulary);
   }
- $vocabulary->machine_name = 'vocabulary-' . preg_replace('/[^a-z0-9]+/', '_', strtolower($vocabulary->name)) ;
-  
-  // If it is a features vocabulary, its cannonic ID is overloaded in the 'module' field.
-  // Makes enough sense. Use that
-  if (strpos($vocabulary->module, 'features_') === 0) {
-    // Simply display the existing machine name if we have one.
-    $vocabulary->machine_name = substr($vocabulary->module, 9);
+
+  if (is_object($vocabulary)) {
+    $vocabulary->machine_name = 'vocabulary-' . preg_replace('/[^a-z0-9]+/', '_', strtolower($vocabulary->name));
+
+    // If it is a features vocabulary, its cannonic ID is overloaded in the 'module' field.
+    // Makes enough sense. Use that
+    if (strpos($vocabulary->module, 'features_') === 0) {
+      // Simply display the existing machine name if we have one.
+      $vocabulary->machine_name = substr($vocabulary->module, 9);
+    }
+
+    $vocabulary = (array) $vocabulary;
+    if (!empty($vocabulary)) {
+      $vocabulary['path'] = 'taxonomy/vocabulary/' . $vocabulary['vid'];
+      $vocabulary['title'] = $vocabulary['name'];
+      $vocabulary['id'] = $vocabulary['machine_name'];
+
+      return $vocabulary;
+    }
   }
- 
-  return array(
-    'path' => 'taxonomy/vocabulary/' . $vocabulary->vid,
-    'title' => $vocabulary->name,
-    'id' => $vocabulary->machine_name,
-  );
+  return FALSE;
 }
 
 
-- 
1.7.5.4

