29
30:- module(api_export, []). 31:- use_module(library(http/http_dispatch)). 32:- use_module(library(http/http_parameters)). 33:- use_module(library(semweb/rdf_turtle_write)). 34:- use_module(library(semweb/rdf_db)). 35:- use_module(library(semweb/rdf_schema)). 36:- use_module(rdfql(rdf_io)). 37:- use_module(rdfql(rdf_turtle_io)). 38:- use_module(user(user_db)). 39
40:- http_handler(api(export_graph), export_graph, []). 41:- http_handler(api(export_graph_schema), export_graph_schema, []).
55export_graph(Request) :-
56 http_parameters(Request,
57 [ graph(Graph),
58 format(Format),
59 mimetype(Mime)
60 ],
61 [ attribute_declarations(http_param)
62 ]
63 ),
64 authorized(read(default, download(Graph))),
65 send_graph(Graph, Format, Mime).
66
67send_graph(Graph, Format, default) :- !,
68 default_mime_type(Format, MimeType),
69 send_graph(Graph, Format, MimeType).
70send_graph(Graph, Format, MimeType) :- !,
71 format('Transfer-Encoding: chunked~n'),
72 format('Content-type: ~w; charset=UTF8~n~n', [MimeType]),
73 send_graph(Graph, Format).
74
75send_graph(Graph, turtle) :- !,
76 rdf_save_turtle(stream(current_output),
77 [ graph(Graph),
78 base(Graph)
79 ]).
80send_graph(Graph, canonical_turtle) :- !,
81 rdf_save_canonical_turtle(stream(current_output), [graph(Graph)]).
82send_graph(Graph, rdfxml) :- !,
83 rdf_save(stream(current_output), [graph(Graph)]).
84
85default_mime_type(turtle, text/turtle).
86default_mime_type(canonical_turtle, text/turtle).
87default_mime_type(rdfxml, application/'rdf+xml').
96export_graph_schema(Request) :-
97 http_parameters(Request,
98 [ graph(Graph),
99 format(Format),
100 mimetype(Mime)
101 ],
102 [ attribute_declarations(http_param)
103 ]
104 ),
105 authorized(read(default, download(Graph))),
106 rdf_graph_schema(Graph, Triples),
107 ( Mime == default
108 -> default_mime_type(Format, MimeType)
109 ; MimeType = Mime
110 ),
111 write_graph(Triples,
112 [ serialization(Format),
113 mimetype(MimeType)
114 ]).
119http_param(graph,
120 [ description('Name of the graph')]).
121http_param(format,
122 [ oneof([turtle,
123 canonical_turtle,
124 rdfxml
125 ]),
126 default(turtle),
127 description('Output serialization')
128 ]).
129http_param(mimetype,
130 [ default(default),
131 description('MIME-type to use. If "default", it depends on format')
132 ])
Export data from the server
*/