(: : Module: Utility functions for the database at www.risten.no. : : $Id$ : : Functions that are safe with respect to the planned enhancements : to allow multiple terminology collections and dictionaries are marked as such. : : The functions defined thus far are: : : makeTermID($term as xs:string, $pos as xs:string) as xs:string : get-doc($lang as xs:string) as xs:string : pre-main - - not yet safe, depends on get-doc : newID() as xs:string - - multiple termcoll safe : dateTime() as xs:string - - N/A : countRecords() as xs:integer - - multiple termcoll safe : lastChanged() as xs:double - - multiple termcoll safe : ristenutil:cleanEntry() - - N/A : get-xmldb-collection() as xs:string - - multiple termcoll safe : get-collection() as xs:string - - multiple termcoll safe : : namespace: "http://www.risten.no/shared/util" : :) module namespace ristenutil="http://www.risten.no/shared/util"; declare namespace util="http://exist-db.org/xquery/util"; declare namespace request="http://exist-db.org/xquery/request"; (: + | Creates a new term ID based on entry string and POS + :) declare function ristenutil:makeTermID($term as xs:string, $pos as xs:string) as xs:string { let $t := translate($term, ' ', '_') let $id := concat($t, '\', $pos) return $id }; (: + | Returns the full path to a term document in the SD-terms collection, | based on language. + :) declare function ristenutil:get-doc($lang as xs:string) as xs:string { let $doc := concat('/db/ordbase/terms/SD-terms/terms-',$lang,'.xml') return $doc }; (: Pre-main function - checks which documents are affected and locks them upon calling the real main function, which includes XUpdates (the reason for the lock) :) declare function ristenutil:pre-main($lang as xs:string, $func as element()) as element()+ { let $centerdoc := concat(ristenutil:get-collection, '/termcenter.xml'), $langdoc := ristenutil:get-doc( $lang ) let $docs := ( doc($centerdoc), doc($langdoc) ) return util:exclusive-lock( $docs, $func ) }; (: + | Creates an xml entry to be displayed + :) declare function ristenutil:display($lang as xs:string, $hits as node()+) as element() { let $entry := item-at($hits,1), $common := $entry/common, $senses := $entry/senses, $id := $entry/@id return {$common} { for $s in $entry/senses/sense let $status := $s/@status order by $status return { if ( $s/def ) then $s/def else (), if ( $s/synonyms ) then $s/synonyms else () } } }; (: + | Create an ID filter expression from query term + :) declare function ristenutil:id-filter($record as xs:string) as xs:string { let $t := concat("'", $record, "'") return concat("@id = ", $t) }; (: : Make a unique numeric ID for termcenter.xml : This function returns () if used on a collection that doesn't use a : numeric centerID type (propnouns doesn't). :) declare function ristenutil:newID() as xs:string { let $coll := "/db/ordbase/terms/propnouns" return if (collection($coll)/termmeta/centerIDType/text() eq "numeric") then let $doc := concat($coll, '/termcenter.xml') let $r := document($doc)//entry/@id (: Find the highest existing ID, increment, & return the non-decimal part: :) return substring-before((max($r) + 1), ".") else () }; (: + | Create a numeric string of the current date&time. The format is: | YYYYMMDDHHMMSS, eg. 20050329123129 | Storing the date as an integer makes it easy to compare dates, sorting etc. + :) declare function ristenutil:dateTime() as xs:string { let $dt:=fn:current-dateTime(), $y:=fn:year-from-dateTime($dt), $m:=fn:month-from-dateTime($dt), $d:=fn:day-from-dateTime($dt), $h:=fn:hours-from-dateTime($dt), $min:=fn:minutes-from-dateTime($dt), $s:=fn:seconds-from-dateTime($dt), $lmon:=fn:string-length($m), $padmon:=fn:concat(fn:string-pad("0",2-$lmon),$m), $lday:=fn:string-length($d), $padday:=fn:concat(fn:string-pad("0",2-$lday),$d), $lhour:=fn:string-length($h), $padhour:=fn:concat(fn:string-pad("0",2-$lhour),$h), $lmin:=fn:string-length($min), $padmin:=fn:concat(fn:string-pad("0",2-$lmin),$min), $lsec:=fn:string-length($s), $padsec:=fn:concat(fn:string-pad("0",2-$lsec),$s) return fn:concat($y,$padmon,$padday,$padhour,$padmin,$padsec) }; (: Count the number of entries in all termcenter.xml files: :) declare function ristenutil:countRecords() as xs:integer { let $r := collection('/db/ordbase/terms')/termCenter/entry return count($r) }; (: Count the number of entries in all terms-$LANG.xml files: :) declare function ristenutil:count-term-entries() as xs:integer { let $r := collection(ristenutil:get-public-termcolls())/terminology/entry return count($r) }; (: Count the number of entries in all dictionary files: :) declare function ristenutil:count-dict-entries() as xs:integer { let $r := collection(ristenutil:get-public-dictcolls())//entry return count($r) }; (: Get the last modified date on all files - this is the date the whole db was last modified: :) declare function ristenutil:lastChanged() as xs:double { let $r := collection('/db/ordbase')//@last-update return max($r) }; (: Clean the entry, by removing spacing chars in the beginning and end, collapsing double spaces to single instances, etc.: :) declare function ristenutil:cleanEntry($entry as xs:string) as xs:string { (: to begin with, we remove leading and trailing spaces, and collapse multiple spaces to one - later we may have to add more. :) let $r := fn:normalize-space($entry) return $r }; (:~ : Utility function to return the term collection being worked with, : with the xmldb: protocol appended. : : @return String with the whole xmldb reference to the collection :) declare function ristenutil:get-xmldb-collection() as xs:string { let $collID:= request:get-session-attribute("collection") let $coll := util:collection-name(/(termmeta|dictmeta|classmeta)/id[text() eq $collID]) let $db := concat('xmldb:exist://',$coll) return $db }; (:~ : Utility function to return the term collection being worked with, : without any xmldb: protocol. : : @return String with the collection reference :) declare function ristenutil:get-collection() as xs:string { let $collID := request:get-session-attribute("collection") let $coll := util:collection-name(/(termmeta|dictmeta|classmeta)/id[text() eq $collID]) return $coll }; (:~ : Returns a list of all term collections publicly visible : (i.e. they have their meta.xml/termmeta/public element set to 'yes') : : @return list of strings :) declare function ristenutil:get-all-public-collections() as xs:string* { for $r in /(termmeta|dictmeta)[public/text() eq "yes"] let $coll := util:collection-name($r/id) return $coll }; (:~ : Returns a list of all term collections publicly visible : (i.e. they have their meta.xml/termmeta/public element set to 'yes') : : @return list of strings :) declare function ristenutil:get-public-termcolls() as xs:string* { for $r in /termmeta[public/text() eq "yes"] let $coll := util:collection-name($r/id) return $coll }; (:~ : Returns a list of all term collections publicly visible : (i.e. they have their meta.xml/termmeta/public element set to 'yes') : : @return list of strings :) declare function ristenutil:get-public-dictcolls() as xs:string* { for $r in /dictmeta[public/text() eq "yes"] let $coll := util:collection-name($r/id) return $coll };