index-viewer.xqm 7.17 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
declare
  %test:setUp
function f-indexviewer:setup() {
if( xmldb:collection-available("/db/sade-projects/textgrid") )
then true()
else
    (: read test config :)
    let $testconfig := doc("tests.xml")
    (: 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
48
(: Main cache function
Mathias Goebel's avatar
Mathias Goebel committed
49
 : @return path to the cached document :)
Mathias Goebel's avatar
   
Mathias Goebel committed
50
51
52
53
declare
  %test:name("cache")

  %test:assertTrue
Mathias Goebel's avatar
Mathias Goebel committed
54
(:  %test:pending (: throws NPE on non-initialized db. needs some data from the grid. :):)
Mathias Goebel's avatar
   
Mathias Goebel committed
55
function local:cache()
Mathias Goebel's avatar
Mathias Goebel committed
56
 {
Mathias Goebel's avatar
Mathias Goebel committed
57
let $set-option := util:declare-option("exist:output-size-limit", "2000000")
Mathias Goebel's avatar
Mathias Goebel committed
58
59
60
61
62
63
64
65
66
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
67
    if(not(local:login($collection-uri)))
Mathias Goebel's avatar
   
Mathias Goebel committed
68
    then error(QName("FONTANE", "INDEXVIEW1"), "unable to authenticate.")
Mathias Goebel's avatar
Mathias Goebel committed
69
    else
Mathias Goebel's avatar
   
Mathias Goebel committed
70
71
72
73
74
75
        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
76
            xmldb:store($collection-uri, $resource, <xhtml:div>{util:eval('f-indexproc:register()')}</xhtml:div>)
Mathias Goebel's avatar
   
Mathias Goebel committed
77
78
79
80
81
82
83
84
85
86
87
88
89
};

(: 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
};

(:~
 : 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)
136
137
then
    f-indexproc:register($index)[./@id = $index]
138
139
    (: xhtml prefixes w/o xmlns attr in the output. work around with: :)
    => serialize() => parse-xml()
Mathias Goebel's avatar
cleanup    
Mathias Goebel committed
140
else
141
142
143
144
145
146
147
148
149
    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
150
151
152
153
154
155
};

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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
(:~
 : 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
174
175
176
177
for $node in $nodes
return
    typeswitch ( $node )
    case element(xhtml:div) return
Mathias Goebel's avatar
   
Mathias Goebel committed
178
        $node/node() ! local:transfrom-tabs(., $index)
Mathias Goebel's avatar
Mathias Goebel committed
179
180
181
    case element(xhtml:ul) return
        element xhtml:ul {
            $node/@*,
Mathias Goebel's avatar
Mathias Goebel committed
182
            $node/* ! local:transfrom-tabs(., $index)
Mathias Goebel's avatar
Mathias Goebel committed
183
184
185
186
187
188
        }
    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
189
                attribute class { $node/@class ! string(.), "active"}
Mathias Goebel's avatar
Mathias Goebel committed
190
            else $node/@class,
Mathias Goebel's avatar
Mathias Goebel committed
191
            $node/* ! local:transfrom-tabs(., $index)
Mathias Goebel's avatar
Mathias Goebel committed
192
193
194
195
196
197
198
199
200
201
202
203
204
        }
    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
205
206

declare function local:login($col) {
Mathias Goebel's avatar
Mathias Goebel committed
207
208
if(false() = xmldb:login($col, config:get('sade.user'), config:get("sade.password")))
then (xmldb:login($col, "admin", ""))
Mathias Goebel's avatar
Mathias Goebel committed
209
210
else true()
};