xquery version "1.0"; (: $Id: media.xq 7220 2008-01-14 10:50:38Z wolfgang_m $ :) declare namespace bib="http://exist-db.org/bibliography"; (: Load library modules :) declare namespace request="http://exist-db.org/xquery/request"; declare namespace session="http://exist-db.org/xquery/session"; declare namespace xsl="http://exist-db.org/xquery/transform"; declare namespace util="http://exist-db.org/xquery/util"; declare namespace xdb="http://exist-db.org/xquery/xmldb"; declare namespace xi="http://www.w3.org/2001/XInclude"; declare namespace system="http://exist-db.org/xquery/system"; import module namespace conf="http://exist-db.org/modules/mods-config" at "memoirs-config.xqm"; import module namespace sort="http://exist-db.org/biblio/sort" at "memoirs-sort.xq"; import module namespace display="http://exist-db.org/biblio/display" at "memoirs-display.xq"; (: declare option exist:output-size-limit "-1"; :) declare function bib:select-conference() as element() { let $rec := "for $m in distinct-values(collection('/db/memorials')/record/conference/text()) order by $m return ", $d := util:eval($rec) return }; declare variable $bib:sort-import { 'import module namespace sort="http://exist-db.org/biblio/sort" at "memoirs-sort.xq";' }; (: Removes a document :) declare function bib:remove() as element()? { let $resources := request:get-parameter("r", ()) return if(empty($resources)) then else for $r in $resources let $log := util:log("DEBUG", ("$r: ", util:get-sequence-type($r), ": ", $r)) let $cached := session:get-attribute("cache"), $rec := $cached[xs:int($r)] return ( xdb:remove(util:collection-name($rec), util:document-name($rec)), session:set-attribute("cache", remove($cached, xs:int($r))) ) }; declare function bib:remove-folder() as element()? { let $folder := request:get-parameter("folder", ()) return if(not($folder)) then else xdb:remove($folder) }; declare function bib:create-folder($homeColl as xs:string) as element()? { let $name := request:get-parameter("name", ()), $parentParam := request:get-parameter("folder", ()), $parent := if ($parentParam) then $parentParam else $homeColl return let $newCol := xdb:create-collection($parent, $name) return () }; declare function bib:folders($root as xs:string, $current as xs:string) as element() { }; declare function bib:scan-collection($home as xs:string, $collection as xs:string, $current as xs:string) as element()+ { let $displayName := if ($home eq $collection) then "All folders" else substring-after(substring-after($collection, $home), '/') return (
  • { if ($collection eq $current) then attribute checked { "checked" } else () } {$displayName}
  • , for $child in xdb:get-child-collections($collection) order by $child return bib:scan-collection($home, concat($collection, '/', $child), $current) ) }; declare function bib:operand($field, $terms) as xs:string { let $mode := request:get-parameter("mode", "all") return if($mode eq "any") then concat($field, " |= '", $terms, "'") else if($mode eq "near") then concat("near(", $field, ", '", $terms, "')") else concat($field, " &= '", $terms, "'") }; (: Create an XPath expression for the current field and search terms :) declare function bib:createXPath($collection as xs:string, $term1 as xs:string?) as xs:string { let $field1 := request:get-parameter("field1", "any"), $queryPart := if($term1) then concat(conf:get-query-root($collection), "[", bib:operand(conf:queryField($field1), $term1)) else conf:get-query-root($collection), $l := util:log("debug", ("Part: ", $term1)), $term2 := request:get-parameter("term2", ()) return if($term2) then let $field2 := request:get-parameter("field2", "any"), $op := request:get-parameter("op", "and") return concat($queryPart, " ", $op, " ", bib:operand(conf:queryField($field2), $term2), "]") else if($term1) then concat($queryPart, "]") else $queryPart }; declare function bib:displayOverview($recs as item()*, $collection) as element()+ { let $count := count($recs), $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, $expandAll := exists(request:get-parameter("expand", ())), $preload := if ($expandAll) then true() else $conf:preload return
    {display:navigation($count, $start, $end + 1, $max, $preload)}
    Sort by: Name Year of Journal Publication Name of Conference

    { for $p in $start to $end let $m := $recs[$p] return ( display:record-short($p, $m, $expandAll), ) }
    }; declare function bib:displayDetails($recs as item()*, $collection) as element() { let $count := count($recs), $max := xs:int(request:get-parameter("howmany", "10")), $start := xs:int(request:get-parameter("start", "1")), $hit := item-at($recs, $start) return {$hit/*} }; declare function bib:display($recs as item()*, $collection) as element()+ { let $display := request:get-parameter("view", "overview") return if($display eq "details") then bib:displayDetails($recs, $collection) else bib:displayOverview($recs, $collection) }; declare function bib:buildQuery($xpath as xs:string, $order as xs:string) as xs:string { let $orderExpr := sort:orderExpr($order) return concat(conf:get-namespace-decls(), $bib:sort-import, "for $m in ", $xpath, " order by ", $orderExpr, " return $m") }; declare function bib:reorder($order as xs:string, $collection as xs:string) as element()+ { let $xpath := session:get-attribute("query"), $l := util:log("debug", ("Cached: ", $xpath)), $recs := util:eval(bib:buildQuery($xpath, $order)), $x := session:set-attribute("cache", $recs) return bib:display($recs, $collection) }; declare function bib:query($collection as xs:string) as element()+ { let $simpleQuery := request:get-parameter("query", ()), $start := request:get-parameter("start", ()), $cached := session:get-attribute("cache"), $orderby := request:get-parameter("order", ""), $term1 := if($simpleQuery) then $simpleQuery else request:get-parameter("term1", ()) return (: if parameter "start" is not set, execute a new query :) if($term1 or $simpleQuery eq "" or empty($cached)) then let $xpath := bib:createXPath($collection, $term1), $l := util:log("debug", ("Query: ", $xpath)), $recs := util:eval(bib:buildQuery($xpath, $orderby)), $x := session:set-attribute("query", $xpath), $r := session:set-attribute("cache", $recs) return bib:display($recs, $collection) (: redisplay previous query results :) else if($orderby != "") then bib:reorder($orderby, $collection) else ( util:log("debug", ("Reading from cache: start = ", $start)), bib:display($cached, $collection) ) }; declare function bib:process-action($collection as xs:string, $homeCol as xs:string) as element()* { let $action := request:get-parameter("action", "") return ( util:log("debug", "checking action"), if($action eq "Remove") then bib:remove() else if ($action eq "Create") then bib:create-folder($homeCol) else if ($action eq "Remove Folder") then bib:remove-folder() else util:log("debug", "no action") ) }; declare function bib:main($collection as xs:string) as element()+ { bib:query($collection) }; declare function bib:get-collection($user as xs:string?) as xs:string { let $colParam := request:get-parameter("collection", ()) return if($colParam) then let $collection := if($colParam eq "_home_") then xdb:get-user-home($user) else $colParam, $s := session:set-attribute("modscol", $collection) return $collection else if(session:get-attribute("modscol")) then session:get-attribute("modscol") else "/db/memorials" (: fallback :) }; session:create(), let $user := "admin", $pass := " ", $s := session:set-attribute("modscol","/db/memorials"), $url := session:encode-url(request:get-uri()), $collection := "/db/memorials", $homeSess := session:get-attribute("root-collection"), $homeCol := if ($homeSess) then $homeSess else ( session:set-attribute("root-collection", $collection), $collection ) return GCAH Index to Conference Journal Memoirs Catalog {bib:process-action($collection, $homeCol)}
    { let $displayForm := request:get-parameter("show-form", ()) return if($displayForm) then conf:query-form($url, $collection) else bib:main($collection) }