bootstrap addition

bootstrap addition

git-svn-id: 2087087167f935058d19856144d26af79f295c86@144438 62cac252-8da6-4816-9e9d-6dc37b19578c

xquery version "1.0-ml";

module namespace bootstrap = "http://marklogic.com/rest-api/resource/bootstrap";
(: Copyright 2002-2010 Mark Logic Corporation.  All Rights Reserved. :)

import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";

declare default function namespace "http://www.w3.org/2005/xpath-functions";

declare variable $p := "http://marklogic.com/ns/test/places";

declare function bootstrap:database-configure(
  $dbid as xs:unsignedLong)
as empty-sequence()
{

    let $c := admin:get-configuration()

    (: collection and word lexicons on :)
    let $c := admin:database-set-collection-lexicon($c, $dbid, true())
    let $c := admin:database-set-maintain-last-modified($c, $dbid, fn:true())

    let $rangespec := admin:database-range-element-index("dateTime",
                                "http://marklogic.com/xdmp/property","last-modified","",false() )
    let $c := admin:database-add-range-element-index($c, $dbid, $rangespec)

    let $rangespec := admin:database-range-element-index("string",
        "http://nwalsh.com/ns/photolib","tag","http://marklogic.com/collation/",false() )
    let $c := admin:database-add-range-element-index($c, $dbid, $rangespec)
    let $rangespec := admin:database-range-element-index("date",
        "http://nwalsh.com/ns/photolib","date","",false() )
    let $c := admin:database-add-range-element-index($c, $dbid, $rangespec)
    let $rangespec := admin:database-range-element-attribute-index("date",
        "http://nwalsh.com/ns/photolib","view","","date","",false() )
    let $c := admin:database-add-range-element-attribute-index($c, $dbid, $rangespec)
    let $c := admin:database-set-triple-index($c, $dbid, true())
    let $rangespec := admin:database-range-element-index("double",
        "","double","",false() )
    let $c := admin:database-add-range-element-index($c, $dbid, $rangespec)
    let $rangespec := admin:database-range-element-index("int",
        "","int","",false() )
    let $c := admin:database-add-range-element-index($c, $dbid, $rangespec)
    let $rangespec := admin:database-range-element-index("string",
        "","grandchild","http://marklogic.com/collation/",false() )
    let $c := admin:database-add-range-element-index($c, $dbid, $rangespec)
    let $rangespec := admin:database-range-element-index("string",
        "","string","http://marklogic.com/collation/",false() )
    let $c := admin:database-add-range-element-index($c, $dbid, $rangespec)
    let $wordspec := admin:database-element-word-lexicon(
        "","suggest","http://marklogic.com/collation/")
    let $c := admin:database-add-element-word-lexicon($c, $dbid, $wordspec)

    (: create geospatial indexes for geo unit test :)
    let $geospec := admin:database-geospatial-element-pair-index(
                       $p, "place", $p, "lat", $p, "long", "wgs84", false())
    let $c := admin:database-add-geospatial-element-pair-index($c, $dbid, $geospec)

    let $f1 := admin:database-field("int1", false())
    let $f2 := admin:database-field("int2", false())
    let $c := admin:database-add-field($c, $dbid, $f1)
    let $c := admin:database-add-field($c, $dbid, $f2)

    return admin:save-configuration-without-restart($c),
    (: you can't create field and field range index in same transaction :)
    let $c := admin:get-configuration()
    let $fi1 := admin:database-range-field-index("int", "int1", "", fn:false() )
    let $fi2 := admin:database-range-field-index("int", "int2", "", fn:false() )
    let $c := admin:database-add-range-field-index($c, $dbid, ($fi1, $fi2))
    return admin:save-configuration-without-restart($c)
};

declare function bootstrap:security-config(
$command as xs:string
)
{
    try {
        xdmp:eval(fn:concat('xquery version "1.0-ml"; ',
                    'import module namespace sec="http://marklogic.com/xdmp/security" at  ',
                    '    "/MarkLogic/security.xqy"; ',
                    $command),
        (),
        <options xmlns="xdmp:eval">
            <database>{ xdmp:database("Security") }</database>
        </options>)
    } catch($e) {
        xdmp:log($e)
    }
};

declare function bootstrap:load-data()
{
    xdmp:document-insert(
        "/sample/first.xml",
        <root name="first">
            <child id="1">
                <leaf>leaf1</leaf>
            </child>
            <child id="2">
                <leaf>leaf2</leaf>
            </child>
            <child id="3">
                <leaf>leaf3</leaf>
            </child>
        </root>
        ),
    xdmp:document-add-collections("/sample/first.xml",("http://some.org/collection1","http://some.org/collection2")),
    xdmp:document-set-permissions("/sample/first.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-set-properties(
        "/sample/first.xml",
        (
            <sample-property>first</sample-property>,
            <container-property><name>alpha</name><number>1</number></container-property>
            )
        ),
    xdmp:document-insert(
        "/sample/second.txt",
        <text><![CDATA[First line
    Second line
    Third line]]></text>/text()
        ),
    xdmp:document-set-permissions("/sample/second.txt",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-set-properties(
        "/sample/second.txt",
        (
            <sample-property>second</sample-property>,
            <container-property><name>beta</name><number>2</number></container-property>
            )
        ),
    xdmp:document-insert(
        "/sample/third.json",
        <text><![CDATA[{
        "firstKey":"first value",
        "secondKey":["first item","second item","third item"],
        "thirdKey":3,
        "fourthKey":{"subKey":"sub value"}
    }]]></text>/text()
        ),
    xdmp:document-set-permissions("/sample/third.json",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-set-properties(
        "/sample/third.json",
        (
            <json:sample-property type="string">third</json:sample-property>,
            <json:container-property type="object">
                <json:name type="string">gamma</json:name>
                <json:number type="number">3</json:number>
            </json:container-property>
            )
        ),
    xdmp:document-insert(
        "/sample/fourth.xml",
    <json:json version="1.0" type="object">
        <json:firstKey type="string">first value</json:firstKey>
        <json:fourthKey type="object">
            <json:subKey type="string">sub value</json:subKey>
        </json:fourthKey>
        <json:secondKey type="array">
            <json:item type="string">first item</json:item>
            <json:item type="string">second item</json:item>
            <json:item type="string">third item</json:item>
        </json:secondKey>
        <json:thirdKey type="number">3</json:thirdKey>
    </json:json>
        ),
    xdmp:document-set-permissions("/sample/fourth.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-set-properties(
        "/sample/fourth.xml",
        (
            <sample-property>fourth</sample-property>,
            <container-property><name>delta</name><number>4</number></container-property>
            )
        ),
    xdmp:document-insert(
        "/sample/fifth.sh",
            <text><![CDATA[#!/bin/sh
    echo "hello, world"
    ]]></text>/text()
        ),
    xdmp:document-set-permissions("/sample/fifth.sh",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-set-properties(
        "/sample/fifth.sh",
        (
            <sample-property>fifth</sample-property>,
            <container-property><name>epsilon</name><number>5</number></container-property>
            )
        ),
    xdmp:document-insert(
        "/sample/databases-icon-mini.png",
    binary{xs:hexBinary(xs:base64Binary(
    "iVBORw0KGgoAAAANSUhEUgAAAA0AAAATCAYAAABLN4eXAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9oIEQEjMtAYogQAAAKvSURBVCjPlZLLbhxFAEVPVVdXVz/G8zCOn0CsKGyQkSIIKzas8xfsWbLkp/gJhCKheIlAJDaj2MYez6u7p7vrxQKUPVc6+yOdK77/4cfXQohJqlOVZdmBSpKY6jQKBM45oVMlgHvrvMuNWRljvlNKq69G2YyqLDg4mLE/2yPNYFRWlFXF/nTC2clRWbc7Fss1IcZzqTA8eWY5eu7p1Hv+WvyBVjnGZOQmI9UKISUqSXDO0bS7Tko0xfGSp18kjM7v+P3+NUMr8T5grWMYLCEErHM474khoCw1t78eU/8mEOpjXpxekJUORIZSCbkxSCnRWpPnBikTqbx31E1DjJHpeIzRhnW9xceI857H5Yr1Zku765jf3DIMtlUAIQRCiFhnabsOH1IEAmstAGWRY11ApykmM0oplTKZjNGZREpJoUueHI0ZFRV7exX7+1Nm0yn9YLm5u2fX96lUseLwxQ0vX8H04i2/XP9Et5H44OkHS920hBDo+56u77GDjcrHjvV1ya3TDO2M01mOUAEAhED+R5IkpKmCiFCOjoc/p+xuLbPpCc+P95HaEqIBIhHoB8t2W/PwsKBudl5FH7GxwUYYouJh5ci7nLbtWW02LBaPvLuef1AdrItKKolJpkivwGrG5QxTCsq8pCxLqqrk7PiIwTmW6y0xRCVTSg4vFnz+raM4+5ur1RtSUZHnOUWeMx5VVFWJTlOstfTWRuk96NIyOUgRRc188RZvgRg/3OffjoFESohxUMvmjqufP+X+MqDTU77+5EvMKKBUQpZpijxHSkluDHvjMW8uL79Rnz07bwSyzDLFqCzwDNw/PNI0O9bbhvVmQ7vb0bQdi+Wq327rl+rko8krodKnCHnofJju+r5oupBstg1KJT7Vuruev185O9zVm/WVUmouYoz83/0DxhRmafe2kasAAAAASUVORK5CYII="
    ))}
        ),
    xdmp:document-insert(
        "/sample/databases-icon-mini.xhtml",
    <xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml">
      <xhtml:head>
        <xhtml:meta name="content-type" content="image/png"/>
        <xhtml:meta name="filter-capabilities" content="none"/>
        <xhtml:meta name="size" content="815"/>
        <xhtml:meta name="MarkLogic_Binary_Source" content="/sample/databases-icon-mini.png"/>
      </xhtml:head>
    </xhtml:html>
    ),
    xdmp:document-set-permissions("/sample/databases-icon-mini.png",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-set-permissions("/sample/databases-icon-mini.xhtml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-separate.xml",
        <metadata-target/>
        ),
    xdmp:document-set-permissions("/sample/metadata-separate.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-all.xml",
        <metadata-target/>
        ),
    xdmp:document-set-permissions("/sample/metadata-all.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-separate2.xml",
        <metadata-target/>
        ),
    xdmp:document-set-permissions("/sample/metadata-separate2.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-all2.xml",
        <metadata-target/>
        ),
    xdmp:document-set-permissions("/sample/metadata-all2.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-all3.xml",
        <metadata-target/>
        ),
    xdmp:document-set-permissions("/sample/metadata-all3.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-all4.xml",
        <metadata-target/>
        ),
    xdmp:document-set-permissions("/sample/metadata-all4.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-separate-json.xml",
        <json:json type="object"/>
        ),
    xdmp:document-set-permissions("/sample/metadata-separate-json.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-all-json.xml",
        <json:json type="object"/>
        ),
    xdmp:document-set-permissions("/sample/metadata-all-json.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-separate-json2.xml",
        <json:json type="object"/>
        ),
    xdmp:document-set-permissions("/sample/metadata-separate-json2.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-all-json2.xml",
        <json:json type="object"/>
        ),
    xdmp:document-set-permissions("/sample/metadata-all-json2.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-all-json3.xml",
        <json:json type="object"/>
        ),
    xdmp:document-set-permissions("/sample/metadata-all-json3.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-set-properties(
        "/sample/prop-only.xml",
        (
            <sample-property>first</sample-property>,
            <container-property><name>alpha</name><number>1</number></container-property>
            )
        ),
    xdmp:document-set-properties(
        "/sample/prop-only.json",
        (
            <json:sample-property type="string">third</json:sample-property>,
            <json:container-property type="object">
                <json:name type="string">gamma</json:name>
                <json:number type="number">3</json:number>
            </json:container-property>
            )
        ),
    xdmp:document-insert(
        "/sample/invalid-separate.xml",
        <metadata-target/>
        ),
    xdmp:document-set-permissions("/sample/invalid-separate.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/invalid-all.xml",
        <metadata-target/>
        ),
    xdmp:document-set-permissions("/sample/invalid-all.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/invalid-separate-json.xml",
        <json:json type="object"/>
        ),
    xdmp:document-set-permissions("/sample/invalid-separate-json.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/invalid-all-json.xml",
        <json:json type="object"/>
        ),
    xdmp:document-set-permissions("/sample/invalid-all-json.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/lexicon-test1.xml",
        <root name="first">
            <child id="1">
                <grandchild>grandchild1</grandchild>
            </child>
            <child id="2">
                <grandchild>grandchild2</grandchild>
            </child>
            <child id="3">
                <grandchild>grandchild3</grandchild>
            </child>
        </root>
        ),
    xdmp:document-add-collections("/sample/lexicon-test1.xml",("http://some.org/collection1","http://some.org/collection2")),
    xdmp:document-set-permissions("/sample/lexicon-test1.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-set-properties(
        "/sample/lexicon-test1.xml",
        (
            <sample-property>lexicon-test</sample-property>,
            <container-property><name>alpha</name><number>1</number></container-property>
            )
        ),
    xdmp:document-insert(
        "/sample/lexicon-test2.xml",
        <root name="first">
            <child id="1">
                <grandchild>grandchild4</grandchild>
            </child>
            <child id="2">
                <grandchild>grandchild5</grandchild>
            </child>
            <child id="3">
                <grandchild>grandchild6</grandchild>
            </child>
        </root>
        ),
    xdmp:document-add-collections("/sample/lexicon-test2.xml",("http://some.org/collection1","http://some.org/collection2")),
    xdmp:document-set-permissions("/sample/lexicon-test2.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-set-properties(
        "/sample/lexicon-test2.xml",
        (
            <sample-property>lexicon-test</sample-property>,
            <container-property><name>alpha</name><number>1</number></container-property>
            )
        ),
    xdmp:document-insert(
        "/sample/lexicon-test3.xml",
        <root name="first">
            <child id="1">
                <double>1.2</double>
                <int>10</int>
            </child>
            <child id="2">
                <double>2.3</double>
                <int>20</int>
            </child>
        </root>
        ),
    xdmp:document-set-permissions("/sample/lexicon-test3.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/lexicon-test4.xml",
        <root name="second">
            <child id="3">
                <double>2.2</double>
                <int>30</int>
            </child>
            <child id="5">
                <double>1.2</double>
                <int>10</int>
            </child>
             <child id="6">
                <double>1.2</double>
                <int>4</int>
            </child>
        </root>
        ),
    xdmp:document-set-permissions("/sample/lexicon-test4.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/udf-test.xml",
        <root name="first">
            <element-test attribute-test="1"/>
            <element-test attribute-test="5"/>
            <element-test attribute-test="9"/>
        </root>),
xdmp:document-insert(
    "/sample/tuples-test1.xml",
    <root name="first">
        <child id="1">
            <double>1.1</double>
            <int>1</int>
            <string>Alaska</string>
        </child>
    </root>
    ),
xdmp:document-set-permissions("/sample/tuples-test1.xml",
    (xdmp:permission("rest-reader","read"),
    xdmp:permission("rest-writer","update"),
    xdmp:permission("app-user","read"),
    xdmp:permission("app-user","update"))
    ),
xdmp:document-insert(
    "/sample/tuples-test2.xml",
    <root name="second">
        <child id="3">
            <double>1.1</double>
            <int>3</int>
            <string>Birmingham</string>
        </child>
    </root>
    ),
xdmp:document-set-permissions("/sample/tuples-test2.xml",
    (xdmp:permission("rest-reader","read"),
    xdmp:permission("rest-writer","update"),
    xdmp:permission("app-user","read"),
    xdmp:permission("app-user","update"))
    ),
xdmp:document-insert(
    "/sample/tuples-test3.xml",
    <root name="second">
        <child id="3">
            <double>1.2</double>
            <int>3</int>
            <string>Birmingham</string>
        </child>
    </root>
    ),
xdmp:document-set-permissions("/sample/tuples-test3.xml",
    (xdmp:permission("rest-reader","read"),
    xdmp:permission("rest-writer","update"),
    xdmp:permission("app-user","read"),
    xdmp:permission("app-user","update"))
    ),
xdmp:document-insert(
    "/sample/tuples-test4.xml",
    <root name="second">
        <child id="3">
            <double>1.2</double>
            <int>3</int>
            <string>Alaska</string>
        </child>
    </root>
    ),
xdmp:document-set-permissions("/sample/tuples-test4.xml",
    (xdmp:permission("rest-reader","read"),
    xdmp:permission("rest-writer","update"),
    xdmp:permission("app-user","read"),
    xdmp:permission("app-user","update"))
    ),
    xdmp:document-set-permissions("/sample/udf-test.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/legacy space file.xml",
        <samplelegacy>document</samplelegacy>
        ),
    xdmp:document-set-permissions("/sample/legacy space file.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"))
        ),

    xdmp:document-insert("/sample/suggestion.xml",
        <suggest><string>FINDME</string>Something I love to suggest is sugar with savory succulent limes.</suggest>),
    xdmp:document-set-permissions("/sample/suggestion.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"))
        ),
    xdmp:document-add-collections("/sample/suggestion.xml",("http://some.org/complexsuggestions")),
    xdmp:document-insert("/sample2/suggestion.xml",
        <suggest>Something I hate to suggest is liver with lard.</suggest>),
    xdmp:document-set-permissions("/sample2/suggestion.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"))
            ),
    xdmp:document-add-collections("/sample2/suggestion.xml",("http://some.org/suggestions"))
};

