Skip to content

Commit 95c5f7c

Browse files
committed
Refactor and cleanup of TEI retrieval for XForms XQuery code
1 parent ff11ad2 commit 95c5f7c

1 file changed

Lines changed: 167 additions & 169 deletions

File tree

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

Lines changed: 167 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -3,190 +3,188 @@ 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+
9+
import module namespace http = "http://expath.org/ns/http-client";
10+
import module namespace request = "http://exist-db.org/xquery/request";
1111

1212
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
13+
declare namespace tei = "http://www.tei-c.org/ns/1.0";
14+
1315
declare option output:method "xml";
1416
declare option output:media-type "text/xml";
1517

16-
(:element {fn:QName("http://www.w3.org/2013/XSL/json", "map")} { }:)
18+
19+
declare variable $local:start as xs:string := request:get-parameter("start", "1");
20+
declare variable $local:search-uri as xs:string := request:get-parameter("searchURI", "http://localhost:8080/exist/apps/majlis/modules/content-negotiation/content-negotiation.xql?results=manuForma");
21+
declare variable $local:post-data as xs:string? := request:get-parameter("postdata", ())[. ne ""];
22+
declare variable $local:template as xs:boolean := lower-case(request:get-parameter("template", "false")) eq "true";
23+
declare variable $local:path as xs:string? := request:get-parameter("path", ())[. ne ""];
24+
declare variable $local:search as xs:boolean := lower-case(request:get-parameter("search", "false")) eq "true";
25+
declare variable $local:view as xs:string? := request:get-parameter("view", ());
26+
declare variable $local:q as xs:string? := request:get-parameter("q", ());
27+
declare variable $local:facets as map(xs:string, xs:string*)* := request:get-parameter-names()[starts-with(.), "facet"] ! map { "name": ., "value": request:get-parameter(., ()) };
28+
declare variable $local:idno as xs:string? := request:get-parameter("idno", ())[. ne ""];
29+
declare variable $local:base-uri as xs:string? := request:get-parameter("baseURI", ())[. ne ""];
30+
declare variable $local:github as xs:string? := request:get-parameter("github", ());
31+
declare variable $local:collection-path as xs:string := request:get-parameter("eXistCollection", concat($config:app-root, "/data"));
32+
33+
1734
(: Recurse through child nodes :)
1835
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-
36+
for $node in $nodes
37+
return
38+
typeswitch($node)
39+
40+
case processing-instruction()
41+
case comment()
42+
return
43+
$node
44+
45+
case text()
46+
return
47+
normalize-space($node)
48+
49+
case element(tei:p)
50+
return
51+
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
52+
if ($node[following-sibling::tei:p]) then
53+
(
54+
local:markdown($node/node()),
55+
"&#10;&#10;"
56+
)
57+
else
58+
local:markdown($node/node())
59+
else
60+
local:passthru($node)
61+
62+
case element(tei:lb)
63+
return
64+
"&#10;"
65+
66+
case element(tei:em)
67+
return
68+
(
69+
"*",
70+
local:markdown($node/node()),
71+
"*"
72+
)
73+
74+
case element(tei:relation)
75+
return
76+
if ($node[exists((@active, @mutual, @passive))]) then
77+
element {node-name($node)} {
78+
$node/@*[not(local-name(.) = ('active', 'mutual', 'passive'))],
79+
for $ref in tokenize($node/@active, " ")[. ne ""]
80+
let $label := local:get-label($ref)
81+
return
82+
<tei:active ref="{$ref}">{$label}</active>
83+
,
84+
for $ref in tokenize($node/@passive, " ")[. ne ""]
85+
let $label := local:get-label($ref)
86+
return
87+
<tei:mutual ref="{$ref}">{$label}</active>
88+
,
89+
for $ref in tokenize($node/@mutual, " ")[. ne ""]
90+
let $label := local:get-label($ref)
91+
return
92+
<tei:passive ref="{$ref}">{$label}</active>
93+
,
94+
local:markdown($node/node())
11695
}
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())
96+
97+
else
98+
local:passthru($node)
99+
100+
case element()
101+
return
102+
local:passthru($node)
103+
104+
default
105+
return
106+
local:markdown($node/node())
122107
};
123108

124109
(: Recurse through child nodes :)
125110
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()))
111+
element {node-name($node)} {
112+
$node/@*,
113+
local:markdown($node/node())
128114
}
129115
};
130116

117+
declare function local:get-label($ref as xs:string) as xs:string {
118+
let $collection := "/db/apps/majlis-data/data"
119+
let $doc := (collection($collection)//tei:idno[@type eq "URI"][. = ($ref, concat($ref, "/"), concat($ref, "/tei"))]/ancestor::tei:TEI)[1]
120+
let $label := $doc/descendant::tei:title[1]/descendant-or-self::text()
121+
return
122+
normalize-space($label)
123+
};
124+
125+
declare function local:get-data() {
126+
if (exists($local:post-data)) then
127+
$local:post-data
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+
else if ($local:template and exists($local:path)) then
130+
if (starts-with($local:path, "/db/") or contains($local:path, $config:app-root)) then
131+
doc($local:path)
132+
else
133+
doc($config:app-root || $local:path)
134+
135+
else if ($local:search) then
136+
if ($view = "all") then
137+
<data>
138+
{
139+
for $r in collection($local:collection-path)//tei:TEI
154140
let $title := string($r/descendant::tei:title[1])
155-
let $idno := string($r/descendant::tei:publicationStmt[1]/tei:idno[@type='URI'][1])
141+
let $idno := string($r/descendant::tei:publicationStmt[1]/tei:idno[@type eq "URI"][1])
156142
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)
143+
return
144+
<record src="{document-uri(root($r))}" name="{$title}" idno="{concat("[", $idno, "]")}"/>
145+
}
146+
</data>
147+
148+
else if (exists($local:q)) then
149+
<data>
150+
{
151+
let $facet-params := $local:facets ! ("&amp;" || .?name || "=" || escape-uri(.?value, true()))
152+
let $url := $local:search-uri || "&amp;q=" || escape-uri($local:q, true()) || "&amp;existCollection=" || $local:collection-path || "&amp;start=" || $local:start || $facet-params
153+
let $hits := http:send-request(<http:request http-version="1.1" href="{xs:anyURI($url)}" method="get"/>)
154+
return
155+
$hits
156+
}
157+
</data>
158+
159+
else if (exists($local:idno)) then
160+
let $idno as xs:string :=
161+
if (starts-with($local:idno, "http")) then
162+
$local:idno
163+
else if (exists($local:base-uri) then
164+
concat($local:base-uri, $local:idno)
165+
else
166+
concat("/", $local:idno)
167+
return
168+
<data idno="{$idno}">
169+
{
170+
for $idno in collection($local:collection-path)//tei:idno[. = ($idno, concat($idno, "/tei")) or ends-with(., $idno)]
171+
let $title := string($idno/ancestor::tei:TEI/descendant::tei:title[1])
172+
order by $title
173+
return
174+
<record src="{document-uri(root($idno))}" name="{$title}" idno="{concat("[", $idno, "]")}"/>
175+
}
176+
</data>
177+
178+
else ()
179+
180+
else if ($local:github = "browse") then
181+
gitcommit:list-files()
182+
183+
else
184+
request:get-data()
185+
};
186+
187+
188+
let $data := local:get-data()
189+
return
190+
local:markdown($data)

0 commit comments

Comments
 (0)