Skip to content

Commit 997c518

Browse files
committed
[bugfix] Allow the API param wrapElement to specify an element by (a) local-name, (b) prefix:local-name, or (c) Q{namespace}local-name
1 parent cb6762d commit 997c518

2 files changed

Lines changed: 175 additions & 41 deletions

File tree

src/main/xar-resources/modules/content-negotiation/content-negotiation.xql

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import module namespace config="http://srophe.org/srophe/config" at "../config.x
1212
import module namespace cntneg="http://srophe.org/srophe/cntneg" at "content-negotiation.xqm";
1313
import module namespace tei2html="http://srophe.org/srophe/tei2html" at "tei2html.xqm";
1414

15+
import module namespace rh = "http://localhost/manuForma/request-helper" at "../request-helper.xqm";
16+
1517
(: Data processing module. :)
1618
import module namespace data="http://srophe.org/srophe/data" at "../lib/data.xqm";
1719

@@ -26,6 +28,22 @@ declare namespace rest = "http://exquery.org/ns/restxq";
2628
declare namespace http="http://expath.org/ns/http-client";
2729
declare namespace srophe="https://srophe.app";
2830

31+
declare variable $local:api as xs:string? := rh:request-param("api");
32+
declare variable $local:collection as xs:string? := rh:request-param("collection");
33+
declare variable $local:doc as xs:string? := rh:request-param("doc");
34+
declare variable $local:element as xs:string? := rh:request-param("element");
35+
declare variable $local:exist-collection as xs:string? := rh:request-param("existCollection");
36+
declare variable $local:format as xs:string := rh:request-param("format", "xml");
37+
declare variable $local:geo as xs:string? := rh:request-param("geo");
38+
declare variable $local:id as xs:string? := rh:request-param("id");
39+
declare variable $local:limit as xs:string? := rh:request-param("limit");
40+
declare variable $local:per-page as xs:integer := rh:request-param-integer("perpage", 10);
41+
declare variable $local:q as xs:string* := rh:request-param("q");
42+
declare variable $local:results as xs:string? := rh:request-param("results");
43+
declare variable $local:start as xs:integer := rh:request-param-integer("start", 1);
44+
declare variable $local:type as xs:string? := rh:request-param("type");
45+
declare variable $local:wrap-element as xs:QName? := rh:request-param("wrapElement") ! xs:QName(.);
46+
2947
(:~
3048
: Search API
3149
: @note: This function is important! Used by eKtobe
@@ -48,10 +66,10 @@ declare function local:search-element($element as xs:string?, $q as xs:string*,
4866
for $c in tokenize($collection,',')
4967
return data:apiSearch($c, $e, $q, ())
5068
else data:apiSearch($collection, $e, $q, ())
51-
let $hits := if(request:get-parameter('limit', '') != '') then
69+
let $hits := if($local:limit) then
5270
for $hit in $hits
5371
let $id := replace($hit/ancestor-or-self::tei:TEI/descendant::tei:publicationStmt/tei:idno[@type='URI'][1],'/tei','')
54-
where contains($id,request:get-parameter('limit', ''))
72+
where contains($id, $limit)
5573
return $hit
5674
else $hits
5775
return
@@ -104,8 +122,10 @@ declare function local:search-element($element as xs:string?, $q as xs:string*,
104122
<bibl xmlns="http://www.tei-c.org/ns/1.0">
105123
<bibl xmlns="http://www.tei-c.org/ns/1.0" xml:id="{$xmlID[1]}">
106124
{
107-
element {xs:QName(request:get-parameter('wrapElement', ''))}
108-
{attribute { "ref" } { $recID }, $headword[1]}
125+
element { ($local:wrap-element, 'value')[1] } {
126+
attribute { "ref" } { $recID },
127+
$headword[1]
128+
}
109129
}
110130
<ptr target="{$recID}"/>
111131
</bibl>
@@ -143,19 +163,20 @@ declare function local:search-element($element as xs:string?, $q as xs:string*,
143163
{attribute { "ref" } { $recID }, concat($type,$headword[1]) }
144164
</passive>
145165
else ()
146-
else if(request:get-parameter('wrapElement', '') != '') then
147-
if(request:get-parameter('wrapElement', '') = 'author') then
148-
element {xs:QName(request:get-parameter('wrapElement', ''))}
149-
{attribute { "ref" } { $recID }, $headword[1]}
150-
else if(request:get-parameter('wrapElement', '') = 'relation') then
151-
element {xs:QName(request:get-parameter('wrapElement', ''))}
152-
{attribute { "ref" } { $recID }, $headword[1]}
166+
else if ($local:wrap-element) then
167+
if (local-name-from-QName($local:wrap-element) = ('author', 'relation')) then
168+
element { $local:wrap-element } {
169+
attribute { "ref" } { $recID },
170+
$headword[1]
171+
}
153172
else
154-
element {xs:QName(request:get-parameter('wrapElement', ''))}
155-
{attribute { "ref" } { $recID },
156-
element {xs:QName($element)}
157-
{attribute { "ref" } { $recID }, $headword[1] }
173+
element { $local:wrap-element } {
174+
attribute { "ref" } { $recID },
175+
element { xs:QName($element) } {
176+
attribute { "ref" } { $recID },
177+
$headword[1]
158178
}
179+
}
159180
else
160181
element {xs:QName($element)}
161182
{attribute { "ref" } { $recID }, $headword[1] }
@@ -211,25 +232,21 @@ declare function local:coordinates($type as xs:string?, $collection as xs:string
211232
return util:eval($path)
212233
};
213234

214-
let $path := if(request:get-parameter('id', '') != '') then
215-
request:get-parameter('id', '')
216-
else if(request:get-parameter('doc', '') != '') then
217-
request:get-parameter('doc', '')
218-
else ()
235+
let $path as xs:string? := ($local:id, $local:doc)[1]
219236
let $data :=
220-
if(request:get-parameter('id', '') != '' or request:get-parameter('doc', '') != '') then
237+
if ($path) then
221238
data:get-document()
222-
else if(request:get-parameter-names() != '') then
223-
if(request:get-parameter('api', '') != '') then
224-
if(request:get-parameter('element', '') !='' and request:get-parameter('q', '') != '') then
225-
local:search-element(request:get-parameter('element', ''), request:get-parameter('q', ''), if(request:get-parameter('collection', '')) then request:get-parameter('collection', '') else ())
226-
else if(request:get-parameter('geo', '') != '') then
227-
local:coordinates(request:get-parameter('type', ''), request:get-parameter('collection', ''))
239+
else if (exists(request:get-parameter-names())) then
240+
if ($local:api) then
241+
if (exists($local:element) and exist($local:q)) then
242+
local:search-element($local:element, $local:q, $local:collection)
243+
else if ($local:geo) then
244+
local:coordinates($local:type, $local:collection)
228245
else <div>Nothing, check params: {request:get-parameter-names()}</div>
229246
else
230247
let $collectionParam :=
231-
if(request:get-parameter('existCollection','') != '') then
232-
tokenize(replace(request:get-parameter('existCollection',''),'/tei',''),'/')[last()]
248+
if ($local:exist-collection) then
249+
tokenize(replace($local:exist-collection, '/tei', ''), '/')[last()]
233250
else ()
234251
let $collection := $collectionParam
235252
let $hits := data:search($collection,'','')
@@ -241,24 +258,22 @@ let $collection := $collectionParam
241258
return concat('&amp;',$param, '=',request:get-parameter($param, '')),'')}</action>
242259
<info count="{count($hits)}">hits: {count($hits)}</info>
243260
{
244-
let $start := if(request:get-parameter('start','') != '') then request:get-parameter('start','') else '1'
245-
let $perpage := if(request:get-parameter('perpage', 10)) then request:get-parameter('perpage', 10) else 10
246-
let $next := xs:integer($start) + xs:integer($perpage)
261+
let $next := xs:integer($local:start) + xs:integer($local:per-page)
247262
return
248-
(<start>{$start}</start>,
263+
(<start>{$local:start}</start>,
249264
if($next lt count($hits)) then
250265
<next>{$next}</next>
251266
else (),
252267
<results>{
253268

254-
for $hit in subsequence($hits,$start,$perpage)
269+
for $hit in subsequence($hits, $local:start, $local:per-page)
255270
let $id := replace($hit/descendant::tei:idno[starts-with(.,$config:base-uri)][1],'/tei','')
256271
let $title := $hit/descendant::tei:titleStmt/tei:title[1]
257272
let $expanded := kwic:expand($hit)
258273
return
259274
<json:value json:array="true">
260275
{
261-
if(request:get-parameter('results', '') = 'manuForma') then
276+
if ($local:results = 'manuForma') then
262277
<record src="{document-uri(root($hit))}" name="{$title}" idno="{concat('[',$id,']')}"/>
263278
else ()
264279
}
@@ -319,16 +334,15 @@ let $collection := $collectionParam
319334
</json:value>
320335
</root>
321336
else ()
322-
let $format := if(request:get-parameter('format', '') != '') then request:get-parameter('format', '') else 'xml'
323337
return
324338
if(not(empty($data))) then
325-
if(request:get-parameter('api', '') != '') then
326-
if(request:get-parameter('geo', '')) then
327-
if($format = 'kml') then
339+
if ($local:api) then
340+
if ($local:geo) then
341+
if($local:format = 'kml') then
328342
cntneg:content-negotiation($data,'kml',())
329343
else cntneg:content-negotiation($data,'geojson',())
330344
else
331-
let $format := if(request:get-parameter('format', '') = 'xml') then 'xml' else 'json'
345+
let $format := if ($local:format eq 'xml' then 'xml' else 'json'
332346
return cntneg:content-negotiation($data, $format, ())
333347
else cntneg:content-negotiation($data, $format, $path)
334-
else ()
348+
else ()
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
xquery version "3.1";
2+
3+
module namespace rh = "http://localhost/manuForma/request-helper";
4+
5+
import module namespace request = "http://exist-db.org/xquery/request";
6+
7+
(:~
8+
: Get a HTTP request parameter.
9+
: Unlike request:get-parameter this will ignore parameters that have an empty string value.
10+
:
11+
: @param $name the name of the HTTP request parameter
12+
:
13+
: @return the value of the parameter if it is valid, or the empty sequence.
14+
:)
15+
declare function rh:request-param($name as xs:string) {
16+
rh:request-param($name, ())
17+
};
18+
19+
(:~
20+
: Get a HTTP request parameter.
21+
: Unlike request:get-parameter this will ignore parameters that have an empty string value, and return the default.
22+
:
23+
: @param $name the name of the HTTP request parameter
24+
: @param $default the default value to return if there is no valid parameter
25+
:
26+
: @return the value of the parameter if it is valid, or the default.
27+
:)
28+
declare function rh:request-param($name as xs:string, $default) {
29+
let $values := request:get-parameter($name, ())[. ne ""]
30+
return
31+
if (exists($values)) then
32+
$values
33+
else
34+
$default
35+
};
36+
37+
(:~
38+
: Get a HTTP request parameter as a boolean value.
39+
: Unlike request:get-parameter this will ignore parameters that have an empty string value.
40+
:
41+
: @param $name the name of the HTTP request parameter
42+
:
43+
: @return the boolean value of the parameter.
44+
:)
45+
declare function rh:request-param-bool($name as xs:string) as xs:boolean {
46+
lower-case(rh:request-param($name, "false")[1]) eq "true"
47+
};
48+
49+
(:~
50+
: Get a HTTP request parameter as a "yes" or "no" value.
51+
: "true" is also considered as "yes".
52+
: Unlike request:get-parameter this will ignore parameters that have an empty string value.
53+
:
54+
: @param $name the name of the HTTP request parameter
55+
:
56+
: @return either "yes", or "no".
57+
:)
58+
declare function rh:request-param-yes-no($name as xs:string) as xs:string {
59+
if (lower-case(rh:request-param($name, "no")[1]) = ("yes", "true"))
60+
then
61+
"yes"
62+
else
63+
"no"
64+
};
65+
66+
(:~
67+
: Get a HTTP request parameter as an integer value.
68+
: Unlike request:get-parameter this will ignore parameters that have an empty string value.
69+
:
70+
: @param $name the name of the HTTP request parameter
71+
:
72+
: @return the integer value of the parameter, or the empty sequence.
73+
:)
74+
declare function rh:request-param-integer($name as xs:string) as xs:integer? {
75+
rh:request-param-integer($name, ())
76+
};
77+
78+
(:~
79+
: Get a HTTP request parameter as an integer value.
80+
: Unlike request:get-parameter this will ignore parameters that have an empty string value.
81+
:
82+
: @param $default the default value to return if there is no valid parameter
83+
:
84+
: @return the integer value of the parameter if it is valid, or the default.
85+
:)
86+
declare function rh:request-param-integer($name as xs:string, $default as xs:integer?) as xs:integer? {
87+
(rh:request-param($name) ! xs:integer(.), $default)[1]
88+
};
89+
90+
91+
(:~
92+
: Get HTTP request parameters.
93+
: Unlike request:get-parameter this will ignore parameters that have an empty string value.
94+
:
95+
: @param $name-prefix the name prefix of the HTTP request parameters
96+
:
97+
: @return a map or the names and values of the parameters if valid, or the empty sequence.
98+
:)
99+
declare function rh:request-params($name-prefix as xs:string) as map(xs:string, xs:string*)* {
100+
rh:request-params($name-prefix, ())
101+
};
102+
103+
(:~
104+
: Get HTTP request parameters.
105+
: Unlike request:get-parameter this will ignore parameters that have an empty string value, and return the default.
106+
:
107+
: @param $name-prefix the name prefix of the HTTP request parameters
108+
: @param $default the default value to return if there is no valid parameter
109+
:
110+
: @return a map or the names and values of the parameters if valid, or the default.
111+
:)
112+
declare function rh:request-params($name-prefix as xs:string, $default-value) as map(xs:string, xs:string*)* {
113+
for $name in request:get-parameter-names()[starts-with(., $name-prefix)]
114+
let $value := rh:request-param($name, $default-value)
115+
return
116+
if (exists($value)) then
117+
map { "name": $name, "value": $value }
118+
else
119+
()
120+
};

0 commit comments

Comments
 (0)