:- module(enrichment, [ enrich_item/2, uri_label/2, metadata/2, object_annotations/2, annotations_user/2 ]). :- use_module(library(semweb/rdf_label)). :- use_module(library(sandbox)). :- use_module(library(http/http_open)). :- use_module(library(http/url_cache)). :- use_module(library(http/http_path)). :- use_module(library(http/http_ssl_plugin)). :- use_module(library(semweb/rdf_db)). :- rdf_register_prefix(edm, 'http://www.europeana.eu/schemas/edm/'). :- rdf_register_prefix(oa, 'http://www.w3.org/ns/oa#'). :- rdf_register_prefix(ann_ui, 'http://semanticweb.cs.vu.nl/annotate/ui/'). %% metadata(+Uri, -Metadata) % % Get all properties and subjects attached to a Uri metadata(Uri, Metadata) :- findall(property_pair{%predicate=Predicate, predicate_label:PredicateLabel, %object=Object, object_label:ObjectLabel}, ( rdf(Uri, Predicate, Object), rdf_display_label(Predicate, _, PredicateLabel), rdf_display_label(Object, _, ObjectLabel) ), Properties), get_title(Uri, DisplayTitle), Metadata = metadata{display_title:DisplayTitle, properties:Properties}. %% object_annotations(+Uri, -Metadata) % % Get all properties and subjects attached to a Uri object_annotations(Uri, Annotations) :- findall(annotation{ field:FieldLabel, body:NiceBody}, ( get_annotation(Uri, AnnotationBody, AnnotationHash), process_annotation(AnnotationBody, NiceBody), rdf(AnnotationHash, ann_ui:annotationField, FieldUri), rdf_display_label(FieldUri, _, FieldLabel) ), FoundAnnotations), get_title(Uri, DisplayTitle), Annotations = annotations{display_title:DisplayTitle, annotations:FoundAnnotations}. get_annotation(Uri, AnnotationBody, AnnotationHash) :- rdf(AnnotationHash, oa:hasTarget, Uri), rdf(AnnotationHash, oa:hasBody, AnnotationBody). process_annotation(literal(Annotation), Annotation) :- !. process_annotation(Annotation, Label) :- rdf_display_label(Annotation, _, Label). % object_annotations(+UserUri, -ObjectUris) % % Get all objects the user has annotated annotations_user(UserUri, ObjectUris) :- setof(Object, AnnotationHash^Selector^ ( rdf_has(AnnotationHash, oa:annotatedBy, UserUri), rdf_has(AnnotationHash, oa:hasTarget, Selector), rdf_has(Selector, oa:hasSource, Object) ), ObjectUris). %% uri_label(Uri, Label) % % Get the label of an Uri uri_label(Uri, Label) :- rdf_display_label(Uri, _, Label). %% enrich_item(+Item, -EnrichedItem) % % Add a url of the thumbnail, a link to the resultpage and the % title to an item. enrich_item(Uri, EnrichedItem) :- thumbnail_url(Uri, ThumbnailUrl), get_title(Uri, Title), concat('annotate_image.html?uri=', Uri, PartLink), http_absolute_location(root(PartLink), Link, []), EnrichedItem = _{uri:Uri,thumb:ThumbnailUrl,link:Link,title:Title}, debug(enrich_item, 'Uri: ~p ThumbnailUrl: ~p Link: ~p Title: ~p', [Uri,ThumbnailUrl,Link,Title]). %% thumbnail_url(+Uri, -ThumbUrl) % % * Check if image is present, if so, return request url. % * If the image is not present, check if present at url. % * If not present in cache, database and at url, send image stub. thumbnail_url(Uri, ThumbUrl) :- check_cache_shown(Uri, Image), !, thumb_link(Image, ThumbUrl). thumbnail_url(Uri, ThumbUrl) :- catch(check_server_shown(Uri, Image), _, fail), !, thumb_link(Image, ThumbUrl). thumbnail_url(Uri, ThumbUrl) :- check_cache_view(Uri, Image), !, thumb_link(Image, ThumbUrl). thumbnail_url(Uri, ThumbUrl) :- catch(check_server_view(Uri, Image), _, fail), !, thumb_link(Image, ThumbUrl). thumbnail_url(_, Stub) :- http_absolute_location(img('stub.png'), Stub, []). check_cache_shown(Uri, Image) :- rdf(Aggregation, edm:aggregatedCHO, Uri), rdf(Aggregation, edm:isShownBy, Image), url_cached(Image, file(_)). check_cache_view(Uri, Image) :- rdf(Aggregation, edm:aggregatedCHO, Uri), rdf(Aggregation, edm:hasView, Image0), check_if_local(Image0, Image), url_cached(Image, file(_)). check_server_shown(Uri, Image) :- rdf(Aggregation, edm:aggregatedCHO, Uri), rdf(Aggregation, edm:isShownBy, Image), https_header_response(Image, Status), Status == 200. check_server_view(Uri, Image) :- rdf(Aggregation, edm:aggregatedCHO, Uri), rdf(Aggregation, edm:hasView, Image0), check_if_local(Image0, Image), https_header_response(Image, Status), Status == 200. thumb_link(Image, ThumbUrl) :- concat('cache/fit?uri=', Image, RequestUrl), http_absolute_location(root(RequestUrl), ThumbUrl, []). check_if_local(Image, ImageUrl) :- not(concat('http', _, Image)), http_absolute_uri(img(Image), ImageUrl). check_if_local(ImageUrl, ImageUrl). %% https_header_response(+URL, -Status) % % Return the response header of input URL. https_header_response(URL, Status) :- http_open(URL, In, [method(head), status_code(Status), cert_verify_hook(ssl_verify) ]), close(In). :- public ssl_verify/5. %% ssl_verify(+SSL, +ProblemCert, +AllCerts, +FirstCert, +Error) % % Currently we accept all certificates. ssl_verify(_SSL, _ProblemCertificate, _AllCertificates, _FirstCertificate, _Error). %% get_title(+Uri, -Title) % % Returns a title or the last part of the URI if no title is found. get_title(Uri, Title) :- rdf(Uri, dc:title, literal(lang(_,Title))), !. get_title(Uri, UriLabel) :- iri_xml_namespace(Uri, _, UriLabel), !. get_title(Uri, Uri). sandbox:safe_primitive(rdf_label:rdf_display_label(_,_,_)). sandbox:safe_primitive(enrichment:enrich_item(_,_)). sandbox:safe_primitive(sgml:iri_xml_namespace(_,_,_)).