accurator/commit

Added enrichment prolog code to accurator, resolves #144

authorChris Dijkshoorn
Mon Nov 9 14:24:13 2015 +0100
committerChris Dijkshoorn
Mon Nov 9 14:24:13 2015 +0100
commit39efd1d7c4c1aaa526dfce6d79079079bc1b2104
tree5a18fa90d2aede36eebe6ea4481e9fb07bf2feda
parent1e64176979f41690c70c50aa40d23b3a1faf06c5
Diff style: patch stat
diff --git a/api/accurator.pl b/api/accurator.pl
index 1d29b56..d3d8a6e 100644
--- a/api/accurator.pl
+++ b/api/accurator.pl
@@ -4,16 +4,15 @@
 */
 
 :- use_module(library(accurator/accurator_user)).
-:- use_module(library(accurator/annotate_page)).
 :- use_module(library(accurator/domain)).
 :- use_module(library(accurator/expertise)).
 :- use_module(library(accurator/recommendation/strategy_random)).
 :- use_module(library(accurator/recommendation/strategy_expertise)).
 :- use_module(library(accurator/ui_elements)).
+:- use_module(library(accurator/annotation_statistics)).
 :- use_module(library(accurator/subset_selection)).
 :- use_module(library(accurator/concept_scheme_selection)).
 :- use_module(api(cluster_search)).
-:- use_module(library(cluster_search_ui/metadata)).
 :- use_module(library(semweb/rdf_db)).
 :- use_module(library(http/http_dispatch)).
 :- use_module(library(http/http_server_files)).
diff --git a/lib/accurator/annotation_statistics.pl b/lib/accurator/annotation_statistics.pl
new file mode 100644
index 0000000..8ce2b71
--- /dev/null
+++ b/lib/accurator/annotation_statistics.pl
@@ -0,0 +1,43 @@
+:- module(annotation_statistics, [
+			  object_annotations/2,
+			  annotations_user/2
+		  ]).
+
+:- use_module(library(accurator/ui_elements)).
+:- use_module(library(semweb/rdf_db)).
+:- use_module(library(semweb/rdf_label)).
+
+%%	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).
diff --git a/lib/accurator/ui_elements.pl b/lib/accurator/ui_elements.pl
index 165fce6..a7fb5dd 100644
--- a/lib/accurator/ui_elements.pl
+++ b/lib/accurator/ui_elements.pl
@@ -1,9 +1,35 @@
-:- module(ui_elements, [get_elements/3]).
+:- module(ui_elements, [
+			  get_title/2,
+			  uri_label/2,
+			  get_elements/3,
+			  metadata/2,
+			  metadata_thumbnail/2
+		  ]).
 
 /** <module> Accurator UI elements
 */
 
 :- use_module(library(semweb/rdf_db)).
+:- use_module(library(semweb/rdf_label)).
+:- 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)).
+
+%%	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).
+
+%%	uri_label(Uri, Label)
+%
+%	Get the label of an Uri
+uri_label(Uri, Label) :-
+    rdf_display_label(Uri, _, Label).
 
 %%	get_elements(+Type, -Dic, +Options)
 %
@@ -118,3 +144,124 @@ get_selector_labels(Selector, Locale, LiteralDict) :-
 				dict_pairs(LabelDict, elements, [label-Literal, id-Id])),
 			LiteralArray),
 	dict_pairs(LiteralDict, elements, LiteralArray).
+
+%%	metadata(+Uri, -Metadata)
+%
+%	Get all properties and subjects attached to a Uri
+metadata(Uri, Metadata) :-
+    findall(property_pair{
+				predicate_label:PredicateLabel,
+				object_label:ObjectLabel},
+	    (	rdf(Uri, Predicate, Object),
+			rdf_display_label(Predicate, _, PredicateLabel),
+			rdf_display_label(Object, _, ObjectLabel)
+	    ),
+	    Properties),
+	get_title(Uri, DisplayTitle),
+	image_url(Uri, ImageLink),
+	Metadata = metadata{title:DisplayTitle,
+						image:ImageLink,
+						properties:Properties}.
+
+%%	image_url(+Uri, -ImageUrl) is det.
+%
+%	* Check if image is present, if so, return request url.
+%	* If the image is not present, check if present at url.
+%	isShownBy triple.
+%	* If not present in cache, database and at
+%	server, send image stub.
+image_url(Uri, ImageUrl) :-
+	check_cache_shown(Uri, Image), !,
+    image_link(Image, ImageUrl).
+image_url(Uri, ImageUrl) :-
+	check_server_shown(Uri, Image), !,
+    image_link(Image, ImageUrl).
+image_url(Uri, ImageUrl) :-
+	check_cache_view(Uri, Image), !,
+    image_link(Image, ImageUrl).
+image_url(Uri, ImageUrl) :-
+	check_server_view(Uri, Image), !,
+    image_link(Image, ImageUrl).
+image_url(_, Stub) :- http_absolute_location(img('stub_vertical.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.
+check_if_local(Image, ImageUrl) :-
+	not(concat('http', _, Image)),
+	http_absolute_uri(img(Image), ImageUrl).
+check_if_local(ImageUrl, ImageUrl).
+
+image_link(Image, ThumbUrl) :-
+	concat('cache/original?uri=', Image, RequestUrl),
+    http_absolute_location(root(RequestUrl), ThumbUrl, []).
+
+%%	metadata_thumbnail(+Uri, -Metadata)
+%
+%	Add a url of the thumbnail and the title to an item.
+metadata_thumbnail(Uri, EnrichedItem) :-
+    thumbnail_url(Uri, ThumbnailUrl),
+    get_title(Uri, Title),
+    EnrichedItem = _{uri:Uri,thumb:ThumbnailUrl,title:Title},
+    debug(enrich_item, 'Uri: ~p ThumbnailUrl: ~p Title: ~p',
+	  [Uri,ThumbnailUrl,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, []).
+
+thumb_link(Image, ThumbUrl) :-
+	concat('cache/fit?uri=', Image, RequestUrl),
+    http_absolute_location(root(RequestUrl), ThumbUrl, []).
+
+%%	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).
diff --git a/web/img/stub.png b/web/img/stub.png
new file mode 100644
index 0000000..300350b
Binary files /dev/null and b/web/img/stub.png differ
diff --git a/web/img/stub_vertical.png b/web/img/stub_vertical.png
new file mode 100644
index 0000000..e96067b
Binary files /dev/null and b/web/img/stub_vertical.png differ