<?php
/**
* Implementation of hook_rdf_schema().
*/
function node_rdf_schema() {
$rdf = array();
$rdf['node'] = array(
'label' => t('content'),
'class' => 'sioc:post'
);
$rdf['node']['properties'] = array(
'nid' => array(
'label' => t('internal content id'),
'type' => 'literal', // default, so it can be omitted.
'datatype' => 'integer',
// If no callback is given the API should automatically use the key 'nid'
// for retrieving the data and make sure the data is in the right format,
// e.g. by using intval().
),
'uid' => array(
'label' => t("author's internal user id"),
'property' => 'node:uid', // Should we use something like this as default if none is specified?
'datatype' => 'integer',
),
'author' => array(
'label' => t('author'),
'type' => 'uri',
'property' => 'dc:creator',
'class' => 'user',
'callback' => 'node_rdf_get_author',
),
'title' => array(
'label' => t('content title'),
'datatype' => 'string',
),
'created' => array(
'label' => t('creation date'),
'datatype' => 'date',
// Timestamps could be automatically converted right, if not we would need
// a callback here.
),
);
return $rdf;
}
/**
* Implementation of hook_rdf_schema().
*/
function mymodule_rdf_schema_alter($schema) {
//Customize properties, classes, ..
}
/**
* Callback for getting the uri
*/
function node_rdf_get_author($node, $key, $info) {
return new RDFClassLocal('user', $node->uid);
}
/**
* A generic reference to an rdf class + it's properties.
* For actually getting the RDF data lazy loading should be used.
*/
abstract class RDFClass {
/**
* Returns the types of this class.
*/
abstract function getRdfTypes() {
}
}
abstract class RDFClassLocal extends RDFClass {
var $entity;
public function __construct($class, $id) {
}
function getRdfTypes() {
return array('user');
}
// Returns another RDFClass or a literal.
function getPropertyValue($property_name) {
// First load the entity if necesarry.
return rdf_get_property('user', $property_name, $entity);
}
/**
* Lazy load the entity.
*/
abstract function loadEntity() {
}
function getRdfaAttributes($property_name, $content = FALSE) {
// Return an array of attributes needed for RDFa, suited for drupal_attributes().
// Add in the content attribute if requested.
}
function getRdfaTags() {
// Retrieve the property and return some span tags for RDFa output.
}
}
class RDFClassLocalUser extends RDFClassLocal {
/**
* Lazy load the entity.
*/
function loadEntity() {
$this->entity = user_load($this->id);
}
}
/**
* Not really required, but could be implemented for handling remote data.
*/
class RDFClassRemote extends RDFClass {
function __construct($class, $url) {
// Retrieve rdf data from $url if requested.
}
function getRdfTypes() {
return array($class);
}
function getPropertyValue($property_name) {
//TODO
}
}
/**
* Actually retrieves the value for the given property.
*/
function rdf_get_property($entity_name, $property_name, $entity) {
$schema = rdf_get_schema();
if (isset($schema[$entity_name]['properties'][$property_name]['callback'])) {
return $schema[$entity_name]['properties'][$property_name]['callback']();
}
else {
if ($schema[$entity_name]['properties'][$property_name]['type'] == 'uri') {
return new RDFClassRemote($entity->$entity_name);
}
else {
// Literal
return rdf_ensure_datatype($schema[$entity_name]['properties'][$property_name]['datatype'], $entity->$entity_name);
}
}
}
function rdf_ensure_datatype($datatype, $value) {
//ensure the datatype fits!
}