declare function bootstrap:load-search-data()
{
    ()
};

declare function bootstrap:post(
    $context as map:map,
    $params  as map:map,
    $input as document-node()*
) as document-node()*
{
   let $ncre := bootstrap:security-config('sec:create-user("rest-admin","rest-admin user", "x",("rest-admin"),(),(),())')
    let $ncre := bootstrap:security-config('sec:create-user("rest-reader","rest-reader user", "x",("rest-reader"),(),(),())')
    let $ncre := bootstrap:security-config('sec:create-user("rest-writer","rest-writer user", "x",("rest-writer"),(),(),())')
    let $ncre := bootstrap:security-config('sec:create-user("valid","valid unprivileged user", "x",(),(),(),())')
    let $dbid := xdmp:database("java-unittest")
    let $config := bootstrap:database-configure($dbid)
    let $_ := xdmp:log(("Configured Java test database:", $config))
    let $d1 := bootstrap:load-data()
    return ()
};

xquery version "1.0-ml";

module namespace bootstrap = "http://marklogic.com/rest-api/resource/bootstrap";
(: Copyright 2002-2010 Mark Logic Corporation.  All Rights Reserved. :)

import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";

declare default function namespace "http://www.w3.org/2005/xpath-functions";

declare variable $p := "http://marklogic.com/ns/test/places";

declare function bootstrap:database-configure(
  $dbid as xs:unsignedLong)
