xquery version "1.0";
declare namespace util="http://exist-db.org/xquery/util";
declare namespace fn="http://exist-db.org/local-functions";
declare namespace i18n="http://apache.org/cocoon/i18n/2.1";
(: Map query field parameter to xpath selection :)
declare function fn:query-field($field as xs:string) as xs:string
{
if ($field = "head") then
"common/head"
else if ($field = "def") then
"senses/sense/def"
else
"."
};
(: Create filter expression from query term, field and mode,
and add a classification filter if specified :)
declare function fn:filter-expr($term as xs:string,
$field as xs:string,
$mode as xs:string) as xs:string
{
let $class := request:request-parameter("class", "")
let $c := if ($class = 'all') then ".//@top &= '*'"
else if (contains($class, '-') ) then
concat(".//@botm &= '", substring-after($class, '-'), "'" )
else if ( string-length($class) = 1) then
concat(".//@top &= '", $class, "'" )
else
concat(".//@mid &= '", $class, "'" )
let $f := fn:query-field($field),
$t := concat("'", $term, "'"),
$t2 := replace($term,"\?",".?"),
$t3 := replace($t2,"\*",".*"),
$ttrad := concat("'^", $t3, "$'")
let $termsearch :=
if ($mode = "trad") then
concat("matches(", $f, ", ", $ttrad, ")")
else if ($mode = "near") then
concat("near(", $f, ", ", $t, ")")
else if ($mode = "exact") then
concat($f, " = ", $t)
else
concat($f, " &= ", $t)
return
(: Removed the condition $term = '*', since querying the entire db seems
acceptably fast - orig. condition was: $term = '*' or $term = '' :)
if ($term = '' ) then
$c
else if ( $class = 'all') then
$termsearch
else
concat($termsearch, " and ", $c)
};
(: Map order parameter to xpath for order by clause :)
(: Since the ordering element is not selectable, this function does
not make any sense any more, but I'll keep it for now just in case. :)
declare function fn:order-expr($field as xs:string) as xs:string
{
if ($field = "head") then
"common/head"
else
"common/qa, $r/common/head"
};
(: Build the list of documents to search based on language selection:)
declare function fn:doc-expr($docpref as xs:string,
$srchlang as xs:string)
as xs:string
{
let $termpref := "terms-",
$fileext := ".xml",
$docs :=
if ($srchlang = "all") then
concat (
"'", $docpref, $termpref, "lat", $fileext,"',",
"'", $docpref, $termpref, "sme", $fileext,"',",
"'", $docpref, $termpref, "sma", $fileext,"',",
"'", $docpref, $termpref, "smj", $fileext,"',",
"'", $docpref, $termpref, "nor", $fileext,"'"
)
else
for $lang in tokenize($srchlang, " ")
(: Does not return multiple values yet, don't know why -
the search form has been changed similarly to only
allow single selections: all languages or one. :)
return
concat("'",$docpref,$termpref,$lang,$fileext,"'")
return $docs
};
(: Assemble the query string :)
declare function fn:build-query($term as xs:string,
$orderby as xs:string)
as xs:string
{
let $field := request:request-parameter("field", "any"),
$mode := request:request-parameter("mode", "all"),
$expr := fn:filter-expr($term, $field, $mode),
$t := request:set-session-attribute("query", $expr),
$srchlang := request:request-parameter("srchlang","all"),
$docpref := "/db/ordbase/terms/SD-terms/",
$docs := fn:doc-expr($docpref, $srchlang)
return
concat("for $r in document(", $docs, ")//entry[",
$expr, "] order by $r/",
fn:order-expr($orderby), " collation '?lang=sme-SE' return $r")
};
(: Present an overview of query results :)
declare function fn:displayHitlist($hits as node()+)
as element()
{
let $count := count($hits),
$max := request:request-parameter("howmany", "10") cast as xs:int,
$start := request:request-parameter("start", "1") cast as xs:int,
$end := if ($start + $max - 1 < $count) then $start + $max - 1 else $count
let $c1 := request:request-parameter("class", ""),
$c2 := request:get-session-attribute("class"),
$class := if ( $c1 ) then $c1 else $c2,
$topc := substring($class, 1, 1),
$midc := if (string-length($class) > 1 and $class != 'all') then
substring($class, 1, 5) else "",
$botmc := if (contains($class, '-') ) then substring-after($class, '-') else ""
return
Your query has timed out. Please hit «Find» or enter a new search query.
else let $hits := util:eval( $query ), $s := request:set-session-attribute("results", $hits), $t := request:set-session-attribute("class", $class) return if (empty($hits)) then (Your document string: {$testdocs}
Your $lang string: {$srchlang}
Your $class string: {$class}
Your session attributes: {$session}
Your query filter/old query:
{$queryOld}
Your complete search query:
{$query}
Your document string: {$testdocs}
Your $lang string: {$srchlang}
Your $class string: {$class}
Your session attributes: {$session}
Your query filter/old query:
{$queryOld}
Your complete search query:
{$query}