Skip to content
This repository has been archived by the owner on Aug 25, 2022. It is now read-only.

DCAT demo module #31

Open
wants to merge 6 commits into
base: 8.x-1.x
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Grant 'update' rights to the SPARQL user:
System admin -> Users -> SPARQL (edit)
Account roles -> Put SPARQL_UPDATE in 'Selected'

## Connecting Drupal to the Sparql endpoint
## Connecting Drupal to the SPARQL endpoint
The following example demonstrates the use with a local Virtuoso installation.
To connect Drupal to the endpoint, the db connection should be added to the
settings.php file.
Expand All @@ -54,6 +54,9 @@ settings.php file.
'prefix' => '',
'host' => '127.0.0.1',
'port' => '8890',
// Optional. This is actually the endpoint path. If omitted, 'sparql' will
// be used.
'database' => 'data/endpoint',
'namespace' => 'Drupal\\rdf_entity\\Database\\Driver\\sparql',
'driver' => 'sparql',
];
Expand Down
2 changes: 1 addition & 1 deletion config/schema/rdf_entity.schema.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Schema for the configuration files of the rdf_entity module.

rdf_entity.rdfentity.*:
type: config_entity
type: bundle_entity_with_plural_labels
label: 'Rdf type'
mapping:
name:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
langcode: en
status: true
dependencies:
config:
- rdf_entity.graph.default
- rdf_entity.rdfentity.catalog
third_party_settings: { }
id: rdf_entity.catalog
entity_type_id: rdf_entity
bundle: catalog
rdf_type: 'http://www.w3.org/ns/dcat#Catalog'
base_fields_mapping:
rid:
target_id:
predicate: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
format: resource
label:
value:
predicate: 'http://purl.org/dc/terms/title'
format: t_literal
created:
value:
predicate: 'http://purl.org/dc/terms/issued'
format: 'xsd:dateTime'
changed:
value:
predicate: 'http://purl.org/dc/terms/modified'
format: 'xsd:dateTime'
uuid:
value:
predicate: ''
format: ''
langcode:
value:
predicate: 'http://joinup.eu/language'
format: t_literal
default_langcode:
value:
predicate: 'http://joinup.eu/language/default'
format: literal
content_translation_source:
value:
predicate: 'http://joinup.eu/language/translation_source'
format: t_literal
content_translation_outdated:
value:
predicate: 'http://joinup.eu/language/translation_outdated'
format: t_literal
content_translation_uid:
target_id:
predicate: 'http://joinup.eu/language/translation_author'
format: t_literal
content_translation_status:
value:
predicate: 'http://joinup.eu/language/translation_status'
format: t_literal
content_translation_created:
value:
predicate: 'http://joinup.eu/language/translation_created_time'
format: t_literal
content_translation_changed:
value:
predicate: 'http://joinup.eu/language/translation_changed_time'
format: t_literal
graph:
value:
predicate: ''
format: ''
graph:
default: 'http://joinup.eu/catalog/published'
draft: 'http://joinup.eu/catalog/draft'
entity_id_plugin: joinup_po_namespace
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
langcode: en
status: true
dependencies: { }
third_party_settings: { }
name: Dataset
rid: dataset
description: 'Represents a set of data.'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
langcode: en
status: true
dependencies: { }
third_party_settings: { }
name: Catalog
rid: catalog
description: 'A catalog of data-sets.'
7 changes: 7 additions & 0 deletions modules/dcat_demo/dcat_demo.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: 'DCAT Demo'
type: module
description: 'DCAT Demo'
core: 8.x
package: Custom
dependencies:
- rdf_entity
60 changes: 60 additions & 0 deletions modules/dcat_demo/tests/src/Functional/DcatDemoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Drupal\Tests\dcat_demo\Functional;

use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\rdf_entity\Traits\RdfDatabaseConnectionTrait;

