使用 php sparql 仅获取存在于 wikidata 上的项目的标签
Get label only for items that exists on wikidata with php sparql
我正在使用 PHP 和 RDF4J Workbench 服务进行个人项目,我有一个包含 3 个上下文的存储库,一个用于机器人类别,一个用于属于这些类别的机器人,以及另一个我将 owl:sameAs 属性 存储到初始数据的 wikidata 标识符中。
问题是用户可以插入机器人,而新插入的机器人不会链接到维基数据标识符,而是具有唯一的 URI,只需删除额外的 space 并连接用户的名称即可已插入。现在,当我查询我需要的数据时,我希望显示所有这些机器人,但是如果我的查询如下:
PREFIX : <http://example.com#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX wd: <http://www.wikidata.org/entity/>
SELECT ?name ?image ?releaseDate ?skills ?manufacturer ?mass
WHERE {
{ SELECT ?robot ?image ?releaseDate ?skills ?manufacturer ?mass
WHERE {
<http://example.com#EducationalRobots> a :Category; :hasRobots [:idRobot ?x] .
?x owl:sameAs ?robot .
?x :hasImage ?image .
?x :releaseDate ?releaseDate .
?x :hasSkills ?skills .
?x :manufacturer ?manufacturer.
?x :hasMass ?mass
}
}
SERVICE <https://query.wikidata.org/sparql> {
?robot rdfs:label ?name .
FILTER(lang(?name)="en")
}
}
最后我只会得到我插入的初始语句,那些确实链接了维基数据标识符的语句。所以我想也许在后端有所不同,使用 if 语句,我可以在其中询问所请求类别中的项目是否具有标识符,如果有,则执行特定查询,即上面的查询,否则只需替换查询和检索本地 URI。但是有没有一种方法可以直接在查询中执行此操作,这样代码看起来会更简洁?比如SELECT IF语句等等。我不熟悉高级查询实践,如有任何建议,我将不胜感激。
图表片段:
要求:
function getDetails(id) {
//console.log(id);
let url = "query.php";
$.ajax({
type: "POST",
url:url,
data: {"id": id },
success: function (response){
let final = JSON.parse(response);
console.log(final.results);
$('#tableId').find('tbody')
.empty();
final.results.bindings.forEach((item, index)=>{
console.log(item);
if (item.name) {
s = item.name;
id = s.split(" ").join("");
$('#tableId').find('tbody')
.append($(`<tr id=${id} class="border_bottom">`)
.append($('<td class="mt-3 text-center">')
.append(`<p class='mt-4'><b><i>${item.name.value} </i></b></p><br><button class='btn btn-danger' id='${item.name.value}' onclick="deleteMovie(this.id, '${id}')">Delete</button></div>`)
)
.append($('<td class="text-center">')
.append($('<img height="150" width="250">')
.attr('src', 'images/'+item.image.value)
)
)
.append($('<td>')
.append($('<small> Manufacturer: </small><p>'+ item.manufacturer.value+ '</p><small>Release date: </small><p>' + item.releaseDate.value +'</p><small>Skills: </small><p>'+item.skills.value +'</p><small> Mass: </small><p>'+item.mass.value + ' kg</p>'))
)
)
}
})
}
});
}
在后端,我正在使用 EasyRdf\Sparql\Client:
<?php
$id = $_POST['id'];
require 'vendor/autoload.php';
$address = "http://localhost:8080/rdf4j-server/repositories/graph?query=";
$query = "PREFIX : <http://example.com#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX wd: <http://www.wikidata.org/entity/>
SELECT ?name ?image ?releaseDate ?skills ?manufacturer ?mass
WHERE { { SELECT ?robot ?image ?releaseDate ?skills ?manufacturer ?mass WHERE {<".$id."> a :Category; :hasRobots [:idRobot ?x] . ?x owl:sameAs ?robot . ?x :hasImage ?image . ?x :releaseDate ?releaseDate . ?x :hasSkills ?skills . ?x :manufacturer ?manufacturer. ?x :hasMass ?mass} } SERVICE <https://query.wikidata.org/sparql> { ?robot rdfs:label ?name . FILTER(lang(?name)=\"en\") } }";
$qry = urlencode($query);
// print $qry;
$client = new EasyRdf\Http\Client($address.$qry);
$client->setHeaders("Accept","application/sparql-results+json");
$resultJSON = $client->request()->getBody();
// print $resultJSON;
$finalResult = new EasyRdf\Sparql\Result($rezultatJSON,"application/sparql-results+json");
// print $finalResult;
return $finalResult;
谢谢!
最终查询:
PREFIX : <http://example.com#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX wd: <http://www.wikidata.org/entity/>
SELECT ?name ?image ?releaseDate ?skills ?manufacturer ?mass
FROM :robots FROM :samenessgraph FROM :categories WHERE {
:EducationalRobots a :Category; :hasRobots [:idRobot ?x] .
?x :hasImage ?image .
?x :releaseDate ?releaseDate .
?x :hasSkills ?skills .
?x :manufacturer ?manufacturer .
?x :hasMass ?mass .
OPTIONAL { ?x owl:sameAs ?robot }
BIND(COALESCE(?robot, "unknown") as ?input) OPTIONAL {
SERVICE <https://query.wikidata.org/sparql> {
?input rdfs:label ?wdName .
FILTER(lang(?name)="en")
}
}
OPTIONAL { ?x rdfs:label ?label }
BIND(COALESCE(?wdName, ?label, strafter(str(?x), str(:))) as ?name) }
如评论中所述。
我正在使用 PHP 和 RDF4J Workbench 服务进行个人项目,我有一个包含 3 个上下文的存储库,一个用于机器人类别,一个用于属于这些类别的机器人,以及另一个我将 owl:sameAs 属性 存储到初始数据的 wikidata 标识符中。
问题是用户可以插入机器人,而新插入的机器人不会链接到维基数据标识符,而是具有唯一的 URI,只需删除额外的 space 并连接用户的名称即可已插入。现在,当我查询我需要的数据时,我希望显示所有这些机器人,但是如果我的查询如下:
PREFIX : <http://example.com#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX wd: <http://www.wikidata.org/entity/>
SELECT ?name ?image ?releaseDate ?skills ?manufacturer ?mass
WHERE {
{ SELECT ?robot ?image ?releaseDate ?skills ?manufacturer ?mass
WHERE {
<http://example.com#EducationalRobots> a :Category; :hasRobots [:idRobot ?x] .
?x owl:sameAs ?robot .
?x :hasImage ?image .
?x :releaseDate ?releaseDate .
?x :hasSkills ?skills .
?x :manufacturer ?manufacturer.
?x :hasMass ?mass
}
}
SERVICE <https://query.wikidata.org/sparql> {
?robot rdfs:label ?name .
FILTER(lang(?name)="en")
}
}
最后我只会得到我插入的初始语句,那些确实链接了维基数据标识符的语句。所以我想也许在后端有所不同,使用 if 语句,我可以在其中询问所请求类别中的项目是否具有标识符,如果有,则执行特定查询,即上面的查询,否则只需替换查询和检索本地 URI。但是有没有一种方法可以直接在查询中执行此操作,这样代码看起来会更简洁?比如SELECT IF语句等等。我不熟悉高级查询实践,如有任何建议,我将不胜感激。
图表片段:
要求:
function getDetails(id) {
//console.log(id);
let url = "query.php";
$.ajax({
type: "POST",
url:url,
data: {"id": id },
success: function (response){
let final = JSON.parse(response);
console.log(final.results);
$('#tableId').find('tbody')
.empty();
final.results.bindings.forEach((item, index)=>{
console.log(item);
if (item.name) {
s = item.name;
id = s.split(" ").join("");
$('#tableId').find('tbody')
.append($(`<tr id=${id} class="border_bottom">`)
.append($('<td class="mt-3 text-center">')
.append(`<p class='mt-4'><b><i>${item.name.value} </i></b></p><br><button class='btn btn-danger' id='${item.name.value}' onclick="deleteMovie(this.id, '${id}')">Delete</button></div>`)
)
.append($('<td class="text-center">')
.append($('<img height="150" width="250">')
.attr('src', 'images/'+item.image.value)
)
)
.append($('<td>')
.append($('<small> Manufacturer: </small><p>'+ item.manufacturer.value+ '</p><small>Release date: </small><p>' + item.releaseDate.value +'</p><small>Skills: </small><p>'+item.skills.value +'</p><small> Mass: </small><p>'+item.mass.value + ' kg</p>'))
)
)
}
})
}
});
}
在后端,我正在使用 EasyRdf\Sparql\Client:
<?php
$id = $_POST['id'];
require 'vendor/autoload.php';
$address = "http://localhost:8080/rdf4j-server/repositories/graph?query=";
$query = "PREFIX : <http://example.com#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX wd: <http://www.wikidata.org/entity/>
SELECT ?name ?image ?releaseDate ?skills ?manufacturer ?mass
WHERE { { SELECT ?robot ?image ?releaseDate ?skills ?manufacturer ?mass WHERE {<".$id."> a :Category; :hasRobots [:idRobot ?x] . ?x owl:sameAs ?robot . ?x :hasImage ?image . ?x :releaseDate ?releaseDate . ?x :hasSkills ?skills . ?x :manufacturer ?manufacturer. ?x :hasMass ?mass} } SERVICE <https://query.wikidata.org/sparql> { ?robot rdfs:label ?name . FILTER(lang(?name)=\"en\") } }";
$qry = urlencode($query);
// print $qry;
$client = new EasyRdf\Http\Client($address.$qry);
$client->setHeaders("Accept","application/sparql-results+json");
$resultJSON = $client->request()->getBody();
// print $resultJSON;
$finalResult = new EasyRdf\Sparql\Result($rezultatJSON,"application/sparql-results+json");
// print $finalResult;
return $finalResult;
谢谢!
最终查询:
PREFIX : <http://example.com#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX wd: <http://www.wikidata.org/entity/>
SELECT ?name ?image ?releaseDate ?skills ?manufacturer ?mass
FROM :robots FROM :samenessgraph FROM :categories WHERE {
:EducationalRobots a :Category; :hasRobots [:idRobot ?x] .
?x :hasImage ?image .
?x :releaseDate ?releaseDate .
?x :hasSkills ?skills .
?x :manufacturer ?manufacturer .
?x :hasMass ?mass .
OPTIONAL { ?x owl:sameAs ?robot }
BIND(COALESCE(?robot, "unknown") as ?input) OPTIONAL {
SERVICE <https://query.wikidata.org/sparql> {
?input rdfs:label ?wdName .
FILTER(lang(?name)="en")
}
}
OPTIONAL { ?x rdfs:label ?label }
BIND(COALESCE(?wdName, ?label, strafter(str(?x), str(:))) as ?name) }
如评论中所述。