index-viewer.xqm 7.44 KB
Newer Older
Mathias Goebel's avatar
Mathias Goebel committed
1
2
3
4
5
6
7
8
9
10
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
:)
module namespace f-indexviewer="http://fontane-nb.dariah.eu/indexviewer";

import module namespace f-indexproc="http://fontane-nb.dariah.eu/indexproc" at "index-processor.xqm";
Mathias Goebel's avatar
   
Mathias Goebel committed
11
import module namespace config="http://textgrid.de/ns/SADE/config" at "../config/config.xqm";
Mathias Goebel's avatar
Mathias Goebel committed
12
13
14
15

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
16
declare namespace test="http://exist-db.org/xquery/xqsuite";
Mathias Goebel's avatar
Mathias Goebel committed
17
18
declare namespace xhtml="http://www.w3.org/1999/xhtml";

Mathias Goebel's avatar
Mathias Goebel committed
19
20
21
22
23
24
25
declare
  %test:setUp
function f-indexviewer:setup() {
if( xmldb:collection-available("/db/sade-projects/textgrid") )
then true()
else
    (: read test config :)
Mathias Goebel's avatar
Mathias Goebel committed
26
    let $testconfig := doc("tests.xml")//testdata[@module="f-indexviewer"]
Mathias Goebel's avatar
Mathias Goebel committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    (: prepare the collections :)
    let $prepare-collections :=
        for $create in $testconfig//create/collection
        return
            xmldb:create-collection($create/@target, $create/@new)
    (: prepare documents :)
    let $prepare-documents :=
        for $create in $testconfig//create/document
        return
            xmldb:store($create/@collection, $create/@name, $create/*)
    let $prepare-documents-from-template :=
        for $create in $testconfig//create/document-template
        let $template := $create/template/*
        return
            for $data in $create//data
            let $doc := doc( xmldb:store($create/@collection, $data/@name, $template) )
            return
                update insert $data/* into $doc//tei:body
    return true()
};
Mathias Goebel's avatar
Mathias Goebel committed
47
48
49
50
51
declare
  %test:tearDown
function f-misc:teardown(){
    xmldb:remove("/db/sade-projects")
};
Mathias Goebel's avatar
Mathias Goebel committed
52

Mathias Goebel's avatar
   
Mathias Goebel committed
53
(: Main cache function
Mathias Goebel's avatar
Mathias Goebel committed
54
 : @return path to the cached document :)
Mathias Goebel's avatar
   
Mathias Goebel committed
55
56
57
58
declare
  %test:name("cache")

  %test:assertTrue
Mathias Goebel's avatar
Mathias Goebel committed
59
(:  %test:pending (: throws NPE on non-initialized db. needs some data from the grid. :):)
Mathias Goebel's avatar
   
Mathias Goebel committed
60
function local:cache()
Mathias Goebel's avatar
Mathias Goebel committed
61
 {
Mathias Goebel's avatar
Mathias Goebel committed
62
let $set-option := util:declare-option("exist:output-size-limit", "2000000")
Mathias Goebel's avatar
Mathias Goebel committed
63
64
65
66
67
68
69
70
71
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 :)
let $lastMod := $uris ! xmldb:last-modified($f-indexproc:dataPath, . || '.xml')

return
Mathias Goebel's avatar
Mathias Goebel committed
72
    if(not(local:login($collection-uri)))
Mathias Goebel's avatar
   
Mathias Goebel committed
73
    then error(QName("FONTANE", "INDEXVIEW1"), "unable to authenticate.")
Mathias Goebel's avatar
Mathias Goebel committed
74
    else
Mathias Goebel's avatar
   
Mathias Goebel committed
75
76
77
78
79
80
        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 :)
Mathias Goebel's avatar
Mathias Goebel committed
81
            xmldb:store($collection-uri, $resource, <xhtml:div>{util:eval('f-indexproc:register()')}</xhtml:div>)
Mathias Goebel's avatar
   
Mathias Goebel committed
82
83
84
85
86
87
88
89
90
91
92
93
94
};

(: forces the creation of a new cache file
 : @return document-node() of the index cache file :)
declare function f-indexviewer:prepare-new-cache()
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()
return
    doc( local:cache() )
Mathias Goebel's avatar
Mathias Goebel committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
};

(:~
 : Recursive transformation of the index document, if a specific item is requested.
 : @param $nodes – node to be parsed
 : @param $id – a single id prepared with generate-id()
 : :)
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
};

declare function f-indexviewer:main($model as map()*, $node as node(), $index as xs:string) {
if ($f-indexproc:notebookParam)
141
142
then
    f-indexproc:register($index)[./@id = $index]
143
144
    (: xhtml prefixes w/o xmlns attr in the output. work around with: :)
    => serialize() => parse-xml()
Mathias Goebel's avatar
cleanup    
Mathias Goebel committed
145
else
146
147
148
149
150
151
152
153
154
    let $doc := doc( local:cache() )
    return
        if ($f-indexproc:getEntity)
        then
            let $id := generate-id($doc//xhtml:li[@id = $f-indexproc:getEntity])
            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
155
156
157
158
159
160
};

declare function f-indexviewer:tabs($nodes as node(), $model as map(*), $index as xs:string) {
    local:transfrom-tabs($nodes/*, $index)
};

Mathias Goebel's avatar
   
Mathias Goebel committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
(:~
 : Recursive transformation of a given node from the template to a
 : represent a list of tabs with current index view marked `active`.
 : @param $nodes – nodes to transform
 : @param $index – the index that should be marked `active`
 :   :)
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
179
180
181
182
for $node in $nodes
return
    typeswitch ( $node )
    case element(xhtml:div) return
Mathias Goebel's avatar
   
Mathias Goebel committed
183
        $node/node() ! local:transfrom-tabs(., $index)
Mathias Goebel's avatar
Mathias Goebel committed
184
185
186
    case element(xhtml:ul) return
        element xhtml:ul {
            $node/@*,
Mathias Goebel's avatar
Mathias Goebel committed
187
            $node/* ! local:transfrom-tabs(., $index)
Mathias Goebel's avatar
Mathias Goebel committed
188
189
190
191
192
193
        }
    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
194
                attribute class { $node/@class ! string(.), "active"}
Mathias Goebel's avatar
Mathias Goebel committed
195
            else $node/@class,
Mathias Goebel's avatar
Mathias Goebel committed
196
            $node/* ! local:transfrom-tabs(., $index)
Mathias Goebel's avatar
Mathias Goebel committed
197
198
199
200
201
202
203
204
205
206
207
208
209
        }
    case element(xhtml:a) return
        element xhtml:a {
            if($f-indexproc:notebookParam)
            then
                ($node/@*[local-name() != "href"],
                attribute href { string($node/@href) || "?nb=" || $f-indexproc:notebookParam })
            else $node/@*,
            $node/(*,text())
        }
    default return
        $node
};
Mathias Goebel's avatar
Mathias Goebel committed
210
211

declare function local:login($col) {
Mathias Goebel's avatar
Mathias Goebel committed
212
213
214
215
216
217
(: 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
218
219
else true()
};