index-viewer.xqm 10.1 KB
Newer Older
Mathias Goebel's avatar
Mathias Goebel committed
1
2
3
4
5
6
7
xquery version "3.1";
(:~
: This module prepares the index html.
: @author Mathias Göbel
: @version 1.0
: @see https://fontane-nb.dariah.eu/register.html
:)
Mathias Goebel's avatar
Mathias Goebel committed
8
module namespace ixv="http://fontane-nb.dariah.eu/index-viewer";
Mathias Goebel's avatar
Mathias Goebel committed
9

Mathias Goebel's avatar
Mathias Goebel committed
10
import module namespace ixp="http://fontane-nb.dariah.eu/index-processor" at "index-processor.xqm";
11
12
import module namespace index="https://fontane-nb.dariah.eu/indexapi" at "index-api.xqm";

Mathias Goebel's avatar
   
Mathias Goebel committed
13
import module namespace config="http://textgrid.de/ns/SADE/config" at "../config/config.xqm";
Mathias Goebel's avatar
Mathias Goebel committed
14
15
import module namespace functx="http://www.functx.com";

Mathias Goebel's avatar
Mathias Goebel committed
16
17
18
declare namespace ore="http://www.openarchives.org/ore/terms/";
declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare namespace tei="http://www.tei-c.org/ns/1.0";
Mathias Goebel's avatar
   
Mathias Goebel committed
19
declare namespace test="http://exist-db.org/xquery/xqsuite";
Mathias Goebel's avatar
Mathias Goebel committed
20
21
declare namespace xhtml="http://www.w3.org/1999/xhtml";

Mathias Goebel's avatar
   
Mathias Goebel committed
22
(: Main cache function
Mathias Goebel's avatar
Mathias Goebel committed
23
 : @return path to the cached document :)
Mathias Goebel's avatar
   
Mathias Goebel committed
24
25
declare
  %test:name("cache")
Mathias Goebel's avatar
Mathias Goebel committed
26
(:  %test:pending (: throws NPE on non-initialized db. needs some data from the grid. :):)
Mathias Goebel's avatar
   
Mathias Goebel committed
27
function local:cache()
Mathias Goebel's avatar
Mathias Goebel committed
28
 {
29
30
31
32
let $test4lock := if (doc-available("/db/sade-projects/textgrid/data/xml/xhtml/index.lock"))
                    then error(QName("FONTANE", "INDEXVIEW2"), "unable to lock resource. index is in preparation or preparation failed.")
                    else true()

Mathias Goebel's avatar
Mathias Goebel committed
33
let $set-option := util:declare-option("exist:output-size-limit", "2000000")
Mathias Goebel's avatar
Mathias Goebel committed
34
35
36
37
38
39
let $collection-uri := "/db/sade-projects/textgrid/data/xml/xhtml",
    $resource := "index.xml"
let $agg := '253st.xml' (: TextGrid Aggregation containing all index files :)
let $uris := (doc('/db/sade-projects/textgrid/data/xml/agg/' || $agg)
          //ore:aggregates/substring-after(@rdf:resource, 'textgrid:')
        )[. != "25547"] (: omit the bibliography here :)
Mathias Goebel's avatar
Mathias Goebel committed
40
let $lastMod := $uris ! xmldb:last-modified($ixp:dataPath, . || '.xml')
Mathias Goebel's avatar
Mathias Goebel committed
41
42

return
Mathias Goebel's avatar
Mathias Goebel committed
43
    if(not(local:login($collection-uri)))
Mathias Goebel's avatar
   
Mathias Goebel committed
44
    then error(QName("FONTANE", "INDEXVIEW1"), "unable to authenticate.")
Mathias Goebel's avatar
Mathias Goebel committed
45
    else
Mathias Goebel's avatar
   
Mathias Goebel committed
46
47
48
49
50
51
        if( xmldb:last-modified($collection-uri, $resource) gt max($lastMod) )
        then
            (: prerendered index younger than index files: do nothing :)
            $collection-uri || "/" || $resource
        else
            (: create the cache :)
52
            let $lock := xmldb:store("/db/sade-projects/textgrid/data/xml/xhtml", "index.lock", <lock/>),
Mathias Goebel's avatar
Mathias Goebel committed
53
                $cache := xmldb:store($collection-uri, $resource, <xhtml:div>{util:eval('ixp:register()')}</xhtml:div>),
54
55
56
                $removeLock := xmldb:remove("/db/sade-projects/textgrid/data/xml/xhtml", "index.lock")
            return
                $cache
Mathias Goebel's avatar
   
Mathias Goebel committed
57
58
59
60
};

(: forces the creation of a new cache file
 : @return document-node() of the index cache file :)
Mathias Goebel's avatar
Mathias Goebel committed
61
declare function ixv:prepare-new-cache()
Mathias Goebel's avatar
   
Mathias Goebel committed
62
63
64
65
66
67
as document-node() {
let $path := "/db/sade-projects/textgrid/data/xml/xhtml/index.xml"
let $cleanup :=
    if( doc-available($path) )
    then xmldb:remove("/db/sade-projects/textgrid/data/xml/xhtml", "index.xml")
    else true()
68
let $prepare := local:cache()
Mathias Goebel's avatar
   
Mathias Goebel committed
69
return
70
    doc( $prepare )
Mathias Goebel's avatar
Mathias Goebel committed
71
72
73
74
};

