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
targetEntityType: node
bundle: forum
types:
- 'sioc:Post'
- 'sioct:BoardPost'
- 'schema:Discussion'
fieldMappings:
taxonomy_forums:
title:
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'
comment_count:
properties:
- 'schema:interactionCount'
datatype_callback: 'Drupal\rdf\SchemaOrgDataConverter::interactionCount'
......@@ -4,3 +4,10 @@ bundle: forums
types:
- 'sioc:Container'
- '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() {
$comment_count_mapping = array(
'properties' => array('sioc:num_replies'),
'datatype' => 'xsd:integer',
'datatype_callback' => 'Drupal\rdf\CommonDataConverter::rawValue',
);
$article_mapping->setFieldMapping('comment_count', $comment_count_mapping)->save();
......@@ -69,7 +70,7 @@ public function setUp() {
$mapping = rdf_get_mapping('comment', 'comment_node_article');
$mapping->setBundleMapping(array('types' => array('sioc:Post', 'sioct:Comment')))->save();
$field_mappings = array(
'title' => array(
'subject' => array(
'properties' => array('dc:title'),
),
'created' => array(
......@@ -251,7 +252,7 @@ function _testBasicCommentRdfaMarkup($graph, $comment, $account = array()) {
'value' => $comment->subject->value,
'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.
$expected_value = array(
......
......@@ -84,6 +84,28 @@ function testDatatypeCallback() {
$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'.
*/
......
This diff is collapsed.
......@@ -77,5 +77,8 @@ function testTaxonomyTermRdfaAttributes() {
'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).');
// @todo Add test for term description once it is a field:
// https://drupal.org/node/569434
}
}
......@@ -76,19 +76,6 @@ function testUserAttributesInMarkup() {
'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).');
// 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.
$this->drupalLogin($user2);
......
......@@ -92,6 +92,7 @@ function rdf_rdf_namespaces() {
'foaf' => 'http://xmlns.com/foaf/0.1/',
'og' => 'http://ogp.me/ns#',
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
'schema' => 'http://schema.org/',
'sioc' => 'http://rdfs.org/sioc/ns#',
'sioct' => 'http://rdfs.org/sioc/types#',
'skos' => 'http://www.w3.org/2004/02/skos/core#',
......@@ -174,7 +175,7 @@ function rdf_rdfa_attributes($mapping, $data = NULL) {
// Convert $data to a specific format as per the callback function.
if (isset($data) && !empty($mapping['datatype_callback'])) {
$callback = $mapping['datatype_callback'];
$attributes['content'] = $callback($data);
$attributes['content'] = call_user_func($callback, $data);
}
if (isset($mapping['datatype'])) {
$attributes['datatype'] = $mapping['datatype'];
......@@ -295,11 +296,7 @@ function rdf_preprocess_node(&$variables) {
if (isset($variables['node']->comment_count) && !empty($comment_count_mapping['properties'])) {
// Annotates the 'x comments' link in teaser view.
if (isset($variables['content']['links']['comment']['#links']['comment-comments'])) {
$comment_count_attributes['property'] = $comment_count_mapping['properties'];
$comment_count_attributes['content'] = $variables['node']->comment_count;
if (isset($comment_count_mapping['datatype'])) {
$comment_count_attributes['datatype'] = $comment_count_mapping['datatype'];
}
$comment_count_attributes = rdf_rdfa_attributes($comment_count_mapping, $variables['node']->comment_count);
// 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
// original node URL from the about attribute of the parent container we
......@@ -375,15 +372,6 @@ function rdf_preprocess_user(&$variables) {
// If we are on the user account page, add the relationship between the
// sioc:UserAccount and the foaf:Person who holds the account.
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
// rdf_preprocess_username().
$name_mapping = $mapping->getPreparedFieldMapping('name');
......@@ -399,7 +387,6 @@ function rdf_preprocess_user(&$variables) {
);
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) {
$variables['created'] .= $created_metadata_markup;
$variables['submitted'] .= $created_metadata_markup;
}
$title_mapping = $mapping->getPreparedFieldMapping('title');
$title_mapping = $mapping->getPreparedFieldMapping('subject');
if (!empty($title_mapping)) {
// 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
......@@ -517,6 +504,8 @@ function rdf_preprocess_comment(&$variables) {
// Annotates the parent relationship between the current comment and the node
// 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');
if (!empty($pid_mapping)) {
// Adds the relation to the parent node.
......
......@@ -2,12 +2,8 @@ id: user.user
targetEntityType: user
bundle: user
types:
- 'sioc:UserAccount'
- 'schema:Person'
fieldMappings:
name:
properties:
- 'foaf:name'
homepage:
properties:
- 'foaf:page'
mapping_type: 'rel'
- 'schema:name'
......@@ -2,34 +2,23 @@ id: comment.comment_node_article
targetEntityType: comment
bundle: comment_node_article
types:
- 'sioc:Post'
- 'sioct:Comment'
- 'schema:Comment'
fieldMappings:
title:
subject:
properties:
- 'dc:title'
- 'schema:name'
created:
properties:
- 'dc:date'
- 'dc:created'
datatype: 'xsd:dateTime'
- 'schema:dateCreated'
datatype_callback: 'date_iso8601'
changed:
properties:
- 'dc:modified'
datatype: 'xsd:dateTime'
- 'schema:dateModified'
datatype_callback: 'date_iso8601'
comment_body:
properties:
- 'content:encoded'
- 'schema:text'
uid:
properties:
- 'sioc:has_creator'
mapping_type: 'rel'
name:
properties:
- 'foaf:name'
pid:
properties:
- 'sioc:reply_of'
- 'schema:author'
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
targetEntityType: node
bundle: article
types:
- 'foaf:Document'
- 'sioc:Item'
- 'schema:Article'
fieldMappings:
title:
properties:
- 'dc:title'
- 'schema:name'
created:
properties:
- 'dc:date'
- 'dc:created'
datatype: 'xsd:dateTime'
- 'schema:dateCreated'
datatype_callback: 'date_iso8601'
changed:
properties:
- 'dc:modified'
datatype: 'xsd:dateTime'
- 'schema:dateModified'
datatype_callback: 'date_iso8601'
body:
properties:
- 'content:encoded'
- 'schema:text'
uid:
properties:
- 'sioc:has_creator'
- 'schema:author'
mapping_type: 'rel'
name:
properties:
- 'foaf:name'
comment_count:
properties:
- 'sioc:num_replies'
datatype: 'xsd:integer'
last_activity:
properties:
- 'sioc:last_activity_date'
datatype: 'xsd:dateTime'
datatype_callback: 'date_iso8601'
- 'schema:interactionCount'
datatype_callback: 'Drupal\rdf\SchemaOrgDataConverter::interactionCount'
field_image:
properties:
- 'og:image'
- 'rdfs:seeAlso'
- 'schema:image'
mapping_type: 'rel'
field_tags:
properties:
- 'dc:subject'
- 'schema:about'
mapping_type: 'rel'
......@@ -2,39 +2,27 @@ id: node.page
targetEntityType: node
bundle: page
types:
- 'foaf:Document'
- 'sioc:Item'
- 'schema:WebPage'
fieldMappings:
title:
properties:
- 'dc:title'
- 'schema:name'
created:
properties:
- 'dc:date'
- 'dc:created'
datatype: 'xsd:dateTime'
- 'schema:dateCreated'
datatype_callback: 'date_iso8601'
changed:
properties:
- 'dc:modified'
datatype: 'xsd:dateTime'
- 'schema:dateModified'
datatype_callback: 'date_iso8601'
body:
properties:
- 'content:encoded'
- 'schema:text'
uid:
properties:
- 'sioc:has_creator'
- 'schema:author'
mapping_type: 'rel'
name:
properties:
- 'foaf:name'
comment_count:
properties:
- 'sioc:num_replies'
datatype: 'xsd:integer'
last_activity:
properties:
- 'sioc:last_activity_date'
datatype: 'xsd:dateTime'
datatype_callback: 'date_iso8601'
- 'schema:interactionCount'
datatype_callback: 'Drupal\rdf\SchemaOrgDataConverter::interactionCount'
......@@ -2,9 +2,11 @@ id: taxonomy_term.tags
targetEntityType: taxonomy_term
bundle: tags
types:
- 'skos:Concept'
- 'schema:Thing'
fieldMappings:
name:
properties:
- 'rdfs:label'
- 'skos:prefLabel'
- 'schema:name'
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