|
Line 0
Link Here
|
|
|
1 |
package Koha::Explore::Wikidata; |
| 2 |
|
| 3 |
use C4::Biblio; |
| 4 |
|
| 5 |
=head1 NAME |
| 6 |
|
| 7 |
Koha::Explore::Wikidata |
| 8 |
|
| 9 |
|
| 10 |
=head1 SYNOPSIS |
| 11 |
|
| 12 |
use Koha::Explore::Wikidata; |
| 13 |
|
| 14 |
=head1 FUNCTIONS |
| 15 |
|
| 16 |
=cut |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
use Koha::Explore::Sparql; |
| 20 |
|
| 21 |
my $endpoint = "https://query.wikidata.org/bigdata/namespace/wdq/sparql"; |
| 22 |
|
| 23 |
=head2 GetDataForAuthor |
| 24 |
Execute sparql query on Sparql endpoint. |
| 25 |
=cut |
| 26 |
sub GetDataForAuthor { |
| 27 |
my ($wdid) = @_; |
| 28 |
my $id = _get_wd_id ($wdid); |
| 29 |
|
| 30 |
my $sparql = ' |
| 31 |
SELECT DISTINCT ?p ?o |
| 32 |
WHERE { |
| 33 |
{ |
| 34 |
wd:'.$id.' ?p ?o . |
| 35 |
FILTER(LANG(?o) = "en") . |
| 36 |
} |
| 37 |
UNION |
| 38 |
{ |
| 39 |
wd:'.$id.' ?p ?o . |
| 40 |
?o rdfs:label ?label . |
| 41 |
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } |
| 42 |
} |
| 43 |
} |
| 44 |
'; |
| 45 |
my %data = Koha::Explore::Sparql::GetPredicatesAndObjectsForSubject ( |
| 46 |
undef,$endpoint,$sparql |
| 47 |
); |
| 48 |
_llog(\%data,"author data"); |
| 49 |
return \%data; |
| 50 |
} |
| 51 |
|
| 52 |
=head2 GetDataForBiblio |
| 53 |
Execute sparql query on Sparql endpoint. |
| 54 |
=cut |
| 55 |
|
| 56 |
sub GetDataForBiblio { |
| 57 |
my ($ark) = @_; |
| 58 |
|
| 59 |
my $ark_part = _get_clean_part($ark); |
| 60 |
|
| 61 |
my $sparql = ' |
| 62 |
SELECT ?wdwork |
| 63 |
WHERE { ?wdwork wdt:P268 ?idbnf FILTER CONTAINS(?idbnf, "'.$ark_part.'") . } |
| 64 |
'; |
| 65 |
my $iterator = Koha::Explore::Sparql::Execute ($sparql,$endpoint); |
| 66 |
return $sparql if (! defined $iterator) ; |
| 67 |
my $firstrow = $iterator->next; |
| 68 |
my $wdid = $firstrow->{wdwork}[1]; |
| 69 |
$wdid =~ /.*\/(.*)/; |
| 70 |
my $id = $1; |
| 71 |
|
| 72 |
$sparql = ' |
| 73 |
SELECT DISTINCT ?p ?o |
| 74 |
WHERE { |
| 75 |
{ |
| 76 |
wd:'.$id.' ?p ?o . |
| 77 |
FILTER(LANG(?o) = "en") . |
| 78 |
} |
| 79 |
UNION |
| 80 |
{ |
| 81 |
wd:'.$id.' ?p ?o . |
| 82 |
?o rdfs:label ?label . |
| 83 |
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } |
| 84 |
} |
| 85 |
} |
| 86 |
'; |
| 87 |
my %data = Koha::Explore::Sparql::GetPredicatesAndObjectsForSubject ( |
| 88 |
"wd:".$wdid,$endpoint,$sparql |
| 89 |
); |
| 90 |
|
| 91 |
_llog(\%data,"biblio data"); |
| 92 |
return \%data; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
#sub GetDataForAuthor { |
| 97 |
# my ($wdworkid) = @_; |
| 98 |
# my $workid = substr($wdworkid,30); |
| 99 |
# _llog($workid, "AUTHIDWD"); |
| 100 |
# |
| 101 |
# my $sparql = ' |
| 102 |
# SELECT ?wdauthor ?birthdate ?deathdate ?image ?workperiod ?name WHERE { |
| 103 |
# wd:'.$workid.' wdt:P50 ?wdauthor. |
| 104 |
# OPTIONAL { ?wdauthor wdt:P18 ?image. } |
| 105 |
# OPTIONAL { ?wdauthor wdt:P2031 ?workperiod. } |
| 106 |
# optional { ?wdauthor wdt:P570 ?deathdate} . |
| 107 |
# OPTIONAL { ?wdauthor wdt:P1477 ?name. } |
| 108 |
# OPTIONAL { ?wdauthor wdt:P569 ?birthdate. } |
| 109 |
# } |
| 110 |
# '; |
| 111 |
# _llog ($sparql,"AUTHOR"); |
| 112 |
#return $sparql; |
| 113 |
# # Autres properties exploitables: |
| 114 |
# # pseudonym P742 |
| 115 |
# # occupation P106 |
| 116 |
# # genre P136 |
| 117 |
# # influenced by P737 |
| 118 |
# # work period (start) P2031 |
| 119 |
# # notable work P800 |
| 120 |
# |
| 121 |
# my $iterator = _execute_sparql_query ($sparql); |
| 122 |
# return $sparql if (!defined $iterator) ; |
| 123 |
# my $firstrow = $iterator->next; |
| 124 |
# ## _llog ($firstrow); |
| 125 |
# my $dataFromWD = { |
| 126 |
# 'wdauthor' => $firstrow->{wdauthor}[1], |
| 127 |
# 'name' => $firstrow->{name}[0], |
| 128 |
# 'birthdate' => $firstrow->{birthdate}[0], |
| 129 |
# 'deathdate' => $firstrow->{deathdate}[0], |
| 130 |
# 'image' => $firstrow->{image}[1], |
| 131 |
# 'workperiod' => $firstrow->{workperiod}[0], |
| 132 |
# }; |
| 133 |
# _llog ($dataFromWD); |
| 134 |
# return $dataFromWD; |
| 135 |
#} |
| 136 |
# |
| 137 |
#=head2 GetDataForBiblio |
| 138 |
# Execute sparql query on Sparql endpoint. |
| 139 |
#=cut |
| 140 |
# |
| 141 |
#sub GetDataForBiblio { |
| 142 |
# my ($ark) = @_; |
| 143 |
# |
| 144 |
# my $ark_part = substr($ark,37); |
| 145 |
# my $offset = length($ark_part) -6; |
| 146 |
# $ark_part = substr ($ark_part, 0, $offset) if ($ark =~ m/#about/); |
| 147 |
# return $ark_part; |
| 148 |
# |
| 149 |
# my $sparql = ' |
| 150 |
# SELECT ?wdwork ?wdworkDescription ?wdauthor ?wdauthorLabel ?countryoriginLabel ?serieLabel ?pubdate ?genreLabel |
| 151 |
# WHERE |
| 152 |
# { |
| 153 |
# ?wdwork wdt:P268 ?idbnf |
| 154 |
# FILTER CONTAINS(?idbnf, "'. $ark_part .'"). |
| 155 |
# ?wdwork wdt:P50 ?wdauthor . |
| 156 |
# OPTIONAL { ?wdwork wdt:P495 ?countryorigin .} |
| 157 |
# OPTIONAL { ?wdwork wdt:P577 ?pubdate .} |
| 158 |
# OPTIONAL { ?wdwork wdt:P179 ?serie .} |
| 159 |
# OPTIONAL { ?wdwork wdt:P136 ?genre .} |
| 160 |
# SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". } |
| 161 |
# } |
| 162 |
# |
| 163 |
# '; |
| 164 |
#return $sparql; |
| 165 |
# |
| 166 |
## Non gérés: |
| 167 |
## si plusieurs résultats |
| 168 |
## si plusieurs valeurs (ex: genre) |
| 169 |
## la langue du service de label |
| 170 |
# |
| 171 |
# my $iterator = _execute_sparql_query ($sparql); |
| 172 |
# return $sparql if (! defined $iterator) ; |
| 173 |
# my $firstrow = $iterator->next; |
| 174 |
# ## _llog ($firstrow); |
| 175 |
# my $dataFromWD = { |
| 176 |
# 'wdwork' => $firstrow->{wdwork}[1], |
| 177 |
# 'wdworkDescription' => $firstrow->{wdworkDescription}[0], |
| 178 |
# 'wdauthor' => $firstrow->{wdauthor}[1], |
| 179 |
# 'wdauthorLabel' => $firstrow->{wdauthorLabel}[0], |
| 180 |
# 'countryoriginLabel' => $firstrow->{countryoriginLabel}[0], |
| 181 |
# 'serieLabel' => $firstrow->{serieLabel}[0], |
| 182 |
# 'pubdate' => $firstrow->{pubDate}[0], |
| 183 |
# 'genreLabel' => $firstrow->{genreLabel}[0], |
| 184 |
# }; |
| 185 |
# _llog ($dataFromWD); |
| 186 |
# return $dataFromWD; |
| 187 |
#} |
| 188 |
|
| 189 |
sub _llog { |
| 190 |
my ($var,$a,$b) = @_; |
| 191 |
use Data::Dumper; |
| 192 |
$a = "" if !defined $a; |
| 193 |
$b = "" if !defined $b; |
| 194 |
warn Dumper($a . '##############################################'); |
| 195 |
warn Dumper($var); |
| 196 |
warn Dumper($b . '##############################################'); |
| 197 |
} |
| 198 |
|
| 199 |
sub _get_clean_part { |
| 200 |
my ($ark) = @_; |
| 201 |
|
| 202 |
$ark =~ /.*\/(.*)/; |
| 203 |
my $ark_part = $1; |
| 204 |
$ark_part = substr ($ark_part, 2, length($ark_part)); |
| 205 |
|
| 206 |
my $offset = length($ark_part) -6; |
| 207 |
$ark_part = substr ($ark_part, 0, $offset) if ($ark =~ m/#about/); |
| 208 |
|
| 209 |
return $ark_part; |
| 210 |
} |
| 211 |
|
| 212 |
sub _get_wd_id { |
| 213 |
my ($wdid) = @_; |
| 214 |
$wdid =~ /.*\/(.*)/; |
| 215 |
return $1; |
| 216 |
} |
| 217 |
|
| 218 |
=head1 AUTHOR |
| 219 |
Claire Hernandez, E<lt>claire.hernandez@biblibre.comE<gt> |
| 220 |
=cut |
| 221 |
|
| 222 |
1; |
| 223 |
|
| 224 |
__END__ |