Oppdatert script for utlisting av generelle se-henv.

xquery version "3.0";

(: Warning: This query will be slow ( >~ 1 min on my computer)
   unless you add indexes on 'term-id' and 'hovedemnefrase' :)

declare function local:term($posts as element()*, $token as xs:string)
as element()*
{
    let $token2 := tokenize($token, ' \(')[1]
    let $rlp := $posts/post[hovedemnefrase = $token2]
    return if ($rlp/se-id) then
        $posts/post[term-id = $rlp/se-id][1]
    else
        $rlp[1]
};

declare function local:toppterm($posts as element()*, $token as xs:string)
as xs:string*
{
    let $ttid := local:term($posts, $token)/toppterm-id/text()[1]
    let $ttpost := $posts/post[term-id = $ttid]
    return
        if (count($ttpost) = 0) then "(ikke funnet)"
        else $ttpost[1]/hovedemnefrase/text()
};

<posts>
{
    let $posts := doc("humord.xml")/hume/ .
	for $post in $posts/post[gen-se-henvisning]
	return <post>
		{ $post/hovedemnefrase }
		{
		    let $parts := tokenize($post/gen-se-henvisning/text(), ' \* ')
		    return (
		        <part1>{ $parts[1] }</part1>,
		        <toppterm1>{ local:toppterm($posts, $parts[1]) }</toppterm1>,

		            if (count($parts) > 1) then
		                (
		                    		        <part2>{ $parts[2] }</part2>,
		        <toppterm2>{ local:toppterm($posts, $parts[2]) }</toppterm2>) else ()

		        )
		}
	</post>
}
</posts>

<posts>
{
	for $post in doc("humord.xml")/hume/post[gen-se-henvisning]
	return $post
}
</posts>


Add bg-colour to footer nav

Add bg-colour to footer nav

xquery version "1.0" encoding "utf-8";

