Fix for RDF in core idea

  1. <?php
  2.  
  3. /**
  4.  * Implementation of hook_rdf_schema().
  5.  */
  6. function node_rdf_schema() {
  7.   $rdf = array();
  8.   $rdf['node'] = array(
  9.     'label' => t('content'),
  10.     'class' => 'sioc:post'
  11.   );
  12.   $rdf['node']['properties'] = array(
  13.     'nid' => array(
  14.       'label' => t('internal content id'),
  15.       'type' => 'literal', // default, so it can be omitted.
  16.       'datatype' => 'integer',
  17.       // If no callback is given the API should automatically use the key 'nid'
  18.       // for retrieving the data and make sure the data is in the right format,
  19.       // e.g. by using intval().
  20.     ),
  21.     'uid' => array(
  22.       'label' => t("author's internal user id"),
  23.       'property' => 'node:uid', // Should we use something like this as default if none is specified?
  24.       'datatype' => 'integer',
  25.     ),
  26.     'author' => array(
  27.       'label' => t('author'),
  28.       'type' => 'uri',
  29.       'property' => 'dc:creator',
  30.       'class' => 'user',
  31.       'callback' => 'node_rdf_get_author',
  32.     ),
  33.     'title' => array(
  34.       'label' => t('content title'),
  35.       'datatype' => 'string',
  36.     ),
  37.     'created' => array(
  38.       'label' => t('creation date'),
  39.       'datatype' => 'date',
  40.       // Timestamps could be automatically converted right, if not we would need
  41.       // a callback here.
  42.     ),
  43.   );
  44.   return $rdf;
  45. }
  46.  
  47. /**
  48.  * Implementation of hook_rdf_schema().
  49.  */
  50. function mymodule_rdf_schema_alter($schema) {
  51.  
  52.   //Customize properties, classes, ..
  53. }
  54.  
  55.  
  56. /**
  57.  * Callback for getting the uri
  58.  */
  59. function node_rdf_get_author($node, $key, $info) {
  60.   return new RDFClassLocal('user', $node->uid);
  61. }
  62.  
  63.  
  64.  
  65. /**
  66.  * A generic reference to an rdf class + it's properties.
  67.  * For actually getting the RDF data lazy loading should be used.
  68.  */
  69. abstract class RDFClass {
  70.    
  71.   /**
  72.    * Returns the types of this class.
  73.    */
  74.   abstract function getRdfTypes() {
  75.    
  76.   }
  77.  
  78.  
  79. }
  80.  
  81.  
  82. abstract class RDFClassLocal extends RDFClass {
  83.  
  84.   var $entity;
  85.  
  86.   public function __construct($class, $id) {
  87.    
  88.   }
  89.  
  90.   function getRdfTypes() {
  91.     return array('user');
  92.   }
  93.  
  94.   // Returns another RDFClass or a literal.
  95.   function getPropertyValue($property_name) {
  96.    
  97.     // First load the entity if necesarry.
  98.    
  99.     return rdf_get_property('user', $property_name, $entity);
  100.   }
  101.  
  102.  
  103.   /**
  104.    * Lazy load the entity.
  105.    */
  106.   abstract function loadEntity() {
  107.    
  108.   }
  109.  
  110.   function getRdfaAttributes($property_name, $content = FALSE) {
  111.     // Return an array of attributes needed for RDFa, suited for drupal_attributes().
  112.     // Add in the content attribute if requested.
  113.   }
  114.  
  115.   function getRdfaTags() {
  116.     // Retrieve the property and return some span tags for RDFa output.
  117.   }
  118. }
  119.  
  120.  
  121.  
  122. class RDFClassLocalUser extends RDFClassLocal {
  123.  
  124.   /**
  125.    * Lazy load the entity.
  126.    */
  127.   function loadEntity() {
  128.     $this->entity = user_load($this->id);
  129.   }
  130. }
  131.  
  132.  
  133.  
  134. /**
  135.  * Not really required, but could be implemented for handling remote data.
  136.  */
  137. class RDFClassRemote extends RDFClass {
  138.  
  139.   function __construct($class, $url) {
  140.     // Retrieve rdf data from $url if requested.
  141.   }
  142.  
  143.   function getRdfTypes() {
  144.     return array($class);
  145.   }
  146.  
  147.   function getPropertyValue($property_name) {
  148.     //TODO
  149.   }
  150. }
  151.  
  152.  
  153.  
  154. /**
  155.  * Actually retrieves the value for the given property.
  156.  */
  157. function rdf_get_property($entity_name, $property_name, $entity) {
  158.   $schema = rdf_get_schema();
  159.  
  160.   if (isset($schema[$entity_name]['properties'][$property_name]['callback'])) {
  161.     return $schema[$entity_name]['properties'][$property_name]['callback']();
  162.   }
  163.   else {
  164.     if ($schema[$entity_name]['properties'][$property_name]['type'] == 'uri') {
  165.       return new RDFClassRemote($entity->$entity_name);
  166.     }
  167.     else {
  168.       // Literal
  169.       return rdf_ensure_datatype($schema[$entity_name]['properties'][$property_name]['datatype'], $entity->$entity_name);
  170.     }
  171.   }
  172. }
  173.  
  174. function rdf_ensure_datatype($datatype, $value) {
  175.   //ensure the datatype fits!
  176. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.