Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Major refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtich committed Mar 10, 2016
1 parent 8229e29 commit bd47bfb
Show file tree
Hide file tree
Showing 17 changed files with 532 additions and 343 deletions.
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: vendor/bin/heroku-php-apache2 wrappers/
web: vendor/bin/heroku-php-apache2 src/
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ First install dependencies with [composer](https://getcomposer.org/):

You can directly serve wrappers via PHP for testing (don't use for production!):

$ php -S localhost:8080 -t wrappers
$ php -S localhost:8080 -t src

The examples can also be tried online at
And accesed via <http://localhost:8080>.

The examples can also be tried online at
<https://jskos-php-examples.herokuapp.com>

(may be out of date).

# Installation
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
},
"scripts": {
"test": [
"vendor/bin/phpunit tests"
"vendor/bin/phpunit tests --tap"
],
"debug-test": [
"vendor/bin/phpunit tests --debug"
],
"travis-test": [
"vendor/bin/phpunit tests --coverage-clover build/logs/clover.xml"
Expand Down
30 changes: 30 additions & 0 deletions src/GND.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* Implements a basic JSKOS concepts endpoint for GND.
*
* This wrapper converts GND RDF/XML to JSKOS.
*
* @package JSKOS
*/

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;
}
*/

6 changes: 6 additions & 0 deletions src/OpenSKOS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

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

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

6 changes: 6 additions & 0 deletions src/Wikidata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

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

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

93 changes: 93 additions & 0 deletions src/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>JSKOS-PHP Examples</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
</head>
<body>
<a href="https://github.com/gbv/jskos-php-examples"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/e7bbb0521b397edbd5fe43e7f760759336b5e05f/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677265656e5f3030373230302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_green_007200.png"></a>
<div class="container">

<h1>JSKOS-PHP Examples</h1>
<p>
This page provides sample applications and services that make use of
<a href="http://gbv.github.io/jskos">JSKOS data format for Knowledge Organization Systems</a>.
All examples are written in PHP, based on
<a href="https://packagist.org/packages/gbv/jskos">JSKOS PHP library</a>, and available
as Open Source <a href="https://github.com/gbv/jskos-php-examples">at GitHub</a>. Please use
<a href="https://github.com/gbv/jskos-php-examples/issues">the issue tracker</a> for feedback!
</p>
<h2>Terminology Services</h2>
<p>
The following web services wrap existing terminology databases to provide
access via <a href="https://gbv.github.io/jskos-api">JSKOS-API</a>.
JSKOS-API is far from being finally specified, so this is a preview.
</p>
<?php

include '../vendor/autoload.php';

use Symfony\Component\Yaml\Yaml;
$services = Yaml::parse(file_get_contents('services.yaml'));

foreach ($services as $name => $service) {
$source = "src/lib/${name}Service.php";
?>
<h3><?= $name ?><small>
&nbsp;
<a href='https://github.com/gbv/jskos-php-examples/blob/master/<?= $source ?>'>
<i class="fa fa-github"></i> source</a>
</small>
</h2>
<?php if ($service['about'] or $service['url']) { ?>
<p>
<span class="glyphicon glyphicon-arrow-right"></span>
<?php if ($service['url']) echo "<a href='".$service['url']."'>"; ?>
<?= htmlspecialchars($service['about'] ? $service['about'] : $service['url']); ?>
<?php if ($service['url']) echo "</a>"; ?>
</p>
<?php }
$queryExamples= $service['examples'];
if ($queryExamples) {
echo "<table class='table table-condensed'>";
echo "<thead><tr><th>sample queries to <a href='$name.php'>JSKOS-API endpoint</a></th></tr></thead>";
echo "<tr>";
foreach ($queryExamples as $query) {
$query = http_build_query($query);
echo "<td>";
echo "<a href='".htmlentities("$name.php?$query")."'>";
echo "?".htmlentities(urldecode($query));
echo "</a></td></tr>";
}
echo "</table>";
} else {
echo "<a href='$name.php'>service endpoint</a>";
}
echo "</p>";
}
?>

<hr>
<a href="https://travis-ci.org/gbv/jskos-php-examples">
<img src="https://travis-ci.org/gbv/jskos-php-examples.svg?branch=master">
</a>
<a href="https://coveralls.io/r/gbv/jskos-php-examples">
<img src="https://coveralls.io/repos/github/gbv/jskos-php-examples/badge.svg?branch=master">
</a>

</div>
</body>
</html>

177 changes: 177 additions & 0 deletions src/lib/GNDService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

/**
* JSKOS-API Wrapper to GND via LOD access via URI.
*/

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

use JSKOS\Service;
use JSKOS\Concept;
use JSKOS\Page;
use JSKOS\Error;

EasyRdf_Namespace::set('gnd','http://d-nb.info/standards/elementset/gnd#');

class GNDService extends Service {

static $GNDOntology2JSKOS = [
'broader' => [
'type' => 'URI',
'properties' => [
'gnd:broaderTermPartitive'
]
],
'related' => [
'type' => 'URI',
'properties' => [
'gnd:accordingWork',
'gnd:acquaintanceshipOrFriendship',
]
],
'relatedPlace' => [
'type' => 'URI',
'properties' => [
'gnd:associatedPlace',
'gnd:characteristicPlace',
'gnd:otherPlace',
'gnd:spatialAreaOfActivity',
'gnd:relatedPlaceOrGeographicName',
'gnd:place'
],
],
'prefLabel' => [
'type' => 'literal',
'unique' => true,
'properties' => [
'gnd:preferredName',
'gnd:preferredNameForThePerson',
'gnd:preferredNameForTheConferenceOrEvent',
'gnd:preferredNameForThePlaceOrGeographicName',
'gnd:preferredNameForTheFamily',
'gnd:preferredNameForTheSubjectHeading',
'gnd:preferredNameForTheCorporateBody',
'gnd:preferredNameForTheWork',
],
],
'scopeNote' => [
'type' => 'literal',
'properties' => [
'gnd:biographicalOrHistoricalInformation'
]
],
'definition' => [
'type' => 'literal',
'properties' => ['gnd:definition']
],
'relatedDate' => [
'type' => 'datatype',
'properties' => [
'gnd:associatedDate',
'gnd:dateOfDiscovery',
'gnd:periodOfActivity',
]
]

];


protected $supportedParameters = ['notation'];

public function query($query) {

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;
}

$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;
}

if ($rdf->isEmpty() or !$gnd) {
return null;
}

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

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

foreach ( $gnd->typesAsResources() as $type ) {
$jskos->type[] = (string)$type;
}
#error_log($rdf->serialise('turtle'));


foreach (static::$GNDOntology2JSKOS as $property => $mapping)
{
$type = $mapping['type'];

foreach ( $mapping['properties'] as $gndProperty ) {

if ($type == 'URI')
{
foreach ( $gnd->allResources($gndProperty) as $resource ) {
if (!isset($jskos->$property)) {
$jskos->$property = [];
}
array_push( $jskos->$property, ['uri' => (string)$resource] );
}

} elseif ($type == 'literal')
{
foreach ( $gnd->allLiterals($gndProperty) as $literal ) {
$value = (string)$literal;
$language = $literal->getLang() ? $literal->getLang() : "de";

$languageMap = isset($jskos->$property) ? $jskos->$property : [];

if (isset($mapping['unique'])) {
$languageMap[$language] = $value;
} else {
$languageMap[$language][] = $value;
}

$jskos->$property = $languageMap;
}
} elseif ($type == 'datatype') {
foreach ( $gnd->allLiterals($gndProperty) as $literal ) {
$value = (string)$literal;
if (!isset($jskos->$property)) {
$jskos->$property = [];
}
array_push( $jskos->$property, $value );
}
}
}
}

return $jskos;
}
}
Loading

0 comments on commit bd47bfb

Please sign in to comment.