| Conditions | 6 |
| Paths | 38 |
| Total Lines | 30 |
| Code Lines | 22 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 5 | public function resolve(int $timeout): ?EasyRdf\Resource |
||
| 6 | { |
||
| 7 | $graph = new EasyRdf\Graph($this->uri); |
||
| 8 | // guess the concept scheme based on the URI |
||
| 9 | if (preg_match('|(http://id.loc.gov/[^/]+/[^/]+)/.*|', $this->uri, $matches)) { |
||
| 10 | $graph->addResource($this->uri, 'skos:inScheme', $matches[1]); |
||
| 11 | } |
||
| 12 | |||
| 13 | try { |
||
| 14 | $opts = array('http' => array('method' => 'HEAD', |
||
| 15 | 'user_agent' => 'Skosmos', |
||
| 16 | 'timeout' => $timeout)); |
||
| 17 | $context = stream_context_create($opts); |
||
| 18 | $fd = fopen($this->uri, 'rb', false, $context); |
||
| 19 | if ($fd === false) { |
||
| 20 | return null; |
||
| 21 | } |
||
| 22 | $headers = stream_get_meta_data($fd)['wrapper_data']; |
||
| 23 | foreach ($headers as $header) { |
||
| 24 | if (strpos(strtolower($header), 'x-preflabel:') === 0) { |
||
| 25 | $elems = explode(' ', $header, 2); |
||
| 26 | $prefLabel = $elems[1]; |
||
| 27 | $graph->addLiteral($this->uri, 'skos:prefLabel', $prefLabel, 'en'); |
||
| 28 | } |
||
| 29 | } |
||
| 30 | fclose($fd); |
||
| 31 | return $graph->resource($this->uri); |
||
| 32 | } catch (Exception $e) { |
||
| 33 | $this->model->getLogger()->info("LOC resolution failed for <{$this->uri}>: $e"); |
||
| 34 | return null; |
||
| 35 | } |
||
| 38 |