(:~
 : Recursive transformation of the index document, if a specific item is requested.
Mathias Goebel's avatar
Mathias Goebel committed
75
76
 : @param $nodes Node to be parsed
 : @param $id A single id prepared with generate-id()
Mathias Goebel's avatar
Mathias Goebel committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
 : :)
declare function local:transform-entity($nodes as node()+, $id as xs:string)
as node()* {
for $node in $nodes
let $thisId := generate-id($node)
return
    if ($thisId = $id)
    then
      (: set 'active' class :)
        element { node-name($node) } {
            $node/@*[local-name() != "class"],
            attribute class {
                string($node/@class),
                "fhighlighted"
            },
            local:transform-entity($node/node(), $id)
        }
    else if(starts-with($id, $thisId))
    then
        typeswitch ($node)
        case element(*) return
            element { node-name($node) } {
                if(contains($node/@class, "tab-pane"))
                then
                    ($node/@*[local-name() != "class"],
                    attribute class {
                        string($node/@class),
                        "active"
                    })
                else
                    $node/@*,
                local:transform-entity($node/node(), $id)
            }
        default return
            $node
    else $node
};

Mathias Goebel's avatar
Mathias Goebel committed
115
116
declare function ixv:main($model as map()*, $node as node(), $index as xs:string) {
if ($ixp:notebookParam)
117
then
Mathias Goebel's avatar
Mathias Goebel committed
118
    ixp:register($index)[./@id = $index]
Mathias Goebel's avatar
Mathias Goebel committed
119
    => functx:change-element-ns-deep("http://www.w3.org/1999/xhtml", "")
Mathias Goebel's avatar
cleanup    
Mathias Goebel committed
120
else
121
122
    let $doc := doc( local:cache() )
    return
Mathias Goebel's avatar
Mathias Goebel committed
123
        if ($ixp:getEntity)
124
        then
Mathias Goebel's avatar
Mathias Goebel committed
125
            let $id := generate-id($doc//xhtml:li[@id = $ixp:getEntity])
126
127
128
129
            return
                local:transform-entity($doc/xhtml:div/xhtml:div[@id = $index], $id)
        else
            $doc/*/xhtml:div[@id = $index]
Mathias Goebel's avatar
Mathias Goebel committed
130
131
};

Mathias Goebel's avatar
Mathias Goebel committed
132
declare function ixv:tabs($nodes as node(), $model as map(*), $index as xs:string) {
Mathias Goebel's avatar
Mathias Goebel committed
133
134
135
    local:transfrom-tabs($nodes/*, $index)
};

Mathias Goebel's avatar
   
Mathias Goebel committed
136
137
138
(:~
 : Recursive transformation of a given node from the template to a
 : represent a list of tabs with current index view marked `active`.
Mathias Goebel's avatar
Mathias Goebel committed
139
140
 : @param $nodes Nodes to transform
 : @param $index The index that should be marked `active`
Mathias Goebel's avatar
   
Mathias Goebel committed
141
142
143
144
145
146
147
148
149
150
151
152
153
 :   :)
declare
  %test:name("mark active tab")

  %test:arg("nodes", "<li xmlns=""http://www.w3.org/1999/xhtml""><a href=""register-listEvent.html"">Ereignisse</a></li>")
  %test:arg("index", "listEvent")
  %test:assertXPath("$result/@class = 'active'")

  %test:arg("nodes", "<div xmlns=""http://www.w3.org/1999/xhtml""/>")
  %test:arg("index", "listEvent")
  %test:assertEmpty

function local:transfrom-tabs($nodes as element()+, $index as xs:string) {
Mathias Goebel's avatar
Mathias Goebel committed
154
155
156
157
for $node in $nodes
return
    typeswitch ( $node )
    case element(xhtml:div) return
Mathias Goebel's avatar
   
Mathias Goebel committed
158
        $node/node() ! local:transfrom-tabs(., $index)
Mathias Goebel's avatar
Mathias Goebel committed
159
160
161
    case element(xhtml:ul) return
        element xhtml:ul {
            $node/@*,
Mathias Goebel's avatar
Mathias Goebel committed
162
            $node/* ! local:transfrom-tabs(., $index)
Mathias Goebel's avatar
Mathias Goebel committed
163
164
165
166
167
168
        }
    case element(xhtml:li) return
        element xhtml:li {
            $node/@*[not(@class)],
            if($node/xhtml:a/@href = "register-" || $index || ".html")
            then
Mathias Goebel's avatar
   
Mathias Goebel committed
169
                attribute class { $node/@class ! string(.), "active"}
Mathias Goebel's avatar
Mathias Goebel committed
170
            else $node/@class,
Mathias Goebel's avatar
Mathias Goebel committed
171
            $node/* ! local:transfrom-tabs(., $index)
Mathias Goebel's avatar
Mathias Goebel committed
172
173
174
        }
    case element(xhtml:a) return
        element xhtml:a {
Mathias Goebel's avatar
Mathias Goebel committed
175
            if($ixp:notebookParam)
Mathias Goebel's avatar
Mathias Goebel committed
176
177
            then
                ($node/@*[local-name() != "href"],
Mathias Goebel's avatar
Mathias Goebel committed
178
                attribute href { string($node/@href) || "?nb=" || $ixp:notebookParam })
Mathias Goebel's avatar
Mathias Goebel committed
179
180
181
182
183
184
            else $node/@*,
            $node/(*,text())
        }
    default return
        $node
};
Mathias Goebel's avatar
Mathias Goebel committed
185
186

declare function local:login($col) {
Mathias Goebel's avatar
Mathias Goebel committed
187
188
189
190
191
192
(: check for db availability :)
if(httpclient:get(xs:anyURI("http://localhost:8080"), false(), ())/@statusCode = 200)
then
    if(false() = xmldb:login($col, config:get('sade.user'), config:get("sade.password")))
    then (xmldb:login($col, "admin", ""))
    else true()
Mathias Goebel's avatar
Mathias Goebel committed
193
194
else true()
};
195

Mathias Goebel's avatar
Mathias Goebel committed
196
declare function ixv:item($node, $map, $e as xs:string?, $nb as xs:string?) {
Mathias Goebel's avatar
Mathias Goebel committed
197
    if(string-length($e) gt 1)
198
199
200
    then
        index:rendered-entity($e)
    else ()
201
};
Mathias Goebel's avatar
Mathias Goebel committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258

declare function ixv:items($node, $map, $nb as xs:string, $index as xs:string) {
let $sourceDoc := doc("/db/sade-projects/textgrid/data/xml/data/" || $nb || ".xml")/tei:TEI/tei:sourceDoc
return
switch ($index)
    case "listPerson"
    return
        let $prefix := "psn:"
        let $references := ($sourceDoc//tei:rs[contains(@ref, $prefix)]/string(@ref) ! tokenize(., " "))[starts-with(., $prefix)]
        let $list := $references ! substring-after(., $prefix)
        let $persDoc := doc("/db/sade-projects/textgrid/data/xml/data/253sx.xml")//tei:body/tei:listPerson
        return
            ixp:persons( $persDoc, $list)
    case "listPlace" return
        let $prefix := "plc:"
        let $references := ($sourceDoc//tei:rs[contains(@ref, $prefix)]/string(@ref) ! tokenize(., " "))[starts-with(., $prefix)]
        let $list := $references ! substring-after(., $prefix)
        let $placeDoc := doc("/db/sade-projects/textgrid/data/xml/data/253t2.xml")//tei:body/tei:listPlace
        return
            ixp:places( $placeDoc, $list )
    case "listOrg" return
        let $prefix := "org:"
        let $references := ($sourceDoc//tei:rs[contains(@ref, $prefix)]/string(@ref) ! tokenize(., " "))[starts-with(., $prefix)]
        let $list := $references ! substring-after(., $prefix)
        let $orgDoc := doc("/db/sade-projects/textgrid/data/xml/data/253t1.xml")//tei:body/tei:listOrg
        return
            ixp:orgs( $orgDoc, $list )
    case "listEvent" return
        let $prefix := "eve:"
        let $references := ($sourceDoc//tei:rs[contains(@ref, $prefix)]/string(@ref) ! tokenize(., " "))[starts-with(., $prefix)]
        let $list := $references ! substring-after(., $prefix)
        let $eventDoc := doc("/db/sade-projects/textgrid/data/xml/data/253t0.xml")//tei:body/tei:listEvent
        return
            ixp:events( $eventDoc, $list )
    case "list-works" return
        let $prefix := "wrk:"
        let $references := ($sourceDoc//tei:rs[contains(@ref, $prefix)]/string(@ref) ! tokenize(., " "))[starts-with(., $prefix)]
        let $list := $references ! substring-after(., $prefix)
        let $itemDoc := doc("/db/sade-projects/textgrid/data/xml/data/253t3.xml")//tei:list[@type="works"]
        return
            ixp:works( $itemDoc, $list )
    case "list-Fontane" return
        let $prefix := "wrk:"
        let $references := ($sourceDoc//tei:rs[contains(@ref, $prefix)]/string(@ref) ! tokenize(., " "))[starts-with(., $prefix)]
        let $list := $references ! substring-after(., $prefix)
        let $itemDoc := doc("/db/sade-projects/textgrid/data/xml/data/253t3.xml")//tei:list[@type="Fontane"]
        return
            ixp:works( $itemDoc, $list )
    case "list-periodicals" return
        let $prefix := "wrk:"
        let $references := ($sourceDoc//tei:rs[contains(@ref, $prefix)]/string(@ref) ! tokenize(., " "))[starts-with(., $prefix)]
        let $list := $references ! substring-after(., $prefix)
        let $itemDoc := doc("/db/sade-projects/textgrid/data/xml/data/253t3.xml")//tei:list[@type="periodicals"]
        return
            ixp:works( $itemDoc, $list )
default return ()
};