Commit 36975492 authored by Dries's avatar Dries

Issue #1784234 by linclark: Use schema.org types and properties in RDF mappings.

parent d5c0b6dc
...@@ -2,10 +2,27 @@ id: node.forum ...@@ -2,10 +2,27 @@ id: node.forum
targetEntityType: node targetEntityType: node
bundle: forum bundle: forum
types: types:
- 'sioc:Post' - 'schema:Discussion'
- 'sioct:BoardPost'
fieldMappings: fieldMappings:
taxonomy_forums: title:
properties: properties:
- 'sioc:has_container' - 'schema:name'
created:
properties:
- 'schema:dateCreated'
datatype_callback: 'date_iso8601'
changed:
properties:
- 'schema:dateModified'
datatype_callback: 'date_iso8601'
body:
properties:
- 'schema:text'
uid:
properties:
- 'schema:author'
mapping_type: 'rel' mapping_type: 'rel'
comment_count:
properties:
- 'schema:interactionCount'
datatype_callback: 'Drupal\rdf\SchemaOrgDataConverter::interactionCount'
...@@ -4,3 +4,10 @@ bundle: forums ...@@ -4,3 +4,10 @@ bundle: forums
types: types:
- 'sioc:Container' - 'sioc:Container'
- 'sioc:Forum' - 'sioc:Forum'
fieldMappings:
name:
properties:
- 'schema:name'
description:
properties:
- 'schema:description'
<?php
/**
* @file
* Contains \Drupal\rdf\CommonDataConverter.
*/
namespace Drupal\rdf;
/**
* Contains methods for common data conversions.
*/
class CommonDataConverter {
/**
* Provides a passthrough to place unformatted values in content attributes.
*
* @param mixed $data
* The data to be placed in the content attribute.
*
* @return mixed
* Returns the data.
*/
static function rawValue($data) {
return $data;
}
}
<?php
/**
* @file
* Contains \Drupal\rdf\SchemaOrgDataConverter.
*/
namespace Drupal\rdf;
class SchemaOrgDataConverter {
/**
* Converts an interaction count to a string with the interaction type.
*
* Schema.org defines a number of different interaction types.
*
* @param int $count
* The interaction count.
*
* @return string
* The formatted string.
*
* @see http://schema.org/UserInteraction
* @todo Support other interaction types, see https://drupal.org/node/2020001
*/
static function interactionCount($count) {
return "UserComment:$count";
}
}
...@@ -54,6 +54,7 @@ public function setUp() { ...@@ -54,6 +54,7 @@ public function setUp() {
$comment_count_mapping = array( $comment_count_mapping = array(
'properties' => array('sioc:num_replies'), 'properties' => array('sioc:num_replies'),
'datatype' => 'xsd:integer', 'datatype' => 'xsd:integer',
'datatype_callback' => 'Drupal\rdf\CommonDataConverter::rawValue',
); );
$article_mapping->setFieldMapping('comment_count', $comment_count_mapping)->save(); $article_mapping->setFieldMapping('comment_count', $comment_count_mapping)->save();
...@@ -69,7 +70,7 @@ public function setUp() { ...@@ -69,7 +70,7 @@ public function setUp() {
$mapping = rdf_get_mapping('comment', 'comment_node_article'); $mapping = rdf_get_mapping('comment', 'comment_node_article');
$mapping->setBundleMapping(array('types' => array('sioc:Post', 'sioct:Comment')))->save(); $mapping->setBundleMapping(array('types' => array('sioc:Post', 'sioct:Comment')))->save();
$field_mappings = array( $field_mappings = array(
'title' => array( 'subject' => array(
'properties' => array('dc:title'), 'properties' => array('dc:title'),
), ),
'created' => array( 'created' => array(
...@@ -251,7 +252,7 @@ function _testBasicCommentRdfaMarkup($graph, $comment, $account = array()) { ...@@ -251,7 +252,7 @@ function _testBasicCommentRdfaMarkup($graph, $comment, $account = array()) {
'value' => $comment->subject->value, 'value' => $comment->subject->value,
'lang' => 'en', 'lang' => 'en',
); );
$this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/title', $expected_value), 'Comment title found in RDF output (dc:title).'); $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/title', $expected_value), 'Comment subject found in RDF output (dc:title).');
// Comment date. // Comment date.
$expected_value = array( $expected_value = array(
......
...@@ -84,6 +84,28 @@ function testDatatypeCallback() { ...@@ -84,6 +84,28 @@ function testDatatypeCallback() {
$this->_testAttributes($expected_attributes, $mapping, $date); $this->_testAttributes($expected_attributes, $mapping, $date);
} }
/**
* Test attribute creation for mappings which use data converters.
*/
function testDatatypeCallbackWithConverter() {
$properties = array('schema:interactionCount');
$data = "23";
$content = "UserComment:23";
$mapping = array(
'properties' => $properties,
'datatype_callback' => 'Drupal\rdf\SchemaOrgDataConverter::interactionCount',
);
$expected_attributes = array(
'property' => $properties,
'content' => $content,
);
$this->_testAttributes($expected_attributes, $mapping, $data);
}
/** /**
* Test attribute creation for mappings which use 'rel'. * Test attribute creation for mappings which use 'rel'.
*/ */
......
This diff is collapsed.
...@@ -77,5 +77,8 @@ function testTaxonomyTermRdfaAttributes() { ...@@ -77,5 +77,8 @@ function testTaxonomyTermRdfaAttributes() {
'lang' => 'en', 'lang' => 'en',
); );
$this->assertTrue($graph->hasProperty($term_uri, 'http://www.w3.org/2004/02/skos/core#prefLabel', $expected_value), 'Term label found in RDF output (skos:prefLabel).'); $this->assertTrue($graph->hasProperty($term_uri, 'http://www.w3.org/2004/02/skos/core#prefLabel', $expected_value), 'Term label found in RDF output (skos:prefLabel).');
// @todo Add test for term description once it is a field:
// https://drupal.org/node/569434
} }
} }
...@@ -76,19 +76,6 @@ function testUserAttributesInMarkup() { ...@@ -76,19 +76,6 @@ function testUserAttributesInMarkup() {
'value' => $user2->name, 'value' => $user2->name,
); );
$this->assertTrue($graph->hasProperty($account_uri, 'http://xmlns.com/foaf/0.1/name', $expected_value), 'User name found in RDF output (foaf:name).'); $this->assertTrue($graph->hasProperty($account_uri, 'http://xmlns.com/foaf/0.1/name', $expected_value), 'User name found in RDF output (foaf:name).');
// Person type.
$expected_value = array(
'type' => 'uri',
'value' => 'http://xmlns.com/foaf/0.1/Person',
);
$this->assertTrue($graph->hasProperty($person_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Person type found in RDF output (foaf:Person).');
// Person relation to account.
$expected_value = array(
'type' => 'uri',
'value' => $account_uri,
);
$this->assertTrue($graph->hasProperty($person_uri, 'http://xmlns.com/foaf/0.1/account', $expected_value), 'Person relation to account found in RDF output (foaf:account).');
// User 2 creates a node. // User 2 creates a node.
$this->drupalLogin($user2); $this->drupalLogin($user2);
......
...@@ -92,6 +92,7 @@ function rdf_rdf_namespaces() { ...@@ -92,6 +92,7 @@ function rdf_rdf_namespaces() {
'foaf' => 'http://xmlns.com/foaf/0.1/', 'foaf' => 'http://xmlns.com/foaf/0.1/',
'og' => 'http://ogp.me/ns#', 'og' => 'http://ogp.me/ns#',
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#', 'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
'schema' => 'http://schema.org/',
'sioc' => 'http://rdfs.org/sioc/ns#', 'sioc' => 'http://rdfs.org/sioc/ns#',
'sioct' => 'http://rdfs.org/sioc/types#', 'sioct' => 'http://rdfs.org/sioc/types#',
'skos' => 'http://www.w3.org/2004/02/skos/core#', 'skos' => 'http://www.w3.org/2004/02/skos/core#',
...@@ -174,7 +175,7 @@ function rdf_rdfa_attributes($mapping, $data = NULL) { ...@@ -174,7 +175,7 @@ function rdf_rdfa_attributes($mapping, $data = NULL) {
// Convert $data to a specific format as per the callback function. // Convert $data to a specific format as per the callback function.
if (isset($data) && !empty($mapping['datatype_callback'])) { if (isset($data) && !empty($mapping['datatype_callback'])) {
$callback = $mapping['datatype_callback']; $callback = $mapping['datatype_callback'];
$attributes['content'] = $callback($data); $attributes['content'] = call_user_func($callback, $data);
} }
if (isset($mapping['datatype'])) { if (isset($mapping['datatype'])) {
$attributes['datatype'] = $mapping['datatype']; $attributes['datatype'] = $mapping['datatype'];
...@@ -295,11 +296,7 @@ function rdf_preprocess_node(&$variables) { ...@@ -295,11 +296,7 @@ function rdf_preprocess_node(&$variables) {
if (isset($variables['node']->comment_count) && !empty($comment_count_mapping['properties'])) { if (isset($variables['node']->comment_count) && !empty($comment_count_mapping['properties'])) {
// Annotates the 'x comments' link in teaser view. // Annotates the 'x comments' link in teaser view.
if (isset($variables['content']['links']['comment']['#links']['comment-comments'])) { if (isset($variables['content']['links']['comment']['#links']['comment-comments'])) {
$comment_count_attributes['property'] = $comment_count_mapping['properties']; $comment_count_attributes = rdf_rdfa_attributes($comment_count_mapping, $variables['node']->comment_count);
$comment_count_attributes['content'] = $variables['node']->comment_count;
if (isset($comment_count_mapping['datatype'])) {
$comment_count_attributes['datatype'] = $comment_count_mapping['datatype'];
}
// According to RDFa parsing rule number 4, a new subject URI is created // According to RDFa parsing rule number 4, a new subject URI is created
// from the href attribute if no rel/rev attribute is present. To get the // from the href attribute if no rel/rev attribute is present. To get the
// original node URL from the about attribute of the parent container we // original node URL from the about attribute of the parent container we
...@@ -375,15 +372,6 @@ function rdf_preprocess_user(&$variables) { ...@@ -375,15 +372,6 @@ function rdf_preprocess_user(&$variables) {
// If we are on the user account page, add the relationship between the // If we are on the user account page, add the relationship between the
// sioc:UserAccount and the foaf:Person who holds the account. // sioc:UserAccount and the foaf:Person who holds the account.
if (current_path() == $uri['path']) { if (current_path() == $uri['path']) {
$account_holder_meta = array(
'#tag' => 'meta',
'#attributes' => array(
'about' => url($uri['path'], array_merge($uri['options'], array('fragment' => 'me'))),
'typeof' => array('foaf:Person'),
'rel' => array('foaf:account'),
'resource' => url($uri['path'], $uri['options']),
),
);
// Adds the markup for username as language neutral literal, see // Adds the markup for username as language neutral literal, see
// rdf_preprocess_username(). // rdf_preprocess_username().
$name_mapping = $mapping->getPreparedFieldMapping('name'); $name_mapping = $mapping->getPreparedFieldMapping('name');
...@@ -399,7 +387,6 @@ function rdf_preprocess_user(&$variables) { ...@@ -399,7 +387,6 @@ function rdf_preprocess_user(&$variables) {
); );
drupal_add_html_head($username_meta, 'rdf_user_username'); drupal_add_html_head($username_meta, 'rdf_user_username');
} }
drupal_add_html_head($account_holder_meta, 'rdf_user_account_holder');
} }
} }
...@@ -505,7 +492,7 @@ function rdf_preprocess_comment(&$variables) { ...@@ -505,7 +492,7 @@ function rdf_preprocess_comment(&$variables) {
$variables['created'] .= $created_metadata_markup; $variables['created'] .= $created_metadata_markup;
$variables['submitted'] .= $created_metadata_markup; $variables['submitted'] .= $created_metadata_markup;
} }
$title_mapping = $mapping->getPreparedFieldMapping('title'); $title_mapping = $mapping->getPreparedFieldMapping('subject');
if (!empty($title_mapping)) { if (!empty($title_mapping)) {
// Adds RDFa markup to the subject of the comment. Because the RDFa markup // Adds RDFa markup to the subject of the comment. Because the RDFa markup
// is added to an <h3> tag which might contain HTML code, we specify an // is added to an <h3> tag which might contain HTML code, we specify an
...@@ -517,6 +504,8 @@ function rdf_preprocess_comment(&$variables) { ...@@ -517,6 +504,8 @@ function rdf_preprocess_comment(&$variables) {
// Annotates the parent relationship between the current comment and the node // Annotates the parent relationship between the current comment and the node
// it belongs to. If available, the parent comment is also annotated. // it belongs to. If available, the parent comment is also annotated.
// @todo When comments are turned into fields, this should be changed.
// Currently there is no mapping relating a comment to its node.
$pid_mapping = $mapping->getPreparedFieldMapping('pid'); $pid_mapping = $mapping->getPreparedFieldMapping('pid');
if (!empty($pid_mapping)) { if (!empty($pid_mapping)) {
// Adds the relation to the parent node. // Adds the relation to the parent node.
......
...@@ -2,12 +2,8 @@ id: user.user ...@@ -2,12 +2,8 @@ id: user.user
targetEntityType: user targetEntityType: user
bundle: user bundle: user
types: types:
- 'sioc:UserAccount' - 'schema:Person'
fieldMappings: fieldMappings:
name: name:
properties: properties:
- 'foaf:name' - 'schema:name'
homepage:
properties:
- 'foaf:page'
mapping_type: 'rel'
...@@ -2,34 +2,23 @@ id: comment.comment_node_article ...@@ -2,34 +2,23 @@ id: comment.comment_node_article
targetEntityType: comment targetEntityType: comment
bundle: comment_node_article bundle: comment_node_article
types: types:
- 'sioc:Post' - 'schema:Comment'
- 'sioct:Comment'
fieldMappings: fieldMappings:
title: subject:
properties: properties:
- 'dc:title' - 'schema:name'
created: created:
properties: properties:
- 'dc:date' - 'schema:dateCreated'
- 'dc:created'
datatype: 'xsd:dateTime'
datatype_callback: 'date_iso8601' datatype_callback: 'date_iso8601'
changed: changed:
properties: properties:
- 'dc:modified' - 'schema:dateModified'
datatype: 'xsd:dateTime'
datatype_callback: 'date_iso8601' datatype_callback: 'date_iso8601'
comment_body: comment_body:
properties: properties:
- 'content:encoded' - 'schema:text'
uid: uid:
properties: properties:
- 'sioc:has_creator' - 'schema:author'
mapping_type: 'rel'
name:
properties:
- 'foaf:name'
pid:
properties:
- 'sioc:reply_of'
mapping_type: 'rel' mapping_type: 'rel'
id: comment.comment_node_page
targetEntityType: comment
bundle: comment_node_page
types:
- 'schema:Comment'
fieldMappings:
subject:
properties:
- 'schema:name'
created:
properties:
- 'schema:dateCreated'
datatype_callback: 'date_iso8601'
changed:
properties:
- 'schema:dateModified'
datatype_callback: 'date_iso8601'
comment_body:
properties:
- 'schema:text'
uid:
properties:
- 'schema:author'
mapping_type: 'rel'
...@@ -2,48 +2,35 @@ id: node.article ...@@ -2,48 +2,35 @@ id: node.article
targetEntityType: node targetEntityType: node
bundle: article bundle: article
types: types:
- 'foaf:Document' - 'schema:Article'
- 'sioc:Item'
fieldMappings: fieldMappings:
title: title:
properties: properties:
- 'dc:title' - 'schema:name'
created: created:
properties: properties:
- 'dc:date' - 'schema:dateCreated'
- 'dc:created'
datatype: 'xsd:dateTime'
datatype_callback: 'date_iso8601' datatype_callback: 'date_iso8601'
changed: changed:
properties: properties:
- 'dc:modified' - 'schema:dateModified'
datatype: 'xsd:dateTime'
datatype_callback: 'date_iso8601' datatype_callback: 'date_iso8601'
body: body:
properties: properties:
- 'content:encoded' - 'schema:text'
uid: uid:
properties: properties:
- 'sioc:has_creator' - 'schema:author'
mapping_type: 'rel' mapping_type: 'rel'
name:
properties:
- 'foaf:name'
comment_count: comment_count:
properties: properties:
- 'sioc:num_replies' - 'schema:interactionCount'
datatype: 'xsd:integer' datatype_callback: 'Drupal\rdf\SchemaOrgDataConverter::interactionCount'
last_activity:
properties:
- 'sioc:last_activity_date'
datatype: 'xsd:dateTime'
datatype_callback: 'date_iso8601'
field_image: field_image:
properties: properties:
- 'og:image' - 'schema:image'
- 'rdfs:seeAlso'
mapping_type: 'rel' mapping_type: 'rel'
field_tags: field_tags:
properties: properties:
- 'dc:subject' - 'schema:about'
mapping_type: 'rel' mapping_type: 'rel'
...@@ -2,39 +2,27 @@ id: node.page ...@@ -2,39 +2,27 @@ id: node.page
targetEntityType: node targetEntityType: node
bundle: page bundle: page
types: types:
- 'foaf:Document' - 'schema:WebPage'
- 'sioc:Item'
fieldMappings: fieldMappings:
title: title:
properties: properties:
- 'dc:title' - 'schema:name'
created: created:
properties: properties:
- 'dc:date' - 'schema:dateCreated'
- 'dc:created'
datatype: 'xsd:dateTime'
datatype_callback: 'date_iso8601' datatype_callback: 'date_iso8601'
changed: changed:
properties: properties:
- 'dc:modified' - 'schema:dateModified'
datatype: 'xsd:dateTime'
datatype_callback: 'date_iso8601' datatype_callback: 'date_iso8601'
body: body:
properties: properties:
- 'content:encoded' - 'schema:text'
uid: uid:
properties: properties:
- 'sioc:has_creator' - 'schema:author'
mapping_type: 'rel' mapping_type: 'rel'
name:
properties:
- 'foaf:name'
comment_count: comment_count:
properties: properties:
- 'sioc:num_replies' - 'schema:interactionCount'
datatype: 'xsd:integer' datatype_callback: 'Drupal\rdf\SchemaOrgDataConverter::interactionCount'
last_activity:
properties:
- 'sioc:last_activity_date'
datatype: 'xsd:dateTime'
datatype_callback: 'date_iso8601'
...@@ -2,9 +2,11 @@ id: taxonomy_term.tags ...@@ -2,9 +2,11 @@ id: taxonomy_term.tags
targetEntityType: taxonomy_term targetEntityType: taxonomy_term
bundle: tags bundle: tags
types: types:
- 'skos:Concept' - 'schema:Thing'
fieldMappings: fieldMappings:
name: name:
properties: properties:
- 'rdfs:label' - 'schema:name'
- 'skos:prefLabel' description:
properties:
- 'schema:description'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment