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

Commit

Permalink
Merge pull request #73 from ec-europa/construct-queries
Browse files Browse the repository at this point in the history
Support construct queries.
  • Loading branch information
claudiu-cristea authored Dec 6, 2018
2 parents e534a43 + f66229a commit b024d4a
Show file tree
Hide file tree
Showing 24 changed files with 512 additions and 5 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"license": "GPL-2.0+",
"require": {
"php": ">=7.1",
"easyrdf/easyrdf": "0.10.0-alpha.1 as 0.9.2"
"easyrdf/easyrdf": "0.10.0-alpha.1 as 0.9.2",
"ml/json-ld": "^1.0"
},
"autoload": {
"classmap": [
Expand Down
33 changes: 33 additions & 0 deletions driver/sparql/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Drupal\rdf_entity\Database\Driver\sparql\ConnectionInterface;
use Drupal\rdf_entity\Database\Driver\sparql\StatementStub;
use Drupal\rdf_entity\Exception\SparqlQueryException;
use EasyRdf\Graph;
use EasyRdf\Http\Exception as EasyRdfException;
use EasyRdf\Sparql\Client;
use EasyRdf\Sparql\Result;
Expand Down Expand Up @@ -89,6 +90,38 @@ public function __construct(Client $easy_rdf_client, array $connection_options)
* {@inheritdoc}
*/
public function query(string $query, array $args = [], array $options = []): Result {
return $this->doQuery($query, $args, $options);
}

/**
* {@inheritdoc}
*/
public function constructQuery(string $query, array $args = [], array $options = []): Graph {
return $this->doQuery($query, $args, $options);
}

/**
* Execute the query against the endpoint.
*
* @param string $query
* The string query to execute.
* @param array $args
* An array of arguments for the query.
* @param array $options
* An associative array of options to control how the query is run.
*
* @return \EasyRdf\Sparql\Result|\EasyRdf\Graph
* The query result.
*
* @throws \InvalidArgumentException
* If $args value is passed but arguments replacement is not yet
* supported. To be removed in #55.
* @throws SparqlQueryException
* Exception during query execution, e.g. timeout.
*
* @see https://github.com/ec-europa/rdf_entity/issues/55
*/
protected function doQuery(string $query, array $args = [], array $options = []) {
// @todo Remove this in #55.
// @see https://github.com/ec-europa/rdf_entity/issues/55
if ($args) {
Expand Down
3 changes: 2 additions & 1 deletion modules/rdf_export/rdf_export.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ core: 8.x

# These modules are required by the tests, must be available at bootstrap time
dependencies:
- rdf_entity
- rdf_entity:rdf_entity
- drupal:serialization
32 changes: 32 additions & 0 deletions modules/rdf_export/rdf_export.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,35 @@ services:
rdf_export.serializer:
class: Drupal\rdf_export\RdfSerializer
arguments: ['@sparql_endpoint']
rdf_export.encoder_base:
abstract: true
class: Drupal\rdf_export\Encoder\RdfEncoder
rdf_export.encoder.jsonld:
parent: rdf_export.encoder_base
tags:
- { name: encoder, format: jsonld }
rdf_export.encoder.rdfxml:
parent: rdf_export.encoder_base
tags:
- { name: encoder, format: rdfxml }
rdf_export.encoder.ntriples:
parent: rdf_export.encoder_base
tags:
- { name: encoder, format: ntriples }
rdf_export.encoder.turtle:
parent: rdf_export.encoder_base
tags:
- { name: encoder, format: turtle }
rdf_export.encoder.n3:
parent: rdf_export.encoder_base
tags:
- { name: encoder, format: n3 }
rdf_export.rdfsubscriber:
class: Drupal\rdf_export\EventSubscriber\RdfSubscriber
tags:
- { name: event_subscriber }
serializer.normalizer.entity.rdf:
class: Drupal\rdf_export\Normalizer\RdfEntityNormalizer
arguments: ['@rdf_export.serializer']
tags:
- { name: normalizer, priority: 10 }
52 changes: 52 additions & 0 deletions modules/rdf_export/src/Encoder/RdfEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types = 1);

namespace Drupal\rdf_export\Encoder;

use Drupal\rdf_export\RdfEncoderInterface;
use EasyRdf\Format;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;

/**
* Adds RDF encoder support for the Serialization API.
*/
class RdfEncoder implements RdfEncoderInterface {

/**
* Static cache for supported formats.
*
* @var \EasyRdf\Serialiser[]
*/
protected static $supportedFormats;

/**
* {@inheritdoc}
*/
public function supportsEncoding($format): bool {
return !empty(static::getSupportedFormats()[$format]);
}

/**
* {@inheritdoc}
*/
public function encode($data, $format, array $context = []): string {
if (!isset($data['_rdf_entity'])) {
throw new UnexpectedValueException("Data to be encoded is missing the '_rdf_entity' key.");
}
return $data['_rdf_entity'];
}

/**
* {@inheritdoc}
*/
public static function getSupportedFormats(): array {
if (!isset(static::$supportedFormats)) {
$container_registered_formats = \Drupal::getContainer()->getParameter('rdf_export.encoders');
$rdf_serializers = Format::getFormats();
static::$supportedFormats = array_intersect_key($rdf_serializers, $container_registered_formats);
}
return static::$supportedFormats;
}

}
37 changes: 37 additions & 0 deletions modules/rdf_export/src/EventSubscriber/RdfSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Drupal\rdf_export\EventSubscriber;

use Drupal\rdf_export\Encoder\RdfEncoder;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Event subscriber for adding RDF content types to the request.
*/
class RdfSubscriber implements EventSubscriberInterface {

/**
* Register content type formats on the request object.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* The Event to process.
*/
public function onKernelRequest(GetResponseEvent $event) {
/** @var \EasyRdf\Format $format */
foreach (RdfEncoder::getSupportedFormats() as $format) {
$mime = array_keys($format->getMimeTypes());
$event->getRequest()->setFormat($format->getName(), $mime);
}
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = ['onKernelRequest'];
return $events;
}

}
20 changes: 20 additions & 0 deletions modules/rdf_export/src/Normalizer/NormalizerBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Drupal\rdf_export\Normalizer;

use Drupal\rdf_export\Encoder\RdfEncoder;
use Drupal\serialization\Normalizer\NormalizerBase as SerializationNormalizerBase;

/**
* Base class for Normalizers.
*/
abstract class NormalizerBase extends SerializationNormalizerBase {

/**
* {@inheritdoc}
*/
protected function checkFormat($format = NULL) {
return !empty(RdfEncoder::getSupportedFormats()[$format]);
}

}
40 changes: 40 additions & 0 deletions modules/rdf_export/src/Normalizer/RdfEntityNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Drupal\rdf_export\Normalizer;

use Drupal\rdf_entity\RdfInterface;
use Drupal\rdf_export\RdfSerializer;
use Drupal\serialization\Normalizer\FieldableEntityNormalizerTrait;

/**
* Converts the Drupal entity object structure to a HAL array structure.
*/
class RdfEntityNormalizer extends NormalizerBase {

use FieldableEntityNormalizerTrait;

/**
* The interface or class that this Normalizer supports.
*
* @var string
*/
protected $supportedInterfaceOrClass = RdfInterface::class;

/**
* RdfEntityNormalizer constructor.
*
* @param \Drupal\rdf_export\RdfSerializer $rdf_serializer
* RDF Serializer service.
*/
public function __construct(RdfSerializer $rdf_serializer) {
$this->rdfSerializer = $rdf_serializer;
}

/**
* {@inheritdoc}
*/
public function normalize($entity, $format = NULL, array $context = []) {
return ['_rdf_entity' => $this->rdfSerializer->serializeEntity($entity, $format)];
}

}
33 changes: 33 additions & 0 deletions modules/rdf_export/src/RdfEncoderCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types = 1);

namespace Drupal\rdf_export;

use EasyRdf\Format;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Collects all RDF encoders and stores them into a service container parameter.
*/
class RdfEncoderCompilerPass implements CompilerPassInterface {

/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container): void {
$rdf_formats = array_keys(Format::getFormats());
$encoders = [];
foreach ($container->findTaggedServiceIds('encoder') as $id => $attributes) {
$class = $container->getDefinition($id)->getClass();
$interfaces = class_implements($class);
$format = $attributes[0]['format'];
if (isset($interfaces[RdfEncoderInterface::class]) && in_array($format, $rdf_formats)) {
$encoders[$format] = $format;
}
$container->setParameter('rdf_export.encoders', $encoders);
}
}

}
22 changes: 22 additions & 0 deletions modules/rdf_export/src/RdfEncoderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types = 1);

