[FIX] DBA does not enforce authentication when reading logs

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

import module namespace G = 'dba/global' at '../modules/global.xqm';
import module namespace html = 'dba/html' at '../modules/html.xqm';
import module namespace tmpl = 'dba/tmpl' at '../modules/tmpl.xqm';
import module namespace web = 'dba/web' at '../modules/web.xqm';

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

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

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

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

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

{ $loglists }

        </form>

      {
        if($name) then (

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

,

,

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

  )
};

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

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

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

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

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

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

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

import module namespace G = 'dba/global' at '../modules/global.xqm';
import module namespace html = 'dba/html' at '../modules/html.xqm';
import module namespace tmpl = 'dba/tmpl' at '../modules/tmpl.xqm';
import module namespace web = 'dba/web' at '../modules/web.xqm';

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

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

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

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

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

{ $loglists }

        </form>

      {
        if($name) then (

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

,

,

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

  )
};

(:~
 : Returns log data.
 : @param  $name     name of log file
 : @param  $sort     table sort key
 : @param  $loglist  loglist
 : @param  $query    query
 : @return html elements
 :)
declare
  %rest:POST
  %rest:path("dba/logs")
  %rest:query-param("name",    "{$name}")
  %rest:query-param("sort",    "{$sort}")
  %rest:query-param("loglist", "{$loglist}")
  %rest:query-param("query",   "{$query}")
  %output:method("html")
