xquery version "3.0"; module namespace SatniResource-v1="http://satni.org/apps/satni/resource/representation/v1"; import module namespace sort="http://exist-db.org/xquery/sort"; import module namespace config="http://satni.org/apps/satni/config" at "config.xqm"; import module namespace functx = "http://www.functx.com"; declare namespace http="http://expath.org/ns/http-client"; declare namespace rest="http://exquery.org/ns/restxq"; declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization"; declare namespace json="http://www.json.org"; (: GET /dictionaries :) declare %rest:GET %rest:path("/satni/dictionaries") %rest:produces("application/json") %output:media-type("application/json") %output:method("json") function SatniResource-v1:PUBLIC.f-dictionaries() { let $resource := { for $dict in (collection($config:data-root)//dictmeta, collection($config:data-root)//termmeta) return {string($dict//id)} {$dict/name} {$dict/description} {$dict/copyright} {$dict/editors} } return SatniResource-v1:return-resource($resource) }; (: GET /search :) declare %rest:GET %rest:path("/satni/search") %rest:query-param("query", "{$query}") %rest:query-param("dict", "{$dictid}", "all") %rest:query-param("from", "{$from}", 1) %rest:query-param("howMany", "{$howMany}", 1000) %rest:produces("application/json") %output:media-type("application/json") %output:method("json") function SatniResource-v1:PUBLIC.f-search($query as xs:string*, $dictid as xs:string*, $from as xs:int*, $howMany as xs:int*) { let $resource := { let $hits := if ($dictid eq "all") then SatniResource-v1:do-query($query) else SatniResource-v1:do-query($query, $dictid) let $terms := for $hit in $hits return string(subsequence($hit, $from, $howMany)) return (:transform:transform(( :) sort:create-index("terms", $hits, $terms, ), (: let $item := subsequence($sorted, $from, $howMany) :) let $hits2 := if ($dictid eq "all") then SatniResource-v1:do-query($query) else SatniResource-v1:do-query($query, $dictid) for $sorted at $p in subsequence($hits2, $from, $howMany) let $term := string($sorted) let $dict := functx:if-empty(root($sorted)/r/@id, functx:substring-after-last(util:collection-name($sorted), '/')) let $lang := string(root($sorted)/r/@xml:lang) order by sort:index("terms", $sorted) return {$term} {$dict} {$lang} {for $target-lang in util:expand($sorted/../../mg)/tg[./t != '']/@xml:lang return {string($target-lang)} } (:), xs:anyURI("add-json-array.xsl"), ()):) } return SatniResource-v1:return-resource($resource) }; (: GET /article/$article :) declare %rest:GET %rest:path("/satni/article/{$article}") %rest:produces("application/json") %output:media-type("application/json") %output:method("json") function SatniResource-v1:PUBLIC.f-article($article as xs:string*) { let $resource := { let $queryStr := concat('^', util:unescape-uri($article, "UTF-8"), '$') let $hits := collection($config:data-root)//l[matches(., $queryStr)]/../.. let $distinct-hits := functx:distinct-deep($hits) return for $hit in $distinct-hits let $entry := util:expand($hit/mg) let $d-entries := functx:distinct-deep($entry) return for $e in $d-entries let $dict := string(root($hit)/r/@id) let $idref := functx:if-empty($e/@idref, -1) let $lang := root($hit)/r/@xml:lang let $category := collection($config:data-root)//e[matches(./@id, $idref)]/@category return ( {string($hit/lg/l)} {string($hit/lg/l/@pos)} {$dict} {string($hit/status)} {$lang ! {string(.)}} {$category ! {string(.)}} {$idref ! {string(.)}} {string($e/def)} {string($e/expl)} { for $translation in $e/tg[./t != ''] return $translation } ) } return SatniResource-v1:return-resource($resource) }; declare %private function SatniResource-v1:do-query($query as xs:string*) { let $queryStr := (: ristenxq:create-query($query) :) concat('^', $query) let $hits := (: ft:query-field("term", $queryStr) :) collection($config:data-root)//l[ngram:wildcard-contains(., $queryStr)] return $hits }; declare %private function SatniResource-v1:do-query($query as xs:string*, $dict as xs:string*) { let $queryStr := (: SatniResource-v1:create-query($query) :) concat('^', $query) let $hits := collection($config:data-root)//r[@id=$dict]//l[ngram:wildcard-contains(., $queryStr)] return $hits }; declare %private function SatniResource-v1:create-query($query as xs:string*) { ^{lower-case($query)}.* }; declare %private function SatniResource-v1:return-resource($resource) { SatniResource-v1:create-response(200,'OK',map { 'Content-Type' := 'application/json; charset=UTF-8', 'Access-Control-Allow-Origin' := '*' }), $resource }; declare %private function SatniResource-v1:create-response($status as xs:integer, $message as xs:string, $headers) { { for $key in map:keys($headers) return } };