Skip to content

Commit 9184d22

Browse files
committed
Refactor and cleanup of TEI retrieval for XForms XQuery code
1 parent 3fd7239 commit 9184d22

1 file changed

Lines changed: 173 additions & 170 deletions

File tree

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

Lines changed: 173 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -3,190 +3,193 @@ 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";
14-
declare option output:media-type "text/xml";
17+
declare option output:media-type "application/xml";
18+
declare option output:omit-xml-declaration "no";
19+
declare option output:indent "yes";
20+
21+
declare variable $local:start as xs:string := rh:request-param("start", "1");
22+
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");
23+
declare variable $local:post-data as xs:string? := rh:request-param("postdata");
24+
declare variable $local:template as xs:boolean := rh:request-param-bool("template");
25+
declare variable $local:path as xs:string? := rh:request-param("path");
26+
declare variable $local:search as xs:boolean := rh:request-param-bool("search");
27+
declare variable $local:view as xs:string? := rh:request-param("view");
28+
declare variable $local:q as xs:string? := rh:request-param("q");
29+
declare variable $local:facets as map(xs:string, xs:string*)* := rh:request-params("facet");
30+
declare variable $local:idno as xs:string? := rh:request-param("idno");
31+
declare variable $local:base-uri as xs:string? := rh:request-param("baseURI");
32+
declare variable $local:github as xs:string? := rh:request-param("github");
33+
declare variable $local:exist-collection-path as xs:string := rh:request-param("eXistCollection", concat($config:app-root, "/data"));
34+
1535

16-
(:element {fn:QName("http://www.w3.org/2013/XSL/json", "map")} { }:)
1736
(: Recurse through child nodes :)
1837
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-
38+
for $node in $nodes
39+
return
40+
typeswitch($node)
41+
42+
case processing-instruction()
43+
return
44+
$node
45+
46+
case comment()
47+
return
48+
$node
49+
50+
case text()
51+
return
52+
normalize-space($node)
53+
54+
case element(tei:p)
55+
return
56+
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
57+
if ($node[following-sibling::tei:p]) then
58+
(
59+
local:markdown($node/node()),
60+
"&#10;&#10;"
61+
)
62+
else
63+
local:markdown($node/node())
64+
else
65+
local:passthru($node)
66+
67+
case element(tei:lb)
68+
return
69+
"&#10;"
70+
71+
case element(tei:em)
72+
return
73+
(
74+
"*",
75+
local:markdown($node/node()),
76+
"*"
77+
)
78+
79+
case element(tei:relation)
80+
return
81+
if ($node[exists((@active, @mutual, @passive))]) then
82+
element {node-name($node)} {
83+
$node/@*[not(local-name(.) = ('active', 'mutual', 'passive'))],
84+
for $ref in tokenize($node/@active, " ")[. ne ""]
85+
let $label := local:get-label($ref)
86+
return
87+
<tei:active ref="{$ref}">{$label}</tei:active>
88+
,
89+
for $ref in tokenize($node/@passive, " ")[. ne ""]
90+
let $label := local:get-label($ref)
91+
return
92+
<tei:mutual ref="{$ref}">{$label}</tei:mutual>
93+
,
94+
for $ref in tokenize($node/@mutual, " ")[. ne ""]
95+
let $label := local:get-label($ref)
96+
return
97+
<tei:passive ref="{$ref}">{$label}</tei:passive>
98+
,
99+
local:markdown($node/node())
116100
}
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())
101+
102+
else
103+
local:passthru($node)
104+
105+
case element()
106+
return
107+
local:passthru($node)
108+
109+
default
110+
return
111+
local:markdown($node/node())
122112
};
123113

124114
(: Recurse through child nodes :)
125115
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()))
116+
element {node-name($node)} {
117+
$node/@*,
118+
local:markdown($node/node())
128119
}
129120
};
130121

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

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
134+
else if ($local:template and exists($local:path)) then
135+
if (starts-with($local:path, "/db/") or contains($local:path, $config:app-root)) then
136+
doc($local:path)
137+
else
138+
doc($config:app-root || $local:path)
139+
140+
else if ($local:search) then
141+
if ($local:view = "all") then
142+
<data>
143+
{
144+
for $r in collection($local:exist-collection-path)//tei:TEI
154145
let $title := string($r/descendant::tei:title[1])
155-
let $idno := string($r/descendant::tei:publicationStmt[1]/tei:idno[@type='URI'][1])
146+
let $idno := string($r/descendant::tei:publicationStmt[1]/tei:idno[@type eq "URI"][1])
156147
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)
148+
return
149+
<record src="{document-uri(root($r))}" name="{$title}" idno="{concat("[", $idno, "]")}"/>
150+
}
151+
</data>
152+
153+
else if (exists($local:q)) then
154+
<data>
155+
{
156+
let $facet-params := $local:facets ! ("&amp;" || .?name || "=" || escape-uri(.?value, true()))
157+
let $url := $local:search-uri || "&amp;q=" || escape-uri($local:q, true()) || "&amp;existCollection=" || $local:exist-collection-path || "&amp;start=" || $local:start || $facet-params
158+
let $hits := http:send-request(<http:request http-version="1.1" href="{xs:anyURI($url)}" method="get"/>)
159+
return
160+
$hits
161+
}
162+
</data>
163+
164+
else if (exists($local:idno)) then
165+
let $idno as xs:string :=
166+
if (starts-with($local:idno, "http")) then
167+
$local:idno
168+
else if (exists($local:base-uri)) then
169+
concat($local:base-uri, $local:idno)
170+
else
171+
concat("/", $local:idno)
172+
return
173+
<data idno="{$idno}">
174+
{
175+
for $idno in collection($local:exist-collection-path)//tei:idno[. = ($idno, concat($idno, "/tei")) or ends-with(., $idno)]
176+
let $title := string($idno/ancestor::tei:TEI/descendant::tei:title[1])
177+
order by $title
178+
return
179+
<record src="{document-uri(root($idno))}" name="{$title}" idno="{concat("[", $idno, "]")}"/>
180+
}
181+
</data>
182+
183+
else ()
184+
185+
else if ($local:github = "browse") then
186+
gitcommit:list-files()
187+
188+
else
189+
request:get-data()
190+
};
191+
192+
193+
let $data := local:get-data()
194+
return
195+
local:markdown($data)

0 commit comments

Comments
 (0)