PageRenderTime 23ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/SeRQL/server.pl

http://github.com/motools/gnarql
Perl | 303 lines | 261 code | 42 blank | 0 comment | 6 complexity | 2fa3058b8f089924ec1da8a2502fe158 MD5 | raw file
  1. /* This file is part of ClioPatria.
  2. Author:
  3. HTTP: http://e-culture.multimedian.nl/
  4. GITWEB: http://gollem.science.uva.nl/git/ClioPatria.git
  5. GIT: git://gollem.science.uva.nl/home/git/ClioPatria.git
  6. GIT: http://gollem.science.uva.nl/home/git/ClioPatria.git
  7. Copyright: 2007, E-Culture/MultimediaN
  8. ClioPatria is free software: you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation, either version 2 of the License, or
  11. (at your option) any later version.
  12. ClioPatria is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with ClioPatria. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. :- module(server,
  20. [ server/0,
  21. restrict_server/0, % Update security
  22. daemon/0,
  23. canonical_host/1 % -HostName
  24. ]).
  25. % First setup search paths
  26. :- load_files(paths, [silent(true)]).
  27. % Load PlDoc
  28. :- load_files(pldoc, [silent(true)]).
  29. % Namespaces are needed everywhere
  30. :- load_files(namespaces, [ silent(true)]).
  31. % Load these into module user, so we can use them from the toplevel
  32. % for interactive exploration of the triples.
  33. :- load_files([ user:library('semweb/rdf_db'),
  34. user:library('semweb/rdf_turtle')
  35. ],
  36. [ silent(true) ]).
  37. % NOTE: namespaces (rdf_register_ns/2) are defined in namespaces.pl
  38. % loaded in parms.pl, and then loaded again to overrule old declarations
  39. % in getty(t20getty)
  40. :- load_files([ serql(load),
  41. serql(serql_log),
  42. serql(user_db), % Deny actions
  43. library(lists),
  44. version,
  45. util(state),
  46. util(user_profile),
  47. util(interface_util),
  48. rdfbrowsing(load),
  49. util(rdf_util),
  50. util(fuzzy),
  51. search_keyword(http_search),
  52. api(http_tree),
  53. util(http_util),
  54. util(http_files),
  55. util(html_util),
  56. library('http/html_write'),
  57. library('http/http_dispatch'),
  58. library('http/http_authenticate'),
  59. library('semweb/rdf_persistency'),
  60. library('semweb/rdf_litindex'),
  61. library('semweb/rdf_http_plugin'),
  62. library('semweb/rdf_zlib_plugin'),
  63. library(socket),
  64. library(settings),
  65. % user:serql(debug),
  66. library('http/http_error'),
  67. % user:artchive_base(artchive),
  68. fbrowse(fbrowse),
  69. relsearch(relation_search),
  70. rdfbrowsing(browser),
  71. suggest(suggest),
  72. annotate(annotate),
  73. annotate(picturepoint),
  74. myart(myart),
  75. api(load),
  76. maintain(backtrace)
  77. ],
  78. [ silent(true),
  79. if(not_loaded)
  80. ]).
  81. :- load_files([ application(load)
  82. ],
  83. [ silent(true),
  84. if(not_loaded)
  85. ]).
  86. :- multifile
  87. mime:mime_extension/2.
  88. mime:mime_extension(class, application/'octet-stream').
  89. mime:mime_extension(kml, application/'vnd.google-earth.kml+xml').
  90. mime:mime_extension(kmz, application/'vnd.google-earth.kmz').
  91. mime:mime_extension(js, text/javascript).
  92. mime:mime_extension(vbs, text/vbscript).
  93. mime:mime_extension(ico, image/'x-ico').
  94. mime:mime_extension(jar, application/'java-archive').
  95. mime:mime_extension(swf, application/'x-shockwave-flash').
  96. mime:mime_extension(xspf, application/'xspf+xml').
  97. mime:mime_extension(mp3, audio/mpeg).
  98. mime:mime_extension(svg, image/'svg+xml').
  99. %% server
  100. %
  101. % Start the Semantic Web based E-culture server. Settings such as
  102. % the port to use are loaded from setting/2.
  103. %
  104. % Starting the server loads the persistent database from the
  105. % directory =|SeRQL-store|=. To start from scratch, delete this
  106. % directory and load the initial state as described in README.
  107. server :-
  108. init_state_options,
  109. restrict_server,
  110. serql_server([ base_ontologies([]),
  111. after_load(server:(%attach_picture_point_trigger,
  112. stem_literals))
  113. ]),
  114. rdf_persistency(anonymous_user_settings, false),
  115. rdf_check_changed_sources(list),
  116. compile_iface_owl_class.
  117. %% restrict_server
  118. %
  119. % Restrict some operations on the server for all users except
  120. % admin. We do not want someone to delete our work by accident!
  121. restrict_server :-
  122. retractall(user_db:denied(_)), % HACK
  123. % /servlets/unloadSource
  124. deny_all_users(write(_, unload(_))),
  125. % /servlets/uploadData
  126. % /servlets/uploadFile
  127. % /servlets/uploadURL
  128. % deny_all_users(write(_, load(_))),
  129. deny_all_users(write(_, remove_statements(-,_,_))),
  130. deny_all_users(write(_, remove_statements(_,-,_))).
  131. %% daemon
  132. %
  133. % Start as a deamon. See the script =daemon.sh=.
  134. daemon :-
  135. write_state([state=initializing]),
  136. rdf_set_literal_index_option([verbose(false)]),
  137. catch(server, E, server_failed(E)),
  138. write_state([state=running]),
  139. on_signal(hup, _, stop_daemon),
  140. on_signal(term, _, stop_daemon),
  141. on_signal(int, _, stop_daemon),
  142. on_signal(segv, _, default),
  143. on_signal(bus, _, default),
  144. on_signal(fpe, _, default),
  145. loop.
  146. server_failed(E) :-
  147. print_message(error, E),
  148. write_state([state=error]),
  149. halt(1).
  150. loop :-
  151. sleep(10000),
  152. loop.
  153. %% stop_daemon(+Signal)
  154. %
  155. % We received Signal. Add message to log-file and die.
  156. stop_daemon(Signal) :-
  157. format(user_error, 'Stopping daemon on signal ~w ...~n', [Signal]),
  158. serql_log('~q.~n', [signal(Signal)]),
  159. write_state([state=stopped]),
  160. halt.
  161. %% canonical_host(-Host)
  162. %
  163. % Canonical name of the host in *lowercase*.
  164. :- dynamic
  165. canonical_host_cache/1.
  166. canonical_host(Host) :-
  167. canonical_host_cache(Host0), !,
  168. Host = Host0.
  169. canonical_host(Host) :-
  170. ( getenv('HOST', Host0) % allow redefining
  171. -> true
  172. ; gethostname(Host0)
  173. ),
  174. downcase_atom(Host0, Host),
  175. assert(canonical_host_cache(Host)).
  176. %% write_state(+State) is det.
  177. %
  178. % Write current state to a file. Name of the file is in environment
  179. % variable =SERQL_STATE_FILE=.
  180. %
  181. % @param State List of Name=Value pairs
  182. write_state(State) :-
  183. getenv('SERQL_STATE_FILE', StateFile),
  184. StateFile \== none, !,
  185. open(StateFile, write, Out),
  186. forall(member(Name=Value, State),
  187. format(Out, '~w: ~w~n', [Name, Value])),
  188. close(Out).
  189. write_state(_).
  190. /*******************************
  191. * TRIPLE20 *
  192. *******************************/
  193. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  194. Add default Triple20 commandline options. --nobase simply starts
  195. Triple20 without doing a scan for base ontologies.
  196. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  197. :- multifile
  198. triple20:option/1.
  199. triple20:option('--nobase').
  200. triple20:option('--nochecksaved').
  201. /*******************************
  202. * REPLY *
  203. *******************************/
  204. :- multifile
  205. serql_http:sidebar_menu/2.
  206. :- http_handler('/', main_page, [priority(10)]).
  207. :- http_handler('/favicon.ico',
  208. http_reply_file(www('img/favicon.ico'), []), []).
  209. /*
  210. serql_http:sidebar_menu(-, 'e-culture demo').
  211. serql_http:sidebar_menu(Link, -) :- menu_link('/facet', '/facet', Link).
  212. serql_http:sidebar_menu(Link, -) :- menu_link('/search', 'search', Link).
  213. serql_http:sidebar_menu(Link, -) :- menu_link('/path', 'Relation Search', Link).
  214. serql_http:sidebar_menu(Link, -) :- menu_link('/search/graph', 'Display search graphs', Link).
  215. serql_http:sidebar_menu(Link, -) :- menu_link('/annotate', 'Annotate URL', Link).
  216. serql_http:sidebar_menu(-, 'Deprecated').
  217. serql_http:sidebar_menu(Link, -) :- menu_link('/search/old', 'Basic Search', Link).
  218. serql_http:sidebar_menu(Link, -) :- menu_link('/search/advanced', 'Advanced Search', Link).
  219. serql_http:sidebar_menu(Link, -) :- menu_link('/search/dev', 'Developer\'s Search', Link). %'
  220. serql_http:sidebar_menu(Link, -) :- menu_link('/facet/old', '/facet 0.1', Link).
  221. serql_http:sidebar_menu(Link, -) :- menu_link('/localview?active=http%3a%2f%2fe-culture.multimedian.nl%2fns%2fgetty%2fulan%23associated_with', '/test localview', Link).
  222. serql_http:sidebar_menu(-, 'Maintenance').
  223. serql_http:sidebar_menu(Link, -) :- menu_link('/pldoc/', 'Search source', Link).
  224. serql_http:sidebar_menu(Link, -) :- menu_link('/qa_index', 'RDF QA', Link).
  225. serql_http:sidebar_menu(Link, -) :-
  226. mk_image_uri('MNlogoGifFormat.gif', Image),
  227. Link =
  228. a([target='_top' , href='http://e-culture.multimedian.nl/'],img([
  229. src=Image,
  230. class=logo, alt=logo, style='margin-top: 1em; border-style:none; width: 140px'])).
  231. */
  232. %% menu_link(+Location, +Label, -Link)
  233. %
  234. % Create a link for the server main menu from Location and Label.
  235. menu_link(Path, Label, Link) :-
  236. make_uri(FacetLink, Path, []),
  237. Link = a([target='_top', href=FacetLink], Label).
  238. %% main_page(+Request)
  239. %
  240. % Emit frameset for the E-culture demo.
  241. main_page(_Request) :-
  242. make_uri(Sidebar,'/sidebar.html',[]),
  243. make_uri(Welcome,'/welcome.html',[]),
  244. phrase(html([ head(title('MultimediaN E-Culture Server')),
  245. frameset([cols('200,*')],
  246. [ frame([ src(Sidebar),
  247. name(sidebar)
  248. ]),
  249. frame([ src(Welcome),
  250. name(main)
  251. ])
  252. ])
  253. ]), HTML),
  254. format('Content-type: text/html~n~n'),
  255. print_html(HTML).