xquery version "1.0";
declare namespace session="http://exist-db.org/xquery/session";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace util="http://exist-db.org/xquery/util";
declare namespace i18n="http://apache.org/cocoon/i18n/2.1";
import module namespace ristenutil="http://www.risten.no/shared/util" at "../xquery/ristenutil.xqm";
(: Map query field parameter to xpath selection :)
declare function local: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 local:filter-expr($term as xs:string,
$field as xs:string,
$mode as xs:string) as xs:string
{
let $class := request:get-parameter("class", "all")
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 := local:query-field($field),
$t := concat("'", $term, "'"),
$t2 := replace($term,"\?",".?"),
$t3 := replace($t2,"\*",".*"),
$t4 := replace($t3,"\(","\("),
$t5 := replace($t4,"\)","\)"),
$ttrad := concat("'^", $t5, ".*$'")
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 local: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 local: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 local:build-query($docpref as xs:string,
$srchlang as xs:string+,
$class as xs:string,
$term as xs:string,
$orderby as xs:string)
as xs:string
{
let $field := request:get-parameter("field", "any"),
$mode := request:get-parameter("mode", "all"),
$expr := local:filter-expr($term, $field, $mode),
$t := session:set-attribute("query", $expr)
return
if ($srchlang = "all") then
concat("for $r in xcollection('", $docpref, "')//entry[",
$expr, "] order by $r/",
local:order-expr($orderby), " collation '?lang=sme-SE' return $r")
else
(: The next line is commented out since it does not express the intention:
the possibility to search a selected list of languages. The HTML form is
artificially limited to select only one language until we are ready to
build a proper search expression for a number of languages. :)
(: for $lang in $srchlang :)
let $doc := ristenutil:get-doc($srchlang),
$query := concat("for $r in document('", $doc, "')//entry[",
$expr, "] order by $r/",
local:order-expr($orderby), " collation '?lang=sme-SE' return $r")
return $query
};
(: Present an overview of query results :)
declare function local:displayHitlist($hits as node()+)
as element()
{
let $count := count($hits),
$max := request:get-parameter("howmany", "10") cast as xs:int,
$start := request:get-parameter("start", "1") cast as xs:int,
$end := if ($start + $max - 1 < $count) then $start + $max - 1 else $count
let $c1 := request:get-parameter("class", "all"),
$c2 := session:get-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 ""
let $coll := tokenize(util:collection-name(item-at($hits, 1)), "/") [last()]
return
Parameters: {$params}
Term: {$term}
Selection: {$coll}
Search collection: {$docpref}
Your query has timed out. Please hit «Find» or enter a new search query.
else let $hits := util:eval( $query ), $s := session:set-attribute("results", $hits), $t := session:set-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}