View source
<?php
define('TAXONOMY_XML_RDF_NS', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
define('TAXONOMY_XML_TYPE', TAXONOMY_XML_RDF_NS . 'type');
define('TAXONOMY_XML_UNTYPED', 'UNTYPED');
define('TAXONOMY_XML_RDFS_NS', 'http://www.w3.org/2000/01/rdf-schema#');
define('TAXONOMY_XML_CONTENTLABEL_NS', 'http://www.w3.org/2004/12/q/contentlabel#');
define('TAXONOMY_XML_CATEGORY', TAXONOMY_XML_CONTENTLABEL_NS . 'Category');
define('TAXONOMY_XML_OWL_NS', 'http://www.w3.org/2002/07/owl#');
define('TAXONOMY_XML_W3C_WN', 'http://www.w3.org/2006/03/wn/wn20/');
define('TAXONOMY_XML_W3C_WN_SCHEMA', TAXONOMY_XML_W3C_WN . 'schema/');
define('TAXONOMY_XML_DC_NS', 'http://purl.org/dc/elements/1.1/');
define('TAXONOMY_XML_SKOS_NS', 'http://www.w3.org/2004/02/skos/core#');
define('TAXONOMY_XML_TDWG_NS', 'http://rs.tdwg.org/ontology/voc/Collection#');
define('TAXONOMY_XML_FB_NS', 'http://rdf.freebase.com/ns/');
function rdf_entity_to_xml($entity, DOMDocument $XMLDoc, $domcontainer = NULL) {
if (empty($entity->rdf_mapping)) {
trigger_error("No rdf mapping loaded on this entity " . print_r($entity, 1), E_USER_ERROR);
return FALSE;
}
if (empty($XMLDoc)) {
$XMLDoc = new DOMDocument();
}
static $ns;
if (empty($ns)) {
$ns = rdf_get_namespaces();
}
$mapping = $entity->rdf_mapping;
$rdftype = reset($mapping['rdftype']);
$ns_id = fully_qualified_from_curie($rdftype);
$entity_node = $XMLDoc
->createElementNS($ns_id['ns'], $rdftype);
if (!$entity_node) {
trigger_error("Failed to create an XML node '{$rdftype}' on the XML document");
return FALSE;
}
if (!empty($domcontainer)) {
$domcontainer
->appendchild($entity_node);
}
foreach ($mapping as $property_name => $mapping_details) {
if (empty($mapping_details['predicates'])) {
continue;
}
if (!isset($entity->{$property_name})) {
watchdog('taxonomy_xml', 'Needs work. The rdf mapping wanted to know the value of the property %property_name but the entity did not hve a corresponding value set. ' . __FUNCTION__, array(
'%property_name' => $property_name,
), WATCHDOG_DEBUG);
continue;
}
if (!empty($mapping_details['type']) && ($mapping_details['type'] == 'rel' || $mapping_details['type'] == 'rev')) {
continue;
}
$property_value = trim($entity->{$property_name});
foreach ($mapping_details['predicates'] as $predicate) {
$ns_id = fully_qualified_from_curie($predicate);
if (empty($property_value)) {
continue;
}
if (!is_array($property_value)) {
$property_value = array(
$property_value,
);
}
foreach ($property_value as $property_val) {
$property_node = $entity_node
->appendChild($XMLDoc
->createElementNS($ns_id['ns'], $predicate));
$property_node
->appendChild($XMLDoc
->createTextNode($property_val));
}
}
}
return $entity_node;
}
function taxonomy_xml_rdf_document() {
$dom = new DOMDocument('1.0', 'UTF-8');
$ns = rdf_get_namespaces();
$dom
->appendchild($dom
->createprocessinginstruction('xml-stylesheet', 'href="render-taxonomy-rdf.xsl" type="text/xsl"'));
$dom
->appendchild($dom
->createcomment(xmlentities("\n This file was created by Drupal taxonomy_xml import/export tool.\n http://drupal.org/project/taxonomy_xml\n ")));
watchdog(__FUNCTION__, print_r(get_defined_vars(), 1));
$domcontainer = $dom
->createelementns($ns['rdf'], 'rdf:RDF');
$dom
->appendchild($domcontainer);
$domcontainer
->setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:rdfs", $ns['rdfs']);
$domcontainer
->setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:owl", $ns['owl']);
$domcontainer
->setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:skos", $ns['skos']);
return $domcontainer;
}
function fully_qualified_from_curie($curie) {
$ns = rdf_get_namespaces();
list($prefix, $id) = explode(':', $curie);
return array(
'p' => $prefix,
'id' => $id,
'ns' => $ns[$prefix],
);
}
function rdf_arc2_library_path() {
$arc2_library_path = variable_get('arc2_library_path', '');
if (is_readable($arc2_library_path)) {
return $arc2_library_path;
}
$possibilities = array(
'sites/all/libraries/ARC2/arc',
'sites/all/libraries/arc',
dirname(__FILE__) . '/arc',
);
foreach ($possibilities as $arc2_library_path) {
if (is_readable($arc2_library_path)) {
variable_set('arc2_library_path', $arc2_library_path);
return $arc2_library_path;
}
}
return NULL;
}
function rdf_load_arc2() {
if ($arc2_library_path = rdf_arc2_library_path()) {
require_once $arc2_library_path . "/ARC2.php";
return TRUE;
}
else {
$message = 'ARC2 RDF Parser is unavailable. see !install';
$strings = array(
'!install' => l('INSTALL.txt', drupal_get_path('module', 'taxonomy_xml') . '/INSTALL.txt'),
);
drupal_set_message(t($message, $strings), 'error');
watchdog('taxonomy_xml', $message, $strings, WATCHDOG_ERROR);
return FALSE;
}
}
function taxonomy_xml_rdf_requirements() {
$requirements = array();
$arc2_library_path = rdf_arc2_library_path();
if (!is_readable($arc2_library_path . "/ARC2.php")) {
$requirements['taxonomy_xml_rdf'] = array(
'value' => t('ARC1 RDF Parser is unavailable.'),
'severity' => REQUIREMENT_WARNING,
'description' => t('
See <a href="!install">INSTALL.txt</a>
for the extra features that the external
<a href="!arc">ARC library</a> can add
if you download it to %path.
', array(
'!arc' => 'https://github.com/semsol/arc2',
'!install' => url(drupal_get_path('module', 'taxonomy_xml') . '/INSTALL.txt'),
'%path' => 'sites/all/libraries',
)),
);
}
return $requirements;
}