namespace Drupal\rdf_export;

use Symfony\Component\Serializer\Encoder\EncoderInterface;

/**
* Provides an interface to RDF encoders.
*/
interface RdfEncoderInterface extends EncoderInterface {

/**
* Builds a list of supported formats.
*
* @return \EasyRdf\Serialiser[]
* List of supported formats.
*/
public static function getSupportedFormats(): array;

}
24 changes: 24 additions & 0 deletions modules/rdf_export/src/RdfExportServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types = 1);

namespace Drupal\rdf_export;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;

/**
* RDF Export dependency injection container.
*/
class RdfExportServiceProvider implements ServiceProviderInterface {

/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container) {
// Run this compiler pass after the child definitions were resolved.
$container->addCompilerPass(new RdfEncoderCompilerPass(), PassConfig::TYPE_OPTIMIZE, -10);
}

}
2 changes: 1 addition & 1 deletion modules/rdf_export/src/RdfSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function serializeEntity(RdfInterface $entity, string $format = 'turtle')
SPARQL;

/** @var \EasyRdf\Graph $graph */
$graph = $this->sparqlEndpoint->query($query);
$graph = $this->sparqlEndpoint->constructQuery($query);
return $graph->serialise($format);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"@id":"http://example.com/apple","@type":["http://example.com/type/fruit"],"http://example.com/fruit/uid":[{"@value":0}],"http://example.com/fruit/label":[{"@value":"Apple","@language":"en"}]},{"@id":"http://example.com/type/fruit"}]
6 changes: 6 additions & 0 deletions modules/rdf_export/tests/fixtures/content-negotiation/n3
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@prefix ns0: <http://example.com/fruit/> .

<http://example.com/apple>
a <http://example.com/type/fruit> ;
ns0:uid 0 ;
ns0:label "Apple"@en .
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<http://example.com/apple> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/type/fruit> .
<http://example.com/apple> <http://example.com/fruit/uid> "0"^^<http://www.w3.org/2001/XMLSchema#integer> .
<http://example.com/apple> <http://example.com/fruit/label> "Apple"@en .
11 changes: 11 additions & 0 deletions modules/rdf_export/tests/fixtures/content-negotiation/rdfxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:ns0="http://example.com/fruit/">

<rdf:Description rdf:about="http://example.com/apple">
<rdf:type rdf:resource="http://example.com/type/fruit"/>
<ns0:uid rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">0</ns0:uid>
<ns0:label xml:lang="en">Apple</ns0:label>
</rdf:Description>

</rdf:RDF>
6 changes: 6 additions & 0 deletions modules/rdf_export/tests/fixtures/content-negotiation/turtle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@prefix ns0: <http://example.com/fruit/> .

<http://example.com/apple>
a <http://example.com/type/fruit> ;
ns0:uid 0 ;
ns0:label "Apple"@en .
Loading

0 comments on commit b024d4a

Please sign in to comment.