as empty-sequence()
{

    let $c := admin:get-configuration()

    (: collection and word lexicons on :)
    let $c := admin:database-set-collection-lexicon($c, $dbid, true())
    let $c := admin:database-set-maintain-last-modified($c, $dbid, fn:true())

    let $rangespec := admin:database-range-element-index("dateTime",
                                "http://marklogic.com/xdmp/property","last-modified","",false() )
    let $c := admin:database-add-range-element-index($c, $dbid, $rangespec)

    let $rangespec := admin:database-range-element-index("string",
        "http://nwalsh.com/ns/photolib","tag","http://marklogic.com/collation/",false() )
    let $c := admin:database-add-range-element-index($c, $dbid, $rangespec)
    let $rangespec := admin:database-range-element-index("date",
        "http://nwalsh.com/ns/photolib","date","",false() )
    let $c := admin:database-add-range-element-index($c, $dbid, $rangespec)
    let $rangespec := admin:database-range-element-attribute-index("date",
        "http://nwalsh.com/ns/photolib","view","","date","",false() )
    let $c := admin:database-add-range-element-attribute-index($c, $dbid, $rangespec)
    let $ := admin:database-set-triple-index($c, $dbid, true())
    let $rangespec := admin:database-range-element-index("double",
        "","double","",false() )
    let $c := admin:database-add-range-element-index($c, $dbid, $rangespec)
    let $rangespec := admin:database-range-element-index("int",
        "","int","",false() )
    let $c := admin:database-add-range-element-index($c, $dbid, $rangespec)
    let $rangespec := admin:database-range-element-index("string",
        "","grandchild","http://marklogic.com/collation/",false() )
    let $c := admin:database-add-range-element-index($c, $dbid, $rangespec)
    let $rangespec := admin:database-range-element-index("string",
        "","string","http://marklogic.com/collation/",false() )
    let $c := admin:database-add-range-element-index($c, $dbid, $rangespec)
    let $wordspec := admin:database-element-word-lexicon(
        "","suggest","http://marklogic.com/collation/")
    let $c := admin:database-add-element-word-lexicon($c, $dbid, $wordspec)

    (: create geospatial indexes for geo unit test :)
    let $geospec := admin:database-geospatial-element-pair-index(
                       $p, "place", $p, "lat", $p, "long", "wgs84", false())
    let $c := admin:database-add-geospatial-element-pair-index($c, $dbid, $geospec)

    let $f1 := admin:database-field("int1", false())
    let $f2 := admin:database-field("int2", false())
    let $c := admin:database-add-field($c, $dbid, $f1)
    let $c := admin:database-add-field($c, $dbid, $f2)

    return admin:save-configuration-without-restart($c),
    (: you can't create field and field range index in same transaction :)
    let $c := admin:get-configuration()
    let $fi1 := admin:database-range-field-index("int", "int1", "", fn:false() )
    let $fi2 := admin:database-range-field-index("int", "int2", "", fn:false() )
    let $c := admin:database-add-range-field-index($c, $dbid, ($fi1, $fi2))
    return admin:save-configuration-without-restart($c)
};

declare function bootstrap:security-config(
$command as xs:string
)
{
    try {
        xdmp:eval(fn:concat('xquery version "1.0-ml"; ',
                    'import module namespace sec="http://marklogic.com/xdmp/security" at  ',
                    '    "/MarkLogic/security.xqy"; ',
                    $command),
        (),
        <options xmlns="xdmp:eval">
            <database>{ xdmp:database("Security") }</database>
        </options>)
    } catch($e) {
        xdmp:log($e)
    }
};

declare function bootstrap:load-data()
{
    xdmp:document-insert(
        "/sample/first.xml",
        <root name="first">
            <child id="1">
                <leaf>leaf1</leaf>
            </child>
            <child id="2">
                <leaf>leaf2</leaf>
            </child>
            <child id="3">
                <leaf>leaf3</leaf>
            </child>
        </root>
        ),
    xdmp:document-add-collections("/sample/first.xml",("http://some.org/collection1","http://some.org/collection2")),
    xdmp:document-set-permissions("/sample/first.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-set-properties(
        "/sample/first.xml",
        (
            <sample-property>first</sample-property>,
            <container-property><name>alpha</name><number>1</number></container-property>
            )
        ),
    xdmp:document-insert(
        "/sample/second.txt",
        <text><![CDATA[First line
    Second line
    Third line]]></text>/text()
        ),
    xdmp:document-set-permissions("/sample/second.txt",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-set-properties(
        "/sample/second.txt",
        (
            <sample-property>second</sample-property>,
            <container-property><name>beta</name><number>2</number></container-property>
            )
        ),
    xdmp:document-insert(
        "/sample/third.json",
        <text><![CDATA[{
        "firstKey":"first value",
        "secondKey":["first item","second item","third item"],
        "thirdKey":3,
        "fourthKey":{"subKey":"sub value"}
    }]]></text>/text()
        ),
    xdmp:document-set-permissions("/sample/third.json",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-set-properties(
        "/sample/third.json",
        (
            <json:sample-property type="string">third</json:sample-property>,
            <json:container-property type="object">
                <json:name type="string">gamma</json:name>
                <json:number type="number">3</json:number>
            </json:container-property>
            )
        ),
    xdmp:document-insert(
        "/sample/fourth.xml",
    <json:json version="1.0" type="object">
        <json:firstKey type="string">first value</json:firstKey>
        <json:fourthKey type="object">
            <json:subKey type="string">sub value</json:subKey>
        </json:fourthKey>
        <json:secondKey type="array">
            <json:item type="string">first item</json:item>
            <json:item type="string">second item</json:item>
            <json:item type="string">third item</json:item>
        </json:secondKey>
        <json:thirdKey type="number">3</json:thirdKey>
    </json:json>
        ),
    xdmp:document-set-permissions("/sample/fourth.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-set-properties(
        "/sample/fourth.xml",
        (
            <sample-property>fourth</sample-property>,
            <container-property><name>delta</name><number>4</number></container-property>
            )
        ),
    xdmp:document-insert(
        "/sample/fifth.sh",
            <text><![CDATA[#!/bin/sh
    echo "hello, world"
    ]]></text>/text()
        ),
    xdmp:document-set-permissions("/sample/fifth.sh",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-set-properties(
        "/sample/fifth.sh",
        (
            <sample-property>fifth</sample-property>,
            <container-property><name>epsilon</name><number>5</number></container-property>
            )
        ),
    xdmp:document-insert(
        "/sample/databases-icon-mini.png",
    binary{xs:hexBinary(xs:base64Binary(
    "iVBORw0KGgoAAAANSUhEUgAAAA0AAAATCAYAAABLN4eXAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9oIEQEjMtAYogQAAAKvSURBVCjPlZLLbhxFAEVPVVdXVz/G8zCOn0CsKGyQkSIIKzas8xfsWbLkp/gJhCKheIlAJDaj2MYez6u7p7vrxQKUPVc6+yOdK77/4cfXQohJqlOVZdmBSpKY6jQKBM45oVMlgHvrvMuNWRljvlNKq69G2YyqLDg4mLE/2yPNYFRWlFXF/nTC2clRWbc7Fss1IcZzqTA8eWY5eu7p1Hv+WvyBVjnGZOQmI9UKISUqSXDO0bS7Tko0xfGSp18kjM7v+P3+NUMr8T5grWMYLCEErHM474khoCw1t78eU/8mEOpjXpxekJUORIZSCbkxSCnRWpPnBikTqbx31E1DjJHpeIzRhnW9xceI857H5Yr1Zku765jf3DIMtlUAIQRCiFhnabsOH1IEAmstAGWRY11ApykmM0oplTKZjNGZREpJoUueHI0ZFRV7exX7+1Nm0yn9YLm5u2fX96lUseLwxQ0vX8H04i2/XP9Et5H44OkHS920hBDo+56u77GDjcrHjvV1ya3TDO2M01mOUAEAhED+R5IkpKmCiFCOjoc/p+xuLbPpCc+P95HaEqIBIhHoB8t2W/PwsKBudl5FH7GxwUYYouJh5ci7nLbtWW02LBaPvLuef1AdrItKKolJpkivwGrG5QxTCsq8pCxLqqrk7PiIwTmW6y0xRCVTSg4vFnz+raM4+5ur1RtSUZHnOUWeMx5VVFWJTlOstfTWRuk96NIyOUgRRc188RZvgRg/3OffjoFESohxUMvmjqufP+X+MqDTU77+5EvMKKBUQpZpijxHSkluDHvjMW8uL79Rnz07bwSyzDLFqCzwDNw/PNI0O9bbhvVmQ7vb0bQdi+Wq327rl+rko8krodKnCHnofJju+r5oupBstg1KJT7Vuruev185O9zVm/WVUmouYoz83/0DxhRmafe2kasAAAAASUVORK5CYII="
    ))}
        ),
    xdmp:document-insert(
        "/sample/databases-icon-mini.xhtml",
    <xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml">
      <xhtml:head>
        <xhtml:meta name="content-type" content="image/png"/>
        <xhtml:meta name="filter-capabilities" content="none"/>
        <xhtml:meta name="size" content="815"/>
        <xhtml:meta name="MarkLogic_Binary_Source" content="/sample/databases-icon-mini.png"/>
      </xhtml:head>
    </xhtml:html>
    ),
    xdmp:document-set-permissions("/sample/databases-icon-mini.png",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-set-permissions("/sample/databases-icon-mini.xhtml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-separate.xml",
        <metadata-target/>
        ),
    xdmp:document-set-permissions("/sample/metadata-separate.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-all.xml",
        <metadata-target/>
        ),
    xdmp:document-set-permissions("/sample/metadata-all.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-separate2.xml",
        <metadata-target/>
        ),
    xdmp:document-set-permissions("/sample/metadata-separate2.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-all2.xml",
        <metadata-target/>
        ),
    xdmp:document-set-permissions("/sample/metadata-all2.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-all3.xml",
        <metadata-target/>
        ),
    xdmp:document-set-permissions("/sample/metadata-all3.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-all4.xml",
        <metadata-target/>
        ),
    xdmp:document-set-permissions("/sample/metadata-all4.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-separate-json.xml",
        <json:json type="object"/>
        ),
    xdmp:document-set-permissions("/sample/metadata-separate-json.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-all-json.xml",
        <json:json type="object"/>
        ),
    xdmp:document-set-permissions("/sample/metadata-all-json.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-separate-json2.xml",
        <json:json type="object"/>
        ),
    xdmp:document-set-permissions("/sample/metadata-separate-json2.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-all-json2.xml",
        <json:json type="object"/>
        ),
    xdmp:document-set-permissions("/sample/metadata-all-json2.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/metadata-all-json3.xml",
        <json:json type="object"/>
        ),
    xdmp:document-set-permissions("/sample/metadata-all-json3.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-set-properties(
        "/sample/prop-only.xml",
        (
            <sample-property>first</sample-property>,
            <container-property><name>alpha</name><number>1</number></container-property>
            )
        ),
    xdmp:document-set-properties(
        "/sample/prop-only.json",
        (
            <json:sample-property type="string">third</json:sample-property>,
            <json:container-property type="object">
                <json:name type="string">gamma</json:name>
                <json:number type="number">3</json:number>
            </json:container-property>
            )
        ),
    xdmp:document-insert(
        "/sample/invalid-separate.xml",
        <metadata-target/>
        ),
    xdmp:document-set-permissions("/sample/invalid-separate.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/invalid-all.xml",
        <metadata-target/>
        ),
    xdmp:document-set-permissions("/sample/invalid-all.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/invalid-separate-json.xml",
        <json:json type="object"/>
        ),
    xdmp:document-set-permissions("/sample/invalid-separate-json.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/invalid-all-json.xml",
        <json:json type="object"/>
        ),
    xdmp:document-set-permissions("/sample/invalid-all-json.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/lexicon-test1.xml",
        <root name="first">
            <child id="1">
                <grandchild>grandchild1</grandchild>
            </child>
            <child id="2">
                <grandchild>grandchild2</grandchild>
            </child>
            <child id="3">
                <grandchild>grandchild3</grandchild>
            </child>
        </root>
        ),
    xdmp:document-add-collections("/sample/lexicon-test1.xml",("http://some.org/collection1","http://some.org/collection2")),
    xdmp:document-set-permissions("/sample/lexicon-test1.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-set-properties(
        "/sample/lexicon-test1.xml",
        (
            <sample-property>lexicon-test</sample-property>,
            <container-property><name>alpha</name><number>1</number></container-property>
            )
        ),
    xdmp:document-insert(
        "/sample/lexicon-test2.xml",
        <root name="first">
            <child id="1">
                <grandchild>grandchild4</grandchild>
            </child>
            <child id="2">
                <grandchild>grandchild5</grandchild>
            </child>
            <child id="3">
                <grandchild>grandchild6</grandchild>
            </child>
        </root>
        ),
    xdmp:document-add-collections("/sample/lexicon-test2.xml",("http://some.org/collection1","http://some.org/collection2")),
    xdmp:document-set-permissions("/sample/lexicon-test2.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-set-properties(
        "/sample/lexicon-test2.xml",
        (
            <sample-property>lexicon-test</sample-property>,
            <container-property><name>alpha</name><number>1</number></container-property>
            )
        ),
    xdmp:document-insert(
        "/sample/lexicon-test3.xml",
        <root name="first">
            <child id="1">
                <double>1.2</double>
                <int>10</int>
            </child>
            <child id="2">
                <double>2.3</double>
                <int>20</int>
            </child>
        </root>
        ),
    xdmp:document-set-permissions("/sample/lexicon-test3.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/lexicon-test4.xml",
        <root name="second">
            <child id="3">
                <double>2.2</double>
                <int>30</int>
            </child>
            <child id="5">
                <double>1.2</double>
                <int>10</int>
            </child>
             <child id="6">
                <double>1.2</double>
                <int>4</int>
            </child>
        </root>
        ),
    xdmp:document-set-permissions("/sample/lexicon-test4.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/udf-test.xml",
        <root name="first">
            <element-test attribute-test="1"/>
            <element-test attribute-test="5"/>
            <element-test attribute-test="9"/>
        </root>),
xdmp:document-insert(
    "/sample/tuples-test1.xml",
    <root name="first">
        <child id="1">
            <double>1.1</double>
            <int>1</int>
            <string>Alaska</string>
        </child>
    </root>
    ),
xdmp:document-set-permissions("/sample/tuples-test1.xml",
    (xdmp:permission("rest-reader","read"),
    xdmp:permission("rest-writer","update"),
    xdmp:permission("app-user","read"),
    xdmp:permission("app-user","update"))
    ),
xdmp:document-insert(
    "/sample/tuples-test2.xml",
    <root name="second">
        <child id="3">
            <double>1.1</double>
            <int>3</int>
            <string>Birmingham</string>
        </child>
    </root>
    ),
xdmp:document-set-permissions("/sample/tuples-test2.xml",
    (xdmp:permission("rest-reader","read"),
    xdmp:permission("rest-writer","update"),
    xdmp:permission("app-user","read"),
    xdmp:permission("app-user","update"))
    ),
xdmp:document-insert(
    "/sample/tuples-test3.xml",
    <root name="second">
        <child id="3">
            <double>1.2</double>
            <int>3</int>
            <string>Birmingham</string>
        </child>
    </root>
    ),
xdmp:document-set-permissions("/sample/tuples-test3.xml",
    (xdmp:permission("rest-reader","read"),
    xdmp:permission("rest-writer","update"),
    xdmp:permission("app-user","read"),
    xdmp:permission("app-user","update"))
    ),
xdmp:document-insert(
    "/sample/tuples-test4.xml",
    <root name="second">
        <child id="3">
            <double>1.2</double>
            <int>3</int>
            <string>Alaska</string>
        </child>
    </root>
    ),
xdmp:document-set-permissions("/sample/tuples-test4.xml",
    (xdmp:permission("rest-reader","read"),
    xdmp:permission("rest-writer","update"),
    xdmp:permission("app-user","read"),
    xdmp:permission("app-user","update"))
    ),
    xdmp:document-set-permissions("/sample/udf-test.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"),
        xdmp:permission("app-user","read"),
        xdmp:permission("app-user","update"))
        ),
    xdmp:document-insert(
        "/sample/legacy space file.xml",
        <samplelegacy>document</samplelegacy>
        ),
    xdmp:document-set-permissions("/sample/legacy space file.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"))
        ),

    xdmp:document-insert("/sample/suggestion.xml",
        <suggest><string>FINDME</string>Something I love to suggest is sugar with savory succulent limes.</suggest>),
    xdmp:document-set-permissions("/sample/suggestion.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"))
        ),
    xdmp:document-add-collections("/sample/suggestion.xml",("http://some.org/complexsuggestions")),
    xdmp:document-insert("/sample2/suggestion.xml",
        <suggest>Something I hate to suggest is liver with lard.</suggest>),
    xdmp:document-set-permissions("/sample2/suggestion.xml",
        (xdmp:permission("rest-reader","read"),
        xdmp:permission("rest-writer","update"))
            ),
    xdmp:document-add-collections("/sample2/suggestion.xml",("http://some.org/suggestions"))
};

