diff software/eXist/webapp/mpdl/_stuff/oxygen-projects/monte-project/page-query-result.xquery.old.xquery @ 7:5589d865af7a

Erstellung XQL/XSL Applikation
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 08 Feb 2011 15:16:46 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/software/eXist/webapp/mpdl/_stuff/oxygen-projects/monte-project/page-query-result.xquery.old.xquery	Tue Feb 08 15:16:46 2011 +0100
@@ -0,0 +1,151 @@
+xquery version "1.0";
+
+module namespace ft="http://exist-db.org/xquery/lucene";
+declare namespace request="http://exist-db.org/xquery/request";
+declare namespace text="http://exist-db.org/xquery/text";
+declare namespace transform="http://exist-db.org/xquery/transform";
+declare namespace util="http://exist-db.org/xquery/util";
+declare namespace docc = "http://www.mpiwg-berlin.mpg.de/ns/mpdl/docc"; 
+declare namespace time-util = "http://www.mpiwg-berlin.mpg.de/ns/mpdl/time-util"; 
+
+declare function time-util:duration-as-ms($t) {
+  round((minutes-from-duration($t) * 60 + seconds-from-duration($t)) * 1000 )
+};
+
+declare function docc:ft-search($docPath, $ftQuery) as node() {
+  let $document := doc($docPath)
+  (: performance reasons: all hits (not only the first 10! ) are passed through the :)
+  (: for loop: so the overhead in each loop has to be minimized :)
+  let $t := $document//s[ft:query(., $ftQuery)]
+  let $tempQueryResult := 
+    for $ss at $poss in $t
+    (: where $poss > 100 and $poss <= 200  :)
+    return $ss
+  let $queryResult :=
+    for $s at $pos in $tempQueryResult
+      let $pnOfS := count($document//pb[. << $s])    (: faster: comparison only in pb elements of this document :)
+      let $posOfS := count($document//pb[$pnOfS]/following::s[. << $s]) + 1    (: faster: comparisonon only in s elements of this document :)
+      let $resultElem := 
+        <hit>
+          <pos>{$pos}</pos>
+          <pn>{$pnOfS}</pn>
+          <pos-of-s>{$posOfS}</pos-of-s>
+          {$s}
+        </hit>
+    return $resultElem
+  let $resultSize := count($queryResult)
+  let $result := 
+        <ft-query>
+          <name>{$ftQuery}</name>
+          <result>
+            <size>{$resultSize}</size>
+            <hits>
+              {$queryResult}
+            </hits>
+          </result>
+        </ft-query>  
+   return $result
+};
+
+declare function docc:termEntries($term as xs:string, $data as xs:int+) {
+  let $result := 
+    <entry>  
+       <term>{$term}</term>  
+       <frequency>{$data[1]}</frequency>  
+       <documents>{$data[2]}</documents>  
+       <position>{$data[3]}</position>  
+       <rank>{$data[4]}</rank>  
+     </entry>  
+  return $result
+};
+
+declare function docc:indexTerms($document, $indexTermsStartStr, $count) as node()* {
+  let $docName := util:document-name($document)
+  let $index := /archimedes/info/cvs_file[contains(., $docName)]/root()/archimedes/text
+  let $callback := util:function(QName("http://www.mpiwg-berlin.mpg.de/ns/mpdl/docc", "docc:termEntries"), 2)
+  let $result := text:index-terms($index, $indexTermsStartStr, $callback, $count)
+  return 
+    <index-terms>
+    <start-string>{$indexTermsStartStr}</start-string>
+    {$result}
+    </index-terms>
+};
+
+
+let $document := request:get-parameter("document", "agric_remet_001_la_1556")
+let $mode := request:get-parameter("mode", "image")
+let $pn := number(request:get-parameter("pn", "1"))
+let $ftQuery := request:get-parameter("ft-query", "")
+let $indexTermsStartStr := request:get-parameter("index-terms-start-str", "")
+
+let $currentTimeBegin := util:system-time()
+let $docPath := concat("/db/archimedes/", $document, ".xml")
+let $docRoot := doc($docPath)
+let $archimedesInfo := $docRoot/archimedes/info
+let $locator := $archimedesInfo/locator
+let $countPages := count($docRoot//pb)
+let $pb1 := $docRoot//pb[$pn]
+let $positionOfFirstFigureAfterPB1 := count($pb1/following::figure[1]/preceding::figure) + 1  (: position der ersten figure nach pb1   :)
+let $pb1ImageRefLeft := substring-before($locator, ".")
+let $pb1ImageRefRight := "01"
+let $pb2 := $docRoot//pb[$pn + 1]
+let $errorCode := 
+  if ($pn < $countPages + 1 and $pn > 0)
+  then 0
+  else 1
+
+let $retPageFragment := 
+  if ($mode = "image" or $errorCode > 0)
+  then ()
+  else util:get-fragment-between($pb1, $pb2, true())
+
+let $returnPageFragment := util:parse($retPageFragment)  (: string2xml: returns a valid xml document for that string   :)
+
+let $ftHits := 
+  if ($ftQuery = "")
+  then ()
+  else docc:ft-search($docPath, $ftQuery)
+
+let $indexTerms :=
+  if($indexTermsStartStr = "")
+  then ()
+  else docc:indexTerms($docRoot, $indexTermsStartStr, 100)
+
+let $currentTimeEnd := util:system-time()
+let $neededTime := time-util:duration-as-ms($currentTimeEnd - $currentTimeBegin)
+
+let $xmlResult := 
+  if ($errorCode = 0)
+  then 
+    <result>
+    <mode>{$mode}</mode>
+    <document-description>
+      <document-name>{$document}</document-name>
+      {$archimedesInfo}
+      <count-pages>{$countPages}</count-pages>
+      <performance>{$neededTime}</performance>
+    </document-description>
+    <page>
+      <number>{$pn}</number>
+      <image>http://nausikaa2.rz-berlin.mpg.de/digitallibrary/digilib.jsp?fn=/permanent/archimedes/{$document}/{$pb1ImageRefLeft}-{$pb1ImageRefRight}-pageimg&amp;pn={$pn}</image>
+      <image-echo>http://echo.mpiwg-berlin.mpg.de/zogilib?fn=/permanent/archimedes/{$document}/{$pb1ImageRefLeft}-{$pb1ImageRefRight}-pageimg&amp;pn={$pn}</image-echo>
+      <image-scaler>http://nausikaa2.rz-berlin.mpg.de/digitallibrary/servlet/Scaler?fn=/permanent/archimedes/{$document}/{$pb1ImageRefLeft}-{$pb1ImageRefRight}-pageimg&amp;pn={$pn}</image-scaler>
+      <xml-url>?document={$document}&amp;pn={$pn}&amp;mode=xml</xml-url>
+      <firstFigurePosition>{$positionOfFirstFigureAfterPB1}</firstFigurePosition>
+      <content>{$returnPageFragment}</content>
+    </page>
+    {$ftHits}
+    {$indexTerms}
+    </result>
+  else if ($errorCode = 1)
+  then <error>No result: Page {$pn} not found</error>
+  else <error>undefined error: {$errorCode}</error>  
+
+let $declare := 
+  if ($mode = "text" or $mode = "image" or $mode = "xml")
+  then util:declare-option("exist:serialize", "method=html media-type=text/html omit-xml-declaration=no indent=yes encoding=UTF-8")
+  else util:declare-option("exist:serialize", "method=xml media-type=text/xml omit-xml-declaration=no indent=yes encoding=UTF-8")
+
+let $result := transform:transform($xmlResult, doc("/db/xsl/archimedes-page-fragment.xsl"), ())
+
+return $result