Warm tip: This article is reproduced from serverfault.com, please click

javascript-仅使用 php sparql 获取维基数据上存在的项目的标签

(javascript - Get label only for items that exists on wikidata with php sparql)

发布于 2021-05-02 06:57:21

我正在使用 PHP 和 RDF4J 工作台服务进行个人项目,我有一个包含 3 个上下文的存储库,一个用于机器人类别,一个用于属于这些类别的机器人,另一个用于将owl:sameAs属性存储到 wikidata初始数据的标识符。

问题是用户可以插入机器人,而新插入的机器人不会链接到维基数据标识符,而是通过删除额外的空间并连接用户插入的名称而产生唯一的 URI。现在,当我查询我需要的数据时,我希望显示所有这些机器人,但如果我的查询如下所示:

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") 
        } 
}

最后,我只会得到我插入的初始语句,那些确实链接了 wikidata 标识符的语句。所以我想可能会在后端有所不同,使用 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;

谢谢 !

Questioner
theospace
Viewed
0
theospace 2021-05-03 00:35:05

最后查询:

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

如评论中所述。