function _:query(
  $name     as xs:string,
  $sort     as xs:string?,
  $query    as xs:string?,
  $loglist  as xs:string?
) as element()* {
  let $logs := try {
    web:eval("admin:logs($n, true())[matches(., $q, 'i')]",
      map { 'n': $name, 'q': $query }
    )
  } catch * { () }

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

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

(:~
 : Returns log data.
 : @param  $sort   table sort key
 : @param  $query  query
 : @return html elements
 :)
declare
  %rest:POST
  %rest:path("dba/loglist")
  %rest:query-param("sort",  "{$sort}")
  %rest:query-param("query", "{$query}")
  %output:method("html")
function _:loglist(
  $sort   as xs:string?,
  $query  as xs:string?
) as element()* {
  let $logs := try {
    web:eval("for $a in admin:logs()
      let $n := $a/(@date,text())/string()
      where not($query) or (some $a in admin:logs($n) satisfies matches($a, $query, 'i'))
      order by $n descending
      return $a", map { 'query': $query }
    )
  } catch * { () }
  where $logs
  let $entries :=
    (: legacy (7.8.1): $a/@date :)
    for $a in $logs
    order by $a descending
    (: legacy (7.8.1): $a/@date :)
    return <e name='{ ($a/(@date,text())) }' size='{ $a/@size }'/>
  let $headers := (
    <name>{ html:label($entries, ('Log', 'Logs')) }</name>,
    <size type='bytes'>Size</size>
  )
  let $buttons := html:button('delete-log', 'Delete', true())
  let $link := function($value) { $_:CAT }
  return html:table($entries, $headers, $buttons,
    map { 'sort': $sort, 'loglist': $query }, (), $link)
};


fixed small typo in node-position.xq

fixed small typo in node-position.xq

xquery version "1.0";
(:
 : Copyright 2006-2011 The FLWOR Foundation.
 :
 : Licensed under the Apache License, Version 2.0 (the "License");
 : you may not use this file except in compliance with the License.
 : You may obtain a copy of the License at
 :
 : http://www.apache.org/licenses/LICENSE-2.0
 :
 : Unless required by applicable law or agreed to in writing, software
 : distributed under the License is distributed on an "AS IS" BASIS,
 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 : See the License for the specific language governing permissions and
 : limitations under the License.
:)

(:~
 : This module provides a function (np:node-position) that, given a node,
 : returns positional information about the node in the form of an xs:anyURI
 : item. The module also defines functions that use such positional information
 : to determine: (1) structural relationships between two nodes (e.g. if one
 : is the ancestor of another) and (2) properties of a node (e.g. its level in
 : the tree).
 :
 : Within this module, the temp "node position" will be used to refer to an
 : xs:anyURI item that is returned by the np:node-position function.
 :
 : @author Federico Cavalieri
 :)
module namespace np = "http://www.zorba-xquery.com/modules/node-position";

declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
declare option ver:module-version "2.0";

(:~
 : Compute a stable and opaque structural information representation
 : (with type xs:anyURI) for a given node.
 :
 :
The returned URI is stable, i.e. it does not change when other
 : nodes are inserted, deleted or modified.

 :
 :
Each node in a snapshot has a different URI. Note however that
 : different nodes in different snapshots might have the same URI.

 :
 : @param $arg the node for which the structural information URI
 :  should be computed
 :
 : @return the opaque structural information URI of the node.
 :)
declare function np:node-position(
  $arg as node()
) as xs:anyURI external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is an ancestor of the node whose
 : structural information is given as first argument.
 :
 : @param $s-info1 the potential descendant structural information
 : @param $s-info2 the potential ancestor structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is an ancestor of the node whose structural information is $s-info1;
 : false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:ancestor-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is a descendant of the node whose
 : structural information is given as first argument.
 :
 : @param $s-info1 the potential ancestor structural information
 : @param $s-info2 the potential descendant structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is a descendant of the node whose structural information is $s-info1;
 : false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:descendant-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is the parent of the node whose
 : structural information is given as first argument.
 :
 : @param $s-info1 the potential child structural information
 : @param $s-info2 the potential parent structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is the parent of the node whose structural information is $s-info1;
 : false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:parent-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is a child of the node whose
 : structural information is given as first argument.
 :
 : @param $s-info1 the potential parent structural information
 : @param $s-info2 the potential child structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is a child of the node whose structural information is $s-info1;
 : false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:child-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is an attribute of the node whose
 : structural information is given as first argument.
 :
 : @param $s-info1 the potential parent structural information
 : @param $s-info2 the potential attribute structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is an attribute of the node whose structural information is $s-info1;
 : false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:attribute-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is a following-sibling of the node
 : whose structural information is given as first argument.
 :
 : @param $s-info1 the potential preceding-sibling structural information
 : @param $s-info2 the potential following-sibling structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is a following-sibling of the node whose structural information is $s-info1;
 : false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:following-sibling-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is a preceding-sibling of the node
 : whose structural information is given as first argument.
 :
 : @param $s-info1 the potential following-sibling structural information
 : @param $s-info2 the potential preceding-sibling structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is a preceding-sibling of the node whose structural information is $s-info1;
 : false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:preceding-sibling-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is following the node whose
 : structural information is given as first argument.
 :
 : @param $s-info1 the potential preceding node structural information
 : @param $s-info2 the potential following node structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is following the node whose structural information is $s-info1;
 : false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:following-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is following in document order the node whose
 : structural information is given as first argument.
 :
 : @param $s-info1 the potential preceding node structural information
 : @param $s-info2 the potential following node structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is following in document order the node whose structural information
 : is $s-info1; false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:following-in-document-order-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is preceding the node whose
 : structural information is given as first argument.
 :
 : @param $s-info1 the potential following node structural information
 : @param $s-info2 the potential preceding node structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is preceding the node whose structural information is $s-info1;
 : false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:preceding-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is preceding the node whose
 : structural information is given as first argument.
 :
 : @param $s-info1 the potential following node structural information
 : @param $s-info2 the potential preceding node structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is preceding in document order the node whose structural information
 : is $s-info1; false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:preceding-in-document-order-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Computes the level of a node in the tree given its structural
 : information.
 :
 : Note: The first level has the number one.
 :
 : @param $s-info the structural information of the node which
 :  level should be computed
 :
 : @return The level in the tree as xs:integer of the node whose
 : structural information is $s-info.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:level(
  $s-info as xs:anyURI) as xs:integer external;

(:~
 : Determines whether two nodes are siblings given their structural information.
 :
 : @param $s-info1 a node structural information
 : @param $s-info2 a node structural information
 :
 : @return true if the two nodes whose structural information are $s-info1
 : and $s-info2 are siblings; false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:sibling-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether a node is an attribute given its structural information.
 :
 : @param $s-info the potential attribute structural information
 :
 : @return true if the node whose structural information is $s-info
 : is an attribute; false otherwise.
 :
 : @error zerr:ZAPI0028 if the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:is-attribute(
  $s-info1 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether a node is a comment given its structural information.
 :
 : @param $s-info the potential comment structural information
 :
 : @return true if the node whose structural information is $s-info
 : is a comment; false otherwise.
 :
 : @error zerr:ZAPI0028 if the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:is-comment(
  $s-info1 as xs:anyURI) as xs:boolean external;

  (:~
 : Determines whether a node is a document given its structural information.
 :
 : @param $s-info the potential document structural information
 :
 : @return true if the node whose structural information is $s-info
 : is a document; false otherwise.
 :
 : @error zerr:ZAPI0028 if the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:is-document(
  $s-info1 as xs:anyURI) as xs:boolean external;

  (:~
 : Determines whether a node is an element given its structural information.
 :
 : @param $s-info the potential element structural information
 :
 : @return true if the node whose structural information is $s-info
 : is an element; false otherwise.
 :
 : @error zerr:ZAPI0028 if the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:is-element(
  $s-info1 as xs:anyURI) as xs:boolean external;

  (:~
 : Determines whether a node is a processing-instruction given its structural information.
 :
 : @param $s-info the potential processing-instruction structural information
 :
 : @return true if the node whose structural information is $s-info
 : is a processing-instruction; false otherwise.
 :
 : @error zerr:ZAPI0028 if the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:is-processing-instruction(
  $s-info1 as xs:anyURI) as xs:boolean external;

  (:~
 : Determines whether a node is a text given its structural information.
 :
 : @param $s-info the potential text node structural information
 :
 : @return true if the node whose structural information is $s-info
 : is a text node; false otherwise.
 :
 : @error zerr:ZAPI0028 if the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:is-text(
  $s-info1 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether two nodes belong to the same tree given their
 : structural information.
 :
 : @param $s-info1 a node structural information
 : @param $s-info2 a node structural information
 :
 : @return true if the two node whose structural information are $s-info1
 : and $s-info2 belong to the same tree.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:in-same-tree-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether a node belongs to a collection given its structural
 : information.
 :
 : @param $s-info the node structural information
 :
 : @return true if the node whose structural information is $s-info
 : is in a collection; false otherwise.
 :
 : @error zerr:ZAPI0028 if the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:in-collection(
  $s-info as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether two nodes belong to the same collection given their
 : structural information.
 :
 : @param $s-info1 a node structural information
 : @param $s-info2 a node structural information
 :
 : @return true if the two node whose structural information are $s-info1
 : and $s-info2 belong to the same collection.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:in-same-collection-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

xquery version "1.0";
(:
 : Copyright 2006-2011 The FLWOR Foundation.
 :
 : Licensed under the Apache License, Version 2.0 (the "License");
 : you may not use this file except in compliance with the License.
 : You may obtain a copy of the License at
 :
 : http://www.apache.org/licenses/LICENSE-2.0
 :
 : Unless required by applicable law or agreed to in writing, software
 : distributed under the License is distributed on an "AS IS" BASIS,
 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 : See the License for the specific language governing permissions and
 : limitations under the License.
:)

(:~
 : This module provides a function (np:node-position) that given, a node,
 : returns positional information about the node in the form of an xs:anyURI
 : item. The module also defines functions that use such positional information
 : to determine: (1) structural relationships between two nodes (e.g. if one
 : is the ancestor of another) and (2) properties of a node (e.g. its level in
 : the tree).
 :
 : Within this module, the temp "node position" will be used to refer to an
 : xs:anyURI item that is returned by the np:node-position function.
 :
 : @author Federico Cavalieri
 :)
module namespace np = "http://www.zorba-xquery.com/modules/node-position";

declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
declare option ver:module-version "2.0";

(:~
 : Compute a stable and opaque structural information representation
 : (with type xs:anyURI) for a given node.
 :
 :
The returned URI is stable, i.e. it does not change when other
 : nodes are inserted, deleted or modified.

 :
 :
Each node in a snapshot has a different URI. Note however that
 : different nodes in different snapshots might have the same URI.

 :
 : @param $arg the node for which the structural information URI
 :  should be computed
 :
 : @return the opaque structural information URI of the node.
 :)
declare function np:node-position(
  $arg as node()
) as xs:anyURI external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is an ancestor of the node whose
 : structural information is given as first argument.
 :
 : @param $s-info1 the potential descendant structural information
 : @param $s-info2 the potential ancestor structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is an ancestor of the node whose structural information is $s-info1;
 : false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:ancestor-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is a descendant of the node whose
 : structural information is given as first argument.
 :
 : @param $s-info1 the potential ancestor structural information
 : @param $s-info2 the potential descendant structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is a descendant of the node whose structural information is $s-info1;
 : false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:descendant-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is the parent of the node whose
 : structural information is given as first argument.
 :
 : @param $s-info1 the potential child structural information
 : @param $s-info2 the potential parent structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is the parent of the node whose structural information is $s-info1;
 : false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:parent-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is a child of the node whose
 : structural information is given as first argument.
 :
 : @param $s-info1 the potential parent structural information
 : @param $s-info2 the potential child structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is a child of the node whose structural information is $s-info1;
 : false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:child-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is an attribute of the node whose
 : structural information is given as first argument.
 :
 : @param $s-info1 the potential parent structural information
 : @param $s-info2 the potential attribute structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is an attribute of the node whose structural information is $s-info1;
 : false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:attribute-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is a following-sibling of the node
 : whose structural information is given as first argument.
 :
 : @param $s-info1 the potential preceding-sibling structural information
 : @param $s-info2 the potential following-sibling structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is a following-sibling of the node whose structural information is $s-info1;
 : false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:following-sibling-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is a preceding-sibling of the node
 : whose structural information is given as first argument.
 :
 : @param $s-info1 the potential following-sibling structural information
 : @param $s-info2 the potential preceding-sibling structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is a preceding-sibling of the node whose structural information is $s-info1;
 : false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:preceding-sibling-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is following the node whose
 : structural information is given as first argument.
 :
 : @param $s-info1 the potential preceding node structural information
 : @param $s-info2 the potential following node structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is following the node whose structural information is $s-info1;
 : false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:following-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is following in document order the node whose
 : structural information is given as first argument.
 :
 : @param $s-info1 the potential preceding node structural information
 : @param $s-info2 the potential following node structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is following in document order the node whose structural information
 : is $s-info1; false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:following-in-document-order-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is preceding the node whose
 : structural information is given as first argument.
 :
 : @param $s-info1 the potential following node structural information
 : @param $s-info2 the potential preceding node structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is preceding the node whose structural information is $s-info1;
 : false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:preceding-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether the node whose structural information is
 : given as second argument is preceding the node whose
 : structural information is given as first argument.
 :
 : @param $s-info1 the potential following node structural information
 : @param $s-info2 the potential preceding node structural information
 :
 : @return true if the node whose structural information is $s-info2
 : is preceding in document order the node whose structural information
 : is $s-info1; false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:preceding-in-document-order-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Computes the level of a node in the tree given its structural
 : information.
 :
 : Note: The first level has the number one.
 :
 : @param $s-info the structural information of the node which
 :  level should be computed
 :
 : @return The level in the tree as xs:integer of the node whose
 : structural information is $s-info.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:level(
  $s-info as xs:anyURI) as xs:integer external;

(:~
 : Determines whether two nodes are siblings given their structural information.
 :
 : @param $s-info1 a node structural information
 : @param $s-info2 a node structural information
 :
 : @return true if the two nodes whose structural information are $s-info1
 : and $s-info2 are siblings; false otherwise.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:sibling-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether a node is an attribute given its structural information.
 :
 : @param $s-info the potential attribute structural information
 :
 : @return true if the node whose structural information is $s-info
 : is an attribute; false otherwise.
 :
 : @error zerr:ZAPI0028 if the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:is-attribute(
  $s-info1 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether a node is a comment given its structural information.
 :
 : @param $s-info the potential comment structural information
 :
 : @return true if the node whose structural information is $s-info
 : is a comment; false otherwise.
 :
 : @error zerr:ZAPI0028 if the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:is-comment(
  $s-info1 as xs:anyURI) as xs:boolean external;

  (:~
 : Determines whether a node is a document given its structural information.
 :
 : @param $s-info the potential document structural information
 :
 : @return true if the node whose structural information is $s-info
 : is a document; false otherwise.
 :
 : @error zerr:ZAPI0028 if the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:is-document(
  $s-info1 as xs:anyURI) as xs:boolean external;

  (:~
 : Determines whether a node is an element given its structural information.
 :
 : @param $s-info the potential element structural information
 :
 : @return true if the node whose structural information is $s-info
 : is an element; false otherwise.
 :
 : @error zerr:ZAPI0028 if the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:is-element(
  $s-info1 as xs:anyURI) as xs:boolean external;

  (:~
 : Determines whether a node is a processing-instruction given its structural information.
 :
 : @param $s-info the potential processing-instruction structural information
 :
 : @return true if the node whose structural information is $s-info
 : is a processing-instruction; false otherwise.
 :
 : @error zerr:ZAPI0028 if the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:is-processing-instruction(
  $s-info1 as xs:anyURI) as xs:boolean external;

  (:~
 : Determines whether a node is a text given its structural information.
 :
 : @param $s-info the potential text node structural information
 :
 : @return true if the node whose structural information is $s-info
 : is a text node; false otherwise.
 :
 : @error zerr:ZAPI0028 if the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:is-text(
  $s-info1 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether two nodes belong to the same tree given their
 : structural information.
 :
 : @param $s-info1 a node structural information
 : @param $s-info2 a node structural information
 :
 : @return true if the two node whose structural information are $s-info1
 : and $s-info2 belong to the same tree.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:in-same-tree-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether a node belongs to a collection given its structural
 : information.
 :
 : @param $s-info the node structural information
 :
 : @return true if the node whose structural information is $s-info
 : is in a collection; false otherwise.
 :
 : @error zerr:ZAPI0028 if the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:in-collection(
  $s-info as xs:anyURI) as xs:boolean external;

(:~
 : Determines whether two nodes belong to the same collection given their
 : structural information.
 :
 : @param $s-info1 a node structural information
 : @param $s-info2 a node structural information
 :
 : @return true if the two node whose structural information are $s-info1
 : and $s-info2 belong to the same collection.
 :
 : @error zerr:ZAPI0028 if one of the given URI is not a valid node structural
 : information computed by the <tt>np:node-position</tt> function.
 :)
declare function np:in-same-collection-of(
  $s-info1 as xs:anyURI,
  $s-info2 as xs:anyURI) as xs:boolean external;