View source
<?php
module_load_include('inc', 'rdf', 'rdf.pages');
function taxonomy_xml_rdf_export_term($term) {
$term = is_numeric($term) ? taxonomy_term_load($term) : $term;
$data = taxonomy_xml_term_as_rdf_data($term);
rdf_export($data, 'term-' . $term->tid);
}
function taxonomy_xml_term_as_rdf_data($term) {
$term = is_numeric($term) ? taxonomy_term_load($term) : $term;
$subject_uri = url('taxonomy/term/' . $term->tid, array(
'absolute' => TRUE,
));
$subject_curi = 'term:' . $term->tid;
$subject = rdf_is_valid_curie($subject_curi) ? rdf_qname_to_uriref($subject_curi) : rdf_uri($subject_curi);
$data = rdf_query($subject_uri);
$data = rdf_normalize($data);
$statements = array(
'rdf:type' => rdf_qname_to_uriref('rdfs:Class'),
'rdfs:label' => rdf_literal($term->name),
'rdfs:isDefinedBy' => rdf_uri(url('taxonomy/vocabulary/' . $term->vid, array(
'absolute' => TRUE,
))),
);
if (!empty($term->description)) {
$statements['rdfs:comment'] = rdf_literal($term->description);
}
foreach ((array) $term->parents as $ptid) {
$statements['rdfs:subClassOf'][$ptid] = rdf_uri(url('taxonomy/term/' . $ptid, array(
'absolute' => TRUE,
)));
}
foreach ((array) $term->children as $ctid => $child) {
$statements['rdfs:superClassOf'][$ctid] = rdf_uri(url('taxonomy/term/' . $ctid, array(
'absolute' => TRUE,
)));
}
foreach ((array) $term->synonyms as $i => $synonym) {
$statements['owl:equivalentClass'][$i] = rdf_literal($synonym);
}
foreach ((array) $term->related as $i => $seealso) {
$statements['rdfs:seeAlso'][$i] = rdf_uri(url('taxonomy/term/' . $seealso, array(
'absolute' => TRUE,
)));
}
$data = convert_statements_to_rdf_data($subject_uri, $statements);
return $data;
}
function taxonomy_xml_rdf_export_vocabulary($vocabulary) {
$vocabulary = is_numeric($vocabulary) ? taxonomy_vocabulary_load($vocabulary) : $vocabulary;
$subject_uri = url('taxonomy/vocabulary/' . $vocabulary->vid, array(
'absolute' => TRUE,
));
$subject_curi = 'vocabulary:' . $vocabulary->vid;
$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);
$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 = 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,
));
$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 = rdf_normalize($data);
$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);
$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);
}
function convert_statements_to_rdf_data($subject_uri, $statements) {
foreach ($statements as $predicate => $objects) {
$predicate = rdf_is_valid_curie($predicate) ? rdf_qname_to_uriref($predicate) : rdf_uri($predicate);
$objects = rdf_objects($objects);
foreach ($objects as $key => $object) {
$objects[$key] = rdf_is_valid_uri($object) ? rdf_uri($object) : $object;
}
$data[$subject_uri][$predicate->uri] = $objects;
}
return $data;
}
function taxonomy_xml_rdf_classes() {
return array(
'vocabulary' => array(
'title' => t('Taxonomy vocabulary'),
'module' => 'taxonomy',
'table' => 'vocabulary',
'query' => 'SELECT vid FROM {vocabulary}',
'uri' => 'taxonomy/vocabulary/%vid',
'enabled' => TRUE,
),
'term' => array(
'title' => t('Taxonomy term'),
'module' => 'taxonomy',
'table' => 'term_data',
'query' => 'SELECT tid FROM {term_data}',
'uri' => 'taxonomy/term/%tid',
'enabled' => TRUE,
),
);
}
function taxonomy_xml_rdf_resources($context) {
switch ($context) {
case NULL:
return array();
case RDF_SITE_URI:
return array(
RDF_SITE_URI => new RDF_QueryCallback('rdf_load_site'),
);
case RDF_SCHEMA_URI:
return rdf_schema_get_classes();
}
}
function taxonomy_xml_rdf_schema_load_term($term) {
$term = is_object($term) ? $term : taxonomy_term_load((int) $term);
$statements = array(
rdf::type => rdf_uri(rdf_qname_to_uri('drupal:term')),
'term:tid' => (int) $term->tid,
'term:vid' => (int) $term->vid,
'term:name' => $term->name,
'term:body' => $term->description,
);
$statements = array(
'rdf:type' => rdf_qname_to_uriref('rdfs:Class'),
'rdfs:label' => rdf_literal($term->name),
'rdfs:isDefinedBy' => rdf_uri(url('taxonomy/vocabulary/' . $term->vid, array(
'absolute' => TRUE,
))),
);
if (!empty($term->description)) {
$statements['rdfs:comment'] = rdf_literal($term->description);
}
foreach ((array) $term->parents as $ptid) {
$statements['rdfs:subClassOf'][$ptid] = rdf_uri(url('taxonomy/term/' . $ptid, array(
'absolute' => TRUE,
)));
}
foreach ((array) $term->synonyms as $i => $synonym) {
$statements['owl:equivalentClass'][$i] = rdf_literal($synonym);
}
foreach ((array) $term->related as $i => $seealso) {
$statements['rdfs:seeAlso'][$i] = rdf_uri(url('taxonomy/term/' . $seealso, array(
'absolute' => TRUE,
)));
}
return $data;
}