declare function bootstrap:load-search-data()
{
    ()
};

declare function bootstrap:post(
    $context as map:map,
    $params  as map:map,
    $input as document-node()*
) as document-node()*
{
   let $ncre := bootstrap:security-config('sec:create-user("rest-admin","rest-admin user", "x",("rest-admin"),(),(),())')
    let $ncre := bootstrap:security-config('sec:create-user("rest-reader","rest-reader user", "x",("rest-reader"),(),(),())')
    let $ncre := bootstrap:security-config('sec:create-user("rest-writer","rest-writer user", "x",("rest-writer"),(),(),())')
    let $ncre := bootstrap:security-config('sec:create-user("valid","valid unprivileged user", "x",(),(),(),())')
    let $dbid := xdmp:database("java-unittest")
    let $config := bootstrap:database-configure($dbid)
    let $_ := xdmp:log(("Configured Java test database:", $config))
    let $d1 := bootstrap:load-data()
    return ()
};


Term display is handled on the jinja side now.

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/julen/tzos.git/'


Address archive module for #54

Address archive module for #54

xquery version "3.0";
(:~
: BaseX archive module functions
:
: @see http://docs.basex.org/wiki/Archive_Module
:)
module namespace archive = "http://basex.org/modules/archive";

import module namespace a = "http://reecedunn.co.uk/xquery/annotations" at "res://reecedunn.co.uk/xquery/annotations.xqy";

declare %a:since("basex", "9.0") function archive:create($entries as item(), $contents as item()*) as xs:base64Binary external;
declare %a:since("basex", "9.0") function archive:create($entries as item(), $contents as item()*, $options as map(*)?) as xs:base64Binary external;
declare %a:since("basex", "9.0") function archive:create-from($path as xs:string) as xs:base64Binary external;
declare %a:since("basex", "9.0") function archive:create-from($path as xs:string, $options as map(*)?) as xs:base64Binary external;
declare %a:since("basex", "9.0") function archive:create-from($path as xs:string, $options as map(*)?, $entries as item()*) as xs:base64Binary external;
declare %a:since("basex", "9.0") function archive:entries($archive as xs:base64Binary) as element(archive:entry)* external;
declare %a:since("basex", "9.0") function archive:options($archive as xs:base64Binary) as map(*) external;
declare %a:since("basex", "9.0") function archive:extract-text($archive as xs:base64Binary) as xs:string* external;
declare %a:since("basex", "9.0") function archive:extract-text($archive as xs:base64Binary, $entries as item()*) as xs:string* external;
declare %a:since("basex", "9.0") function archive:extract-text($archive as xs:base64Binary, $entries as item()*, $encoding as xs:string) as xs:string* external;
declare %a:since("basex", "9.0") function archive:extract-binary($archive as xs:base64Binary) as xs:base64Binary* external;
declare %a:since("basex", "9.0") function archive:extract-binary($archive as xs:base64Binary, $entries as item()*) as xs:base64Binary* external;
declare %a:since("basex", "9.0") function archive:extract-to($path as xs:string, $archive as xs:base64Binary) as empty-sequence() external;
declare %a:since("basex", "9.0") function archive:extract-to($path as xs:string, $archive as xs:base64Binary, $entries as item()*) as empty-sequence() external;
declare %a:since("basex", "9.0") function archive:update($archive as xs:base64Binary, $entries as item()*, $contents as item()*) as xs:base64Binary external;
declare %a:since("basex", "9.0") function archive:delete($archive as xs:base64Binary, $entries as item()*) as xs:base64Binary external;

error: pathspec 'src/main/resources/builtin/basex.org/9.0/archive.xqy' did not match any file(s) known to git


Add BaseX Strings Module function signatures.

xquery version "3.0";
(:~
: BaseX Strings Module functions
:
: @see http://docs.basex.org/wiki/Strings_Module
:)
module namespace strings = "http://basex.org/modules/strings";

import module namespace a = "http://reecedunn.co.uk/xquery/annotations" at "res://reecedunn.co.uk/xquery/annotations.xqy";

declare %a:since("basex", "8.3") function strings:levenshtein($string1 as xs:string, $string2 as xs:string) as xs:double external;
declare %a:since("basex", "8.3") function strings:soundex($string as xs:string) as xs:string external;
declare %a:since("basex", "8.3") function strings:cologne-phonetic($string as xs:string) as xs:string external;

