author | ymh <ymh.work@gmail.com> |
Tue, 27 Sep 2016 23:43:29 +0200 | |
changeset 304 | 20071981ba2a |
parent 158 | 366509ae2f37 |
child 506 | 8a5bb4b48b85 |
permissions | -rw-r--r-- |
133 | 1 |
<?php |
2 |
namespace CorpusParole\Services; |
|
3 |
||
4 |
use Cache; |
|
5 |
use CorpusParole\Services\BnfResolverInterface; |
|
6 |
||
7 |
class BnfResolver implements BnfResolverInterface { |
|
8 |
||
9 |
public function __construct($sparqlClient) { |
|
10 |
$this->sparqlClient = $sparqlClient; |
|
11 |
} |
|
12 |
||
13 |
private function checkBnfId($id) { |
|
14 |
$bnfid = $id; |
|
15 |
||
16 |
if(strpos($id, config('corpusparole.bnf_ark_base_url')) === 0) { |
|
17 |
$bnfid = config('corpusparole.bnf_base_url').substr($id, strlen(config('corpusparole.bnf_ark_base_url'))); |
|
18 |
} |
|
19 |
elseif(strpos($id, config('corpusparole.bnf_base_url')) !== 0) { |
|
20 |
$bnfid = config('corpusparole.bnf_base_url').$id; |
|
21 |
} |
|
22 |
$bnfid = rtrim($bnfid, '/'); |
|
23 |
if(preg_match("/^".preg_quote(config('corpusparole.bnf_base_url'),"/")."ark\:\/12148\/[[:alnum:]]/", $bnfid) !== 1) { |
|
24 |
throw new BnfResolverException("the provided id \"$id\" is not a BNF id"); |
|
25 |
} |
|
26 |
return $bnfid; |
|
27 |
} |
|
28 |
||
29 |
/** |
|
30 |
* Get label from BNF id |
|
31 |
* @param string $id The id to resolve. Can be an url starting with http://data.bnf.fr/ or http://ark.bnf.fr/ |
|
32 |
* @return a string with the name |
|
33 |
*/ |
|
34 |
public function getLabel($id) { |
|
35 |
$res = $this->getlabels([$id,]); |
|
36 |
assert(array_key_exists($id,$res), "the result must contains $id"); |
|
37 |
return $res[$id]; |
|
38 |
} |
|
39 |
||
40 |
/** |
|
41 |
* Get a list of names from an array of viaf ids. |
|
42 |
* @param array $ids The array of ids to resolve. |
|
43 |
* Each id can be an url starting with http://data.bnf.fr/ or http://ark.bnf.fr/ |
|
44 |
*/ |
|
45 |
public function getLabels(array $ids) { |
|
46 |
||
47 |
if(count($ids) > config('corpusparole.bnf_max_ids')) { |
|
158
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
48 |
|
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
49 |
return array_reduce( |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
50 |
array_map([$this, 'getLabels'], array_chunk($ids, config('corpusparole.bnf_max_ids'))), |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
51 |
'array_merge', |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
52 |
[] |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
53 |
); |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
54 |
//throw new BnfResolverException("Too manys ids provided"); |
133 | 55 |
} |
56 |
||
57 |
$bnfids = array_map([$this, 'checkBnfId'], $ids); |
|
58 |
$bnfidsMap = array_combine($bnfids, $ids); |
|
59 |
||
60 |
$results = []; |
|
61 |
$missingBnfids = []; |
|
62 |
||
63 |
foreach ($bnfidsMap as $bnfid => $bnfidSource) { |
|
64 |
$cachedValue = Cache::get("bnf:$bnfid"); |
|
65 |
if(is_null($cachedValue)) { |
|
66 |
array_push($missingBnfids, $bnfid); |
|
158
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
67 |
} elseif (mb_strlen($cachedValue)>0) { |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
68 |
$results[$bnfidSource] = $cachedValue; |
133 | 69 |
} else { |
158
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
70 |
$results[$bnfidSource] = null; |
133 | 71 |
} |
72 |
} |
|
73 |
||
74 |
if(count($missingBnfids) == 0) { |
|
75 |
return $results; |
|
76 |
} |
|
77 |
||
78 |
$query = "SELECT ?s ?o WHERE {"; |
|
79 |
foreach ($missingBnfids as $index => $bid) { |
|
80 |
if($index > 0) { |
|
81 |
$query .= " UNION "; |
|
82 |
} |
|
83 |
$query .= "{ <$bid> <http://www.w3.org/2004/02/skos/core#prefLabel> ?o. ?s <http://www.w3.org/2004/02/skos/core#prefLabel> ?o. FILTER(?s = <$bid> && lang(?o) = \"fr\")}"; |
|
84 |
} |
|
85 |
$query .= "}"; |
|
86 |
||
87 |
$docs = $this->sparqlClient->query($query); |
|
88 |
||
89 |
$resultsRaw = []; |
|
90 |
||
91 |
foreach ($docs as $doc) { |
|
92 |
$bnfid = $doc->s->getUri(); |
|
93 |
$bnflabel = $doc->o; |
|
94 |
||
95 |
$value = $bnflabel->getValue(); |
|
96 |
||
97 |
if(!empty($value)) { |
|
98 |
$resultsRaw[$bnfid] = $bnflabel; |
|
99 |
} |
|
100 |
} |
|
101 |
||
102 |
foreach ($missingBnfids as $bnfid) { |
|
103 |
$bnfidSource = $bnfidsMap[$bnfid]; |
|
104 |
$missingValue = (array_key_exists($bnfid,$resultsRaw) && $resultsRaw[$bnfid])?mb_strtolower($resultsRaw[$bnfid]->getValue()):""; |
|
105 |
if (mb_strlen($missingValue)>0) { |
|
106 |
Cache::put("bnf:$bnfid", $missingValue, config('corpusparole.bnf_cache_expiration')); |
|
107 |
$results[$bnfidSource] = $missingValue; |
|
108 |
} |
|
109 |
else { |
|
158
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
110 |
Cache::put("bnf:$bnfid", "", config('corpusparole.bnf_cache_expiration')); |
133 | 111 |
$results[$bnfidSource] = null; |
112 |
} |
|
113 |
} |
|
114 |
return $results; |
|
115 |
} |
|
116 |
||
117 |
} |