Skip to content

Commit

Permalink
Refactor with traits (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtich committed May 3, 2016
1 parent a060d2c commit 5746c5a
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 259 deletions.
7 changes: 7 additions & 0 deletions CONTRIBUTING.md
@@ -0,0 +1,7 @@

## Mapping RDF to JSKOS

To facilitate writing wrappers from RDF to JSKOS, a mapping can be written in
form of a YAML file. See `GNDService`/`GNDMapping.yaml` and
`GeonamesService`/`GeonamesMapping` for examples that make use of `RDFTrait`.

12 changes: 12 additions & 0 deletions Makefile
@@ -0,0 +1,12 @@
info:
@echo "Usage: make install|test|run"

install:
composer install

test:
composer debug-test

run:
php -S localhost:8080 -t src

4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -20,7 +20,9 @@ You can directly serve wrappers via PHP for testing (don't use for production!):

$ php -S localhost:8080 -t src

And accesed via <http://localhost:8080>.
And accesed via <http://localhost:8080>. You can also start the server with

$ make run

# Installation

Expand Down
5 changes: 3 additions & 2 deletions src/BARTOC.php
@@ -1,6 +1,7 @@
<?php

include realpath(__DIR__) . '/lib/BARTOCService.php';
$service = 'BARTOCService';

\JSKOS\Server::runService(new BARTOCService());
include realpath(__DIR__) . "/lib/$service.php";
\JSKOS\Server::runService(new $service());

29 changes: 3 additions & 26 deletions src/GND.php
@@ -1,30 +1,7 @@
<?php

/**
* Implements a basic JSKOS concepts endpoint for GND.
*
* This wrapper converts GND RDF/XML to JSKOS.
*
* @package JSKOS
*/
$service = 'GNDService';

include realpath(__DIR__) . '/lib/GNDService.php';

\JSKOS\Server::runService(new GNDService());

/*
$concept = new Concept(["uri" => "$uri"]);
foreach (getResources($xml, 'foaf:page') as $page) {
$concept->subjectOf = [ "uri" => $page ];
}
foreach (getResources($xml, 'gndo:broaderTermGeneral') as $uri) {
$concept->broader[] = [ "uri" => $uri ];
}
foreach (getLiterals($xml, 'gndo:preferredNameForTheSubjectHeading') as $label) {
$concept->prefLabel["de"] = $label;
}
*/
include realpath(__DIR__) . "/lib/$service.php";
\JSKOS\Server::runService(new $service());

11 changes: 3 additions & 8 deletions src/Geonames.php
@@ -1,12 +1,7 @@
<?php

/**
* Implements a basic JSKOS concepts endpoint for Geonames.
*
* @package JSKOS
*/
$service = 'GeonamesService';

include realpath(__DIR__) . '/lib/GeonamesService.php';

\JSKOS\Server::runService(new GeonamesService());
include realpath(__DIR__) . "/lib/$service.php";
\JSKOS\Server::runService(new $service());

5 changes: 3 additions & 2 deletions src/OpenSKOS.php
@@ -1,6 +1,7 @@
<?php

include realpath(__DIR__) . '/lib/OpenSKOSService.php';
$service = 'OpenSKOSService';

\JSKOS\Server::runService(new OpenSKOSService());
include realpath(__DIR__) . "/lib/$service.php";
\JSKOS\Server::runService(new $service());

5 changes: 3 additions & 2 deletions src/Wikidata.php
@@ -1,6 +1,7 @@
<?php

include realpath(__DIR__) . '/lib/WikidataService.php';
$service = 'WikidataService';

\JSKOS\Server::runService(new WikidataService());
include realpath(__DIR__) . "/lib/$service.php";
\JSKOS\Server::runService(new $service());

37 changes: 18 additions & 19 deletions src/lib/BARTOCService.php
@@ -1,10 +1,13 @@
<?php

/**
* Implements a basic JSKOS concept schemes endpoint for BARTOC.
* @package JSKOS
*/

include realpath(__DIR__.'/../..') . '/vendor/autoload.php';
include_once realpath(__DIR__).'/RDFTrait.php';
include_once realpath(__DIR__).'/IDTrait.php';

use JSKOS\Service;
use JSKOS\ConceptScheme;
Expand All @@ -26,48 +29,44 @@ function getUris( $subject, $predicate, $pattern = null ) {
}

class BARTOCService extends Service {
use RDFTrait;
use IDTrait;

protected $supportedParameters = ['search'];
protected $supportedParameters = ['notation','search'];

public function query($query) {
if (isset($query['uri'])) {
return $this->lookupByURI($query['uri']);
$id = $this->idFromQuery($query, '/^http:\/\/bartoc\.org\/en\/node\/([0-9]+)$/', '/^[0-9]+$/');
if (isset($id)) {
return $this->lookupByURI("http://bartoc.org/en/node/$id");
} elseif (isset($query['search'])) {
return new Page( $this->search($query['search']) );
} else {
return;
}
}

public function lookupByURI($uri) {
if ( !preg_match('/^http:\/\/bartoc\.org\/en\/node\/([0-9]+)$/', $uri) ) {
return;
}

try {
$rdf = EasyRdf_Graph::newAndLoad($uri);
$bartoc = $rdf->resource($uri);
if (!$bartoc) {
return;
}
} catch( Exception $e ) {
return;
}
$rdf = $this->loadRDF($uri);
if (!$rdf) return;

$scheme = new ConceptScheme(['uri' => $uri]);

# TODO: use RDF mapping file from YAML instead

# url
foreach ( getUris($bartoc, 'foaf:page') as $url ) {
foreach ( getUris($rdf, 'foaf:page') as $url ) {
if (substr($url,0,26) != 'http://bartoc.org/en/node/') {
$scheme->url = $url;
}
}

# Wikidata item
foreach ( getUris($bartoc, 'dc:relation', '/^http:\/\/www\.wikidata\.org\/entity\/Q[0-9]+$/') as $uri ) {
foreach ( getUris($rdf, 'dc:relation', '/^http:\/\/www\.wikidata\.org\/entity\/Q[0-9]+$/') as $uri ) {
$scheme->identifier = [ $uri ];
}

# prefLabel and notation (FIXME: language is not always English)
foreach ($bartoc->allLiterals('schema:name') as $name) {
foreach ($rdf->allLiterals('schema:name') as $name) {
$name = $name->getValue();
if (preg_match('/^[A-Z]{2,5}$/', $name)) {
$scheme->notation = [ $name ];
Expand Down
66 changes: 20 additions & 46 deletions src/lib/GNDService.php
@@ -1,12 +1,14 @@
<?php

/**
* JSKOS-API Wrapper to GND via LOD access via URI.
* Implements a basic JSKOS concepts endpoint for GND.
*
* The wrapper converts GND RDF/XML to JSKOS.
*/

include_once realpath(__DIR__.'/../..') . '/vendor/autoload.php';
include_once realpath(__DIR__).'/JSKOSRDFMapping.php';
include_once realpath(__DIR__).'/RDFTrait.php';
include_once realpath(__DIR__).'/IDTrait.php';

use JSKOS\Service;
use JSKOS\Concept;
Expand All @@ -15,74 +17,46 @@

class GNDService extends Service {
use RDFTrait;
use IDtrait;

protected $supportedParameters = ['notation'];

static $mapping;

/**
* Initialize Mapping from YAML file.
*/
public function __construct() {
if (!static::$mapping) {
$file = __DIR__.'/GNDMapping.yaml';
static::$mapping = new JSKOSRDFMapping($file);
}
$this->loadMapping(__DIR__.'/GNDMapping.yaml');
parent::__construct();
}

public function query($query) {

$id = $this->idFromQuery($query, '/^http:\/\/d-nb\.info\/gnd\/([0-9X-]+)$/', '/^[0-9X-]+$/');

if (isset($query['uri'])) {
if (preg_match('/^http:\/\/d-nb\.info\/gnd\/([0-9X-]+)$/', $query['uri'], $match)) {
$id = $match[1];
}
}

if (isset($query['notation'])) {
if (preg_match('/^[0-9X-]+$/', $query['notation'])) {
$notation = strtoupper($query['notation']);
if (isset($id) and $id != $notation) {
unset($id);
} else {
$id = $notation;
}
}
}

if (!isset($id)) {
return null;
if (isset($id)) {
$uri = "http://d-nb.info/gnd/$id";
} else {
return;
}

$uri = "http://d-nb.info/gnd/$id";

$rdf = new EasyRdf_Graph();
try {
// TODO: use newAndLoad($uri);
$rdf->load("$uri/about/lds","rdfxml");
$gnd = $rdf->resource($uri);
} catch (Exception $e){
// not found or some error at DNB
return null;
}
# error_log("$uri");

if ($rdf->isEmpty() or !$gnd) {
return null;
}
$rdf = $this->loadRDF("$uri/about/lds", $uri, "rdfxml");
if (!$rdf) return;

# error_log($rdf->getGraph()->serialise('turtle'));

$jskos = new Concept(['uri'=>$uri, 'notation' => [$id]]);

foreach ( $gnd->allResources('owl:sameAs') as $id ) {
foreach ( $rdf->allResources('owl:sameAs') as $id ) {
$jskos->identifier[] = "$id";
}

foreach ( $gnd->typesAsResources() as $type ) {
foreach ( $rdf->typesAsResources() as $type ) {
$jskos->type[] = (string)$type;
}

#error_log($rdf->serialise('turtle'));

static::$mapping->rdf2jskos($gnd, $jskos);
$this->rdf2jskos($rdf, $jskos, 'de');

return $jskos;
}
Expand Down
55 changes: 16 additions & 39 deletions src/lib/GeonamesService.php
Expand Up @@ -5,8 +5,8 @@
*/

include_once realpath(__DIR__.'/../..') . '/vendor/autoload.php';
include_once realpath(__DIR__).'/JSKOSRDFMapping.php';
include_once realpath(__DIR__).'/RDFTrait.php';
include_once realpath(__DIR__).'/IDTrait.php';

use JSKOS\Service;
use JSKOS\Concept;
Expand All @@ -15,21 +15,15 @@

class GeonamesService extends Service {
use RDFTrait;
use IDtrait;

protected $supportedParameters = ['notation'];

static $mapping;

/**
* Initialize Mapping from YAML file.
*/
public function __construct() {
# TODO: move into trait:
# $this->setMapping(__DIR__.'/GeonamesMapping.yaml');
if (!static::$mapping) {
$file = __DIR__.'/GeonamesMapping.yaml';
static::$mapping = new JSKOSRDFMapping($file);
}
$this->loadMapping(__DIR__.'/GeonamesMapping.yaml');
parent::__construct();
}

Expand All @@ -38,41 +32,24 @@ public function __construct() {
*/
public function query($query) {

if (isset($query['uri'])) {
if (preg_match('/^http:\/\/sws\.geonames\.org\/([0-9]+)\/$/', $query['uri'], $match)) {
$id = $match[1];
}
}

if (isset($query['notation'])) {
if (preg_match('/^[0-9]+$/', $query['notation'])) {
$notation = strtoupper($query['notation']);
if (isset($id) and $id != $notation) {
unset($id);
} else {
$id = $notation;
}
}
}
$id = $this->idFromQuery($query, '/^http:\/\/sws\.geonames\.org\/([0-9]+)\/$/', '/^[0-9]+$/');
$jskos = null;

if (!isset($id)) {
return null;
}
// get concept by notation and/or uri
if (isset($id)) {
$uri = "http://sws.geonames.org/$id/";

$uri = "http://sws.geonames.org/$id/";

$rdf = $this->loadRDF($uri);
if (!$rdf) {
return;
}
$rdf = $this->loadRDF($uri);
if (!$rdf) return;

$jskos = new Concept([ 'uri' => $uri, 'notation' => [ $id ]]);
$jskos = new Concept([ 'uri' => $uri, 'notation' => [ $id ]]);

// TODO: get childrenFeatures if requested
// TODO: modified, created, license
// TODO: get childrenFeatures if requested
// TODO: modified, created, license

static::$mapping->rdf2jskos($rdf, $jskos);

$this->rdf2jskos($rdf, $jskos);
}

return $jskos;
}
}

0 comments on commit 5746c5a

Please sign in to comment.