WDQSResource   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 26
dl 0
loc 47
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 43 2
1
<?php
2
3
class WDQSResource extends RemoteResource
4
{
5
    public const WDQS_ENDPOINT = "https://query.wikidata.org/sparql";
6
7
    public function resolve(int $timeout): ?EasyRdf\Resource
8
    {
9
        try {
10
            // unregister the legacy "json" format as it causes problems with CONSTRUCT requests
11
            EasyRdf\Format::unregister('json');
12
            // change the timeout setting for external requests
13
            $httpclient = EasyRdf\Http::getDefaultHttpClient();
14
            $httpclient->setConfig(array('timeout' => $timeout, 'useragent' => 'Skosmos'));
15
            $httpclient->setHeaders('Accept', 'text/turtle');
16
            EasyRdf\Http::setDefaultHttpClient($httpclient);
17
18
            $uri = $this->uri;
19
            $query = <<<EOQ
20
PREFIX wd:     <http://www.wikidata.org/entity/>
21
PREFIX rdfs:   <http://www.w3.org/2000/01/rdf-schema#>
22
PREFIX schema: <http://schema.org/>
23
24
CONSTRUCT {
25
  <$uri> rdfs:label ?label .
26
  ?link schema:about <$uri> ;
27
    a ?linktype ;
28
    schema:isPartOf ?whole ;
29
    schema:inLanguage ?lang .
30
}
31
WHERE
32
{
33
  { <$uri> rdfs:label ?label }
34
  UNION
35
  {
36
    ?link schema:about <$uri> ;
37
      a ?linktype ;
38
      schema:isPartOf ?whole ;
39
      schema:inLanguage ?lang .
40
  }
41
}
42
EOQ;
43
44
            $client = new EasyRdf\Sparql\Client(self::WDQS_ENDPOINT);
45
            $graph = $client->query($query);
46
            return $graph->resource($this->uri);
47
        } catch (Exception $e) {
48
            $this->model->getLogger()->info("WDQS resolution failed for <{$this->uri}>: $e");
49
            return null;
50
        }
51
    }
52
}
53