/**
* Makes a demo as functional test.
*/
class DcatDemoTest extends BrowserTestBase {

use RdfDatabaseConnectionTrait {
getSparqlConnectionInfo as getSparqlConnectionInfoTrait;
}

/**
* {@inheritdoc}
*/
protected static $modules = [
'dcat_demo',
];

/**
* {@inheritdoc}
*/
protected function setUp() {
$this->setUpSparql();
parent::setUp();
}

/**
* {@inheritdoc}
*/
public function test() {
$query = <<<Query
SELECT DISTINCT ?Concept
WHERE {
[] a ?Concept
}
LIMIT 100
Query;
$response = $this->sparql->query($query);
$this->assertNotEmpty($response);
}

/**
* {@inheritdoc}
*/
protected function getSparqlConnectionInfo(): array {
if (!isset($this->sparqlConnectionInfo)) {
$this->getSparqlConnectionInfoTrait();
$this->sparqlConnectionInfo['host'] = 'data.europa.eu';
$this->sparqlConnectionInfo['port'] = 80;
$this->sparqlConnectionInfo['database'] = 'euodp/sparqlep';
}
return $this->sparqlConnectionInfo;
}

}
4 changes: 2 additions & 2 deletions src/Database/Driver/sparql/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ public function getLogger() {
* The EasyRdf connection.
*/
public static function open(array &$connection_options = []) {
// @todo Get endpoint string from settings file.
$connect_string = 'http://' . $connection_options['host'] . ':' . $connection_options['port'] . '/sparql';
$enpoint_path = $connection_options['database'] ?? 'sparql';
$connect_string = "http://{$connection_options['host']}:{$connection_options['port']}/{$enpoint_path}";
return new Client($connect_string);
}

Expand Down
9 changes: 8 additions & 1 deletion src/Entity/RdfEntityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\rdf_entity\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Config\Entity\EntityBundleWithPluralLabelsTrait;
use Drupal\rdf_entity\RdfEntityTypeInterface;

/**
Expand Down Expand Up @@ -36,11 +37,17 @@
* config_export = {
* "name",
* "rid",
* "description"
* "description",
* "label_singular",
* "label_plural",
* "label_count",
* }
* )
*/
class RdfEntityType extends ConfigEntityBundleBase implements RdfEntityTypeInterface {

use EntityBundleWithPluralLabelsTrait;

/**
* The bundle type of RDF entity.
*
Expand Down
3 changes: 2 additions & 1 deletion src/RdfEntityTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\rdf_entity;

use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\Entity\EntityBundleWithPluralLabelsInterface;

/**
* Provides an interface defining a Rdf entity.
Expand All @@ -11,6 +12,6 @@
*
* @ingroup rdf_entity
*/
interface RdfEntityTypeInterface extends ConfigEntityInterface {
interface RdfEntityTypeInterface extends ConfigEntityInterface, EntityBundleWithPluralLabelsInterface {

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ third_party_settings: { }
name: Dummy
rid: dummy
description: ''
label_singular: 'dummy item'
label_plural: 'dummy items'
label_count:
- "1 dummy item\x03@count dummy items"
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ third_party_settings: { }
name: Multifield
rid: multifield
description: ''
label_singular: 'multifield item'
label_plural: 'multifield items'
label_count:
- "1 multifield item\x03@count multifield items"
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ third_party_settings: { }
name: 'With Owner'
rid: with_owner
description: ''
label_singular: "'with owner' item"
label_plural: "'with owner' items"
label_count:
- "1 'with owner' item\x03@count 'with owner' items"
34 changes: 19 additions & 15 deletions tests/src/Traits/RdfDatabaseConnectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ trait RdfDatabaseConnectionTrait {
protected function detectVirtuoso6() {
$client = Http::getDefaultHttpClient();
$client->resetParameters(TRUE);
$client->setUri("http://{$this->sparqlConnectionInfo['host']}:{$this->sparqlConnectionInfo['port']}/");
$client->setUri("http://{$this->getSparqlConnectionInfo()['host']}:{$this->getSparqlConnectionInfo()['port']}/");
$client->setMethod('GET');
$response = $client->request();
$server_header = $response->getHeader('Server');
Expand All @@ -49,21 +49,8 @@ protected function detectVirtuoso6() {
* When SIMPLETEST_SPARQL_DB is not set.
*/
protected function setUpSparql() {
// If the test is run with argument db url then use it.
// export SIMPLETEST_SPARQL_DB='sparql://127.0.0.1:8890/'.
$db_url = getenv('SIMPLETEST_SPARQL_DB');
if (empty($db_url)) {
throw new \LogicException('No Sparql connection was defined. Set the SIMPLETEST_SPARQL_DB environment variable.');
}

$this->sparqlConnectionInfo = Database::convertDbUrlToConnectionInfo($db_url, dirname(dirname(__FILE__)));
$this->sparqlConnectionInfo['namespace'] = 'Drupal\\rdf_entity\\Database\\Driver\\sparql';

// Do not allow Virtuoso 6.
$this->detectVirtuoso6();

Database::addConnectionInfo('sparql_default', 'default', $this->sparqlConnectionInfo);

Database::addConnectionInfo('sparql_default', 'default', $this->getSparqlConnectionInfo());
$this->sparql = Database::getConnection('default', 'sparql_default');
}

Expand All @@ -85,4 +72,21 @@ protected function writeSettings(array $settings) {
parent::writeSettings($settings);
}

/**
* Returns the SPARQL connection info.
*
* @return array
* The connection infp array.
*/
protected function getSparqlConnectionInfo(): array {
if (!isset($this->sparqlConnectionInfo)) {
if (empty($db_url = getenv('SIMPLETEST_SPARQL_DB'))) {
throw new \LogicException('No SPARQL connection was defined. Set the SIMPLETEST_SPARQL_DB environment variable.');
}
$this->sparqlConnectionInfo = Database::convertDbUrlToConnectionInfo($db_url, dirname(dirname(__FILE__)));
$this->sparqlConnectionInfo['namespace'] = 'Drupal\\rdf_entity\\Database\\Driver\\sparql';
}
return $this->sparqlConnectionInfo;
}

}