View source with raw comments or as raw
    1/*  Part of ClioPatria SeRQL and SPARQL server
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2010-2018, University of Amsterdam,
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(rdfql_queries,
   37          [ query_form//1,              % +Options
   38            store_recall//2,            % +Type, +ColsStore-CollsRecall
   39            query_script//0,            %
   40            store_query/3               % +Type, +Id, +Query
   41          ]).   42:- use_module(library(http/http_session)).   43:- use_module(library(http/html_write)).   44:- use_module(library(http/html_head)).   45:- use_module(basics).   46:- use_module(library(option)).   47:- use_module(library(settings)).

Forms for entering SPARQL and SeRQL queries.

This module implements the forms for entering SPARQL and SeRQL queries with a simple query-history mechanism for user-submitted SPARQL queries.

 query_form(+Options)//
HTMP component for an interactive (SPARQL) query-form. This calls to the handler with id evaluate_query. Options is an option list:
query_languages(+List)
Query languages supported. Default is ['SPARQL', 'SeRQL']. Specifying only one removes the query-language menu.
   65query_form(Options) -->
   66    html([ form([ class(query),
   67                  name(query),
   68                  action(location_by_id(evaluate_query)),
   69                  method('GET')
   70                ],
   71                [ \hidden(repository, default),
   72                  \hidden(serialization, rdfxml),
   73                  h3([ 'Interactive ',
   74                       \query_language(Options, Hidden),
   75                       ' query'
   76                     ]),
   77                  Hidden,
   78                  table([ class(query)
   79                        ],
   80                        [ \store_recall(_, 3-2),
   81                          tr([ td(colspan(5),
   82                                  textarea(name(query), ''))
   83                             ]),
   84                          tr([ td([ span(class(label), 'Result format: '),
   85                                    \result_format
   86                                  ]),
   87                               td([ span(class(label), 'Resource: '),
   88                                    \resource_menu
   89                                  ]),
   90                               td([ span(class(label), 'Entailment: '),
   91                                    \entailment
   92                                  ]),
   93                               td(align(right),
   94                                  [ input([ type(reset),
   95                                            value('Clear')
   96                                          ]),
   97                                    input([ type(submit),
   98                                            value('Go!')
   99                                          ])
  100                                  ])
  101                             ])
  102                        ])
  103                ]),
  104           \query_script
  105         ]).
  106
  107
  108result_format -->
  109    html(select(name(resultFormat),
  110                [ option([], xml),
  111                  option([selected], html),
  112                  option([], json),
  113                  option([], csv)
  114                ])).
  115
  116query_language(Options, Hidden) -->
  117    { option(query_languages(LangList), Options, ['SPARQL', 'SeRQL'])
  118    },
  119    (   { LangList = [Lang] }
  120    ->  html([Lang]),
  121        { Hidden = \hidden(queryLanguage, Lang) }
  122    ;   { LangList = [DefLang|More] },
  123        html(select(name(queryLanguage),
  124                    [ option([selected], DefLang)
  125                    | \options(More)
  126                    ])),
  127        { Hidden = '' }
  128    ).
  129
  130options([]) --> [].
  131options([Value|T]) -->
  132    html(option([], Value)),
  133    options(T).
  134
  135
  136resource_menu -->
  137    html(select(name(resourceFormat),
  138                [ option([value(plain)],            plain),
  139                  option([value(ns), selected],     'ns:local'),
  140                  option([value(nslabel)],          'ns:label')
  141                ])).
  142
  143entailment -->
  144    { findall(E, cliopatria:entailment(E, _), Es)
  145    },
  146    html(select(name(entailment),
  147                \entailments(Es))).
  148
  149entailments([]) -->
  150    [].
  151entailments([E|T]) -->
  152    (   { setting(cliopatria:default_entailment, E)
  153        }
  154    ->  html(option([selected], E))
  155    ;   html(option([], E))
  156    ),
  157    entailments(T).
 store_recall(+Type, +ColsSpec)// is det
Creates a table-row (tr) holding a `store' and `recall' element. ColsSpec is a term SpanLeft-SpanRight, containing the colspan-attribute for both created table-cells. Note that a page including this must also include query_script//0 at a place later in the page where a script is allowed.
  168store_recall(Type, SL-SR) -->
  169    { next_query_id(Id), !
  170    },
  171    html(tr([ td([ class(qstore),
  172                   colspan(SL)
  173                 ],
  174                 [ b('Remember as: '),
  175                   input([ id(qid),
  176                           name(storeAs),
  177                           size(30),
  178                           value(Id)
  179                         ])
  180                 ]),
  181              td([ class(qrecall),
  182                   colspan(SR),
  183                   align(right)
  184                 ],
  185                 \recall(Type))
  186            ])).
  187store_recall(_, SL-SR) -->
  188    { Span is SL+SR },
  189    html(tr([ td([ class(qnostore),
  190                   colspan(Span)
  191                 ],
  192                 [ 'Login to enable save/restore of queries'
  193                 ])
  194            ])).
  195
  196
  197recall(Type) -->
  198    { http_in_session(_),
  199      findall(Name-Query, stored_query(Name, Type, Query), Pairs),
  200      Pairs \== []
  201    },
  202    !,
  203    html([ b('Recall: '),
  204           select(name(recall),
  205                  [ option([selected], '')
  206                  | \stored_queries(Pairs)
  207                  ])
  208         ]).
  209recall(_) -->
  210    [].
  211
  212:- thread_local
  213    script_fragment/1.  214
  215stored_queries([]) --> !.
  216stored_queries(List) -->
  217    stored_queries(List, 1),
  218    { assert(script_fragment('\nf1();\n')) }.
  219
  220stored_queries([], _) -->
  221    [].
  222stored_queries([Name-Query|T], I) -->
  223    { I2 is I + 1,
  224      atom_concat(f, I, FName),
  225      js_quoted(Query, QuotedQuery),
  226      format(atom(Script),
  227             'function ~w()\n\c
  228                 { document.query.query.value=\'~w\';\n  \c
  229                   document.getElementById(\'qid\').value="~w";\n\c
  230                 }\n',
  231             [ FName, QuotedQuery, Name ]),
  232      assert(script_fragment(Script)),
  233      format(atom(Call), '~w()', [FName])
  234    },
  235    html(option([onClick(Call)], Name)),
  236    stored_queries(T, I2).
 query_script//
Inserts the <script> holding JavaScript functions that restore the queries.
To be done
- This must be rewritten to use the post/receive mechanism.
  245query_script -->
  246    { findall(S, retract(script_fragment(S)), Fragments),
  247      Fragments \== []
  248    },
  249    !,
  250    [ '\n<script language="JavaScript">\n'
  251    ],
  252    Fragments,
  253    [ '\n</script>\n'
  254    ].
  255query_script -->
  256    [].
 js_quoted(+Raw, -Quoted)
Quote text for use in JavaScript. Quoted does not include the leading and trailing quotes.
  263js_quoted(Raw, Quoted) :-
  264    atom_codes(Raw, Codes),
  265    phrase(js_quote_codes(Codes), QuotedCodes),
  266    atom_codes(Quoted, QuotedCodes).
  267
  268js_quote_codes([]) -->
  269    [].
  270js_quote_codes([0'\r,0'\n|T]) -->
  271    !,
  272    "\\n",
  273    js_quote_codes(T).
  274js_quote_codes([H|T]) -->
  275    js_quote_code(H),
  276    js_quote_codes(T).
  277
  278js_quote_code(0'') -->
  279    !,
  280    "\\'".
  281js_quote_code(0'\\) -->
  282    !,
  283    "\\\\".
  284js_quote_code(0'\n) -->
  285    !,
  286    "\\n".
  287js_quote_code(0'\r) -->
  288    !,
  289    "\\r".
  290js_quote_code(0'\t) -->
  291    !,
  292    "\\t".
  293js_quote_code(C) -->
  294    [C].
  295
  296
  297                 /*******************************
  298                 *         SAVED QUERIES        *
  299                 *******************************/
 store_query(+Type, +Name, +Query) is det
