author | ymh <ymh.work@gmail.com> |
Tue, 20 Mar 2018 15:02:40 +0100 | |
changeset 573 | 25f3d28f51b2 |
parent 524 | 85e8382852e7 |
permissions | -rw-r--r-- |
23 | 1 |
<?php |
2 |
namespace CorpusParole\Services; |
|
3 |
||
4 |
use CorpusParole\Services\ViafResolverInterface; |
|
5 |
||
6 |
use Cache; |
|
7 |
use Config; |
|
8 |
use EasyRdf\Graph; |
|
9 |
use GuzzleHttp\Client; |
|
10 |
use GuzzleHttp\Exception\RequestException; |
|
11 |
use GuzzleHttp\Exception\ClientException; |
|
12 |
use GuzzleHttp\Exception\GuzzleException; |
|
13 |
use Illuminate\Contracts\Cache\Repository as CacheRepository; |
|
14 |
||
15 |
class ViafResolver implements ViafResolverInterface { |
|
16 |
||
17 |
private $client = null; |
|
18 |
||
19 |
public function __construct(Client $httpClient) { |
|
20 |
$this->client = $httpClient; |
|
21 |
} |
|
22 |
||
23 |
/** |
|
24 |
* make the viaf query. |
|
25 |
*/ |
|
26 |
public function queryName($id) { |
|
27 |
||
28 |
$url = config('corpusparole.viaf_base_url').$id; |
|
29 |
||
30 |
try { |
|
31 |
$response = $this->client->get($url."/", ['headers'=>['Accept' => 'application/rdf+xml'],]); |
|
32 |
} |
|
33 |
catch(ClientException $e) { |
|
34 |
if($e->getResponse()->getStatusCode() === 404) { |
|
35 |
return null; |
|
36 |
} else { |
|
37 |
throw new ViafResolverException( |
|
38 |
$e->getMessage(), |
|
39 |
$e->getResponse()->getStatusCode(), |
|
40 |
$e |
|
41 |
); |
|
42 |
} |
|
43 |
} |
|
44 |
catch(RequestException $e) { |
|
45 |
throw new ViafResolverException( |
|
46 |
$e->getMessage(), |
|
47 |
$e->hasResponse()?$e->getResponse()->getStatusCode():500, |
|
48 |
$e |
|
49 |
); |
|
50 |
} |
|
51 |
||
52 |
$graph = new Graph($url, $response->getBody()); |
|
53 |
$names = []; |
|
54 |
foreach ($graph->allLiterals("<$url>", "schema:name") as $nameLit) { |
|
55 |
$lang = $nameLit->getLang(); |
|
56 |
if(!$lang && !isset($names[''])) { |
|
57 |
$names[''] = $nameLit->getvalue(); |
|
524
85e8382852e7
Correct viaf resolver when there is no fr or en names
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
58 |
} elseif (strpos($lang, 'fr') === 0 && !isset($names['fr'])) { |
23 | 59 |
$names['fr'] = $nameLit->getvalue(); |
524
85e8382852e7
Correct viaf resolver when there is no fr or en names
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
60 |
} elseif (strpos($lang, 'en') === 0 && !isset($names['en'])) { |
23 | 61 |
$names['en'] = $nameLit->getvalue(); |
524
85e8382852e7
Correct viaf resolver when there is no fr or en names
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
62 |
} else { |
85e8382852e7
Correct viaf resolver when there is no fr or en names
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
63 |
$names[$lang] = $nameLit->getvalue(); |
23 | 64 |
} |
65 |
} |
|
66 |
||
524
85e8382852e7
Correct viaf resolver when there is no fr or en names
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
67 |
$firstVal = reset($names); |
85e8382852e7
Correct viaf resolver when there is no fr or en names
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
68 |
if($firstVal === false) { |
85e8382852e7
Correct viaf resolver when there is no fr or en names
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
69 |
$firstVal = null; |
85e8382852e7
Correct viaf resolver when there is no fr or en names
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
70 |
} |
85e8382852e7
Correct viaf resolver when there is no fr or en names
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
71 |
return (isset($names['fr'])) ? $names['fr'] : ((isset($names['en'])) ? $names['en'] : $firstVal); |
23 | 72 |
|
73 |
} |
|
74 |
||
75 |
/** |
|
76 |
* Check viaf id format |
|
77 |
*/ |
|
78 |
private function checkViafIdFormat($viafid) { |
|
79 |
return ctype_digit($viafid); |
|
80 |
} |
|
81 |
||
82 |
/** |
|
83 |
* Get name from Viaf id |
|
84 |
* @param string $id The id to resolve. Can be an url starting with http://viaf.org/viaf/ |
|
85 |
* @return a string with the name |
|
86 |
*/ |
|
87 |
public function getName($id) { |
|
88 |
$viafid = $id; |
|
89 |
if(strpos($id, config('corpusparole.viaf_base_url')) === 0) { |
|
90 |
$viafid = substr($id, strlen(config('corpusparole.viaf_base_url'))); |
|
91 |
} |
|
92 |
$viafid = rtrim($viafid, '/'); |
|
93 |
||
94 |
if(!$this->checkViafIdFormat($viafid)) { |
|
95 |
throw new ViafResolverException("ViafId not in correct format", 400); |
|
96 |
} |
|
97 |
||
98 |
$that = $this; |
|
99 |
||
100 |
return Cache::remember("viaf:$viafid", config('corpusparole.viaf_cache_expiration'), function() use ($that, $viafid) { |
|
101 |
||
102 |
return $that->queryName($viafid); |
|
103 |
||
104 |
}); |
|
105 |
||
106 |
} |
|
107 |
||
108 |
/** |
|
109 |
* Get a list of names from an array of viaf ids. |
|
110 |
* @param array $ids The array of ids to resolve. |
|
111 |
* Each id can be an url starting with http://viaf.org/viaf/ |
|
112 |
*/ |
|
113 |
public function getNames(array $ids) { |
|
133 | 114 |
|
115 |
if(count($ids) > config('corpusparole.viaf_max_ids')) { |
|
116 |
throw new ViafResolverException("Too manys ids provided"); |
|
117 |
} |
|
118 |
||
23 | 119 |
return array_combine($ids, array_map([$this,'getName'], $ids)); |
120 |
} |
|
121 |
||
122 |
||
123 |
} |