Skip to content

Commit 1f19d79

Browse files
committed
Refactor and cleanup of TEI retrieval for XForms XQuery code
1 parent f0bef72 commit 1f19d79

1 file changed

Lines changed: 171 additions & 169 deletions

File tree

src/main/xar-resources/services/get-rec.xql

Lines changed: 171 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -3,190 +3,192 @@ xquery version "3.1";
33
: Get data for TEI XForms
44
:
55
:)
6-
import module namespace config="http://localhost/manuForma/config" at "../modules/config.xqm";
7-
import module namespace gitcommit="http://syriaca.org/srophe/gitcommit" at "git-commit.xql";
8-
import module namespace http="http://expath.org/ns/http-client";
9-
declare namespace request="http://exist-db.org/xquery/request";
10-
declare namespace tei = "http://www.tei-c.org/ns/1.0";
6+
import module namespace config = "http://localhost/manuForma/config" at "../modules/config.xqm";
7+
import module namespace gitcommit = "http://syriaca.org/srophe/gitcommit" at "git-commit.xql";
8+
import module namespace rh = "http://localhost/manuForma/request-helper" at "../modules/request-helper.xqm";
9+
10+
import module namespace http = "http://expath.org/ns/http-client";
11+
import module namespace request = "http://exist-db.org/xquery/request";
1112

1213
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
14+
declare namespace tei = "http://www.tei-c.org/ns/1.0";
15+
1316
declare option output:method "xml";
1417
declare option output:media-type "text/xml";
1518

