Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drupal uri predicate #729

Merged
merged 5 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Map URI to predicate context reaction.
* Create a self-reference in RDF when creating JSON-LD.
*
* Formerly called "Map URI to predicate". Renamed for clarity.
*
* @ContextReaction(
* id = "islandora_map_uri_predicate",
* label = @Translation("Map URI to predicate")
* label = @Translation("JSON-LD self-reference")
* )
*/
class MappingUriPredicateReaction extends NormalizerAlterReaction {
class JsonldSelfReferenceReaction extends NormalizerAlterReaction {

const URI_PREDICATE = 'drupal_uri_predicate';
const SELF_REFERENCE_PREDICATE = 'drupal_uri_predicate';

/**
* Media source service.
Expand Down Expand Up @@ -69,19 +71,19 @@ public static function create(ContainerInterface $container, array $configuratio
* {@inheritdoc}
*/
public function summary() {
return $this->t('Map Drupal URI to configured predicate.');
return $this->t('When creating the JSON-LD for this Drupal entity, add a relationship to itself using this predicate.');
}

/**
* {@inheritdoc}
*/
public function execute(EntityInterface $entity = NULL, array &$normalized = NULL, array $context = NULL) {
$config = $this->getConfiguration();
$drupal_predicate = $config[self::URI_PREDICATE];
if (!is_null($drupal_predicate) && !empty($drupal_predicate)) {
$self_ref_predicate = $config[self::SELF_REFERENCE_PREDICATE];
if (!is_null($self_ref_predicate) && !empty($self_ref_predicate)) {
$url = $this->getSubjectUrl($entity);
if ($context['needs_jsonldcontext'] === FALSE) {
$drupal_predicate = NormalizerBase::escapePrefix($drupal_predicate, $context['namespaces']);
$self_ref_predicate = NormalizerBase::escapePrefix($self_ref_predicate, $context['namespaces']);
}
if (isset($normalized['@graph']) && is_array($normalized['@graph'])) {
foreach ($normalized['@graph'] as &$graph) {
Expand All @@ -91,24 +93,24 @@ public function execute(EntityInterface $entity = NULL, array &$normalized = NUL
$file = $this->mediaSource->getSourceFile($entity);
$graph['@id'] = $this->utils->getDownloadUrl($file);
}
if (isset($graph[$drupal_predicate])) {
if (!is_array($graph[$drupal_predicate])) {
if ($graph[$drupal_predicate] == $url) {
if (isset($graph[$self_ref_predicate])) {
if (!is_array($graph[$self_ref_predicate])) {
if ($graph[$self_ref_predicate] == $url) {
// Don't add it if it already exists.
return;
}
$tmp = $graph[$drupal_predicate];
$graph[$drupal_predicate] = [$tmp];
$tmp = $graph[$self_ref_predicate];
$graph[$self_ref_predicate] = [$tmp];
}
elseif (array_search($url, array_column($graph[$drupal_predicate], '@id'))) {
elseif (array_search($url, array_column($graph[$self_ref_predicate], '@id'))) {
// Don't add it if it already exists.
return;
}
}
else {
$graph[$drupal_predicate] = [];
$graph[$self_ref_predicate] = [];
}
$graph[$drupal_predicate][] = ["@id" => $url];
$graph[$self_ref_predicate][] = ["@id" => $url];
return;
}
}
Expand All @@ -121,11 +123,11 @@ public function execute(EntityInterface $entity = NULL, array &$normalized = NUL
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$config = $this->getConfiguration();
$form[self::URI_PREDICATE] = [
$form[self::SELF_REFERENCE_PREDICATE] = [
'#type' => 'textfield',
'#title' => $this->t('Drupal URI predicate'),
'#description' => $this->t("The Drupal object's URI will be added to the resource with this predicate. Must use a defined prefix."),
'#default_value' => isset($config[self::URI_PREDICATE]) ? $config[self::URI_PREDICATE] : '',
'#title' => $this->t('Self-reference predicate'),
'#description' => $this->t("When creating the JSON-LD for this Drupal entity, add a relationship from the entity to itself using this predicate. It must use a defined RDF namespace prefix."),
'#default_value' => isset($config[self::SELF_REFERENCE_PREDICATE]) ? $config[self::SELF_REFERENCE_PREDICATE] : '',
'#size' => 35,
];
return $form;
Expand All @@ -135,19 +137,19 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$drupal_predicate = $form_state->getValue(self::URI_PREDICATE);
if (!is_null($drupal_predicate) and !empty($drupal_predicate)) {
if (preg_match('/^https?:\/\//', $drupal_predicate)) {
$self_ref_predicate = $form_state->getValue(self::SELF_REFERENCE_PREDICATE);
if (!is_null($self_ref_predicate) and !empty($self_ref_predicate)) {
if (preg_match('/^https?:\/\//', $self_ref_predicate)) {
// Can't validate all URIs so we have to trust them.
return;
}
elseif (preg_match('/^([^\s:]+):/', $drupal_predicate, $matches)) {
elseif (preg_match('/^([^\s:]+):/', $self_ref_predicate, $matches)) {
$predicate_prefix = $matches[1];
$rdf = rdf_get_namespaces();
$rdf_prefixes = array_keys($rdf);
if (!in_array($predicate_prefix, $rdf_prefixes)) {
$form_state->setErrorByName(
self::URI_PREDICATE,
self::SELF_REFERENCE_PREDICATE,
$this->t('Namespace prefix @prefix is not registered.',
['@prefix' => $predicate_prefix]
)
Expand All @@ -156,7 +158,7 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form
}
else {
$form_state->setErrorByName(
self::URI_PREDICATE,
self::SELF_REFERENCE_PREDICATE,
$this->t('Predicate must use a defined prefix or be a full URI')
);
}
Expand All @@ -168,7 +170,7 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->setConfiguration([self::URI_PREDICATE => $form_state->getValue(self::URI_PREDICATE)]);
$this->setConfiguration([self::SELF_REFERENCE_PREDICATE => $form_state->getValue(self::SELF_REFERENCE_PREDICATE)]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @package Drupal\Tests\islandora\Functional
* @group islandora
*/
class MappingUriPredicateReactionTest extends IslandoraFunctionalTestBase {
class JsonldSelfReferenceReactionTest extends IslandoraFunctionalTestBase {

/**
* {@inheritdoc}
Expand Down Expand Up @@ -37,7 +37,7 @@ public function setUp() {
}

/**
* @covers \Drupal\islandora\Plugin\ContextReaction\MappingUriPredicateReaction
* @covers \Drupal\islandora\Plugin\ContextReaction\JsonldSelfReferenceReaction
*/
public function testMappingReaction() {
$account = $this->drupalCreateUser([
Expand Down Expand Up @@ -79,21 +79,21 @@ public function testMappingReaction() {
$this->drupalGet("admin/structure/context/$context_name");
// Can't use an undefined prefix.
$this->getSession()->getPage()
->fillField("Drupal URI predicate", "bob:smith");
->fillField("Self-reference predicate", "bob:smith");
$this->getSession()->getPage()->pressButton("Save and continue");
$this->assertSession()
->pageTextContains("Namespace prefix bob is not registered");

// Can't use a straight string.
$this->getSession()->getPage()
->fillField("Drupal URI predicate", "woohoo");
->fillField("Self-reference predicate", "woohoo");
$this->getSession()->getPage()->pressButton("Save and continue");
$this->assertSession()
->pageTextContains("Predicate must use a defined prefix or be a full URI");

// Use an existing prefix.
$this->getSession()->getPage()
->fillField("Drupal URI predicate", "owl:sameAs");
->fillField("Self-reference predicate", "owl:sameAs");
$this->getSession()->getPage()->pressButton("Save and continue");
$this->assertSession()
->pageTextContains("The context $context_name has been saved");
Expand All @@ -114,7 +114,7 @@ public function testMappingReaction() {
$this->drupalGet("admin/structure/context/$context_name");
// Change to a random URL.
$this->getSession()->getPage()
->fillField("Drupal URI predicate", "http://example.org/first/second");
->fillField("Self-reference predicate", "http://example.org/first/second");
$this->getSession()->getPage()->pressButton("Save and continue");
$this->assertSession()
->pageTextContains("The context $context_name has been saved");
Expand All @@ -135,7 +135,7 @@ public function testMappingReaction() {
}

/**
* @covers \Drupal\islandora\Plugin\ContextReaction\MappingUriPredicateReaction
* @covers \Drupal\islandora\Plugin\ContextReaction\JsonldSelfReferenceReaction
*/
public function testMappingReactionForMedia() {
$account = $this->drupalCreateUser([
Expand Down Expand Up @@ -172,7 +172,7 @@ public function testMappingReactionForMedia() {

// Use an existing prefix.
$this->getSession()->getPage()
->fillField("Drupal URI predicate", "iana:describedby");
->fillField("Self-reference predicate", "iana:describedby");
$this->getSession()->getPage()->pressButton("Save and continue");
$this->assertSession()
->pageTextContains("The context $context_name has been saved");
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Functional/JsonldTypeAlterReactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @package Drupal\Tests\islandora\Functional
* @group islandora
*/
class JsonldTypeAlterReactionTest extends MappingUriPredicateReactionTest {
class JsonldTypeAlterReactionTest extends JsonldSelfReferenceReactionTest {

/**
* @covers \Drupal\islandora\Plugin\ContextReaction\JsonldTypeAlterReaction
Expand Down