view software/eXist/webapp/mpdl/interface/doc-query.xql @ 15:e99964f390e4

diverse Fehlerbehebungen
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Mon, 29 Aug 2011 17:40:19 +0200
parents d6f528ad5d96
children
line wrap: on
line source

xquery version "1.0";

import module namespace mpdl-time = "http://www.mpiwg-berlin.mpg.de/ns/mpdl/util/time" at "../util/time.xql";
import module namespace functx = "http://www.functx.com" at "../util/functx.xql";
import module namespace mpdl-lucene = "http://www.mpiwg-berlin.mpg.de/ns/mpdl/lucene/search" at "../lucene/search.xql";
import module namespace mpdl-text = "http://www.mpiwg-berlin.mpg.de/ns/mpdl/text" at "../text/all.xql";

declare namespace request = "http://exist-db.org/xquery/request";
declare namespace transform = "http://exist-db.org/xquery/transform";
declare namespace util = "http://exist-db.org/xquery/util";

declare namespace dcterms="http://purl.org/dc/terms";
declare namespace echo="http://www.mpiwg-berlin.mpg.de/ns/echo/1.0/";

let $mpdlDocUri := request:get-parameter("document", "")
let $mode := request:get-parameter("mode", "image")
let $queryType := request:get-parameter("queryType", "")
let $query := request:get-parameter("query", "")
let $reqQueryResultPageSize := request:get-parameter("queryResultPageSize", "")
let $queryResultPageSize := 
  if ($reqQueryResultPageSize = '' or $reqQueryResultPageSize = '0')
  then 100
  else number($reqQueryResultPageSize)
let $reqQueryResultPN := request:get-parameter("queryResultPN", "")
let $queryResultPN := 
  if ($reqQueryResultPN = '' or $reqQueryResultPN = '0')
  then 1
  else number($reqQueryResultPN)

let $presentationPath := "/db/mpdl/presentation"
(: e.g. mpdlCollectioName is derived from mpdlDocUri: /archimedes/la/yourDoc.xml  :)
let $documentName := substring-before(substring-after(substring-after(substring-after($mpdlDocUri, "/"), "/"), "/"), ".")
let $language := substring-before(substring-after(substring-after($mpdlDocUri, "/"), "/"), "/")
let $docbase := substring-before(substring-after($mpdlDocUri, "/"), "/")
let $fullDocumentUri := 
  if ($queryType = 'fulltext' or $queryType = 'ftIndex')
  then concat('/db/mpdl/documents/standard', $mpdlDocUri)
  else if ($queryType = 'fulltextMorph' or $queryType = 'fulltextMorphLemma' or $queryType = 'ftIndexMorph')
  then concat('/db/mpdl/documents/morph', $mpdlDocUri)
  else concat('/db/mpdl/documents/morph', $mpdlDocUri)
let $currentTimeBegin := util:system-time()
let $documentAvailable := doc-available($fullDocumentUri)
let $document := doc($fullDocumentUri)

(: xQuery inline execution does not work in module so it has to be done here  :) 
let $xQueryPageSize := 100
let $xQueryResultEval := 
  if ($queryType = 'xpath' or $queryType = 'xquery' and $query != "")
  then util:eval-inline($document, $query)
  else ()
let $xQueryFrom := ($queryResultPN * $xQueryPageSize) - $xQueryPageSize + 1
let $xQueryTo := $queryResultPN * $xQueryPageSize
let $xQueryResultEntries := 
  for $entry at $pos in $xQueryResultEval
  where $pos >= $xQueryFrom and $pos <= $xQueryTo
  return $entry
let $xQuerySize := count($xQueryResultEval)
let $xQueryPages := 
  if ($xQuerySize = 0)
  then 0
  else $xQuerySize idiv $xQueryPageSize + 1
let $xQueryResult := 
      <result>
        <size>{$xQuerySize}</size>
        <page-size>{$xQueryPageSize}</page-size>
        <pages>{$xQueryPages}</pages>
        <pn>{$queryResultPN}</pn>
        <hits>{$xQueryResultEntries}</hits>
      </result>  

let $queryResult := 
  if (($queryType = 'fulltext' or $queryType = 'fulltextMorph' or $queryType = 'fulltextMorphLemma') and $query != "")
  then mpdl-lucene:search($docbase, $language, $document, $queryType, $query, $queryResultPN, $queryResultPageSize)
  else if (($queryType = 'ftIndex' or $queryType = 'ftIndexMorph') and $query != "")
  then mpdl-text:indexTerms($docbase, $language, $document, $query, $queryResultPN, $queryResultPageSize)
  else if ($queryType = 'xpath' or $queryType = 'xquery' and $query != "")
  then $xQueryResult
  else if ($queryType = 'toc' or $queryType = 'figures')
  then mpdl-text:get-toc($docbase, $queryType, $document, $queryResultPN, $queryResultPageSize)
  else if ($query = "")
  then 
      <result>
        <size>0</size>
        <page-size>0</page-size>
        <pages>0</pages>
        <pn>0</pn>
        <hits/>
      </result>
  else ()

let $countHits := count($queryResult/result/hits/hit)
let $firstHit := $queryResult/result/hits/hit[1]

(: 10 or more is an error :)
let $errorCode := 
  if (not($documentAvailable))
  then 10
  else 0

let $currentTimeEnd := util:system-time()
let $neededTime := mpdl-time:duration-as-ms($currentTimeEnd - $currentTimeBegin)

let $xmlResult := 
  if ($errorCode < 10)
  then 
    <result>
      <document-description>
        <uri>{$mpdlDocUri}</uri>
        <collection-name>{$docbase}</collection-name>
        <document-name>{$documentName}</document-name>
        <language>{$language}</language>
      </document-description>
      <page>
        <mode>{$mode}</mode>
      </page>
      <query>
        <type>{$queryType}</type>
        <expression>{$query}</expression>
        {$queryResult}
      </query>
      <performance>{$neededTime}</performance>
    </result>
  else if ($errorCode = 10)
  then <error><number>{$errorCode}</number><description>Fulltext document: {$mpdlDocUri} is not available yet</description></error>
  else <error><number>{$errorCode}</number><description>undefined error: {$errorCode}</description></error>  

let $declare := 
  if ($mode = "text" or $mode = "textPollux" 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 if ($mode = "pureXml")
  then util:declare-option("exist:serialize", "method=xml media-type=text/xml 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 $xslFilePath := 
  if($mode = "text" or $mode = "textPollux" or $mode = "image" or $mode = "xml")
  then concat($presentationPath, "/queryHtml.xsl")
  else concat($presentationPath, "/pageXml.xsl")
let $xslDoc := doc($xslFilePath)
let $result := 
  if($errorCode < 10)
  then transform:transform($xmlResult, $xslDoc, ())
  else
    <div>{$xmlResult}</div>  (:  error xml result  :)

return $result