|
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(); |
|
58 } |
|
59 elseif (strpos($lang, 'fr') === 0 && !isset($names['fr'])) { |
|
60 $names['fr'] = $nameLit->getvalue(); |
|
61 } |
|
62 elseif (strpos($lang, 'en') === 0 && !isset($names['en'])) { |
|
63 $names['en'] = $nameLit->getvalue(); |
|
64 } |
|
65 } |
|
66 |
|
67 return (isset($names['fr'])) ? $names['fr'] : ((isset($names['en'])) ? $names['en'] : ((isset($names['']))? $names[''] : null)); |
|
68 |
|
69 } |
|
70 |
|
71 /** |
|
72 * Check viaf id format |
|
73 */ |
|
74 private function checkViafIdFormat($viafid) { |
|
75 return ctype_digit($viafid); |
|
76 } |
|
77 |
|
78 /** |
|
79 * Get name from Viaf id |
|
80 * @param string $id The id to resolve. Can be an url starting with http://viaf.org/viaf/ |
|
81 * @return a string with the name |
|
82 */ |
|
83 public function getName($id) { |
|
84 $viafid = $id; |
|
85 if(strpos($id, config('corpusparole.viaf_base_url')) === 0) { |
|
86 $viafid = substr($id, strlen(config('corpusparole.viaf_base_url'))); |
|
87 } |
|
88 $viafid = rtrim($viafid, '/'); |
|
89 |
|
90 if(!$this->checkViafIdFormat($viafid)) { |
|
91 throw new ViafResolverException("ViafId not in correct format", 400); |
|
92 } |
|
93 |
|
94 $that = $this; |
|
95 |
|
96 return Cache::remember("viaf:$viafid", config('corpusparole.viaf_cache_expiration'), function() use ($that, $viafid) { |
|
97 |
|
98 return $that->queryName($viafid); |
|
99 |
|
100 }); |
|
101 |
|
102 } |
|
103 |
|
104 /** |
|
105 * Get a list of names from an array of viaf ids. |
|
106 * @param array $ids The array of ids to resolve. |
|
107 * Each id can be an url starting with http://viaf.org/viaf/ |
|
108 */ |
|
109 public function getNames(array $ids) { |
|
110 return array_combine($ids, array_map([$this,'getName'], $ids)); |
|
111 } |
|
112 |
|
113 |
|
114 } |