error: pathspec 'src/main/resources/builtin/basex.org/modules/strings.xqy' did not match any file(s) known to git


Folder for task workflows

Folder for task workflows

declare default element namespace "http://www.w3.org/1999/xhtml";

import module namespace logx="http://cefn.com/logx" at "../lib/logx.xq";

logx:webpage(
    for $doc in $logx:collection
        let $type := $logx:tags,
            $typednodes := logx:descendants-of-type($doc,$type),
            $hascontent := logx:hascontent($typednodes)
        where not($hascontent)
    return logx:editor-link($doc)
)

error: pathspec 'ui/xq/tasks/tags.xq' did not match any file(s) known to git


Display natural text instead of the keyword.


Template which wraps HTML within the standard menu and layout table

fatal: unable to access 'https://github.com/cefn/firmware-codesign-readinglog.git/': The requested URL returned error: 403


Create q4.xquery

Create q4.xquery

<q4>
{
for $l in doc("bib.xml") //libro
  where count($l/autor)>0
    return
    <libro>
   {
     <titulo> { string($l/titulo) } </titulo>,
     <autor> { string($l/autor[1]/nombre) } </autor>
   }
   {
     if(count($l/autor) > 1) then <hay-mas-autores> { count($l/autor) } </hay-mas-autores> else ()
   }
   </libro>
}
</q4>

error: pathspec 'Xquery-lab/q4.xquery' did not match any file(s) known to git


new index test

new index test

import module namespace idx = "http://www.w3.org/TestModules/idx" at "module1.xqlib";

declare namespace zorba = "http://www.zorba-xquery.com/zorba/op-extensions";

block
{
  zorba:create-index(xs:QName("idx:foo"));

  for $x in idx:probe(xs:QName("idx:foo"), "person1")
  return <person id = "{$x/@id}">{$x/name}</person>;
}
,
block
{
  insert node
    <person id="person50"><name>Some Name</name></person>
  as first into
    doc("auction.xml")/site/people

  ,

  zorba:refresh-index(xs:QName("idx:foo"));
}

,

for $x in idx:probe(xs:QName("idx:foo"), "person50")
return <person id = "{$x/@id}">{$x/name}</person>

error: pathspec 'test/rbkt/Queries/zorba/index/module1u.xq' did not match any file(s) known to git


Added bulk_2.xq

Added bulk_2.xq

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/ubpb/basex-stuff.git/'


XQuery for formatting in XHTML comments from XFDF files

declare default element namespace "http://www.w3.org/1999/xhtml";
declare namespace xfdf="http://ns.adobe.com/xfdf/";

<xhtml>

issue	author	page	comment

{
for $h at $issue in //xfdf:highlight
let $page := $h/@page
let $author := $h/@title
let $comment := $h/xfdf:contents/text()
return
{fn:string($issue)}	{fn:string($author)}	{fn:string($page)}	{fn:string($comment)}

}

</xhtml>

error: pathspec 'xfdf2xhtml.xquery' did not match any file(s) known to git


Missing module from previous commits today

svn path=/trunk/eXist/; revision=12772

xquery version "1.0";

module namespace sharing = "http://exist-db.org/mods/sharing";

import module namespace config = "http://exist-db.org/mods/config" at "config.xqm";
import module namespace security = "http://exist-db.org/mods/security" at "security.xqm";
declare namespace group = "http://exist-db.org/mods/sharing/group";

declare variable $sharing:groups-collection := fn:concat($config:mods-root, "/groups");

declare function sharing:get-groups() as element(group:group)*
{
    fn:collection($sharing:groups-collection)/group:group
};

declare function sharing:get-group-members($groupId) as xs:string*
{
    let $group := fn:collection($sharing:groups-collection)/group:group[@id eq $groupId] return
        security:get-group-members($group/group:system/group:group)
};

error: pathspec 'webapp/biblio/mods/apps/mods/search/sharing.xqm' did not match any file(s) known to git


[test] Tests for util:get-fragment-between

xquery version "3.1";

module namespace gfb = "http://exist-db.org/test/util/get-fragment-between";

declare namespace test = "http://exist-db.org/xquery/xqsuite";
declare namespace tei = "http://www.tei-c.org/ns/1.0";

declare variable $gfb:DOC1 :=
    <TEI xmlns="http://www.tei-c.org/ns/1.0">
        <text>
            <front>
                <pb facs="1.jpg"/>

Aus dem Leben einer Kartoffel.

                <pb facs="2.jpg"/>
            </front>

                <pb facs="3.jpg"/>

Hubertus Knoll spazierte über das <pb facs="4.jpg"/> Feld.

                <pb facs="5.jpg"/>

        </text>
    </TEI>;

declare variable $gfb:DOC2 :=
    <root xmlns="http://exist-db.org/xquery/xqsuite">
        <x/>
        <y/>
        <z/>
    </root>;

declare
    %test:setUp
function gfb:setup() {
    xmldb:create-collection("/db", "test-gfb"),
    xmldb:store("/db/test-gfb", "doc1.xml", $gfb:DOC1),
    xmldb:store("/db/test-gfb", "doc2.xml", $gfb:DOC2)
};

declare
    %test:tearDown
function gfb:teardown() {
    xmldb:remove("/db/test-gfb")
};