16-
(:element {fn:QName("http://www.w3.org/2013/XSL/json", "map")} { }:)
19+
20+
declare variable $local:start as xs:string := rh:request-param("start", "1");
21+
declare variable $local:search-uri as xs:string := rh:request-param("searchURI", "http://localhost:8080/exist/apps/majlis/modules/content-negotiation/content-negotiation.xql?results=manuForma");
22+
declare variable $local:post-data as xs:string? := rh:request-param("postdata", ());
23+
declare variable $local:template as xs:boolean := rh:request-param-bool("template");
24+
declare variable $local:path as xs:string? := rh:request-param("path", ());
25+
declare variable $local:search as xs:boolean := rh:request-param-bool("search");
26+
declare variable $local:view as xs:string? := rh:request-param("view", ());
27+
declare variable $local:q as xs:string? := rh:request-param("q", ());
28+
declare variable $local:facets as map(xs:string, xs:string*)* := rh:request-params("facet", ());
29+
declare variable $local:idno as xs:string? := rh:request-param("idno", ());
30+
declare variable $local:base-uri as xs:string? := rh:request-param("baseURI", ());
31+
declare variable $local:github as xs:string? := rh:request-param("github", ());
32+
declare variable $local:collection-path as xs:string := rh:request-param("eXistCollection", concat($config:app-root, "/data"));
33+
34+
1735
(: Recurse through child nodes :)
1836
declare function local:markdown($nodes as node()*) as item()* {
19-
for $node in $nodes
20-
return
21-
typeswitch($node)
22-
case processing-instruction() return $node
23-
case comment() return $node
24-
case text() return normalize-space($node)
25-
case element(tei:TEI) return
26-
<TEI xmlns="http://www.tei-c.org/ns/1.0">
27-
{($node/@*, local:markdown($node/node()))}
28-
</TEI>
29-
case element(tei:p) return
30-
if($node/parent::tei:quote or $node/parent::tei:summary or $node/parent::tei:note or $node/parent::tei:desc or $node/parent::tei:ab) then
31-
if($node/following-sibling::tei:p) then (local:markdown($node/node()),'&#10;&#10;')
32-
else (local:markdown($node/node()))
33-
else element {fn:QName("http://www.tei-c.org/ns/1.0",local-name($node))} {($node/@*, local:markdown($node/node()))}
34-
case element(tei:lb) return '&#10;'
35-
case element(tei:em) return
36-
('*',local:markdown($node/node()),'*')
37-
case element(tei:relation) return
38-
if($node[@mutual] or $node[@active] or $node[@passive]) then
39-
let $collection := '/db/apps/majlis-data/data'
40-
return
41-
<relation xmlns="http://www.tei-c.org/ns/1.0">
42-
{$node/@*[not(local-name() = 'mutual') and not(local-name() = 'active') and not(local-name() = 'passive')]}
43-
{
44-
let $active :=
45-
if(contains($node/@active,' ')) then
46-
for $a in tokenize($node/@active,' ')
47-
let $ref := $a
48-
let $doc :=
49-
(collection($collection)//tei:idno[@type='URI'][. = $ref]/ancestor::tei:TEI |
50-
collection($collection)//tei:idno[@type='URI'][. = concat($ref,'/')]/ancestor::tei:TEI |
51-
collection($collection)//tei:idno[@type='URI'][. = concat($ref,'/tei')]/ancestor::tei:TEI)[1]
52-
let $label := $doc/descendant::tei:title[1]/descendant-or-self::text()
53-
return
54-
<active xmlns="http://www.tei-c.org/ns/1.0" ref="{$ref}">{normalize-space($label)}</active>
55-
else
56-
let $ref := string($node/@active)
57-
let $doc :=
58-
if($ref != '') then
59-
(collection($collection)//tei:idno[@type='URI'][. = $ref]/ancestor::tei:TEI |
60-
collection($collection)//tei:idno[@type='URI'][. = concat($ref,'/')]/ancestor::tei:TEI |
61-
collection($collection)//tei:idno[@type='URI'][. = concat($ref,'/tei')]/ancestor::tei:TEI)[1]
62-
else ()
63-
let $label := $doc/descendant::tei:title[1]/descendant-or-self::text()
64-
return
65-
<active xmlns="http://www.tei-c.org/ns/1.0" ref="{$ref}">{normalize-space($label)}</active>
66-
let $passive :=
67-
if(contains($node/@passive,' ')) then
68-
for $p in tokenize($node/@passive,' ')
69-
let $ref := $p
70-
let $doc :=
71-
if($ref != '') then
72-
(collection($collection)//tei:idno[@type='URI'][. = $ref]/ancestor::tei:TEI |
73-
collection($collection)//tei:idno[@type='URI'][. = concat($ref,'/')]/ancestor::tei:TEI |
74-
collection($collection)//tei:idno[@type='URI'][. = concat($ref,'/tei')]/ancestor::tei:TEI)[1]
75-
else ()
76-
let $label := $doc/descendant::tei:title[1]/descendant-or-self::text()
77-
return
78-
<passive xmlns="http://www.tei-c.org/ns/1.0" ref="{$ref}">{normalize-space($label)}</passive>
79-
else
80-
let $ref := string($node/@passive)
81-
let $doc :=
82-
if($ref != '') then
83-
(collection($collection)//tei:idno[@type='URI'][. = $ref]/ancestor::tei:TEI |
84-
collection($collection)//tei:idno[@type='URI'][. = concat($ref,'/')]/ancestor::tei:TEI |
85-
collection($collection)//tei:idno[@type='URI'][. = concat($ref,'/tei')]/ancestor::tei:TEI)[1]
86-
else ()
87-
let $label := $doc/descendant::tei:title[1]/descendant-or-self::text()
88-
return
89-
<passive xmlns="http://www.tei-c.org/ns/1.0" ref="{$ref}">{normalize-space($label)}</passive>
90-
let $mutual :=
91-
if(contains($node/@mutual,' ')) then
92-
for $m in tokenize($node/@mutual,' ')
93-
let $ref := $m
94-
let $doc :=
95-
if($ref != '') then
96-
(collection($collection)//tei:idno[@type='URI'][. = $ref]/ancestor::tei:TEI |
97-
collection($collection)//tei:idno[@type='URI'][. = concat($ref,'/')]/ancestor::tei:TEI |
98-
collection($collection)//tei:idno[@type='URI'][. = concat($ref,'/tei')]/ancestor::tei:TEI)[1]
99-
else ()
100-
let $label := $doc/descendant::tei:title[1]/descendant-or-self::text()
101-
return
102-
<mutual xmlns="http://www.tei-c.org/ns/1.0" ref="{$ref}">{normalize-space($label)}</mutual>
103-
else
104-
let $ref := string($node/@mutual)
105-
let $doc :=
106-
if($ref != '') then
107-
(collection($collection)//tei:idno[@type='URI'][. = $ref]/ancestor::tei:TEI |
108-
collection($collection)//tei:idno[@type='URI'][. = concat($ref,'/')]/ancestor::tei:TEI |
109-
collection($collection)//tei:idno[@type='URI'][. = concat($ref,'/tei')]/ancestor::tei:TEI)[1]
110-
else ()
111-
let $label := $doc/descendant::tei:title[1]/descendant-or-self::text()
112-
return
113-
<mutual xmlns="http://www.tei-c.org/ns/1.0" ref="{$ref}">{normalize-space($label)}</mutual>
114-
return ($active,$passive,$mutual)
115-
37+
for $node in $nodes
38+
return
39+
typeswitch($node)
40+
41+
case processing-instruction()
42+
return
43+
$node
44+
45+
case comment()
46+
return
47+
$node
48+
49+
case text()
50+
return
51+
normalize-space($node)
52+
53+
case element(tei:p)
54+
return
55+
if ($node[parent::tei:quote] or $node[parent::tei:summary] or $node[parent::tei:note] or $node[parent::tei:desc] or $node[parent::tei:ab]) then
56+
if ($node[following-sibling::tei:p]) then
57+
(
58+
local:markdown($node/node()),
59+
"&#10;&#10;"
60+
)
61+
else
62+
local:markdown($node/node())
63+
else
64+
local:passthru($node)
65+
66+
case element(tei:lb)
67+
return
68+
"&#10;"
69+
70+
case element(tei:em)
71+
return
72+
(
73+
"*",
74+
local:markdown($node/node()),
75+
"*"
76+
)
77+
78+
case element(tei:relation)
79+
return
80+
if ($node[exists((@active, @mutual, @passive))]) then
81+
element {node-name($node)} {
82+
$node/@*[not(local-name(.) = ('active', 'mutual', 'passive'))],
83+
for $ref in tokenize($node/@active, " ")[. ne ""]
84+
let $label := local:get-label($ref)
85+
return
86+
<tei:active ref="{$ref}">{$label}</tei:active>
87+
,
88+
for $ref in tokenize($node/@passive, " ")[. ne ""]
89+
let $label := local:get-label($ref)
90+
return
91+
<tei:mutual ref="{$ref}">{$label}</tei:mutual>
92+
,
93+
for $ref in tokenize($node/@mutual, " ")[. ne ""]
94+
let $label := local:get-label($ref)
95+
return
96+
<tei:passive ref="{$ref}">{$label}</tei:passive>
97+
,
98+
local:markdown($node/node())
11699
}
117-
{local:markdown($node/node())}
118-
</relation>
119-
else element {fn:QName("http://www.tei-c.org/ns/1.0",local-name($node))} {($node/@*, local:markdown($node/node()))}
120-
case element() return local:passthru($node)
121-
default return local:markdown($node/node())
100+
101+
else
102+
local:passthru($node)
103+
104+
case element()
105+
return
106+
local:passthru($node)
107+
108+
default
109+
return
110+
local:markdown($node/node())
122111
};
123112

124113
(: Recurse through child nodes :)
125114
declare function local:passthru($node as node()*) as item()* {
126-
element {fn:QName("http://www.tei-c.org/ns/1.0",local-name($node))}
127-
{($node/@*,local:markdown($node/node()))
115+
element {node-name($node)} {
116+
$node/@*,
117+
local:markdown($node/node())
128118
}
129119
};
130120

121+
declare function local:get-label($ref as xs:string) as xs:string {
122+
let $collection := "/db/apps/majlis-data/data"
123+
let $doc := (collection($collection)//tei:idno[@type eq "URI"][. = ($ref, concat($ref, "/"), concat($ref, "/tei"))]/ancestor::tei:TEI)[1]
124+
let $label := $doc/descendant::tei:title[1]/descendant-or-self::text()
125+
return
126+
normalize-space($label)
127+
};
131128

132-
let $start := if(request:get-parameter('start','') != '') then request:get-parameter('start','') else '1'
133-
let $searchURI := if(request:get-parameter('searchURI','') != '') then request:get-parameter('searchURI','') else 'http://localhost:8080/exist/apps/majlis/modules/content-negotiation/content-negotiation.xql?results=manuForma'
134-
let $eXistCollection := if(request:get-parameter('eXistCollection','') != '') then request:get-parameter('eXistCollection','') else concat($config:app-root,'/data')
135-
let $github-path := if(request:get-parameter('githubPath','') != '') then request:get-parameter('githubPath','') else 'data/tei/'
136-
let $github-repo := if(request:get-parameter('githubRepo','') != '') then request:get-parameter('githubRepo','') else 'blogs'
137-
let $github-owner := if(request:get-parameter('githubOwner','') != '') then request:get-parameter('githubOwner','') else 'wsalesky'
138-
let $github-branch := if(request:get-parameter('githubBranch','') != '') then request:get-parameter('githubBranch','') else 'master'
139-
let $data :=
140-
if(request:get-parameter('postdata','')) then
141-
request:get-parameter('postdata','')
142-
else if(request:get-parameter('template','') = 'true') then
143-
if(request:get-parameter('path','') != '') then
144-
if(contains(request:get-parameter('path',''),$config:app-root)) then
145-
doc(request:get-parameter('path',''))
146-
else if(starts-with(request:get-parameter('path',''),'/db/')) then
147-
doc(request:get-parameter('path',''))
148-
else doc($config:app-root || request:get-parameter('path',''))
149-
else ()
150-
else if(request:get-parameter('search','') = 'true') then
151-
if(request:get-parameter('view','') = 'all') then
152-
<data>{
153-
for $r in collection($eXistCollection)//tei:TEI
129+
declare function local:get-data() {
130+
if (exists($local:post-data)) then
131+
$local:post-data
132+
133+
else if ($local:template and exists($local:path)) then
134+
if (starts-with($local:path, "/db/") or contains($local:path, $config:app-root)) then
135+
doc($local:path)
136+
else
137+
doc($config:app-root || $local:path)
138+
139+
else if ($local:search) then
140+
if ($local:view = "all") then
141+
<data>
142+
{
143+
for $r in collection($local:collection-path)//tei:TEI
154144
let $title := string($r/descendant::tei:title[1])
155-
let $idno := string($r/descendant::tei:publicationStmt[1]/tei:idno[@type='URI'][1])
145+
let $idno := string($r/descendant::tei:publicationStmt[1]/tei:idno[@type eq "URI"][1])
156146
order by $title
157-
return <record src="{document-uri(root($r))}" name="{$title}" idno="{concat('[',$idno,']')}"/>
158-
}</data>
159-
else if(request:get-parameter('q','') != '') then
160-
<data>{
161-
let $facetparams :=
162-
let $params := request:get-parameter-names()
163-
for $f in $params
164-
where starts-with($f, 'facet')
165-
return
166-
('&amp;' || $f || '=' || escape-uri(request:get-parameter($f,''),true()))
167-
let $url := ($searchURI ||'&amp;q=' || escape-uri(request:get-parameter('q',''),true()) || '&amp;existCollection=' || $eXistCollection || '&amp;start=' || $start || ($facetparams))
168-
let $hits := http:send-request(<http:request http-version="1.1" href="{xs:anyURI($url)}" method="get"/>)
169-
return $hits
170-
}</data>
171-
else if(request:get-parameter('idno','') != '') then
172-
let $idno := if(starts-with(request:get-parameter('idno',''),'http')) then
173-
request:get-parameter('idno','')
174-
else if(request:get-parameter('baseURI','') != '') then
175-
concat(request:get-parameter('baseURI',''),request:get-parameter('idno',''))
176-
else concat('/',request:get-parameter('idno',''))
177-
return
178-
if($idno != '') then
179-
<data idno="{$idno}">{
180-
for $r in collection($eXistCollection)//tei:idno[. = $idno] | collection($eXistCollection)//tei:idno[. = ($idno || '/tei')]
181-
| collection($eXistCollection)//tei:idno[ends-with(.,$idno)]
182-
let $title := string($r/ancestor::tei:TEI/descendant::tei:title[1])
183-
let $idno := $r
184-
order by $title
185-
return <record src="{document-uri(root($r))}" name="{$title}" idno="{concat('[',$idno,']')}"/>
186-
}</data>
187-
else <div>Empty idno</div>
188-
else ()
189-
else if(request:get-parameter('github','') = 'browse') then
190-
gitcommit:list-files()
191-
else request:get-data()
192-
return local:markdown($data)
147+
return
148+
<record src="{document-uri(root($r))}" name="{$title}" idno="{concat("[", $idno, "]")}"/>
149+
}
150+
</data>
151+
152+
else if (exists($local:q)) then
153+
<data>
154+
{
155+
let $facet-params := $local:facets ! ("&amp;" || .?name || "=" || escape-uri(.?value, true()))
156+
let $url := $local:search-uri || "&amp;q=" || escape-uri($local:q, true()) || "&amp;existCollection=" || $local:collection-path || "&amp;start=" || $local:start || $facet-params
157+
let $hits := http:send-request(<http:request http-version="1.1" href="{xs:anyURI($url)}" method="get"/>)
158+
return
159+
$hits
160+
}
161+
</data>
162+
163+
else if (exists($local:idno)) then
164+
let $idno as xs:string :=
165+
if (starts-with($local:idno, "http")) then
166+
$local:idno
167+
else if (exists($local:base-uri)) then
168+
concat($local:base-uri, $local:idno)
169+
else
170+
concat("/", $local:idno)
171+
return
172+
<data idno="{$idno}">
173+
{
174+
for $idno in collection($local:collection-path)//tei:idno[. = ($idno, concat($idno, "/tei")) or ends-with(., $idno)]
175+
let $title := string($idno/ancestor::tei:TEI/descendant::tei:title[1])
176+
order by $title
177+
return
178+
<record src="{document-uri(root($idno))}" name="{$title}" idno="{concat("[", $idno, "]")}"/>
179+
}
180+
</data>
181+
182+
else ()
183+
184+
else if ($local:github = "browse") then
185+
gitcommit:list-files()
186+
187+
else
188+
request:get-data()
189+
};
190+
191+
192+
let $data := local:get-data()
193+
return
194+
local:markdown($data)

0 commit comments

Comments
 (0)