Commit b4655c58 authored by Dries's avatar Dries

Issue #1869600 by linclark, scor, jesse.d: Refactor RDF mappings to be inline...

Issue #1869600 by linclark, scor, jesse.d: Refactor RDF mappings to be inline with the new Entity Field API.
parent 36df8e87
......@@ -1196,8 +1196,6 @@ function format_date($timestamp, $type = 'medium', $format = '', $timezone = NUL
/**
* Returns an ISO8601 formatted date based on the given date.
*
* Callback for use within hook_rdf_mapping() implementations.
*
* @param $date
* A UNIX timestamp.
*
......
......@@ -1797,48 +1797,6 @@ function comment_ranking() {
);
}
/**
* Implements hook_rdf_mapping().
*/
function comment_rdf_mapping() {
return array(
array(
'type' => 'comment',
'bundle' => RDF_DEFAULT_BUNDLE,
'mapping' => array(
'rdftype' => array('sioc:Post', 'sioct:Comment'),
'title' => array(
'predicates' => array('dc:title'),
),
'created' => array(
'predicates' => array('dc:date', 'dc:created'),
'datatype' => 'xsd:dateTime',
'callback' => 'date_iso8601',
),
'changed' => array(
'predicates' => array('dc:modified'),
'datatype' => 'xsd:dateTime',
'callback' => 'date_iso8601',
),
'comment_body' => array(
'predicates' => array('content:encoded'),
),
'pid' => array(
'predicates' => array('sioc:reply_of'),
'type' => 'rel',
),
'uid' => array(
'predicates' => array('sioc:has_creator'),
'type' => 'rel',
),
'name' => array(
'predicates' => array('foaf:name'),
),
),
),
);
}
/**
* Implements hook_file_download_access().
*/
......
id: node.forum
targetEntityType: node
bundle: forum
types:
- 'sioc:Post'
- 'sioct:BoardPost'
fieldMappings:
taxonomy_forums:
properties:
- 'sioc:has_container'
mapping_type: 'rel'
id: taxonomy_term.forums
targetEntityType: taxonomy_term
bundle: forums
types:
- 'sioc:Container'
- 'sioc:Forum'
......@@ -1312,29 +1312,3 @@ function _forum_update_forum_index($nid) {
->execute();
}
}
/**
* Implements hook_rdf_mapping().
*/
function forum_rdf_mapping() {
return array(
array(
'type' => 'node',
'bundle' => 'forum',
'mapping' => array(
'rdftype' => array('sioc:Post', 'sioct:BoardPost'),
'taxonomy_forums' => array(
'predicates' => array('sioc:has_container'),
'type' => 'rel',
),
),
),
array(
'type' => 'taxonomy_term',
'bundle' => 'forums',
'mapping' => array(
'rdftype' => array('sioc:Container', 'sioc:Forum'),
),
),
);
}
<?php
/**
* @file
* Contains MappingSubscriber.
*/
namespace Drupal\rdf\EventSubscriber;
use Drupal\rdf\RdfMappingEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Default RDF mapping handling.
*/
class MappingSubscriber implements EventSubscriberInterface {
/**
* Stops event if incoming URI is a site schema URI.
*
* If the incoming URI is one of the site's own registered types, then
* mapping is unnecessary. Mapping is only necessary if the incoming type URI
* is from an external vocabulary.
*
* @param \Drupal\rdf\MapTypesFromInputEvent $event
* The mapping event.
*/
public function mapTypesFromInput(\Drupal\rdf\MapTypesFromInputEvent $event) {
$input_uris = $event->getInputUris();
$site_schema_types = $event->getSiteSchemaTypes();
foreach ($input_uris as $input_uri) {
if (isset($site_schema_types[$input_uri])) {
$event->setSiteSchemaUri($input_uri);
$event->stopPropagation();
}
}
}
/**
* Implements EventSubscriberInterface::getSubscribedEvents().
*/
static function getSubscribedEvents() {
$events[RdfMappingEvents::MAP_TYPES_FROM_INPUT] = 'mapTypesFromInput';
return $events;
}
}
<?php
/**
* @file
* Contains MapInputTypesEvent.
*/
namespace Drupal\rdf;
use Symfony\Component\EventDispatcher\Event;
/**
* Represents type mapping as event.
*/
class MapTypesFromInputEvent extends Event {
/**
* An array of incoming RDF type URIs.
*
* @var array
*/
protected $inputUris;
/**
* An array of entity_type/bundles, keyed by site schema type URI
*
* @var array
*/
protected $siteSchemaTypes;
/**
* The site schema type URI.
*
* @var string
*/
protected $siteSchemaUri;
/**
* Constructor.
*
* @param $input_uris
* An array of incoming RDF type URIs.
* @param $site_schema_types
* An array of entity_type/bundles, keyed by site schema type URI.
*/
public function __construct($input_uris, $site_schema_types) {
$this->inputUris = $input_uris;
$this->siteSchemaTypes = $site_schema_types;
$this->siteSchemaUri = FALSE;
}
/**
* Gets the input URI.
*
* @return array
* The array of incoming RDF type URIs.
*/
public function getInputUris() {
return $this->inputUris;
}
/**
* Gets the cache of internal site schema types.
*
* @return array
* The cached site schema type array.
*/
public function getSiteSchemaTypes() {
return $this->siteSchemaTypes;
}
/**
* Gets the site schema URI.
*
* @return string|bool
* The site schema type URI if set, FALSE if otherwise.
*/
public function getSiteSchemaUri() {
return $this->siteSchemaUri;
}
/**
* Sets the site schema URI.
*
* @param string $uri
* The site schema type URI.
*/
public function setSiteSchemaUri($uri) {
$this->siteSchemaUri = $uri;
}
}
<?php
/**
* @file
* Contains \Drupal\rdf\Plugin\Core\Entity\RdfMapping.
*/
namespace Drupal\rdf\Plugin\Core\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\Annotation\EntityType;
use Drupal\Core\Annotation\Translation;
use Drupal\rdf\RdfMappingInterface;
/**
* Config entity for working with RDF mappings.
*
* @EntityType(
* id = "rdf_mapping",
* label = @Translation("RDF mapping"),
* module = "rdf",
* controllers = {
* "storage" = "Drupal\Core\Config\Entity\ConfigStorageController"
* },
* config_prefix = "rdf.mapping",
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid"
* }
* )
*/
class RdfMapping extends ConfigEntityBase implements RdfMappingInterface {
/**
* Unique ID for the config entity.
*
* @var string
*/
public $id;
/**
* UUID for the config entity.
*
* @var string
*/
public $uuid;
/**
* Entity type to be mapped.
*
* @var string
*/
public $targetEntityType;
/**
* Bundle to be mapped.
*
* @var string
*/
public $bundle;
/**
* The RDF type mapping for this bundle.
*
* @var array
*/
protected $types;
/**
* The mappings for fields on this bundle.
*
* @var array
*/
protected $fieldMappings;
/**
* {@inheritdoc}
*/
public function getPreparedBundleMapping() {
$types = array();
if (isset($this->types)) {
$types = $this->types;
}
return array('types' => $types);
}
/**
* {@inheritdoc}
*/
public function getBundleMapping() {
if (isset($this->types)) {
return array('types' => $this->types);
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function setBundleMapping(array $mapping) {
if (isset($mapping['types'])) {
$this->types = $mapping['types'];
}
return $this;
}
/**
* {@inheritdoc}
*/
public function getPreparedFieldMapping($field_name) {
$field_mapping = array(
'properties' => NULL,
'datatype' => NULL,
'datatype_callback' => NULL,
'mapping_type' => NULL,
);
if (isset($this->fieldMappings[$field_name])) {
$field_mapping = array_merge($field_mapping, $this->fieldMappings[$field_name]);
}
return $field_mapping;
}
/**
* {@inheritdoc}
*/
public function getFieldMapping($field_name) {
if (isset($this->fieldMappings[$field_name])) {
return $this->fieldMappings[$field_name];
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function setFieldMapping($field_name, array $mapping = array()) {
$this->fieldMappings[$field_name] = $mapping;
return $this;
}
/**
* {@inheritdoc}
*/
public function id() {
return $this->targetEntityType . '.' . $this->bundle;
}
/**
* {@inheritdoc}
*/
public function save() {
// Build an ID if none is set.
if (empty($this->id)) {
$this->id = $this->id();
}
return parent::save();
}
/**
* {@inheritdoc}
*/
public function getExportProperties() {
$names = array(
'id',
'uuid',
'targetEntityType',
'bundle',
'types',
'fieldMappings',
);
$properties = array();
foreach ($names as $name) {
$properties[$name] = $this->get($name);
}
return $properties;
}
}
<?php
/**
* @file
* Contains RdfConstants.
*/
namespace Drupal\rdf;
/**
* Defines constants for RDF terms.
*/
abstract class RdfConstants {
const RDF_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
// RDF Schema terms.
const RDFS_CLASS = 'http://www.w3.org/2000/01/rdf-schema#class';
const RDFS_DOMAIN = 'http://www.w3.org/2000/01/rdf-schema#domain';
const RDFS_IS_DEFINED_BY = 'http://www.w3.org/2000/01/rdf-schema#isDefinedBy';
const RDFS_RANGE = 'http://www.w3.org/2000/01/rdf-schema#range';
const RDFS_SUB_CLASS_OF = 'http://www.w3.org/2000/01/rdf-schema#subClassOf';
// XSD datatypes.
const XSD_INTEGER = 'http://www.w3.org/2001/XMLSchema#integer';
const XSD_DOUBLE = 'http://www.w3.org/2001/XMLSchema#double';
const XSD_BOOLEAN = 'http://www.w3.org/2001/XMLSchema#boolean';
const XSD_STRING = 'http://www.w3.org/2001/XMLSchema#string';
}
<?php
/**
* @file
* Contains RdfMappingEvents.
*/
namespace Drupal\rdf;
/**
* Contains all events for mapping site schemas to external vocabularies.
*/
final class RdfMappingEvents {
/**
* Maps an array of incoming type URIs to a site schema URI.
*
* Modules can use this event to convert an RDF type from an externally
* defined vocabulary to a URI defined in the site's schema. From the site
* schema URI, the site can derive the Typed Data API ids, which can be used
* to create an entity.
*
* @see \Drupal\rdf\RdfMappingManager
*
* @var string
*/
const MAP_TYPES_FROM_INPUT = 'rdf.map_types_from_input';
}
<?php
/**
* @file
* Contains RdfMappingException.
*/
namespace Drupal\rdf;
/**
* Exception to use when no RDF mapping is found.
*/
class RdfMappingException extends \Exception { }
<?php
/**
* @file
* Contains \Drupal\rdf\RdfMappingInterface
*/
namespace Drupal\rdf;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
/**
* Provides an interface defining an RDF mapping entity.
*/
interface RdfMappingInterface extends ConfigEntityInterface {
/**
* Gets the mapping for the bundle-level data.
*
* The prepared bundle mapping should be used when outputting data in RDF
* serializations such as RDFa. In the prepared mapping, the mapping
* configuration's CURIE arrays are processed into CURIE strings suitable for
* output.
*
* @return array
* The bundle mapping.
*/
public function getPreparedBundleMapping();
/**
* Gets the mapping config for the bundle-level data.
*
* This function returns the bundle mapping as stored in config, which may
* contain CURIE arrays. If the mapping is needed for output in a
* serialization format, such as RDFa, then getPreparedBundleMapping() should
* be used instead.
*
* @return array|null
* The bundle mapping, or NULL if there is no mapping.
*/
public function getBundleMapping();
/**
* Sets the mapping config for the bundle-level data.
*
* This only sets bundle-level mappings, such as the RDF type. Mappings for
* a bundle's fields should be handled with setFieldMapping.
*
* Example usage:
* -Map the 'article' bundle to 'sioc:Post'.
* @code
* rdf_get_mapping('node', 'article')
* ->setBundleMapping(array(
* 'types' => array('sioc:Post'),
* ))
* ->save();
* @endcode
*
* @param array $mapping
* The bundle mapping.
*
* @return \Drupal\rdf\Plugin\Core\Entity\RdfMapping
* The RdfMapping object.
*/
public function setBundleMapping(array $mapping);
/**
* Gets the prepared mapping for a field.
*
* The prepared field mapping should be used when outputting data in RDF
* serializations such as RDFa. In the prepared mapping, the mapping
* configuration's CURIE arrays are processed into CURIE strings suitable for
* output.
*
* @param string $field_name
* The name of the field.
*
* @return array
* The prepared field mapping.
*/
public function getPreparedFieldMapping($field_name);
/**
* Gets the mapping config for a field.
*
* This function returns the field mapping as stored in config, which may
* contain CURIE arrays. If the mapping is needed for output in a
* serialization format, such as RDFa, then getPreparedFieldMapping() should
* be used instead.
*
* @param string $field_name
* The name of the field.
*
* @return array|null
* The field mapping config array, or NULL if there is no mapping.
*/
public function getFieldMapping($field_name);
/**
* Sets the mapping config for a field.
*
* @param string $field_name
* The name of the field.
* @param array $mapping
* The field mapping.
*
* @return \Drupal\rdf\Plugin\Core\Entity\RdfMapping
* The RdfMapping object.
*/
public function setFieldMapping($field_name, array $mapping = array());
}
<?php
/**
* @file
* Contains RdfMappingManager.