declare
    %test:assertEquals('<pb facs="1.jpg"></pb>
Aus dem Leben einer Kartoffel.
')
function gfb:tei-fragment() {
    let $doc := doc("/db/test-gfb/doc1.xml")
    let $pbs := $doc//tei:pb
    let $pb1 := $pbs[1]
    let $pb2 := $pbs[2]
    return
        util:get-fragment-between($pb1,$pb2,false(),true())
};

declare
    %test:assertEquals("<x></x>")
function gfb:fragment-no-namespace() {
    let $doc := doc("/db/test-gfb/doc2.xml")
    let $elems := $doc/test:root/*
    let $fragment := util:get-fragment-between($elems[1], $elems[2], false(), false())
    return
        $fragment
};

declare
    %test:assertEquals("<root><x></x></root>")
function gfb:wrapped-fragment-no-namespace() {
    let $doc := doc("/db/test-gfb/doc2.xml")
    let $elems := $doc/test:root/*
    let $fragment := util:get-fragment-between($elems[1], $elems[2], true(), false())
    return
        $fragment => replace("\s", "")
};

declare
    %test:assertTrue
function gfb:wrapped-fragment-is-parseable() {
    let $doc := doc("/db/test-gfb/doc2.xml")
    let $elems := $doc/test:root/*
    let $fragment := util:get-fragment-between($elems[1], $elems[2], true(), false())
    let $parsed := try { parse-xml($fragment) } catch * { $err:code }
    return
        $parsed instance of document-node(element(root))
};

error: pathspec 'exist-core/src/test/xquery/util/get-fragment-between.xqm' did not match any file(s) known to git


[test] add tests for fn:position

[test] add tests for fn:position

refs #3737

xquery version "3.1";

(: https://github.com/eXist-db/exist/issues/3737 :)
module namespace fn-pos="http://exist-db.org/xquery/test/fn-position";

declare namespace test="http://exist-db.org/xquery/xqsuite";

declare variable $fn-pos:collection := 'test-fn-pos';
declare variable $fn-pos:doc := 'stored-nodes.xml';
declare variable $fn-pos:nodes := document {
    <a>
      <b n="a"/>
      <b n="b"/>
      <b n="c"/>
    </a>
};

declare
    %test:setUp
function fn-pos:setup () {
    xmldb:create-collection('/db', $fn-pos:collection),
    xmldb:store('/db/' || $fn-pos:collection, $fn-pos:doc, $fn-pos:nodes)
};

declare
    %test:tearDown
function fn-pos:teardown () {
    xmldb:remove('/db/' || $fn-pos:collection, $fn-pos:doc)
};

declare
    %test:assertEquals(1,1,2,3)
function fn-pos:test-in-memory-dom() {
    $fn-pos:nodes/a/position(),
    $fn-pos:nodes//b/position()
};

declare
    %test:assertEquals(1,1,2,3)
function fn-pos:test-in-memory-dom-with-predicates() {
    $fn-pos:nodes/a[true()]/position(),
    $fn-pos:nodes//b[true()]/position()
};

declare
    %test:assertEquals(1,1)
function fn-pos:test-in-memory-dom-with-positional-predicates() {
    $fn-pos:nodes/a[1]/position(),
    $fn-pos:nodes//b[3]/position()
};

declare
    %test:assertEquals(1,1,2,3)
function fn-pos:test-persistent-dom() {
    let $dom := doc('/db/' || $fn-pos:collection || '/' || $fn-pos:doc)

    return (
        $dom/a/position(),
        $dom//b/position()
    )
};

declare
    %test:assertEquals(1,1,2,3)
function fn-pos:test-persistent-dom-with-predicates() {
    let $dom := doc('/db/' || $fn-pos:collection || '/' || $fn-pos:doc)

    return (
        $dom/a[true()]/position(),
        $dom//b[true()]/position()
    )
};

declare
    %test:assertEquals(1,1)
function fn-pos:test-persistent-dom-with-positional-predicates() {
    let $dom := doc('/db/' || $fn-pos:collection || '/' || $fn-pos:doc)

    return (
        $dom/a[1]/position(),
        $dom//b[3]/position()
    )
};

error: pathspec 'exist-core/src/test/xquery/fn-position.xqm' did not match any file(s) known to git


Added Hash Library to support CRC32 and Adler32


Added key-usage-report XQuery that uses new DITA collection framework

(: XQuery script to report on the keys used an declared in a map.

   NOTE: This script requires a custom collection() that provides
   the set of maps and topics referenced directly or indirectly
   by the input root map.

   :)

declare function local:getKeyrefs($doc as document-node()) as xs:string* {
for $e in $doc//*[@keyref]
return tokenize($e/@keyref, '/')[1]

};

declare function local:reportKeydef($keydef as element()) as node()* {
    element {name($keydef)} {
        $keydef/@* except ($keydef/@*:ditaarch, $keydef/@class),
        attribute {'mapuri'} { document-uri(root($keydef)) }
    }
};

let $thisDoc := root(.)
let $topicDocs := collection('ditaTopics:/' || document-uri(.))[*[contains(@class, ' topic/topic ')]]
let $mapDocs := ($thisDoc, collection('ditaTopics:/' || document-uri(.))[*[contains(@class, ' map/map ')]])

let $allKeyrefs as xs:string* :=
for $doc in $topicDocs  return local:getKeyrefs($doc)

return
<key-usage-report
   input-doc="{document-uri($thisDoc)}"
>
  <allreffedkeys>
{
  distinct-values($allKeyrefs)
 }</allreffedkeys>
   <unresolvedkeys>{
   for $key in distinct-values($allKeyrefs)
        return
        if (exists(
                 for $doc in $mapDocs
                       return
                             $doc//*[$key = tokenize(./@keys, ' ')])
             )
              then ()
              else '
' || $key
                 }</unresolvedkeys>
  <usedkeydefs>{
  for $key in distinct-values($allKeyrefs)
      return
         for $doc in $mapDocs
             return for $keydef as element() in $doc//*[$key = tokenize(./@keys, ' ')]
                               return local:reportKeydef($keydef)
  }</usedkeydefs>
  <keyrefsbytopic>{
    for $doc in $topicDocs order by lower-case(tokenize(document-uri($doc), '/')[last()])
         return
            <topic uri="{string-join(tokenize(document-uri($doc), '/')[position() ge last() -2 ], '/')}"
            >
               <reffedkeys>{
                   distinct-values(local:getKeyrefs($doc))
               }</reffedkeys>
             </topic>
  }</keyrefsbytopic>
  <navigationKeys>{
    for $doc in $mapDocs
          return for $keydef in $doc//*[@keys][@processing-role = ('normal')]
               return for $key in tokenize($keydef/@keys, ' ')
                   return '
' || $key
  }</navigationKeys>
 </key-usage-report>

error: pathspec 'src/xquery/key-usage-report/key-usage-report.xquery' did not match any file(s) known to git


Create q1.xquery

Create q1.xquery

<libros-y-precio-entre-50-y-250>
{
for $l in doc("bib.xml") //libro
 let $p := $l/precio
 where $p < 250 and $p >50
   return
   <libro>
   {
     <titulo> { string($l/titulo) } </titulo>,
     <precio> { string($l/precio) } </precio>
   }
   </libro>
}
</libros-y-precio-entre-50-y-250>

error: pathspec 'Xquery-lab/q1.xquery' did not match any file(s) known to git


add controller

add controller

xquery version "3.0";
declare variable $exist:path external;
declare variable $exist:resource external;
declare variable $exist:controller external;
declare variable $exist:prefix external;
declare variable $exist:root external;
if (ends-with($exist:resource, ".html")) then
let $template-pages := '/templates/pages/'
let $template-posts := '/templates/posts/'
let $collection := if( matches($exist:path ,'^/index.html$'))then ('home')
else(tokenize($exist:path, '/')[2])

let $colURL :=
  if( matches( $exist:path , '^/archive/index.html$')) then (
      $exist:controller ||  $template-posts ||  'feed' || '.html'
      )
  else if( contains( $exist:path , 'archive/' )) then (
     $exist:controller ||  $template-posts ||  'entry' || '.html'
    )
  else(
    $exist:controller ||  $template-pages ||  $collection || '.html'
    )

let $errorURL := $exist:controller ||  $template-pages || 'error.html'
let $viewURL := $exist:controller ||  '/modules/view.xql'
return
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
  <forward method="get" url="{$colURL}" />
  <view>
    <forward url="{$viewURL}">
      <add-parameter name="exist-root" value="{$exist:root}" />
      <add-parameter name="exist-prefix" value="{$exist:prefix}" />
      <add-parameter name="exist-controller" value="{$exist:controller}" />
      <add-parameter name="exist-resource" value="{$exist:resource}" />
      <add-parameter name="exist-path" value="{$exist:path}" />
    </forward>
  </view>
  <error-handler>
    <forward method="get" url="{$errorURL}" />
    <forward url="{$viewURL}" />
  </error-handler>
</dispatch>
else
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
  <cache-control cache="yes" />
</dispatch>

error: pathspec '.deploy/controller.xql' did not match any file(s) known to git


Added xq file expressing queries using XQuery

Web data management & xml (CSE 5335)/Programming assignment 7/queries.xq

/bin/sh: 1: Syntax error: word unexpected (expecting ")")


Display more information with the term.

Display more information with the term.


[doc] adding nascent one click remove package functionality

svn path=/trunk/eXist/; revision=12145

xquery version "1.0";

import module namespace repo="http://exist-db.org/xquery/repo";

let $host :=  request:get-hostname()
let $name :=  request:get-parameter('name','')
let $name := replace($url, '^.*/([^/]+)$', '$1')
let $install := repo:install(concat('http://', $host,':8080',$url))

return

         {$name} removed: {$install} .

        note: you will need to restart eXist for changes to take affect

error: pathspec 'webapp/repo/remove-package.xqy' did not match any file(s) known to git


Added xq file expressing query 5 using XQuery

Web data management & xml (CSE 5335)/Programming assignment 7/Individual queries/5.xq


test that used to create deadlock

test that used to create deadlock

import module namespace coll =
"http://www.zorba-xquery.com/zorba/collection-functions";

declare sequential function local:test()
{
  coll:create-collection("test", Foo);

  replace value of node collection("test")[1]/b with "Bar";

  coll:insert-nodes-at("test", 2, );

  ();
};

local:test();

error: pathspec 'test/rbkt/Queries/zorba/collections/q1.xq' did not match any file(s) known to git


added overview of the tests/targets

added overview of the tests/targets

xquery version "1.0";
module namespace fcs-tests  = "http://clarin.eu/fcs/1.0/tests";

import module namespace httpclient = "http://exist-db.org/xquery/httpclient";
import module namespace t="http://exist-db.org/xquery/testing";
import module namespace repo-utils = "http://aac.ac.at/content_repository/utils" at  "/db/cr/repo-utils.xqm";

declare namespace zr="http://explain.z3950.org/dtd/2.1/";
declare namespace sru = "http://www.loc.gov/zing/srw/";
declare namespace fcs = "http://clarin.eu/fcs/1.0";
declare namespace diag = "http://www.loc.gov/zing/srw/diagnostic/";
declare namespace xhtml="http://www.w3.org/1999/xhtml";

(: sample input:

 :)
declare variable $fcs-tests:config := doc("config.xml");
declare variable $fcs-tests:cr-config := doc("/db/cr/etc/config.xml");
declare variable $fcs-tests:run-config := "run-config.xml";
declare variable $fcs-tests:testsets-coll := "/db/cr/modules/testing/testsets/";
declare variable $fcs-tests:results-coll := "/db/cr/modules/testing/results/";

(: this function is accessed by the testing-code to get configuration-options from the run-config :)
declare function fcs-tests:config-value($key as xs:string) as xs:string* {
            let $config := doc(concat($fcs-tests:testsets-coll, $fcs-tests:run-config))/config
                return if ($key eq "testset") then  xs:string($config//testset/@key)
                        else $config//target/text()
};

declare function fcs-tests:config-key($key as xs:string) as xs:string* {
            let $config := doc(concat($fcs-tests:testsets-coll, $fcs-tests:run-config))/config
                return if ($key eq "testset") then  xs:string($config//testset/@key)
                        else xs:string($config//target/@key)
};

declare function fcs-tests:get-result-paths($target  as xs:string, $testset as xs:string) as xs:string* {
        (concat($fcs-tests:results-coll, $target, "/"), concat($testset, ".xml"))
};

declare function fcs-tests:get-result-path($target  as xs:string, $testset as xs:string) as xs:string* {
        string-join(fcs-tests:get-result-paths($target , $testset ), "" )
};

(:~ try to get the testset-file based on the testset-key
@returns testset-file if available, otherwise empty result
:)
declare function fcs-tests:get-testset($testset as xs:string) as item()* {
    let $testset-path := concat($fcs-tests:testsets-coll, $testset, ".xml")
    return if (doc-available($testset-path)) then
                    doc($testset-path)
                  else
                  <diagnostics>unknown testset: {$testset}</diagnostics>
};

declare function fcs-tests:run-testset($target  as xs:string, $testset-key as xs:string) as item()* {

(: preparing a configuration for given run, based on the parameters :)
let $run-config := <config>{($fcs-tests:config//target[xs:string(@key) = $target],
                            $fcs-tests:config//testset[xs:string(@key) = $testset-key])}</config>
let $store := repo-utils:store($fcs-tests:testsets-coll, $fcs-tests:run-config, $run-config, true())
let $testset := fcs-tests:get-testset($testset-key)
let $result := if (exists($testset)) then
                        let $tests := $testset//TestSet
                        (: distinguish the testset, that the testing-module can process
                           and the home-made test-doc, that tests URLs :)
                        return if (exists($tests)) then
                                    t:run-testSet($tests)
                                 else
                                    fcs-tests:test-rest($testset)
                   else
                       $testset

let $store-result := fcs-tests:store-result($target, $testset-key, $result)
(:for $test in $tests/tests/test return fcs-tests:run-test($test):)
return $store-result

};

declare function fcs-tests:format-result($result as node()) as item()* {

    if ($result[self::element(diagnostics)]) then

{$result/text()}

          else if (exists($result/info)) then
                    $result
          else repo-utils:serialise-as ($result,"html", "test", $fcs-tests:cr-config)
              (: return quite empty html ??
                t:format-testResult($result) :)
};

declare function fcs-tests:display-page($target  as xs:string, $testset as xs:string, $operation) as item()* {

    let $result-path := fcs-tests:get-result-path($target, $testset)

    let $result := if ($target = '' or $testset = '') then ()
                       else if (doc-available($result-path)) then
                            doc($result-path)
                        else
                            <diagnostics><diagnostic>result unavailable: {$result-path}</diagnostic>
                                <diagnostic>fcs-tests:get-testset($testset)</diagnostic>
                              </diagnostics>

    let $formatted-result := fcs-tests:format-result($result)
    let $opt := util:declare-option("exist:serialize", "media-type=text/html method=xhtml")
    return

            <title>FCS - test suite</title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
             <link rel="stylesheet" type="text/css" href="tests.css" />

FCS - test suite

            <form>
                targets<select name="target">
                    {
                    for $target-elem  in $fcs-tests:config//target
                        let $target-key := xs:string($target-elem/@key)
                        let $option :=  if ($target = $target-key) then
                                            <option selected="selected" value="{$target-key}" >{$target-key}</option>
                                         else
                                            <option value="{$target-key}" >{$target-key } </option>
                        return $option
                    }
                </select>
                test-set<select name="testset">
                    {
                    for $testset-elem in $fcs-tests:config//testset
                        let $testset-key := xs:string($testset-elem/@key)
                        let $option :=  if ($testset= $testset-key) then
                                            <option selected="selected" value="{$testset-key}" >{$testset-key}</option>
                                         else
                                            <option value="{$testset-key}" >{$testset-key}</option>
                        return $option
                    }
                </select>
                test-set
                    <select name="operation">
                       <option value="run" >{if ($operation = 'run') then attribute selected { "selected" } else ()} run</option>
                       <option value="view" > {if ($operation = 'view') then attribute selected { "selected" } else ()} view</option>
                    </select>
                <input type="submit" value="View/Run" />
                </form>

{$formatted-result}

};

declare function fcs-tests:store-result($target as xs:string, $testset as xs:string, $result as node()) as item()* {
    fcs-tests:store-result ($target, $testset, "", $result)
};

declare function fcs-tests:store-result($target as xs:string, $testset as xs:string, $test as xs:string, $result as node()) as item()* {
(: create collection for results for one target :)
   let $create-coll := if (not(xmldb:collection-available(concat($fcs-tests:results-coll, $target)))) then
                           xmldb:create-collection($fcs-tests:results-coll, $target) else ()

  let $result-path := fcs-tests:get-result-paths($target, concat($testset, $test))

  let $store-result := repo-utils:store($result-path[1], $result-path[2], $result, true())

return $store-result
};

(:
process a test-doc
(target is expected to be set in the config)
:)
declare function fcs-tests:test-rest($test-doc as node()) as item()* {

    let $result := local:dispatch($test-doc)

(:
let $targets := if (exists($test/target)) then $test/target else $test/preceding-sibling::target
let $requests := for $target in $targets return concat($target, $test/request/text())
let $data := for $request in $requests return
                <request href="{$request}" id
                httpclient:get(xs:anyURI($request), false(), () )

let $check := for $condition in $test/condition
                        let $expr :=concat("($data/", $condition/text(), " eq ", xs:string($condition/@result), ")")
                         return <check expr="{$expr}" >{util:eval($expr)} </check>
return <test><id>{$test/id}</id>{$test/label}
            <requests>{$requests}</requests>
            <results>{$check}</results>
        </test>
:)
return $result

};

(: This function takes the children of the node and passes them
   back into the typeswitch function. :)
declare function local:passthru($x as node()) as node()*
{
for $z in $x/node() return local:dispatch($z)
};

(: This is the recursive typeswitch function :)
declare function local:dispatch($x as node()) as node()*
{
typeswitch ($x)
  case text() return $x
  case element (test) return element div {$x/@*, fcs-tests:process-request($x)}
  case element() return element {$x/name()} {$x/@*, local:passthru($x)}
  default return local:passthru($x)
};

(:

      search: Haus
      //sru:numberOfRecords

:)
declare function fcs-tests:process-request($test as node()) as item()* {

    let $a := $test/a,
        $test-id := xs:string($test/@id),
        $target := fcs-tests:config-value("target"),
        $target-key := fcs-tests:config-key("target"),
        $request := concat($target, xs:string($a/@href))
    let $a-processed := {$a/text()}
    let $result-data := httpclient:get(xs:anyURI($request), false(), () )

    let $store := fcs-tests:store-result($target-key, $test-id, $result-data)

    (: try to match all the xpath defined for given request :)
    let $check := for $xpath in $test/xpath
                    let $evald := util:eval($xpath/text())

                    return

{if (exists($xpath/@key)) then xs:string($xpath/@key) else $xpath/text()}:
                          {$evald}

     (: TODO: check extra for diagnostics :)
     let $diag := $result-data//diag:diagnostic

return ($a-processed, $check, $diag)
};

xquery version "1.0";
module namespace fcs-tests  = "http://clarin.eu/fcs/1.0/tests";

import module namespace httpclient = "http://exist-db.org/xquery/httpclient";
import module namespace t="http://exist-db.org/xquery/testing";
import module namespace repo-utils = "http://aac.ac.at/content_repository/utils" at  "/db/cr/repo-utils.xqm";

declare namespace zr="http://explain.z3950.org/dtd/2.1/";
declare namespace sru = "http://www.loc.gov/zing/srw/";
declare namespace fcs = "http://clarin.eu/fcs/1.0";
declare namespace diag = "http://www.loc.gov/zing/srw/diagnostic/";

(: sample input:

 :)
declare variable $fcs-tests:config := doc("config.xml");
declare variable $fcs-tests:run-config := "run-config.xml";
declare variable $fcs-tests:testsets-coll := "/db/cr/modules/testing/testsets/";
declare variable $fcs-tests:results-coll := "/db/cr/modules/testing/results/";

(: this function is accesses by the testing-code to get configuration-options from the run-config :)
declare function fcs-tests:config-value($key as xs:string) as xs:string* {
            let $config := doc(concat($fcs-tests:testsets-coll, $fcs-tests:run-config))/config
                return if ($key eq "testset") then  xs:string($config//testset/@key)
                        else $config//target/text()
};

declare function fcs-tests:config-key($key as xs:string) as xs:string* {
            let $config := doc(concat($fcs-tests:testsets-coll, $fcs-tests:run-config))/config
                return if ($key eq "testset") then  xs:string($config//testset/@key)
                        else xs:string($config//target/@key)
};

declare function fcs-tests:get-result-paths($target  as xs:string, $testset as xs:string) as xs:string* {
        (concat($fcs-tests:results-coll, $target, "/"), concat($testset, ".xml"))
};

declare function fcs-tests:get-result-path($target  as xs:string, $testset as xs:string) as xs:string* {
        string-join(fcs-tests:get-result-paths($target , $testset ), "" )
};

declare function fcs-tests:run-testset ($target  as xs:string, $testset as xs:string) as item()* {

(: preparing a configuration for given run, based on the parameters :)
let $run-config := <config>{($fcs-tests:config//target[xs:string(@key) = $target],
                            $fcs-tests:config//testset[xs:string(@key) = $testset])}</config>
let $store := repo-utils:store($fcs-tests:testsets-coll, $fcs-tests:run-config, $run-config, true())
let $testset-path := concat($fcs-tests:testsets-coll, $testset, ".xml")
let $result := if (doc-available($testset-path)) then
                        let $tests := doc($testset-path)//TestSet
                        (: distinguish the testset, that the testing-module can process
                           and the home-made test-doc, that tests URLs :)
                        return if (exists($tests)) then
                                    t:run-testSet($tests)
                                 else
                                    fcs-tests:test-rest(doc($testset-path))
                   else
                        <diagnostics>unknown testset: {$testset}</diagnostics>

let $store-result := fcs-tests:store-result($target, $testset, $result)
(:for $test in $tests/tests/test return fcs-tests:run-test($test):)
return $store-result

};

declare function fcs-tests:format-result($result as node()) as item()* {

    if ($result[self::element(diagnostics)]) then

{$result/text()}

          else if (exists($result/info)) then
                    $result
          else repo-utils:serialise-as ($result,"html", "test")
              (: return quite empty html ??
                t:format-testResult($result) :)
};

declare function fcs-tests:display-page($target  as xs:string, $testset as xs:string, $operation) as item()* {

    let $result-path := fcs-tests:get-result-path($target, $testset)

    let $result := if ($target eq () or $testset eq ()) then ()
                       else if (doc-available($result-path)) then
                            doc($result-path)
                        else
                            <diagnostics>result unavailable: {$result-path}</diagnostics>

    let $formatted-result := fcs-tests:format-result($result)
    let $opt := util:declare-option("exist:serialize", "media-type=text/html method=xhtml")
    return

            <title>FCS - test suite</title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

FCS - test suite

            <form>
                targets<select name="target">
                    {
                    for $target-elem  in $fcs-tests:config//target
                        let $target-key := xs:string($target-elem/@key)
                        let $option :=  if ($target = $target-key) then
                                            <option selected="selected" value="{$target-key}" >{$target-key}</option>
                                         else
                                            <option value="{$target-key}" >{$target-key } </option>
                        return $option
                    }
                </select>
                test-set<select name="testset">
                    {
                    for $testset-elem in $fcs-tests:config//testset
                        let $testset-key := xs:string($testset-elem/@key)
                        let $option :=  if ($testset= $testset-key) then
                                            <option selected="selected" value="{$testset-key}" >{$testset-key}</option>
                                         else
                                            <option value="{$testset-key}" >{$testset-key}</option>
                        return $option
                    }
                </select>
                test-set
                    <select name="operation">
                       <option value="run" >{if ($operation = 'run') then attribute selected { "selected" } else ()} run</option>
                       <option value="view" > {if ($operation = 'view') then attribute selected { "selected" } else ()} view</option>
                    </select>
                <input type="submit" value="View/Run" />
                </form>

{$formatted-result}

};

declare function fcs-tests:store-result($target as xs:string, $testset as xs:string, $result as node()) as item()* {
    fcs-tests:store-result ($target, $testset, "", $result)
};

declare function fcs-tests:store-result($target as xs:string, $testset as xs:string, $test as xs:string, $result as node()) as item()* {
(: create collection for results for one target :)
   let $create-coll := if (not(xmldb:collection-available(concat($fcs-tests:results-coll, $target)))) then
                           xmldb:create-collection($fcs-tests:results-coll, $target) else ()

  let $result-path := fcs-tests:get-result-paths($target, concat($testset, $test))

  let $store-result := repo-utils:store($result-path[1], $result-path[2], $result, true())

return $store-result
};

(:
process a test-doc
(target is expected to be set in the config)
:)
declare function fcs-tests:test-rest ($test-doc as node()) as item()* {

    let $result := local:dispatch($test-doc)

(:
let $targets := if (exists($test/target)) then $test/target else $test/preceding-sibling::target
let $requests := for $target in $targets return concat($target, $test/request/text())
let $data := for $request in $requests return
                <request href="{$request}" id
                httpclient:get(xs:anyURI($request), false(), () )

let $check := for $condition in $test/condition
                        let $expr :=concat("($data/", $condition/text(), " eq ", xs:string($condition/@result), ")")
                         return <check expr="{$expr}" >{util:eval($expr)} </check>
return <test><id>{$test/id}</id>{$test/label}
            <requests>{$requests}</requests>
            <results>{$check}</results>
        </test>
:)
return $result

};

(: This function takes the children of the node and passes them
   back into the typeswitch function. :)
declare function local:passthru($x as node()) as node()*
{
for $z in $x/node() return local:dispatch($z)
};

(: This is the recursive typeswitch function :)
declare function local:dispatch($x as node()) as node()*
{
typeswitch ($x)
  case text() return $x
  case element (test) return element div {$x/@*, fcs-tests:process-request($x)}
  case element() return element {$x/name()} {$x/@*, local:passthru($x)}
  default return local:passthru($x)
};

(:

      search: Haus
      //sru:numberOfRecords

:)
declare function fcs-tests:process-request ($test as node()) as item()* {

    let $a := $test/a,
        $test-id := xs:string($test/@id),
        $target := fcs-tests:config-value("target"),
        $target-key := fcs-tests:config-key("target"),
        $request := concat($target, xs:string($a/@href))
    let $a-processed := {$a/text()}
    let $result-data := httpclient:get(xs:anyURI($request), false(), () )

    let $store := fcs-tests:store-result($target-key, $test-id, $result-data)

    (: try to match all the xpath defined for given request :)
    let $check := for $xpath in $test/xpath return

{if (exists($xpath/@key)) then xs:string($xpath/@key) else $xpath/text()}:
                          {util:eval($xpath/text())}

     (: TODO: check extra for diagnostics :)
     let $diag := $result-data//diag:diagnostic

return ($a-processed, $check, $diag)
};


Display related concepts as links.

Display related concepts as links.

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/julen/tzos.git/'


[test] Tests to show that Path expression with nested positional predicate succeeds on database node Closes https://github.com/eXist-db/exist/issues/1892

(:
 : 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 npt="http://exist-db.org/test/nested-positional-predicate";

declare namespace test="http://exist-db.org/xquery/xqsuite";

declare variable $npt:DATA :=
    document {
        <xml>

                B1

                B2
                <c>correct</c>

                B3
                <c>wrong</c>

        </xml>
    };

declare
    %test:setUp
function npt:setup() {
    xmldb:create-collection("/db", "test"),
    xmldb:store("/db/test", "test.xml", $npt:DATA)
};

declare
    %test:tearDown
function npt:cleanup() {
    xmldb:remove("/db/test")
};

declare
    %test:assertEquals("<result><c>correct</c><c>wrong</c></result>")
function npt:in-memory() {
    <result>{$npt:DATA//c[../preceding-sibling::a]}</result>
};

declare
    %test:assertEquals("<result><c>correct</c><c>wrong</c></result>")
function npt:in-database() {
    <result>{doc("/db/test/test.xml")//c[../preceding-sibling::a]}</result>
};

declare
    %test:assertEquals("<result><c>correct</c><c>wrong</c></result>")
function npt:in-memory-predicate() {
    <result>{$npt:DATA//c[../preceding-sibling::a[1]]}</result>
};

declare
    %test:assertEquals("<result><c>correct</c><c>wrong</c></result>")
function npt:in-database-predicate() {
    <result>{doc("/db/test/test.xml")//c[../preceding-sibling::a[1]]}</result>
};

declare
    %test:assertEquals("<result><c>correct</c><c>wrong</c></result>")
function npt:in-memory-position() {
    <result>{$npt:DATA//c[../preceding-sibling::a[position() eq 1]]}</result>
};

declare
    %test:assertEquals("<result><c>correct</c><c>wrong</c></result>")
function npt:in-database-position() {
    <result>{doc("/db/test/test.xml")//c[../preceding-sibling::a[position() eq 1]]}</result>
};

declare
    %test:assertEquals("<result><c>correct</c></result>")
function npt:in-memory-predicate-and-path() {
    <result>{$npt:DATA//c[../preceding-sibling::a[1]/b = 'B1']}</result>
};

declare
    %test:assertEquals("<result><c>correct</c></result>")
function npt:in-database-predicate-and-path() {
    <result>{doc("/db/test/test.xml")//c[../preceding-sibling::a[1]/b = 'B1']}</result>
};

declare
    %test:assertEquals("<result><c>correct</c></result>")
function npt:in-memory-position-and-path() {
    <result>{$npt:DATA//c[../preceding-sibling::a[position() eq 1]/b = 'B1']}</result>
};

declare
    %test:assertEquals("<result><c>correct</c></result>")
function npt:in-database-position-and-path() {
    <result>{doc("/db/test/test.xml")//c[../preceding-sibling::a[position() eq 1]/b = 'B1']}</result>
};

error: pathspec 'exist-core/src/test/xquery/npt.xqm' did not match any file(s) known to git


Add BaseX Process Module function signatures.

xquery version "3.0";
(:~
: BaseX Process Module functions
:
: @see http://docs.basex.org/wiki/Output_Module
:)
module namespace proc = "http://basex.org/modules/proc";

import module namespace a = "http://reecedunn.co.uk/xquery/annotations" at "res://reecedunn.co.uk/xquery/annotations.xqy";

declare %a:since("basex", "7.3") function proc:system($cmd as xs:string) as xs:string external;
declare %a:since("basex", "7.3") function proc:system($cmd as xs:string, $args as xs:string*) as xs:string external;
declare %a:since("basex", "7.3") function proc:system($cmd as xs:string, $args as xs:string*, $options as map(xs:string, xs:string) (: $options [7.3] as xs:string [8.6] as map(xs:string, xs:string) :)) as xs:string external;
declare %a:since("basex", "7.3") function proc:execute($cmd as xs:string) as element(result) external;
declare %a:since("basex", "7.3") function proc:execute($cmd as xs:string, $args as xs:string*) as element(result) external;
declare %a:since("basex", "7.3") function proc:execute($cmd as xs:string, $args as xs:string*, $options as map(xs:string, xs:string) (: $options [7.3] as xs:string [8.6] as map(xs:string, xs:string) :)) as element(result) external;
declare %a:since("basex", "9.0") function proc:fork($cmd as xs:string) as element(result) external;
declare %a:since("basex", "9.0") function proc:fork($cmd as xs:string, $args as xs:string*) as element(result) external;
declare %a:since("basex", "9.0") function proc:fork($cmd as xs:string, $args as xs:string*, $options as map(xs:string, xs:string)) as element(result) external;
declare %a:since("basex", "8.3") function proc:property($name as xs:string) as xs:string? external;
declare %a:since("basex", "8.3") function proc:property-names() as xs:string* external;

error: pathspec 'src/main/resources/builtin/basex.org/modules/proc.xqy' did not match any file(s) known to git


Some example XQueries which can operate within BaseX after cleaning the notes files to pure xhtml, then concatenating them through prep_data.sh

(: List concepts:)
for $item in //*:p[preceding-sibling::*:h2[1][text()='Concepts']]/text() return concat($item,'

')

error: pathspec 'xquery_examples.xq' did not match any file(s) known to git


Renamed logic.xq to logic0.xq to match expected result file.

(fn:true() or fn:false(),1 or 0,"dafsd" or "",fn:true() and fn:false(),1 and 0,"dafsd" and "")

error: pathspec 'test/rbkt/Queries/boolean/logic0.xq' did not match any file(s) known to git


Add BaseX Web Module function signatures.

xquery version "3.0";
(:~
: BaseX Web Module functions
:
: @see http://docs.basex.org/wiki/Web_Module
:)
module namespace web = "http://basex.org/modules/web";

import module namespace a = "http://reecedunn.co.uk/xquery/annotations" at "res://reecedunn.co.uk/xquery/annotations.xqy";

declare namespace rest = "http://exquery.org/ns/restxq";

declare %a:since("basex", "8.1") function web:content-type($path as xs:string) as xs:string external;
declare %a:since("basex", "8.1") function web:create-url($url as xs:string, $parameters as map(*) (: [8.1] as map(*) [8.2] as map(item(), item()*) [9.0] as map(*) :)) as xs:string external;
declare %a:since("basex", "8.2") function web:encode-url($string as xs:string) as xs:string external;
declare %a:since("basex", "8.2") function web:decode-url($string as xs:string) as xs:string external;
declare %a:since("basex", "8.1") function web:redirect($location as xs:string) as element(rest:response) external;
declare %a:since("basex", "8.1") function web:redirect($location as xs:string, $parameters as map(*)) as element(rest:response) external;
declare %a:since("basex", "8.1") function web:response-header() as element(rest:response) external;
declare %a:since("basex", "8.1") function web:response-header($output as map(*)? (: [8.1] as map(*) [9.0] as map(*)? :)) as element(rest:response) external;
declare %a:since("basex", "8.1") function web:response-header($output as map(*)? (: [8.1] as map(*) [9.0] as map(*)? :), $headers as map(*)? (: [8.1] as map(*) [8.2] as map(xs:string, xs:string) [9.0] as map(*)? :)) as element(rest:response) external;
declare %a:since("basex", "9.0") function web:response-header($output as map(*)?, $headers as map(*)?, $atts as map(*)?) as element(rest:response) external;

error: pathspec 'src/main/resources/builtin/basex.org/modules/web.xqy' did not match any file(s) known to git


Implementing the W3C's official XQuery test suite.

svn path=/trunk/eXist-1.0/; revision=2704

fatal: unable to access 'https://github.com/windauer/exist.git/': The requested URL returned error: 403