import module namespace calli = "http://callimachusproject.org/rdf/2009/framework#" at "/callimachus/layout-functions.xq";

    <meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0,target-densityDpi=device-dpi"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <link rel="icon" href="/favicon.ico" />
    {calli:styles-href(<link rel="stylesheet" />)}
    {calli:scripts-src(
    <![endif]-->
    {calli:head-nodes()}

    {calli:body-attributes()}

                        {calli:home-href(Callimachus)}

                        {calli:login-href(Sign in )}

{calli:lookup-form('Lookup...')}

                            <button type="button" class="btn btn-default navbar-btn dropdown-toggle" data-toggle="dropdown">
                                Main menu

                            </button>

{calli:folder-href(Home folder)}

{calli:changes-href(Recent changes)}

{calli:lookup-href(Lookup resources)}

{calli:callimachus-about-href(About Callimachus)}

{calli:callimachus-getting-started-href(Getting started)}

{calli:callimachus-feedback-href(Send feedback)}

                                {calli:head-links(

,
)}

{calli:whatlinkshere-href(What links here)}

{calli:relatedchanges-href(Related changes)}

{calli:permissions-href(Permissions)}

{calli:introspect-href(Introspect resource)}

Print this page

{calli:profile-href(Account)}

{calli:logout-href(Sign out)}

{calli:breadcrumb-links(

,
)}

            {calli:activate-nav(

{calli:view-href(View)}

{calli:edit-href(Edit)}

{calli:discussion-href(Discussion)}

{calli:describe-href(Describe)}

{calli:history-href(History)}

            )}
            {calli:error-alert(

                <button type="button" class="close" data-dismiss="alert">×</button>

Oops!

)}

        {calli:body-nodes()}

                {calli:generator-p(
)}
                {calli:lastmod-time(
This resource was last modified at <time class="datetime-local"/>
)}


[test] Fixes to map tests. ne on an empty-sequence() is tricksy!

(:
 : eXist-db Open Source Native XML Database
 : Copyright (C) 2001 The eXist-db Authors
 :
 : info@exist-db.org
 : http://www.exist-db.org
 :
 : This library is free software; you can redistribute it and/or
 : modify it under the terms of the GNU Lesser General Public
 : License as published by the Free Software Foundation; either
 : version 2.1 of the License, or (at your option) any later version.
 :
 : This library is distributed in the hope that it will be useful,
 : but WITHOUT ANY WARRANTY; without even the implied warranty of
 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 : Lesser General Public License for more details.
 :
 : You should have received a copy of the GNU Lesser General Public
 : License along with this library; if not, write to the Free Software
 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 :)
xquery version "3.1";

module namespace mt="http://exist-db.org/xquery/test/maps";

import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";

declare %private function mt:getMapFixture () {
    map {
        "Su" : "Sunday",
        "Mo" : "Monday",
        "Tu" : "Tuesday",
        "We" : "Wednesday",
        "Th" : "Thursday",
        "Fr" : "Friday",
        "Sa" : "Saturday"
    }
};

declare variable $mt:daysOfWeek := mt:getMapFixture();

declare variable $mt:integerKeys := map {
    1 : "Sunday",
    2 : "Monday",
    3 : "Tuesday",
    4 : "Wednesday",
    5 : "Thursday",
    6 : "Friday",
    7 : "Saturday"
};

declare variable $mt:mapOfSequences := map {0: (), 1 : ("One", "Two") };

declare
    %test:assertEquals("Wednesday")
function mt:createLiteral1() {
    $mt:daysOfWeek("We")
};

declare
    %test:assertEquals("Wednesday")
function mt:createWithIntKeys() {
    let $map := map {
        1 : "Sunday",
        2 : "Monday",
        3 : "Tuesday",
        4 : "Wednesday",
        5 : "Thursday",
        6 : "Friday",
        7 : "Saturday"
    }
    return
        $map(4)
};

declare
    %test:assertError("err:XPST0003")
function mt:draftSyntaxNotAllowed () {
    (: this needs to be evaled, to catch the static compilation error :)
    util:eval('map { 1 := 1 }')
};

declare
    %test:assertEquals(20)
function mt:createWithFunctionVariable() {
    let $fn := function($p) { $p * 2 }
    let $map := map { "callback" : $fn }
    return
        map:get($map, "callback")(10)
};

declare
    %test:assertEquals(2)
function mt:createWithFunctionValue() {
    let $map := map { "callback" : function($p) { $p * 2 } }
    return
        map:get($map, "callback")(1)
};

declare
    %test:assertEquals("Sunday")
function mt:createWithEntry() {
    let $days := ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
    let $map := map:merge($days ! map:entry(substring(., 1, 2), .))
    return
        $map("Su")
};

declare
    %test:assertXPath("count($result) = 8")
    %test:assertXPath("8 = $result")
function mt:createFromTwoMaps() {
    let $map := map:merge(($mt:integerKeys, map { 8 : "Caturday" }))
    return
        map:keys($map)
};

declare
    %test:assertEquals(0)
function mt:size-empty() {
    let $m := map {}
    return
      map:size($m)
};

declare
    %test:assertEquals("Good")
function mt:put-empty() {
    let $m := map {}
    return
      map:put($m, "Cats", "Good")("Cats")
};

declare
    %test:assertEquals("Bad")
function mt:put-single() {
    let $m := map {
        "Cats" : "Good"
    }
    return
      map:put($m, "Dogs", "Bad")("Dogs")
};

declare
    %test:assertEquals("Duck")
function mt:put() {
    let $m := map {
        "Cats" : "Good",
        "Dogs" : "Bad",
        1 : "Chicken"
    }
    return
      map:put($m, 2, "Duck")(2)
};

declare
    %test:assertEquals(4)
function mt:size() {
    let $m := map {
        "Cats": "Good",
        "Dogs": "Bad",
        1: "Chicken",
        2: "Duck"
    }
    return
        map:size($m)
};

declare
    %test:assertEquals("Sunday", "Tuesday", "Thursday", "Saturday")
function mt:for-each() {
    map:for-each($mt:integerKeys, function($key, $value) {
        if ($key mod 2) then ($value) else ()
    })
};

declare
    %test:assertEquals(3)
function mt:for-each2() {
    let $nm := map:merge(
        map:for-each(
            map { "a": 1, "b": 2 },
            function($k, $v) { map:entry($k, $v + 1) }
        )
    )

    return
        $nm?b
};

declare
    %test:assertEquals("Sunday")
function mt:createWithSingleKey() {
    let $map := map { "Su": "Sunday" }
    return
        $map("Su")
};

declare
    %test:assertEquals("Saturday", "Caturday")
function mt:overwriteKeyInNewMap() {
    let $specialWeek := map:merge(($mt:integerKeys, map { 7 : "Caturday" }))
    return
        ($mt:integerKeys(7), $specialWeek(7))
};

declare
    %test:assertEmpty
function mt:mapEmptyValue() {
    let $map := $mt:mapOfSequences
    return
        $map(0)
};

declare
    %test:assertEquals("One", "Two")
function mt:mapSequenceValue() {
    let $map := $mt:mapOfSequences
    return
        $map(1)
};

declare
    %test:assertEquals("Heinz", "Roland", "Uschi", "Verona")
function mt:orderBzKeyTypeDate () {
    let $map := map {
        xs:date("1975-03-19"): "Uschi",
        xs:date("1980-01-22"): "Verona",
        xs:date("1960-06-14"): "Heinz",
        xs:date("1963-10-21"): "Roland"
    }

    for $key in map:keys($map)
    let $name := $map($key)
    order by $name ascending
    return
        $name
};

declare
    %test:assertEquals(0, "One")
function mt:mixedKeyTypes() {
    let $map := map{ "Zero": 0, 1: "One" }
    return
        ($map("Zero"), $map(1))
};

declare
    %test:assertTrue
function mt:containsOnEmptyValue() {
    let $map := map{ 0: () }
    return
        map:contains($map, 0)
};

declare
    %test:assertEquals("Fr", "Mo", "Sa", "Su", "Th", "Tu", "We")
function mt:keysInFor() {
    for $day in map:keys(mt:getMapFixture())
    order by $day ascending
    return
        $day
};

declare
    %test:assertEquals("Su")
function mt:keysOnMapWithSingleKey() {
    let $map := map:entry("Su", "Sunday")
    return
        map:keys($map)
};

declare
    %test:assertFalse
function mt:remove() {
    let $map := mt:getMapFixture()
    return
        map:contains(map:remove($map, "Tu"), "Tu")
};

declare
    %test:assertEquals("Fr", "Mo", "Sa", "Su", "Th")
function mt:remove2() {
    let $map := mt:getMapFixture()
    for $day in map:keys(map:remove(map:remove($map, "We"), "Tu"))
    order by $day
    return
        $day
};

declare
    %test:assertEmpty
function mt:removeSingleKey() {
    let $empty := map:remove(map { "Su": "Sunday" }, "Su")
    return
        map:keys($empty)
};

declare
    %test:assertEquals("Su")
function mt:removeSingleNonExistentKey() {
    let $map := map:remove(map { "Su": "Sunday" }, "Xx")
    return
        map:keys($map)
};

declare
    %test:assertEquals("Sa")
function mt:removeSequenceOfStringKeys() {
    mt:getMapFixture()
        => map:remove(("Mo", "Tu", "We", "Th", "Fr", "Su"))
        => map:keys()
};

declare
    %test:assertEquals("Sa")
function mt:removeSequenceOfStringKeysWithNonExistent() {
    mt:getMapFixture()
        => map:remove(("Mo", "Tu", "We", "Th", "Fr", "Su", "Xx"))
        => map:keys()
};

declare
    %test:assertEquals(7)
function mt:removeRangeOfIntegerKeys() {
    $mt:integerKeys
        => map:remove(1 to 6)
        => map:keys()
};

declare
    %test:assertEquals(7)
function mt:removeIntegerKeysWithNonExistent() {
    $mt:integerKeys
        => map:remove((1, 2, 3, 4, 5, 6, 10))
        => map:keys()
};

declare
    %test:assertTrue
function mt:contains() {
    map:contains($mt:daysOfWeek, "We")
};

declare
    %test:assertFalse
function mt:containsOnMissingKey() {
    map:contains($mt:daysOfWeek, "Al")
};

declare
    %test:assertTrue
function mt:containsSingleKey() {
    map:contains(map { "Su": "Sunday" }, "Su")
};

declare
    %test:assertEquals("Hello world")
function mt:computedKeyValue() {
    let $map := map { 2 + 1 : concat("Hello ", "world") }
    return
        $map(3)
};

declare
    %test:assertEquals("false", "true")
function mt:immutabilityMerge() {
    let $original := mt:getMapFixture()
    let $derived := map:merge(($original, map:entry("Ca", "Caturday")))
    return (
        map:contains($original, "Ca"),
        map:contains($derived, "Ca")
    )
};

declare
    %test:assertEquals("true", "false")
function mt:immutabilityRemove() {
    let $original := mt:getMapFixture()
    let $derived := map:remove($original, "Fr")
    return (
        map:contains($original, "Fr"),
        map:contains($derived, "Fr")
    )
};

declare
    %test:assertEquals("false", "true")
function mt:immutabilityPut() {
    let $original := mt:getMapFixture()
    let $derived := map:put($original, "Ca", "Caturday")
    return (
        map:contains($original, "Ca"),
        map:contains($derived, "Ca")
    )
};

declare
    %test:assertEquals("Sunday")
function mt:sequenceType1() {
    let $map := map { 1 : "Sunday" }
    return
        map:get(mt:mapTest($map), 1)
};

declare
    %test:assertEquals("Sunday")
function mt:sequenceType2() {
    let $map := map { 1 : "Sunday" }
    return
        map:get(mt:mapTestFail($map), 1)
};

declare
    %private
function mt:mapTest($map as map(*)) as map(xs:integer, xs:string) {
    $map
};

declare
    %private
function mt:mapTestFail($map as map(*)) as map(xs:date, xs:string) {
    $map
};

declare
    %test:assertError("EXMPDY001")
    %test:name("Throw error if key is not a single atomic value")
function mt:wrongCardinality() {
    let $map := map { (1, 2) : "illegal" }
    return
        $map(1)
};

declare
    %test:assertEquals("two")
function mt:nestedMaps() {
    let $map := map { 1: map { 1: "one", 2: "two" } }
    return
        $map(1)(2)
};

declare
    %test:assertEquals("Monday")
function mt:nestedMaps2() {
    let $map := map { "week" : map { "days" : $mt:daysOfWeek } }
    return
        $map("week")("days")("Mo")
};

declare
    %test:assertEquals(2)
function mt:qnameKeys() {
    let $map := map { xs:QName("mt:one") : 1, xs:QName("mt:two") : 2 }
    return
        $map(xs:QName("mt:two"))
};

declare
    %test:assertEquals(2)
function mt:longKeys() {
    let $map := map { xs:long(1) : 1, xs:long(2) : 2 }
    return
        $map(2)
};

declare
    %test:args(2)
    %test:assertEquals(3)
    %test:args("Three")
    %test:assertEmpty
function mt:doubleKeys($key as item()) {
    let $map := map { xs:double(1.1) : 1, xs:double(2) : 2 }
    return
        map:merge(($map, map:entry(xs:double(2), 3)))($key)
};

declare
    %test:assertEquals(2, "value")
function mt:mixedNumeric() {
    let $map := map { 1.1 : 1, 2 : 2, "key" : "value" }
    return
        ($map(2), $map("key"))
};

declare
    %test:assertEquals("<c ns='c'/>", "<c ns='c'/>")
function mt:pathExpr() as element(c)+ {
    let $test :=
        <a>
            <b ns="b">
                <c ns="c"/>
            </b>
        </a>
    let $map :=
        map {
            "a" : $test//c,
            "b" : ($test//c)
        }
    return
        ($map("a"), $map("b"))
};

declare
    %test:assertEquals("What were you thinking?")
function mt:lookupUnaryOperator() {
    let $errors := (
      map { "level" : 1, "text" : "Boys will be boys ..." },
      map { "level" : 2, "text" : "What were you thinking?" },
      map { "level" : 3, "text" : "Call the cops!" }
    )
    return $errors[?level = 2]?text
};

declare
    %test:assertEquals("W0342", "M0535")
function mt:lookupMultipleMaps() {
    let $maps := ( map { "id" : "W0342" }, map { "id" : "M0535" } )
    return $maps?id
};

declare
    %test:assertEquals("W0342", "M0535")
function mt:lookupMultipleMaps2() {
    (
        map { "id" : "W0342" },
        map { "id" : "M0535" }
    )?id
};

declare
    %test:assertEquals("large")
function mt:lookupNestedMap() {
    let $map := map {
        "name": "sofa", "keys": map { "price": 200.0, "size": "large" }
    }
    return
        $map?keys?size
};

declare
    %test:args("One")
    %test:assertEquals(1)
    %test:args("Two")
    %test:assertEquals("2")
function mt:lookupParenthesized($key as xs:string) {
    let $map := map { "one": 1, "two": "2" }
    return
        $map?(lower-case($key))
};

declare
    %test:assertEquals(1)
function mt:lookupParenthesized() {
    let $f := function () { "one" }
    let $map := map { "one": 1 }
    return
        $map($f())
};

declare
    %test:assertError
function mt:lookupWrongType() {
    let $map := map { "one": 1, "two": "2" }
    let $str := "Hello"
    return
        ($map, $str)?one
};

declare
    %test:assertTrue
function mt:lookupWildcard() {
    let $expected := map:keys($mt:daysOfWeek) ! $mt:daysOfWeek(.)
    let $actual := $mt:daysOfWeek?*
    return
        count($actual) eq count($expected)
        and
        (every $day in $actual satisfies $day = $expected)
};

declare
    %test:assertEquals(1)
function mt:compat() {
    let $map := map { "one": 1, "two": "2" }
    return
        $map("one")
};

declare
    %test:assertError
function mt:no-atomization() {
    data(map { "k": "v" })
};

declare
    %test:name("for-each on a map with a single entry caused NPE")
    %test:assertEquals("k")
function mt:single-entry-map() {
    let $map := map:entry("k", ())
    return
        map:for-each($map, function($k, $v) { $k })
};

declare
    %test:assertEquals(5)
function mt:qname() {
    let $a := 1
    let $m := map { $a: fn:string-length("hello") }
    return
        $m?1
};

declare
    %test:assertEmpty
function mt:no-such-entry() {
    map:get(map {"foo": "bar"}, "baz")
};

declare
    %test:assertEqualsPermutation("uk", "nl", "se", "de")
function mt:multi-merge() {
    map:merge((
  	    map { "adam": "uk" },
  	    map { "dannes": "nl" },
  	    map { "leif": "se" },
  	    map { "wolfgang": "de" }
  	))?*
};

(:
  immutability tests for https://github.com/eXist-db/exist/issues/3724
:)
declare variable $mt:test-key-one := 1;
declare variable $mt:test-key-two := 2;
declare function mt:create-test-map() {
    map {
        $mt:test-key-one : true(),
        $mt:test-key-two : true()
    }
};

declare
    %test:assertEquals("true", "true")
function mt:immutable-put-then-put() {
    let $extended := map:put(mt:create-test-map(), $mt:test-key-two, false())
    let $expected := $extended($mt:test-key-one)
    let $result := map:put($extended, $mt:test-key-one, false())
    return
        (
            $expected eq $extended($mt:test-key-one),
            $expected ne $result($mt:test-key-one)
        )
};

declare
    %test:assertEquals("true", "true")
function mt:immutable-put-then-remove() {
    let $extended := map:put(mt:create-test-map(), $mt:test-key-two, false())
    let $expected := $extended($mt:test-key-one)
    let $result := map:remove($extended, $mt:test-key-one)
    return
        (
            $expected eq $extended($mt:test-key-one),
            empty($result($mt:test-key-one))
        )
};

declare
    %test:assertEquals("true", "true")
function mt:immutable-put-then-merge() {
    let $extended := map:put(mt:create-test-map(), $mt:test-key-two, false())
    let $expected := $extended($mt:test-key-one)
    let $result := map:merge(($extended, map { $mt:test-key-one : false() }))
    return
        (
            $expected eq $extended($mt:test-key-one),
            $expected ne $result($mt:test-key-one)
        )
};

declare
    %test:assertEquals("true", "true")
function mt:immutable-remove-then-put() {
    let $removed := map:remove(mt:create-test-map(), $mt:test-key-two)
    let $expected := $removed($mt:test-key-one)
    let $result := map:put($removed, $mt:test-key-one, false())
    return
        (
            $expected eq $removed($mt:test-key-one),
            $expected ne $result($mt:test-key-one)
        )
};

declare
    %test:assertEquals("true", 1, "true", "true", 0)
function mt:immutable-remove-then-remove() {
    let $removed := map:remove(mt:create-test-map(), $mt:test-key-two)
    let $expected := $removed($mt:test-key-one)
    let $result := map:remove($removed, $mt:test-key-one)
    return
        (
            fn:empty($removed($mt:test-key-two)),
            map:size($removed),
            $expected eq $removed($mt:test-key-one),
            fn:empty($result($mt:test-key-one)),
            map:size($result)
        )
};

declare
    %test:assertEquals("true", "true")
function mt:immutable-remove-then-merge() {
    let $removed := map:remove(mt:create-test-map(), $mt:test-key-two)
    let $expected := $removed($mt:test-key-one)
    let $result := map:merge(($removed, map { $mt:test-key-one : false() }))
    return
        (
            $expected eq $removed($mt:test-key-one),
            $expected ne $result($mt:test-key-one)
        )
};

declare
    %test:assertEquals("true", "true")
function mt:immutable-merge-then-put() {
    let $merged := map:merge(mt:create-test-map())
    let $expected := $merged($mt:test-key-one)
    let $result := map:put($merged, $mt:test-key-one, false())
    return
        (
            $expected eq $merged($mt:test-key-one),
            $expected ne $result($mt:test-key-one)
        )
};

declare
    %test:assertEquals(2, "true", "true", 1)
function mt:immutable-merge-then-remove() {
    let $merged := map:merge(mt:create-test-map())
    let $expected := $merged($mt:test-key-one)
    let $result := map:remove($merged, $mt:test-key-one)
    return
        (
            map:size($merged),
            $expected eq $merged($mt:test-key-one),
            fn:empty($result($mt:test-key-one)),
            map:size($result)
        )
};

declare
    %test:assertEquals("true", "true")
function mt:immutable-merge-then-merge() {
    let $merged := map:merge(mt:create-test-map())
    let $expected := $merged($mt:test-key-one)
    let $result := map:merge(($merged, map { $mt:test-key-one : false() }))
    return
        (
            $expected eq $merged($mt:test-key-one),
            $expected ne $result($mt:test-key-one)
        )
};

(:
 : eXist-db Open Source Native XML Database
 : Copyright (C) 2001 The eXist-db Authors
 :
 : info@exist-db.org
 : http://www.exist-db.org
 :
 : This library is free software; you can redistribute it and/or
 : modify it under the terms of the GNU Lesser General Public
 : License as published by the Free Software Foundation; either
 : version 2.1 of the License, or (at your option) any later version.
 :
 : This library is distributed in the hope that it will be useful,
 : but WITHOUT ANY WARRANTY; without even the implied warranty of
 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 : Lesser General Public License for more details.
 :
 : You should have received a copy of the GNU Lesser General Public
 : License along with this library; if not, write to the Free Software
 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 :)
xquery version "3.1";

module namespace mt="http://exist-db.org/xquery/test/maps";

import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";

declare %private function mt:getMapFixture () {
    map {
        "Su" : "Sunday",
        "Mo" : "Monday",
        "Tu" : "Tuesday",
        "We" : "Wednesday",
        "Th" : "Thursday",
        "Fr" : "Friday",
        "Sa" : "Saturday"
    }
};

declare variable $mt:daysOfWeek := mt:getMapFixture();

declare variable $mt:integerKeys := map {
    1 : "Sunday",
    2 : "Monday",
    3 : "Tuesday",
    4 : "Wednesday",
    5 : "Thursday",
    6 : "Friday",
    7 : "Saturday"
};

declare variable $mt:mapOfSequences := map {0: (), 1 : ("One", "Two") };

declare
    %test:assertEquals("Wednesday")
function mt:createLiteral1() {
    $mt:daysOfWeek("We")
};

declare
    %test:assertEquals("Wednesday")
function mt:createWithIntKeys() {
    let $map := map {
        1 : "Sunday",
        2 : "Monday",
        3 : "Tuesday",
        4 : "Wednesday",
        5 : "Thursday",
        6 : "Friday",
        7 : "Saturday"
    }
    return
        $map(4)
};

declare
    %test:assertError("err:XPST0003")
function mt:draftSyntaxNotAllowed () {
    (: this needs to be evaled, to catch the static compilation error :)
    util:eval('map { 1 := 1 }')
};

declare
    %test:assertEquals(20)
function mt:createWithFunctionVariable() {
    let $fn := function($p) { $p * 2 }
    let $map := map { "callback" : $fn }
    return
        map:get($map, "callback")(10)
};

declare
    %test:assertEquals(2)
function mt:createWithFunctionValue() {
    let $map := map { "callback" : function($p) { $p * 2 } }
    return
        map:get($map, "callback")(1)
};

declare
    %test:assertEquals("Sunday")
function mt:createWithEntry() {
    let $days := ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
    let $map := map:merge($days ! map:entry(substring(., 1, 2), .))
    return
        $map("Su")
};

declare
    %test:assertXPath("count($result) = 8")
    %test:assertXPath("8 = $result")
function mt:createFromTwoMaps() {
    let $map := map:merge(($mt:integerKeys, map { 8 : "Caturday" }))
    return
        map:keys($map)
};

declare
    %test:assertEquals(0)
function mt:size-empty() {
    let $m := map {}
    return
      map:size($m)
};

declare
    %test:assertEquals("Good")
function mt:put-empty() {
    let $m := map {}
    return
      map:put($m, "Cats", "Good")("Cats")
};

declare
    %test:assertEquals("Bad")
function mt:put-single() {
    let $m := map {
        "Cats" : "Good"
    }
    return
      map:put($m, "Dogs", "Bad")("Dogs")
};

declare
    %test:assertEquals("Duck")
function mt:put() {
    let $m := map {
        "Cats" : "Good",
        "Dogs" : "Bad",
        1 : "Chicken"
    }
    return
      map:put($m, 2, "Duck")(2)
};

declare
    %test:assertEquals(4)
function mt:size() {
    let $m := map {
        "Cats": "Good",
        "Dogs": "Bad",
        1: "Chicken",
        2: "Duck"
    }
    return
        map:size($m)
};

declare
    %test:assertEquals("Sunday", "Tuesday", "Thursday", "Saturday")
function mt:for-each() {
    map:for-each($mt:integerKeys, function($key, $value) {
        if ($key mod 2) then ($value) else ()
    })
};

declare
    %test:assertEquals(3)
function mt:for-each2() {
    let $nm := map:merge(
        map:for-each(
            map { "a": 1, "b": 2 },
            function($k, $v) { map:entry($k, $v + 1) }
        )
    )

    return
        $nm?b
};

declare
    %test:assertEquals("Sunday")
function mt:createWithSingleKey() {
    let $map := map { "Su": "Sunday" }
    return
        $map("Su")
};

declare
    %test:assertEquals("Saturday", "Caturday")
function mt:overwriteKeyInNewMap() {
    let $specialWeek := map:merge(($mt:integerKeys, map { 7 : "Caturday" }))
    return
        ($mt:integerKeys(7), $specialWeek(7))
};

declare
    %test:assertEmpty
function mt:mapEmptyValue() {
    let $map := $mt:mapOfSequences
    return
        $map(0)
};

declare
    %test:assertEquals("One", "Two")
function mt:mapSequenceValue() {
    let $map := $mt:mapOfSequences
    return
        $map(1)
};

declare
    %test:assertEquals("Heinz", "Roland", "Uschi", "Verona")
function mt:orderBzKeyTypeDate () {
    let $map := map {
        xs:date("1975-03-19"): "Uschi",
        xs:date("1980-01-22"): "Verona",
        xs:date("1960-06-14"): "Heinz",
        xs:date("1963-10-21"): "Roland"
    }

    for $key in map:keys($map)
    let $name := $map($key)
    order by $name ascending
    return
        $name
};

declare
    %test:assertEquals(0, "One")
function mt:mixedKeyTypes() {
    let $map := map{ "Zero": 0, 1: "One" }
    return
        ($map("Zero"), $map(1))
};

declare
    %test:assertTrue
function mt:containsOnEmptyValue() {
    let $map := map{ 0: () }
    return
        map:contains($map, 0)
};

declare
    %test:assertEquals("Fr", "Mo", "Sa", "Su", "Th", "Tu", "We")
function mt:keysInFor() {
    for $day in map:keys(mt:getMapFixture())
    order by $day ascending
    return
        $day
};

declare
    %test:assertEquals("Su")
function mt:keysOnMapWithSingleKey() {
    let $map := map:entry("Su", "Sunday")
    return
        map:keys($map)
};

declare
    %test:assertFalse
function mt:remove() {
    let $map := mt:getMapFixture()
    return
        map:contains(map:remove($map, "Tu"), "Tu")
};

declare
    %test:assertEquals("Fr", "Mo", "Sa", "Su", "Th")
function mt:remove2() {
    let $map := mt:getMapFixture()
    for $day in map:keys(map:remove(map:remove($map, "We"), "Tu"))
    order by $day
    return
        $day
};

declare
    %test:assertEmpty
function mt:removeSingleKey() {
    let $empty := map:remove(map { "Su": "Sunday" }, "Su")
    return
        map:keys($empty)
};

declare
    %test:assertEquals("Su")
function mt:removeSingleNonExistentKey() {
    let $map := map:remove(map { "Su": "Sunday" }, "Xx")
    return
        map:keys($map)
};

declare
    %test:assertEquals("Sa")
function mt:removeSequenceOfStringKeys() {
    mt:getMapFixture()
        => map:remove(("Mo", "Tu", "We", "Th", "Fr", "Su"))
        => map:keys()
};

declare
    %test:assertEquals("Sa")
function mt:removeSequenceOfStringKeysWithNonExistent() {
    mt:getMapFixture()
        => map:remove(("Mo", "Tu", "We", "Th", "Fr", "Su", "Xx"))
        => map:keys()
};

declare
    %test:assertEquals(7)
function mt:removeRangeOfIntegerKeys() {
    $mt:integerKeys
        => map:remove(1 to 6)
        => map:keys()
};

declare
    %test:assertEquals(7)
function mt:removeIntegerKeysWithNonExistent() {
    $mt:integerKeys
        => map:remove((1, 2, 3, 4, 5, 6, 10))
        => map:keys()
};

declare
    %test:assertTrue
function mt:contains() {
    map:contains($mt:daysOfWeek, "We")
};

declare
    %test:assertFalse
function mt:containsOnMissingKey() {
    map:contains($mt:daysOfWeek, "Al")
};

declare
    %test:assertTrue
function mt:containsSingleKey() {
    map:contains(map { "Su": "Sunday" }, "Su")
};

declare
    %test:assertEquals("Hello world")
function mt:computedKeyValue() {
    let $map := map { 2 + 1 : concat("Hello ", "world") }
    return
        $map(3)
};

declare
    %test:assertEquals("false", "true")
function mt:immutabilityMerge() {
    let $original := mt:getMapFixture()
    let $derived := map:merge(($original, map:entry("Ca", "Caturday")))
    return (
        map:contains($original, "Ca"),
        map:contains($derived, "Ca")
    )
};

declare
    %test:assertEquals("true", "false")
function mt:immutabilityRemove() {
    let $original := mt:getMapFixture()
    let $derived := map:remove($original, "Fr")
    return (
        map:contains($original, "Fr"),
        map:contains($derived, "Fr")
    )
};

declare
    %test:assertEquals("false", "true")
function mt:immutabilityPut() {
    let $original := mt:getMapFixture()
    let $derived := map:put($original, "Ca", "Caturday")
    return (
        map:contains($original, "Ca"),
        map:contains($derived, "Ca")
    )
};

declare
    %test:assertEquals("Sunday")
function mt:sequenceType1() {
    let $map := map { 1 : "Sunday" }
    return
        map:get(mt:mapTest($map), 1)
};

declare
    %test:assertEquals("Sunday")
function mt:sequenceType2() {
    let $map := map { 1 : "Sunday" }
    return
        map:get(mt:mapTestFail($map), 1)
};

declare
    %private
function mt:mapTest($map as map(*)) as map(xs:integer, xs:string) {
    $map
};

declare
    %private
function mt:mapTestFail($map as map(*)) as map(xs:date, xs:string) {
    $map
};

declare
    %test:assertError("EXMPDY001")
    %test:name("Throw error if key is not a single atomic value")
function mt:wrongCardinality() {
    let $map := map { (1, 2) : "illegal" }
    return
        $map(1)
};

declare
    %test:assertEquals("two")
function mt:nestedMaps() {
    let $map := map { 1: map { 1: "one", 2: "two" } }
    return
        $map(1)(2)
};

declare
    %test:assertEquals("Monday")
function mt:nestedMaps2() {
    let $map := map { "week" : map { "days" : $mt:daysOfWeek } }
    return
        $map("week")("days")("Mo")
};

declare
    %test:assertEquals(2)
function mt:qnameKeys() {
    let $map := map { xs:QName("mt:one") : 1, xs:QName("mt:two") : 2 }
    return
        $map(xs:QName("mt:two"))
};

declare
    %test:assertEquals(2)
function mt:longKeys() {
    let $map := map { xs:long(1) : 1, xs:long(2) : 2 }
    return
        $map(2)
};

declare
    %test:args(2)
    %test:assertEquals(3)
    %test:args("Three")
    %test:assertEmpty
function mt:doubleKeys($key as item()) {
    let $map := map { xs:double(1.1) : 1, xs:double(2) : 2 }
    return
        map:merge(($map, map:entry(xs:double(2), 3)))($key)
};

declare
    %test:assertEquals(2, "value")
function mt:mixedNumeric() {
    let $map := map { 1.1 : 1, 2 : 2, "key" : "value" }
    return
        ($map(2), $map("key"))
};

declare
    %test:assertEquals("<c ns='c'/>", "<c ns='c'/>")
function mt:pathExpr() as element(c)+ {
    let $test :=
        <a>
            <b ns="b">
                <c ns="c"/>
            </b>
        </a>
    let $map :=
        map {
            "a" : $test//c,
            "b" : ($test//c)
        }
    return
        ($map("a"), $map("b"))
};

declare
    %test:assertEquals("What were you thinking?")
function mt:lookupUnaryOperator() {
    let $errors := (
      map { "level" : 1, "text" : "Boys will be boys ..." },
      map { "level" : 2, "text" : "What were you thinking?" },
      map { "level" : 3, "text" : "Call the cops!" }
    )
    return $errors[?level = 2]?text
};

declare
    %test:assertEquals("W0342", "M0535")
function mt:lookupMultipleMaps() {
    let $maps := ( map { "id" : "W0342" }, map { "id" : "M0535" } )
    return $maps?id
};

declare
    %test:assertEquals("W0342", "M0535")
function mt:lookupMultipleMaps2() {
    (
        map { "id" : "W0342" },
        map { "id" : "M0535" }
    )?id
};

declare
    %test:assertEquals("large")
function mt:lookupNestedMap() {
    let $map := map {
        "name": "sofa", "keys": map { "price": 200.0, "size": "large" }
    }
    return
        $map?keys?size
};

declare
    %test:args("One")
    %test:assertEquals(1)
    %test:args("Two")
    %test:assertEquals("2")
function mt:lookupParenthesized($key as xs:string) {
    let $map := map { "one": 1, "two": "2" }
    return
        $map?(lower-case($key))
};

declare
    %test:assertEquals(1)
function mt:lookupParenthesized() {
    let $f := function () { "one" }
    let $map := map { "one": 1 }
    return
        $map($f())
};

declare
    %test:assertError
function mt:lookupWrongType() {
    let $map := map { "one": 1, "two": "2" }
    let $str := "Hello"
    return
        ($map, $str)?one
};

declare
    %test:assertTrue
function mt:lookupWildcard() {
    let $expected := map:keys($mt:daysOfWeek) ! $mt:daysOfWeek(.)
    let $actual := $mt:daysOfWeek?*
    return
        count($actual) eq count($expected)
        and
        (every $day in $actual satisfies $day = $expected)
};

declare
    %test:assertEquals(1)
function mt:compat() {
    let $map := map { "one": 1, "two": "2" }
    return
        $map("one")
};

declare
    %test:assertError
function mt:no-atomization() {
    data(map { "k": "v" })
};

declare
    %test:name("for-each on a map with a single entry caused NPE")
    %test:assertEquals("k")
function mt:single-entry-map() {
    let $map := map:entry("k", ())
    return
        map:for-each($map, function($k, $v) { $k })
};

declare
    %test:assertEquals(5)
function mt:qname() {
    let $a := 1
    let $m := map { $a: fn:string-length("hello") }
    return
        $m?1
};

declare
    %test:assertEmpty
function mt:no-such-entry() {
    map:get(map {"foo": "bar"}, "baz")
};

declare
    %test:assertEqualsPermutation("uk", "nl", "se", "de")
function mt:multi-merge() {
    map:merge((
  	    map { "adam": "uk" },
  	    map { "dannes": "nl" },
  	    map { "leif": "se" },
  	    map { "wolfgang": "de" }
  	))?*
};

(:
  immutability tests for https://github.com/eXist-db/exist/issues/3724
:)
declare variable $mt:test-key-one := 1;
declare variable $mt:test-key-two := 2;
declare function mt:create-test-map() {
    map {
        $mt:test-key-one : true(),
        $mt:test-key-two : true()
    }
};

declare
    %test:assertEquals("true", "true")
function mt:immutable-put-then-put() {
    let $extended := map:put(mt:create-test-map(), $mt:test-key-two, false())
    let $expected := $extended($mt:test-key-one)
    let $result := map:put($extended, $mt:test-key-one, false())
    return
        (
            $expected eq $extended($mt:test-key-one),
            $expected ne $result($mt:test-key-one)
        )
};

declare
    %test:assertEquals("true", "true")
function mt:immutable-put-then-remove() {
    let $extended := map:put(mt:create-test-map(), $mt:test-key-two, false())
    let $expected := $extended($mt:test-key-one)
    let $result := map:remove($extended, $mt:test-key-one)
    return
        (
            $expected eq $extended($mt:test-key-one),
            empty($result($mt:test-key-one))
        )
};

declare
    %test:assertEquals("true", "true")
function mt:immutable-put-then-merge() {
    let $extended := map:put(mt:create-test-map(), $mt:test-key-two, false())
    let $expected := $extended($mt:test-key-one)
    let $result := map:merge(($extended, map { $mt:test-key-one : false() }))
    return
        (
            $expected eq $extended($mt:test-key-one),
            $expected ne $result($mt:test-key-one)
        )
};

declare
    %test:assertEquals("true", "true")
function mt:immutable-remove-then-put() {
    let $removed := map:remove(mt:create-test-map(), $mt:test-key-two)
    let $expected := $removed($mt:test-key-one)
    let $result := map:put($removed, $mt:test-key-one, false())
    return
        (
            $expected eq $removed($mt:test-key-one),
            $expected ne $result($mt:test-key-one)
        )
};

declare
    %test:assertEquals("true", "true")
function mt:immutable-remove-then-remove() {
    let $removed := map:remove(mt:create-test-map(), $mt:test-key-two)
    let $expected := $removed($mt:test-key-one)
    let $result := map:remove($removed, $mt:test-key-one)
    return
        (
            $expected eq $removed($mt:test-key-one),
            $expected ne $result($mt:test-key-one)
        )
};

declare
    %test:assertEquals("true", "true")
function mt:immutable-remove-then-merge() {
    let $removed := map:remove(mt:create-test-map(), $mt:test-key-two)
    let $expected := $removed($mt:test-key-one)
    let $result := map:merge(($removed, map { $mt:test-key-one : false() }))
    return
        (
            $expected eq $removed($mt:test-key-one),
            $expected ne $result($mt:test-key-one)
        )
};

declare
    %test:assertEquals("true", "true")
function mt:immutable-merge-then-put() {
    let $merged := map:merge(mt:create-test-map())
    let $expected := $merged($mt:test-key-one)
    let $result := map:put($merged, $mt:test-key-one, false())
    return
        (
            $expected eq $merged($mt:test-key-one),
            $expected ne $result($mt:test-key-one)
        )
};

declare
    %test:assertEquals("true", "true")
function mt:immutable-merge-then-remove() {
    let $merged := map:merge(mt:create-test-map())
    let $expected := $merged($mt:test-key-one)
    let $result := map:remove($merged, $mt:test-key-one)
    return
        (
            $expected eq $merged($mt:test-key-one),
            $expected ne $result($mt:test-key-one)
        )
};

declare
    %test:assertEquals("true", "true")
function mt:immutable-merge-then-merge() {
    let $merged := map:merge(mt:create-test-map())
    let $expected := $merged($mt:test-key-one)
    let $result := map:merge(($merged, map { $mt:test-key-one : false() }))
    return
        (
            $expected eq $merged($mt:test-key-one),
            $expected ne $result($mt:test-key-one)
        )
};


webap fixup

webap fixup

module namespace page = 'http://basex.org/modules/web-page';

import module namespace csr_proc = "https://github.com/openhie/openinfoman/csr_proc";
import module namespace csd_mcs = "https://github.com/openhie/openinfoman/csd_mcs";
import module namespace csd_dm = "https://github.com/openhie/openinfoman/csd_dm";
import module namespace csd_webconf =  "https://github.com/openhie/openinfoman/csd_webconf";
import module namespace csd_webui =  "https://github.com/openhie/openinfoman/csd_webui";

declare   namespace   csd = "urn:ihe:iti:csd:2013";

declare function page:is_merge($search_name) {
  let $ufunction := csr_proc:get_updating_function_definition($csd_webconf:db,$search_name)
  let $ext := $ufunction//csd:extension[  @urn='urn:openhie.org:openinfoman:adapter' and @type='merge']
  return (count($ext) > 0)
};

declare
  %rest:path("/CSD/csr/{$doc_name}/careServicesRequest/{$search_name}/adapter/merge")
  %output:method("xhtml")
  %rest:GET
  function page:show_endpoints($search_name,$doc_name)
{
    if (not(page:is_merge($search_name)) )
      then concat('Not a stored function for a merge: ' , $search_name    )
    else
      let $action := concat("/CSD/csr/",$doc_name,"/careServicesRequest/",$search_name, "/adapter/merge")
      let $ufunction := csr_proc:get_updating_function_definition($csd_webconf:db,$search_name)
      let $contents :=

Merge into {$doc_name}

Merge Cached Service Directoriesn

{$ufunction/csd:description}

	      <form method='POST' action="{$action}">
		{
		  let $docs := csd_dm:registered_documents($csd_webconf:db)
		  return

		    {
		      for $doc in $docs
		      where not($doc = $doc_name)
		      return (<input type='checkbox'  name="merge" value="{$doc}">{$doc}</input>,
)
		    }

		    <input type='submit' value='Merge'/>

		}
	      </form>

      return csd_webconf:wrapper($contents)
};

declare updating
  %rest:path("/CSD/csr/{$doc_name}/careServicesRequest/{$search_name}/adapter/merge")
(:  %output:method("xhtml") :)
  %rest:query-param("merge", "{$merge}")
  %rest:POST
  function page:perform_merge($search_name,$doc_name,$merge)
{
  let $doc :=  csd_dm:open_document($csd_webconf:db,$doc_name)
  let $function := csr_proc:get_function_definition($csd_webconf:db,$search_name)
  let $action := concat("/CSD/csr/",$doc_name,"/careServicesRequest/",$search_name, "/adapter/merge")
  let $requestParams :=
    <csd:requestParams function="{$search_name}" resource="{$doc_name}" base_url="{csd_webui:generateURL('')}">
      <documents >
	{
          for $name in $merge
	  where not ($name = $doc_name)
	  return  <document resource="{$name}"/>
	}
      </documents>
    </csd:requestParams >

    return
      (
	csr_proc:process_updating_CSR_stored_results($csd_webconf:db, $doc,$requestParams)
	,
	() (: csd_webui:redirect_out($action)  :)
      )
};

module namespace page = 'http://basex.org/modules/web-page';

import module namespace csr_proc = "https://github.com/openhie/openinfoman/csr_proc";
import module namespace csd_mcs = "https://github.com/openhie/openinfoman/csd_mcs";
import module namespace csd_dm = "https://github.com/openhie/openinfoman/csd_dm";
import module namespace csd_webconf =  "https://github.com/openhie/openinfoman/csd_webconf";

declare   namespace   csd = "urn:ihe:iti:csd:2013";

declare function page:is_merge($search_name) {
  let $ufunction := csr_proc:get_updating_function_definition($csd_webconf:db,$search_name)
  let $ext := $ufunction//csd:extension[  @urn='urn:openhie.org:openinfoman:adapter' and @type='merge']
  return (count($ext) > 0)
};

declare
  %rest:path("/CSD/csr/{$doc_name}/careServicesRequest/{$search_name}/adapter/merge")
  %output:method("xhtml")
  %rest:GET
  function page:show_endpoints($search_name,$doc_name)
{
    if (not(page:is_merge($search_name)) )
      then concat('Not a stored function for a merge: ' , $search_name    )
    else
      let $action := concat("/CSD/csr/",$doc_name,"/careServicesRequest/",$search_name, "/adapter/merge")
      let $ufunction := csr_proc:get_updating_function_definition($csd_webconf:db,$search_name)
      let $contents :=

Merge into {$doc_name}

Merge Cached Service Directoriesn

{$ufunction/csd:description}

	      <form method='POST' action="{$action}">
		{
		  let $docs := csd_dm:registered_documents($csd_webconf:db)
		  return

		    {
		      for $doc in $docs
		      where not($doc = $doc_name)
		      return (<input type='checkbox'  name="merge" value="{$doc}">{$doc}</input>,
)
		    }

		    <input type='submit' value='Merge'/>

		}
	      </form>

      return csd_webconf:wrapper($contents)
};

declare updating
  %rest:path("/CSD/csr/{$doc_name}/careServicesRequest/{$search_name}/adapter/merge")
(:  %output:method("xhtml") :)
  %rest:query-param("merge", "{$merge}")
  %rest:POST
  function page:perform_merge($search_name,$doc_name,$merge)
{
  let $doc :=  csd_dm:open_document($csd_webconf:db,$doc_name)
  let $function := csr_proc:get_function_definition($csd_webconf:db,$search_name)
  let $action := concat("/CSD/csr/",$doc_name,"/careServicesRequest/",$search_name, "/adapter/merge")
  let $requestParams :=
    <csd:requestParams function="{$search_name}" resource="{$doc_name}" base_url="{csd_webui:generateURL('')}">
      <documents >
	{
          for $name in $merge
	  where not ($name = $doc_name)
	  return  <document resource="{$name}"/>
	}
      </documents>
    </csd:requestParams >

    return
      (
	csr_proc:process_updating_CSR_stored_results($csd_webconf:db, $doc,$requestParams)
	,
	() (: csd_webui:redirect_out($action)  :)
      )
};


Include the not supported features in test report.

xquery version "3.0";

import module namespace eval = "http://expath.org/ns/invoker" at "lib/invoker.xqy";

declare namespace h = "http://expath.org/ns/invoker/test/hello";
declare namespace l = "http://expath.org/ns/invoker/test/libdules";

(:~~~~~
 : Functions to be called/invoked in tests.
 :)

declare function local:hello() {
   local:hello('world')
};

declare function local:hello-doc() {
   local:hello(fn:doc('/who.xml'))
};

declare function local:hello($who as xs:string) {
   'Hello, ' || $who || '!'
};

(:~~~~~
 : Test wrapper for eval:invoke functions.
 :)

declare function local:invoke(
   $arg1 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string,
   $cmp  as xs:string
)
{
   local:test(
      eval:invoke($arg1),
      $exp, 'invoke', $what, $how, $cmp)
};

declare function local:invoke(
   $arg1 as item()*,
   $arg2 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string,
   $cmp  as xs:string
)
{
   local:test(
      eval:invoke($arg1, $arg2),
      $exp, 'invoke', $what, $how, $cmp)
};

declare function local:invoke(
   $arg1 as item()*,
   $arg2 as item()*,
   $arg3 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string,
   $cmp  as xs:string
)
{
   local:test(
      eval:invoke($arg1, $arg2, $arg3),
      $exp, 'invoke', $what, $how, $cmp)
};

declare function local:invoke(
   $arg1 as item()*,
   $arg2 as item()*,
   $arg3 as item()*,
   $arg4 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string,
   $cmp  as xs:string
)
{
   local:test(
      eval:invoke($arg1, $arg2, $arg3, $arg4),
      $exp, 'invoke', $what, $how, $cmp)
};

declare function local:invoke(
   $arg1 as item()*,
   $arg2 as item()*,
   $arg3 as item()*,
   $arg4 as item()*,
   $arg5 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string,
   $cmp  as xs:string
)
{
   local:test(
      eval:invoke($arg1, $arg2, $arg3, $arg4, $arg5),
      $exp, 'invoke', $what, $how, $cmp)
};

(:~~~~~
 : Test wrapper for eval:call functions.
 :)

declare function local:call(
   $arg1 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test(
      eval:call($arg1),
      $exp, 'call', $what, $how)
};

declare function local:call(
   $arg1 as item()*,
   $arg2 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test(
      eval:call($arg1, $arg2),
      $exp, 'call', $what, $how)
};

declare function local:call(
   $arg1 as item()*,
   $arg2 as item()*,
   $arg3 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test(
      eval:call($arg1, $arg2, $arg3),
      $exp, 'call', $what, $how)
};

declare function local:call(
   $arg1 as item()*,
   $arg2 as item()*,
   $arg3 as item()*,
   $arg4 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test(
      eval:call($arg1, $arg2, $arg3, $arg4),
      $exp, 'call', $what, $how)
};

declare function local:call(
   $arg1 as item()*,
   $arg2 as item()*,
   $arg3 as item()*,
   $arg4 as item()*,
   $arg5 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test(
      eval:call($arg1, $arg2, $arg3, $arg4, $arg5),
      $exp, 'call', $what, $how)
};

(:~~~~~
 : Test wrapper for eval:module functions.
 :)

declare function local:module(
   $arg1 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test(
      eval:module($arg1),
      $exp, 'module', $what, $how)
};

declare function local:module(
   $arg1 as item()*,
   $arg2 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test(
      eval:module($arg1, $arg2),
      $exp, 'module', $what, $how)
};

declare function local:module(
   $arg1 as item()*,
   $arg2 as item()*,
   $arg3 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test(
      eval:module($arg1, $arg2, $arg3),
      $exp, 'module', $what, $how)
};

declare function local:module(
   $arg1 as item()*,
   $arg2 as item()*,
   $arg3 as item()*,
   $arg4 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test(
      eval:module($arg1, $arg2, $arg3, $arg4),
      $exp, 'module', $what, $how)
};

(:~~~~~
 : Test result formatting functions.
 :)

declare function local:todo($msg  as xs:string)
{
   local:todo($msg, (), ())
};

declare function local:todo(
   $msg  as xs:string,
   $what as xs:string?,
   $how  as xs:string?
)
{
   local:todo($msg, $what, $how, ())
};

declare function local:todo(
   $msg  as xs:string,
   $what as xs:string?,
   $how  as xs:string?,
   $cmp  as xs:string?
)
{

 {
      ✘,
      ' ',
      fn:string-join(($cmp, $what, $how), ' / '),
      ' - ',
      $msg
   }

};

declare function local:test(
   $res  as item()*,
   $exp  as xs:string,
   $name as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test($res, $exp, $name, $what, $how, ())
};

declare function local:test(
   $res  as item()*,
   $exp  as xs:string,
   $name as xs:string,
   $what as xs:string,
   $how  as xs:string,
   $cmp  as xs:string?
)
{

 {
      if ( $res instance of xs:string and $res eq $exp ) then (
         ✔,
         ' ',
         fn:string-join(($cmp, $what, $how), ' / ')
      )
      else (
         ✘,
         ' ',
         fn:string-join(($cmp, $what, $how), ' / '),
         ' - expected: ',
         $exp,
         ' - got: ',
         $res
      )
   }

};

(:~~~~~
 : Actual test cases.
 :)

Invoker tests

Ad-hoc unit test page for the ml-invoker library.

eval:invoke

{
   local:invoke(
      <eval:function name="l:hello" xmlns:l="http://www.w3.org/2005/xquery-local-functions"/>,
      'Hello, world!', 'name', 'both', 'function'),

   local:invoke(
      <eval:module href="/hello-world.xqy"/>,
      'Hello, world!', 'href', 'both', 'module'),

   local:invoke(
      <dummy>
         <eval:module href="/hello-world.xqy" id="me"/>
      </dummy>,
      'me',
      'Hello, world!', 'id', 'both', 'module'),

   local:invoke(
      <eval:module href="/hello-doc.xqy"/>,
      (),
      'Documents',
      'Hello, document!', 'db', 'param', 'module'),

   local:invoke(
      <eval:module href="/hello-doc.xqy" db="Documents"/>,
      'Hello, document!', 'db', 'elem', 'module'),

   local:invoke(
      <eval:module href="/hello-modules.xqy"/>,
      (),
      (),
      'invoker-db-two',
      'Hello, modules!', 'modules', 'param', 'module'),

   local:invoke(
      <eval:module href="/hello-modules.xqy" modules-db="invoker-db-two"/>,
      'Hello, modules!', 'modules', 'elem', 'module'),

   local:invoke(
      <eval:module href="/hello-world.xqy"/>,
      (),
      (),
      (),
      'xquery',
      'Hello, world!', 'xquery', 'param', 'module'),

   local:invoke(
      <eval:module href="/hello-world.xqy" lang="xquery"/>,
      'Hello, world!', 'xquery', 'elem', 'module'),

   local:invoke(
      <eval:module href="/hello-world.sjs"/>,
      (),
      (),
      (),
      'js',
      'Hello, js!', 'javascript', 'param', 'module'),

   local:invoke(
      <eval:module href="/hello-world.sjs" lang="js"/>,
      'Hello, js!', 'javascript', 'elem', 'module')
}

eval:call

{
   local:call(
      fn:function-lookup(xs:QName('local:hello'), 0),
      'Hello, world!', 'name', 'param'),

   local:call(
      <dummy name="l:hello" xmlns:l="http://www.w3.org/2005/xquery-local-functions"/>,
      'Hello, world!', 'name', 'elem'),

   local:call(
      local:hello#0,
      'Hello, world!', 'function I', 'param'),

   local:call(
      function() { 'Hello, world!' },
      'Hello, world!', 'function II', 'param'),

   local:call(
      function() { local:hello() },
      'Hello, world!', 'function III', 'param'),

   local:call(
      function() { local:hello('world') },
      'Hello, world!', 'function IV', 'param'),

   local:call(
      xs:QName('h:hello'), '/hello-lib.xqy',
      'Hello, world!', 'href', 'param'),

   local:call(
      <dummy xmlns:h="http://expath.org/ns/invoker/test/hello"
             name="h:hello" href="/hello-lib.xqy"/>,
      'Hello, world!', 'href', 'elem'),

   (: TODO: What about a JS fun, in an SJS file? :)
   local:todo('Calling a JavaScript function not supported yet', 'href sjs', 'param'),
   (:
   local:call(
      'hello', '/hello-lib.sjs',
      'Hello, world!', 'href sjs', 'param'),
   :)

   local:call(
      local:hello-doc#0, (), 'Documents',
      'Hello, document!', 'db', 'param'),

   local:call(
      <dummy xmlns:l="http://www.w3.org/2005/xquery-local-functions"
             name="l:hello-doc" db="Documents"/>,
      'Hello, document!', 'db', 'elem'),

   local:call(
      xs:QName('l:hello'), '/hello-libdules.xqy', (), 'invoker-db-two',
      'Hello, world!', 'modules', 'param'),

   local:call(
      <dummy xmlns:l="http://expath.org/ns/invoker/test/libdules"
             name="l:hello" href="/hello-libdules.xqy" modules-db="invoker-db-two"/>,
      'Hello, world!', 'modules', 'elem'),

   local:call(
      local:hello#0, (), (), (), 'xquery',
      'Hello, world!', 'xquery', 'param'),

   local:call(
      <dummy xmlns:l="http://www.w3.org/2005/xquery-local-functions"
             name="l:hello" lang="xquery"/>,
      'Hello, world!', 'xquery', 'elem'),

   (: TODO: Not supported yet... :)
   local:todo('Calling a JavaScript function not supported yet', 'javascript', 'param')
   (:
   local:call(
      ???, (), (), (), 'js',
      'Hello, world!', 'javascript', 'param')

   local:call(
      <dummy name="hello" lang="js"/>,
      'Hello, world!', 'javascript', 'elem')
   :)
}

eval:module

{
   local:module(
      '/hello-world.xqy',
      'Hello, world!', 'href', 'param'),

   local:module(
      <dummy href="/hello-world.xqy"/>,
      'Hello, world!', 'href', 'elem'),

   local:module(
      '/hello-doc.xqy', 'Documents',
      'Hello, document!', 'db', 'param'),

   local:module(
      <dummy href="/hello-doc.xqy" db="Documents"/>,
      'Hello, document!', 'db', 'elem'),

   local:module(
      '/hello-modules.xqy', (), 'invoker-db-two',
      'Hello, modules!', 'modules', 'param'),

   local:module(
      <dummy href="/hello-modules.xqy" modules-db="invoker-db-two"/>,
      'Hello, modules!', 'modules', 'elem'),

   local:module(
      '/hello-world.xqy', (), (), 'xquery',
      'Hello, world!', 'xquery', 'param'),

   local:module(
      <dummy href="/hello-world.xqy" lang="xquery"/>,
      'Hello, world!', 'xquery', 'elem'),

   local:module(
      '/hello-world.sjs', (), (), 'js',
      'Hello, js!', 'javascript', 'param'),

   local:module(
      <dummy href="/hello-world.sjs" lang="js"/>,
      'Hello, js!', 'javascript', 'elem')
}

eval:script

{
   local:todo('eval:script not supported yet')
}

eval:eval

{
   local:todo('eval:eval not supported yet')
}

import module namespace eval = "http://expath.org/ns/invoker" at "lib/invoker.xqy";

declare namespace h = "http://expath.org/ns/invoker/test/hello";
declare namespace l = "http://expath.org/ns/invoker/test/libdules";

(:~~~~~
 : Functions to be called/invoked in tests.
 :)

declare function local:hello() {
   local:hello('world')
};

declare function local:hello-doc() {
   local:hello(fn:doc('/who.xml'))
};

declare function local:hello($who as xs:string) {
   'Hello, ' || $who || '!'
};

(:~~~~~
 : Test wrapper for eval:invoke functions.
 :)

declare function local:invoke(
   $arg1 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string,
   $cmp  as xs:string
)
{
   local:test(
      eval:invoke($arg1),
      $exp, 'invoke', $what, $how, $cmp)
};

declare function local:invoke(
   $arg1 as item()*,
   $arg2 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string,
   $cmp  as xs:string
)
{
   local:test(
      eval:invoke($arg1, $arg2),
      $exp, 'invoke', $what, $how, $cmp)
};

declare function local:invoke(
   $arg1 as item()*,
   $arg2 as item()*,
   $arg3 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string,
   $cmp  as xs:string
)
{
   local:test(
      eval:invoke($arg1, $arg2, $arg3),
      $exp, 'invoke', $what, $how, $cmp)
};

declare function local:invoke(
   $arg1 as item()*,
   $arg2 as item()*,
   $arg3 as item()*,
   $arg4 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string,
   $cmp  as xs:string
)
{
   local:test(
      eval:invoke($arg1, $arg2, $arg3, $arg4),
      $exp, 'invoke', $what, $how, $cmp)
};

declare function local:invoke(
   $arg1 as item()*,
   $arg2 as item()*,
   $arg3 as item()*,
   $arg4 as item()*,
   $arg5 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string,
   $cmp  as xs:string
)
{
   local:test(
      eval:invoke($arg1, $arg2, $arg3, $arg4, $arg5),
      $exp, 'invoke', $what, $how, $cmp)
};

(:~~~~~
 : Test wrapper for eval:call functions.
 :)

declare function local:call(
   $arg1 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test(
      eval:call($arg1),
      $exp, 'call', $what, $how)
};

declare function local:call(
   $arg1 as item()*,
   $arg2 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test(
      eval:call($arg1, $arg2),
      $exp, 'call', $what, $how)
};

declare function local:call(
   $arg1 as item()*,
   $arg2 as item()*,
   $arg3 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test(
      eval:call($arg1, $arg2, $arg3),
      $exp, 'call', $what, $how)
};

declare function local:call(
   $arg1 as item()*,
   $arg2 as item()*,
   $arg3 as item()*,
   $arg4 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test(
      eval:call($arg1, $arg2, $arg3, $arg4),
      $exp, 'call', $what, $how)
};

declare function local:call(
   $arg1 as item()*,
   $arg2 as item()*,
   $arg3 as item()*,
   $arg4 as item()*,
   $arg5 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test(
      eval:call($arg1, $arg2, $arg3, $arg4, $arg5),
      $exp, 'call', $what, $how)
};

(:~~~~~
 : Test wrapper for eval:module functions.
 :)

declare function local:module(
   $arg1 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test(
      eval:module($arg1),
      $exp, 'module', $what, $how)
};

declare function local:module(
   $arg1 as item()*,
   $arg2 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test(
      eval:module($arg1, $arg2),
      $exp, 'module', $what, $how)
};

declare function local:module(
   $arg1 as item()*,
   $arg2 as item()*,
   $arg3 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test(
      eval:module($arg1, $arg2, $arg3),
      $exp, 'module', $what, $how)
};

declare function local:module(
   $arg1 as item()*,
   $arg2 as item()*,
   $arg3 as item()*,
   $arg4 as item()*,
   $exp  as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test(
      eval:module($arg1, $arg2, $arg3, $arg4),
      $exp, 'module', $what, $how)
};

(:~~~~~
 : Test result formatting functions.
 :)

declare function local:test(
   $res  as item()*,
   $exp  as xs:string,
   $name as xs:string,
   $what as xs:string,
   $how  as xs:string
)
{
   local:test($res, $exp, $name, $what, $how, ())
};

declare function local:test(
   $res  as item()*,
   $exp  as xs:string,
   $name as xs:string,
   $what as xs:string,
   $how  as xs:string,
   $cmp  as xs:string?
)
{

 {
      if ( $res instance of xs:string and $res eq $exp ) then (
         ✔,
         ' ',
         fn:string-join(($cmp, $what, $how), ' / ')
      )
      else (
         ✘,
         ' ',
         fn:string-join(($cmp, $what, $how), ' / '),
         ' - expected: ',
         $exp,
         ' - got: ',
         $res
      )
   }

};

(:~~~~~
 : Actual test cases.
 :)

Invoker tests

Ad-hoc unit test page for the ml-invoker library.

eval:invoke

{
   local:invoke(
      <eval:function name="l:hello" xmlns:l="http://www.w3.org/2005/xquery-local-functions"/>,
      'Hello, world!', 'name', 'both', 'function'),

   local:invoke(
      <eval:module href="/hello-world.xqy"/>,
      'Hello, world!', 'href', 'both', 'module'),

   local:invoke(
      <dummy>
         <eval:module href="/hello-world.xqy" id="me"/>
      </dummy>,
      'me',
      'Hello, world!', 'id', 'both', 'module'),

   local:invoke(
      <eval:module href="/hello-doc.xqy"/>,
      (),
      'Documents',
      'Hello, document!', 'db', 'param', 'module'),

   local:invoke(
      <eval:module href="/hello-doc.xqy" db="Documents"/>,
      'Hello, document!', 'db', 'elem', 'module'),

   local:invoke(
      <eval:module href="/hello-modules.xqy"/>,
      (),
      (),
      'invoker-db-two',
      'Hello, modules!', 'modules', 'param', 'module'),

   local:invoke(
      <eval:module href="/hello-modules.xqy" modules-db="invoker-db-two"/>,
      'Hello, modules!', 'modules', 'elem', 'module'),

   local:invoke(
      <eval:module href="/hello-world.xqy"/>,
      (),
      (),
      (),
      'xquery',
      'Hello, world!', 'xquery', 'param', 'module'),

   local:invoke(
      <eval:module href="/hello-world.xqy" lang="xquery"/>,
      'Hello, world!', 'xquery', 'elem', 'module'),

   local:invoke(
      <eval:module href="/hello-world.sjs"/>,
      (),
      (),
      (),
      'js',
      'Hello, js!', 'javascript', 'param', 'module'),

   local:invoke(
      <eval:module href="/hello-world.sjs" lang="js"/>,
      'Hello, js!', 'javascript', 'elem', 'module')
}

eval:call

{
   local:call(
      fn:function-lookup(xs:QName('local:hello'), 0),
      'Hello, world!', 'name', 'param'),

   local:call(
      <dummy name="l:hello" xmlns:l="http://www.w3.org/2005/xquery-local-functions"/>,
      'Hello, world!', 'name', 'elem'),

   local:call(
      local:hello#0,
      'Hello, world!', 'function I', 'param'),

   local:call(
      function() { 'Hello, world!' },
      'Hello, world!', 'function II', 'param'),

   local:call(
      function() { local:hello() },
      'Hello, world!', 'function III', 'param'),

   local:call(
      function() { local:hello('world') },
      'Hello, world!', 'function IV', 'param'),

   (: TODO: What about a JS fun, in an SJS file? :)
   local:call(
      xs:QName('h:hello'), '/hello-lib.xqy',
      'Hello, world!', 'href', 'param'),

   local:call(
      <dummy xmlns:h="http://expath.org/ns/invoker/test/hello"
             name="h:hello" href="/hello-lib.xqy"/>,
      'Hello, world!', 'href', 'elem'),

   local:call(
      local:hello-doc#0, (), 'Documents',
      'Hello, document!', 'db', 'param'),

   local:call(
      <dummy xmlns:l="http://www.w3.org/2005/xquery-local-functions"
             name="l:hello-doc" db="Documents"/>,
      'Hello, document!', 'db', 'elem'),

   local:call(
      xs:QName('l:hello'), '/hello-libdules.xqy', (), 'invoker-db-two',
      'Hello, world!', 'modules', 'param'),

   local:call(
      <dummy xmlns:l="http://expath.org/ns/invoker/test/libdules"
             name="l:hello" href="/hello-libdules.xqy" modules-db="invoker-db-two"/>,
      'Hello, world!', 'modules', 'elem'),

   local:call(
      local:hello#0, (), (), (), 'xquery',
      'Hello, world!', 'xquery', 'param'),

   local:call(
      <dummy xmlns:l="http://www.w3.org/2005/xquery-local-functions"
             name="l:hello" lang="xquery"/>,
      'Hello, world!', 'xquery', 'elem')

   (: TODO: Not supported yet... :)
   (:
   local:call(
      ???, (), (), (), 'js',
      'Hello, world!', 'javascript', 'param')

   local:call(
      <dummy name="hello" lang="js"/>,
      'Hello, world!', 'javascript', 'elem')
   :)
}

eval:module

{
   local:module(
      '/hello-world.xqy',
      'Hello, world!', 'href', 'param'),

   local:module(
      <dummy href="/hello-world.xqy"/>,
      'Hello, world!', 'href', 'elem'),

   local:module(
      '/hello-doc.xqy', 'Documents',
      'Hello, document!', 'db', 'param'),

   local:module(
      <dummy href="/hello-doc.xqy" db="Documents"/>,
      'Hello, document!', 'db', 'elem'),

   local:module(
      '/hello-modules.xqy', (), 'invoker-db-two',
      'Hello, modules!', 'modules', 'param'),

   local:module(
      <dummy href="/hello-modules.xqy" modules-db="invoker-db-two"/>,
      'Hello, modules!', 'modules', 'elem'),

   local:module(
      '/hello-world.xqy', (), (), 'xquery',
      'Hello, world!', 'xquery', 'param'),

   local:module(
      <dummy href="/hello-world.xqy" lang="xquery"/>,
      'Hello, world!', 'xquery', 'elem'),

   local:module(
      '/hello-world.sjs', (), (), 'js',
      'Hello, js!', 'javascript', 'param'),

   local:module(
      <dummy href="/hello-world.sjs" lang="js"/>,
      'Hello, js!', 'javascript', 'elem')
}


[MIN] Logs: placeholder added

[MIN] Logs: placeholder added

(:~
 : Logging page.
 :
 : @author Christian Grün, BaseX GmbH, 2014-15
 :)
module namespace _ = 'dba/logs';

import module namespace cons = 'dba/cons' at '../modules/cons.xqm';
import module namespace html = 'dba/html' at '../modules/html.xqm';
import module namespace tmpl = 'dba/tmpl' at '../modules/tmpl.xqm';
import module namespace util = 'dba/util' at '../modules/util.xqm';

(:~ Top category :)
declare variable $_:CAT := 'logs';

(:~
 : Logging page.
 : @param  $sort     table sort key
 : @param  $name     name (date) of log file
 : @param  $loglist  search term for log list
 : @param  $logs     search term for logs
 : @param  $error    error string
 : @param  $info     info string
 : @return page
 :)
declare
  %rest:GET
  %rest:path("/dba/logs")
  %rest:query-param("sort",    "{$sort}", "")
  %rest:query-param("name",    "{$name}")
  %rest:query-param("loglist", "{$loglist}")
  %rest:query-param("logs",    "{$logs}")
  %rest:query-param("error",   "{$error}")
  %rest:query-param("info",    "{$info}")
  %output:method("html")
function _:logs(
  $sort     as xs:string,
  $name     as xs:string?,
  $loglist  as xs:string?,
  $logs     as xs:string?,
  $error    as xs:string?,
  $info     as xs:string?
) as element(html) {
  cons:check(),

  let $loglists := _:loglist($sort, $loglist)
  let $error := if($loglists) then $error else $cons:DATA-ERROR
  return tmpl:wrap(map { 'top': $_:CAT, 'info': $info, 'error': $error },

        <form action="javascript:void(0);">

{ if($name) then Logs else 'Logs' }:
            <input size="14" name="loglist" id="loglist" value="{ $loglist }"
              placeholder="regular expression"
              onkeyup="logslist('Please wait…', 'Query was successful.');"/>

        </form>
        <form action="{ $_:CAT }" method="post" class="update" autocomplete="off">
          <input type='hidden' name='name' id='name' value='{ $name }'/>
          <input type='hidden' name='sort' id='sort' value='{ $sort }'/>

{ $loglists }

        </form>

      {
        if($name) then (

            { $name }:
            <input size="40" id="logs" value="{ ($loglist, $logs)[1] }"
              placeholder="regular expression"
              onkeyup="logentries('Please wait…', 'Query was successful.');"/>

,

,

        ) else (),
        html:focus(if($name) then 'logs' else 'loglist')
      }

  )
};

(:~
 : Returns log entries of a specific log file.
 : @param  $query    query
 : @param  $names    name of selected log files
 : @param  $sort     table sort key
 : @param  $loglist  loglist
 : @return html elements
 :)
declare
  %rest:POST("{$query}")
  %rest:path("/dba/log")
  %rest:query-param("name",    "{$name}")
  %rest:query-param("sort",    "{$sort}")
  %rest:query-param("loglist", "{$loglist}")
  %output:method("html")
function _:query(
  $name     as xs:string,
  $sort     as xs:string?,
  $query    as xs:string?,
  $loglist  as xs:string?
) as element()* {
  cons:check(),

  let $logs := try {
    util:eval("admin:logs($n, true())[matches(., $q, 'i')]",
      map { 'n': $name, 'q': $query }
    )
  } catch * { () }

  where $logs
  let $entries := $logs !
    <e t='{ @time }' a='{ @address }' u='{ @user}' p='{ @type }' m='{ @ms }' d='{ . }'/>
  let $headers := (
    <t type='time' order='desc'>Time</t>,
    Address,
    User,

Type
,
    <m type='decimal' order='desc'>ms</m>,
    <d>{ html:label($entries, ('Log Entry', 'Log Entries')) }</d>
  )
  return html:table($entries, $headers, (),
    map { 'name': $name, 'loglist': $loglist, 'logs': $query }, $sort)
};

(:~
 : Returns log data.
 : @param  $sort   table sort key
 : @param  $query  query
 : @return html elements
 :)
declare
  %rest:POST("{$query}")
  %rest:path("/dba/loglist")
  %rest:query-param("sort",  "{$sort}")
  %output:method("html")
function _:loglist(
  $sort   as xs:string?,
  $query  as xs:string?
) as element()* {
  cons:check(),

  let $logs := try {
    util:eval("for $a in admin:logs()
      let $n := $a/(@date,text())/string()
      where not($query) or (some $a in admin:logs($n) satisfies matches($a, $query, 'i'))
      order by $n descending
      return $a", map { 'query': $query }
    )
  } catch * { () }
  where $logs
  let $entries :=
    (: legacy (7.8.1): $a/@date :)
    for $a in $logs
    order by $a descending
    (: legacy (7.8.1): $a/@date :)
    return <e name='{ ($a/(@date,text())) }' size='{ $a/@size }'/>
  let $headers := (
    <name>{ html:label($entries, ('Log', 'Logs')) }</name>,
    <size type='bytes'>Size</size>
  )
  let $buttons := html:button('delete-logs', 'Delete', true())
  let $link := function($value) { $_:CAT }
  return html:table($entries, $headers, $buttons,
    map { 'sort': $sort, 'loglist': $query }, (), $link)
};

(:~
 : Redirects to the specified action.
 : @param  $action  action to perform
 : @param  $names   names of selected databases
 :)
declare
  %rest:POST
  %rest:path("/dba/logs")
  %rest:query-param("action", "{$action}")
  %rest:query-param("name",   "{$names}")
  %output:method("html")
function _:action(
  $action  as xs:string,
  $names   as xs:string*
) {
  web:redirect($action, map { 'name': $names[.], 'redirect': $_:CAT })
};

(:~
 : Logging page.
 :
 : @author Christian Grün, BaseX GmbH, 2014-15
 :)
module namespace _ = 'dba/logs';

import module namespace cons = 'dba/cons' at '../modules/cons.xqm';
import module namespace html = 'dba/html' at '../modules/html.xqm';
import module namespace tmpl = 'dba/tmpl' at '../modules/tmpl.xqm';
import module namespace util = 'dba/util' at '../modules/util.xqm';

(:~ Top category :)
declare variable $_:CAT := 'logs';

(:~
 : Logging page.
 : @param  $sort     table sort key
 : @param  $name     name (date) of log file
 : @param  $loglist  search term for log list
 : @param  $logs     search term for logs
 : @param  $error    error string
 : @param  $info     info string
 : @return page
 :)
declare
  %rest:GET
  %rest:path("/dba/logs")
  %rest:query-param("sort",    "{$sort}", "")
  %rest:query-param("name",    "{$name}")
  %rest:query-param("loglist", "{$loglist}")
  %rest:query-param("logs",    "{$logs}")
  %rest:query-param("error",   "{$error}")
  %rest:query-param("info",    "{$info}")
  %output:method("html")
function _:logs(
  $sort     as xs:string,
  $name     as xs:string?,
  $loglist  as xs:string?,
  $logs     as xs:string?,
  $error    as xs:string?,
  $info     as xs:string?
) as element(html) {
  cons:check(),

  let $loglists := _:loglist($sort, $loglist)
  let $error := if($loglists) then $error else $cons:DATA-ERROR
  return tmpl:wrap(map { 'top': $_:CAT, 'info': $info, 'error': $error },

        <form action="javascript:void(0);">

{ if($name) then Logs else 'Logs' }:
            <input size="14" name="loglist" id="loglist" value="{ $loglist }"
              onkeyup="logslist('Please wait…', 'Query was successful.');"/>

        </form>
        <form action="{ $_:CAT }" method="post" class="update" autocomplete="off">
          <input type='hidden' name='name' id='name' value='{ $name }'/>
          <input type='hidden' name='sort' id='sort' value='{ $sort }'/>

{ $loglists }

        </form>

      {
        if($name) then (

            { $name }:
            <input size="40" id="logs" value="{ ($loglist, $logs)[1] }"
              onkeyup="logentries('Please wait…', 'Query was successful.');"/>

,

,

        ) else (),
        html:focus(if($name) then 'logs' else 'loglist')
      }

  )
};

(:~
 : Returns log entries of a specific log file.
 : @param  $query    query
 : @param  $names    name of selected log files
 : @param  $sort     table sort key
 : @param  $loglist  loglist
 : @return html elements
 :)
declare
  %rest:POST("{$query}")
  %rest:path("/dba/log")
  %rest:query-param("name",    "{$name}")
  %rest:query-param("sort",    "{$sort}")
  %rest:query-param("loglist", "{$loglist}")
  %output:method("html")
function _:query(
  $name     as xs:string,
  $sort     as xs:string?,
  $query    as xs:string?,
  $loglist  as xs:string?
) as element()* {
  cons:check(),

  let $logs := try {
    util:eval("admin:logs($n, true())[matches(., $q, 'i')]",
      map { 'n': $name, 'q': $query }
    )
  } catch * { () }

  where $logs
  let $entries := $logs !
    <e t='{ @time }' a='{ @address }' u='{ @user}' p='{ @type }' m='{ @ms }' d='{ . }'/>
  let $headers := (
    <t type='time' order='desc'>Time</t>,
    Address,
    User,

Type
,
    <m type='decimal' order='desc'>ms</m>,
    <d>{ html:label($entries, ('Log Entry', 'Log Entries')) }</d>
  )
  return html:table($entries, $headers, (),
    map { 'name': $name, 'loglist': $loglist, 'logs': $query }, $sort)
};

(:~
 : Returns log data.
 : @param  $sort   table sort key
 : @param  $query  query
 : @return html elements
 :)
declare
  %rest:POST("{$query}")
  %rest:path("/dba/loglist")
  %rest:query-param("sort",  "{$sort}")
  %output:method("html")
function _:loglist(
  $sort   as xs:string?,
  $query  as xs:string?
) as element()* {
  cons:check(),

  let $logs := try {
    util:eval("for $a in admin:logs()
      let $n := $a/(@date,text())/string()
      where not($query) or (some $a in admin:logs($n) satisfies matches($a, $query, 'i'))
      order by $n descending
      return $a", map { 'query': $query }
    )
  } catch * { () }
  where $logs
  let $entries :=
    (: legacy (7.8.1): $a/@date :)
    for $a in $logs
    order by $a descending
    (: legacy (7.8.1): $a/@date :)
    return <e name='{ ($a/(@date,text())) }' size='{ $a/@size }'/>
  let $headers := (
    <name>{ html:label($entries, ('Log', 'Logs')) }</name>,
    <size type='bytes'>Size</size>
  )
  let $buttons := html:button('delete-logs', 'Delete', true())
  let $link := function($value) { $_:CAT }
  return html:table($entries, $headers, $buttons,
    map { 'sort': $sort, 'loglist': $query }, (), $link)
};

(:~
 : Redirects to the specified action.
 : @param  $action  action to perform
 : @param  $names   names of selected databases
 :)
declare
  %rest:POST
  %rest:path("/dba/logs")
  %rest:query-param("action", "{$action}")
  %rest:query-param("name",   "{$names}")
  %output:method("html")
function _:action(
  $action  as xs:string,
  $names   as xs:string*
) {
  web:redirect($action, map { 'name': $names[.], 'redirect': $_:CAT })
};