Store the SPARQL/SeRQL Query under Name in the current session. Succeeds without doing anything if there is no session.
  306store_query(_, '', _) :- !.
  307store_query(Type, As, Query) :-
  308    http_in_session(_),
  309    !,
  310    set_high_id(As),
  311    http_session_retractall(stored_query(As, Type, _)),
  312    http_session_retractall(stored_query(_, Type, Query)),
  313    http_session_asserta(stored_query(As, Type, Query)).
  314store_query(_, _, _).
  315
  316stored_query(As, Type, Query) :-
  317    http_session_data(stored_query(As, Type, Query)).
  318
  319set_high_id(Name) :-
  320    http_in_session(_),
  321    atom_concat('Q-', Id, Name),
  322    catch(atom_number(Id, N), _, fail),
  323    !,
  324    (   http_session_data(qid(N0))
  325    ->  (   N > N0
  326        ->  http_session_retract(qid(_)),
  327            http_session_assert(qid(N))
  328        ;   true
  329        )
  330    ;   http_session_assert(qid(N))
  331    ).
  332set_high_id(_).
  333
  334
  335next_query_id(Id) :-
  336    http_in_session(_Session),
  337    !,
  338    (   http_session_data(qid(Id0))
  339    ->  Next is Id0+1
  340    ;   Next is 1
  341    ),
  342    atomic_list_concat(['Q-',Next], Id)