Commit 6f2fcc9e authored by Mathias Goebel's avatar Mathias Goebel 🎠
Browse files

refactored wiki module for exist 5.2.0

parent 52844fb5
......@@ -39,8 +39,6 @@ declare function wiki:dokuwiki($node as node(), $model as map(*), $id as xs:stri
as node()* {
(: check for valid id and replace id not in whitelist :)
let $id:= if($id = doc('/db/sade-projects/textgrid/data/xml/data/2shfj.xml')//text()) then $id else 'gesamtdokumentation'
(: trigger image reload :)
let $imageLoad := if( request:get-parameter-names() = "reload" ) then wiki:dokuwikiImageReload($id) else false()
let $doc :=
doc(
......@@ -80,28 +78,6 @@ return
}
};
(:~
: Gets the currently stored revision number in the database for a given id
: @param $id A pagename in the wikispace according to config.xml
: @return the revision number stored in the database
:)
declare function wiki:revision-local($id as xs:string)
as xs:int {
let $document-name as xs:string? :=
xmldb:get-child-resources('/sade-projects/textgrid/data/xml/doku/')
[substring-before(., '.rev') = $id]
[not(ends-with(., ".lock.xml"))]
return
if(not($document-name))
then xs:int(-1)
else
$document-name
=> substring-after('.rev')
=> substring-before('.xml')
=> xs:int()
};
(:~
: Gets the last revision number from the wikis feed and removes an old version
: in the database. TODO: move remove operation somewhere else
......@@ -137,17 +113,37 @@ return
else xs:int(-1)
};
(:~
: Gets the currently stored revision number in the database for a given id
: @param $id A pagename in the wikispace according to config.xml
: @return the revision number stored in the database
:)
declare function wiki:revision-local($id as xs:string)
as xs:int {
let $document-name as xs:string? :=
xmldb:get-child-resources('/sade-projects/textgrid/data/xml/doku/')
[substring-before(., '.rev') = $id]
[not(ends-with(., ".lock.xml"))]
return
if(not($document-name))
then xs:int(-1)
else
$document-name
=> substring-after('.rev')
=> substring-before('.xml')
=> xs:int()
};
declare function wiki:lastModProcessor() {
xmldb:last-modified("/db/apps/SADE/modules/wiki", "wiki.xqm")
};
declare function wiki:lock-resource($id as xs:string) {
wiki:login(),
xmldb:store('/sade-projects/textgrid/data/xml/doku/', $id || ".lock.xml", <lock/>)
};
declare function wiki:unlock-resource($id as xs:string) {
wiki:login(),
xmldb:remove('/sade-projects/textgrid/data/xml/doku/', $id || ".lock.xml")
};
......@@ -168,6 +164,7 @@ declare
function wiki:reload($id as xs:string) {
let $remote := wiki:revision-remote($id)
let $local := wiki:revision-local($id)
return
if( $remote le wiki:revision-local($id) )
then
......@@ -191,34 +188,17 @@ let $url :=
} catch * {
error(QName("FONTANE", "DOKU04"), "unable to load resource." )
},
$result := if( $export//DIV )
then wiki:HTML2html($export)
else $export,
$result := $export,
$result := <div>{wiki:dokuwikiparser( $result/*[not(@id='dw__toc')] )}</div>,
$login := wiki:login(),
$remove := if($local = -1) then true() else
xmldb:remove('/sade-projects/textgrid/data/xml/doku/', $id || '.rev' || $local || '.xml'),
$store := xmldb:store( '/sade-projects/textgrid/data/xml/doku/', $id || '.rev' || $remote || '.xml' , $result ),
$imageReload := wiki:dokuwikiImageReload($id)
$store := xmldb:store( '/sade-projects/textgrid/data/xml/doku/', $id || '.rev' || $remote || '.xml' , $result )
let $unlock := wiki:unlock-resource($id)
let $unlock := wiki:unlock-resource($id)
return
<true/>
};
(:~
: Helper function to recursivly translate upper case element names to lower case
:)
declare function wiki:HTML2html($node as node())
as node() {
for $node in $node
return
typeswitch ( $node )
case element() return
element { lower-case($node/local-name()) } { $node/@*, wiki:HTML2html($node/node()) }
default return $node
};
declare function wiki:dokuwikiparser($nodes as node()*){
wiki:dokuwikiparser($nodes, false())
};
......@@ -227,46 +207,46 @@ declare function wiki:dokuwikiparser($nodes as node()*, $char as xs:boolean){
let $char := false()
for $node in $nodes return
typeswitch($node)
case element(h1)
case element(xhtml:h1)
return
element xhtml:h2 {
<div class="block-header">
<h2><span class="title">{wiki:dokuwikiparser($node/text(), $char)}</span><span class="decoration"></span><span class="decoration"></span><span class="decoration"></span></h2>
<div id="toggleToc"><i class="fa fa-caret-down" aria-hidden="true"></i></div>
<div id="dokuToc">
{ wiki:dokuwikiTOC( $node/parent::div/div[@id="dw__toc"]/div/ul/li/ul) }
{ $node/root()//xhtml:div[@id="dw__toc"]/xhtml:div/xhtml:ul/xhtml:li/xhtml:ul ! wiki:dokuwikiTOC(.) }
</div>
</div>
}
case element(h2)
case element (xhtml:h2)
return
element xhtml:h3 {
$node/@id,
<span class="dokuLink"><a href="#{$node/@id}"> <i class="fa fa-link" aria-hidden="true"></i></a></span>,
wiki:dokuwikiparser($node/node(), $char)
}
case element(h3)
case element (xhtml:h3)
return
element xhtml:h4 {
$node/@id,
<span class="dokuLink"><a href="#{$node/@id}"> <i class="fa fa-link" aria-hidden="true"></i></a></span>,
wiki:dokuwikiparser($node/node(), $char)
}
case element(h4)
case element (xhtml:h4)
return
element xhtml:h5 {
$node/@id,
<span class="dokuLink"><a href="#{$node/@id}"> <i class="fa fa-link" aria-hidden="true"></i></a></span>,
wiki:dokuwikiparser($node/node(), $char)
}
case element(h5)
case element (xhtml:h5)
return
element xhtml:h6 {
$node/@id,
<span class="dokuLink"><a href="#{$node/@id}"> <i class="fa fa-link" aria-hidden="true"></i></a></span>,
wiki:dokuwikiparser($node/node(), $char)
}
case element(h6)
case element(xhtml:h6)
return
element xhtml:h7 {
$node/@id,
......@@ -274,7 +254,7 @@ for $node in $nodes return
wiki:dokuwikiparser($node/node(), $char)
}
case attribute(class) return ()
case element(div)
case element (xhtml:div)
return
element xhtml:div {(
if($node/@class = "noteimportant")
......@@ -283,40 +263,40 @@ for $node in $nodes return
else (),
wiki:dokuwikiparser($node/node(), $char)
)}
case element(br) return
case element(xhtml:br) return
element xhtml:br {}
case element(table)
case element (xhtml:table)
return
element xhtml:table {
attribute class {'table'},
wiki:dokuwikiparser($node/node(), $char)
}
case element (thead)
case element (xhtml:thead)
return
element xhtml:thead {
wiki:dokuwikiparser($node/node(), $char)
}
case element (th)
case element (xhtml:th)
return
element xhtml:th {
wiki:dokuwikiparser($node/node(), $char)
}
case element (tr)
case element (xhtml:tr)
return
element xhtml:tr {
wiki:dokuwikiparser($node/node(), $char)
}
case element (tbody)
case element (xhtml:tbody)
return
element xhtml:tbody {
wiki:dokuwikiparser($node/node(), $char)
}
case element (td)
case element (xhtml:td)
return
element xhtml:td {
wiki:dokuwikiparser($node/node(), $char)
}
case element(a)
case element (xhtml:a)
return
if(exists($node/img)) then wiki:dokuwikiparser($node/node(), $char) else
element xhtml:a {
......@@ -332,51 +312,42 @@ for $node in $nodes return
$node/@id,
wiki:dokuwikiparser($node/node(), $char)
}
case element(p)
case element (xhtml:p)
return
element xhtml:p {
wiki:dokuwikiparser($node/node(), $char)
}
case element (ul)
case element (xhtml:ul)
return
element xhtml:ul {
wiki:dokuwikiparser($node/node(), $char)
}
case element (li)
case element (xhtml:li)
return
element xhtml:li {
wiki:dokuwikiparser($node/node(), $char)
}
case element (pre)
case element (xhtml:pre)
return
$node
(: let $code := try {:)
(: string($node) => parse-xml():)
(: } catch * {:)
(: try {:)
(: ("<temporary-wrap>" || string($node) || "</temporary-wrap>") => parse-xml():)
(: } catch * {<temporary-wrap>{string($node)}</temporary-wrap>}:)
(: }:)
(: return:)
(: code:main($code) => wiki:remove-temporary-root() => functx:change-element-ns-deep("http://www.w3.org/1999/xhtml", ""):)
case element (em)
case element (xhtml:em)
return
element xhtml:em {
if($node/@class = 'u') then attribute class {'underline'} else (),
wiki:dokuwikiparser($node/node(), $char)
}
case element (strong)
case element (xhtml:strong)
return
element xhtml:strong {
wiki:dokuwikiparser($node/node(), $char)
}
case element (del)
case element (xhtml:del)
return
element xhtml:del {
wiki:dokuwikiparser($node/node(), $char)
}
case element (img)
case element (xhtml:img)
return
(element xhtml:img {
$node/@alt,
......@@ -388,81 +359,16 @@ for $node in $nodes return
}, () )
case text() return $node
default return wiki:dokuwikiparser($node/node(), $char)
(: case text() return $node:)
(: default return wiki:dokuwikiparser($node/node()):)
};
declare function wiki:dokuwikiTOC($nodes) {
declare function wiki:dokuwikiTOC($nodes as node()+) {
for $node in $nodes return
typeswitch($node)
case element(div) return
case element (xhtml:div) return
wiki:dokuwikiTOC($node/node())
case element() return
case element () return
element { $node/local-name() } { $node/@*[local-name() != 'class'], wiki:dokuwikiTOC($node/node()) }
case text() return $node
default return wiki:dokuwikiTOC($node/node())
};
declare function wiki:dokuwikiImageReload($id as xs:string) {
try {
let $path := system:get-exist-home() || '/tools/jetty/webapps/portal/public/doku/'
let $credentials := '&amp;u='|| config:get("dokuwiki.user") ||'&amp;p='|| config:get("dokuwiki.password")
let $url:= config:get("dokuwiki.url")||'/doku.php?id=fontane:'|| $id ||'&amp;do=export_xhtml' || $credentials,
$request :=
<hc:request method="get" href="{ $url }">
<hc:header name="Accept-Charset" value="UTF-8"/>,
</hc:request>,
$result:= hc:send-request($request)[2]//div[@class="dokuwiki export"]/*[not(@id='dw__toc')]
let $images := distinct-values( $result//img/substring-after(@data-original, 'fontane:') )
return
for $image in $images
return
let $id := $image
let $req := <http:request href="{config:get("dokuwiki.url") ||"/lib/exe/fetch.php?media=fontane:"|| $id || $credentials }" method="get">
<http:header name="Connection" value="close"/>
</http:request>
let $result := try {http:send-request($req)}
catch * {string($err:code)}
let $mime := xs:string($result[1]//http:header[@name="content-type"]/@value)
let $last-modified := xs:string($result[1]//http:header[@name="last-modified"]/@value)
let $cache-control := xs:string($result[1]//http:header[@name="cache-control"]/@value)
let $image := xs:base64Binary($result[2])
let $login:= wiki:login()
let $store := if( file:exists($path || $id)) then () else file:serialize-binary($image, $path || $id)
return
()
} catch * {
error(QName("FONTANE", "DOKU03"), "unable to (re-)load images." )
}
};
declare %private function wiki:login() {
if(false() = xmldb:login( '/sade-projects/textgrid/data/xml/doku/', config:get('sade.user') , config:get("sade.password")))
then (xmldb:login('/sade-projects/textgrid/data/xml/doku/', "admin", ""))
else true()
};
declare function wiki:remove-temporary-root($nodes) {
let $test := <xhtml:span class="hljs-tag">&lt;<xhtml:span class="hljs-name">temporary-wrap</xhtml:span>&gt;</xhtml:span>
for $node in $nodes
return
typeswitch ( $node )
case element( xhtml:span ) return
if( deep-equal($node, $test) ) then ()
else
element {$node/node-name()} {
$node/@*,
wiki:remove-temporary-root($node/node())
}
case text() return
normalize-space($node)
default return
element {$node/node-name()} {
$node/@*,
wiki:remove-temporary-root($node/node())
}
};
};
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment