Index: MpdlXmlTextServer.py
===================================================================
--- MpdlXmlTextServer.py	(revision 482)
+++ MpdlXmlTextServer.py	(revision 495)
@@ -1,54 +1,12 @@
+
 from OFS.SimpleItem import SimpleItem
 from Products.PageTemplates.PageTemplateFile import PageTemplateFile 
 
 from Ft.Xml import EMPTY_NAMESPACE, Parse
-from Ft.Xml.Domlette import NonvalidatingReader
-import Ft.Xml.Domlette
-import cStringIO
-
-import xml.etree.ElementTree as ET
-
-import re
+
+import sys
 import logging
-import urllib
-
-from SrvTxtUtils import getInt, getText, getHttpData
-
-def serialize(node):
-    """returns a string containing an XML snippet of node"""
-    s = ET.tostring(node, 'UTF-8')
-    # snip off XML declaration
-    if s.startswith('<?xml'):
-        i = s.find('?>')
-        return s[i+3:]
-
-    return s
-
-
-def getTextFromNode(node):
-    """get the cdata content of a node"""
-    if node is None:
-        return ""
-
-    # 4Suite:
-    nodelist=node.childNodes
-    text = ""
-    for n in nodelist:
-        if n.nodeType == node.TEXT_NODE:
-           text = text + n.data
-    
-    return text
-
-def serializeNode(node, encoding="utf-8"):
-    """returns a string containing node as XML"""
-    #s = ET.tostring(node)
-    
-    # 4Suite:
-    stream = cStringIO.StringIO()
-    Ft.Xml.Domlette.Print(node, stream=stream, encoding=encoding)
-    s = stream.getvalue()
-    stream.close()
-
-    return s
+import documentViewer
+from documentViewer import getTextFromNode, serializeNode
 
 
@@ -63,5 +21,5 @@
     manage_changeMpdlXmlTextServerForm = PageTemplateFile("zpt/manage_changeMpdlXmlTextServer", globals())
         
-    def __init__(self,id,title="",serverUrl="http://mpdl-text.mpiwg-berlin.mpg.de/mpdl/interface/", serverName=None, timeout=40):
+    def __init__(self,id,title="",serverUrl="http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/", serverName=None, timeout=40):
         """constructor"""
         self.id=id
@@ -72,23 +30,26 @@
         else:
             self.serverUrl = "http://%s/mpdl/interface/"%serverName
+
         
     def getHttpData(self, url, data=None):
         """returns result from url+data HTTP request"""
-        return getHttpData(url,data,timeout=self.timeout)
+        return documentViewer.getHttpData(url,data,timeout=self.timeout)
+
     
     def getServerData(self, method, data=None):
         """returns result from text server for method+data"""
         url = self.serverUrl+method
-        return getHttpData(url,data,timeout=self.timeout)
-
-    # WTF: what does this really do? can it be integrated in getPage?
-    def getSearch(self, pageinfo=None,  docinfo=None):
+        return documentViewer.getHttpData(url,data,timeout=self.timeout)
+
+
+    def getSearch(self, pn=1, pageinfo=None,  docinfo=None, query=None, queryType=None, lemma=None):
         """get search list"""
-        logging.debug("getSearch()")
         docpath = docinfo['textURLPath'] 
         url = docinfo['url']
+        logging.debug("documentViewer (gettoc) docpath: %s"%(docpath))
+        logging.debug("documentViewer (gettoc) url: %s"%(url))
         pagesize = pageinfo['queryPageSize']
-        pn = pageinfo.get('searchPN',1)
-        sn = pageinfo.get('sn',None) #TODO: is this s now?
+        pn = pageinfo['searchPN']
+        sn = pageinfo['sn']
         highlightQuery = pageinfo['highlightQuery']
         query =pageinfo['query']
@@ -96,25 +57,12 @@
         viewMode=  pageinfo['viewMode']
         tocMode = pageinfo['tocMode']
-        characterNormalization = pageinfo['characterNormalization']
-        #optionToggle = pageinfo['optionToggle']
         tocPN = pageinfo['tocPN']
         selfurl = self.absolute_url()
-        data = self.getServerData("doc-query.xql","document=%s&mode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s&sn=%s&viewMode=%s&characterNormalization=%s&highlightQuery=%s"%(docpath, 'text', queryType, urllib.quote(query), pagesize, pn, sn, viewMode,characterNormalization, urllib.quote(highlightQuery)))
+        
+        data = self.getServerData("doc-query.xql","document=%s&mode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s&sn=%s&viewMode=%s&highlightQuery=%s"%(docpath, 'text', queryType, query, pagesize, pn, sn, viewMode,highlightQuery))
+        #page=self.template.fulltextclient.eval("/mpdl/interface/doc-query.xql","document=%s&mode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s&sn=%s&viewMode=%s&highlightQuery=%s"%(docpath, 'text', queryType, query, pagesize, pn, sn, viewMode,highlightQuery) ,outputUnicode=False)                
+        
         pagexml = data.replace('?document=%s'%str(docpath),'?url=%s'%url)
         pagedom = Parse(pagexml)
-        
-        """
-        pagedivs = pagedom.xpath("//div[@class='queryResultHits']") 
-        if (pagedivs == pagedom.xpath("//div[@class='queryResultHits']")):
-            if len(pagedivs)>0:
-                docinfo['queryResultHits'] = int(getTextFromNode(pagedivs[0]))
-                s = getTextFromNode(pagedivs[0])
-                s1 = int(s)/10+1
-                try:
-                    docinfo['queryResultHits'] = int(s1)
-                    logging.debug("SEARCH ENTRIES: %s"%(s1))
-                except:
-                    docinfo['queryResultHits'] = 0
-        """
         if (queryType=="fulltext")or(queryType=="xpath")or(queryType=="xquery")or(queryType=="fulltextMorphLemma"):   
             pagedivs = pagedom.xpath("//div[@class='queryResultPage']")
@@ -128,7 +76,6 @@
                         if href.startswith('page-fragment.xql'):
                             selfurl = self.absolute_url()            
-                            pagexml=href.replace('mode=text','mode=texttool&viewMode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s&tocMode=%s&searchPN=%s&tocPN=%s&characterNormalization=%s'%(viewMode,queryType,urllib.quote(query),pagesize,pn,tocMode,pn,tocPN, characterNormalization))
+                            pagexml=href.replace('mode=text','mode=texttool&viewMode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s&tocMode=%s&searchPN=%s&tocPN=%s'%(viewMode,queryType,query,pagesize,pn,tocMode,pn,tocPN))
                             hrefNode.nodeValue = pagexml.replace('page-fragment.xql','%s'%selfurl)                                           
-                #logging.debug("PUREXML :%s"%(serializeNode(pagenode)))
                 return serializeNode(pagenode)        
         if (queryType=="fulltextMorph"):
@@ -143,8 +90,8 @@
                         if href.startswith('page-fragment.xql'):
                             selfurl = self.absolute_url()       
-                            pagexml=href.replace('mode=text','mode=texttool&viewMode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s&tocMode=%s&searchPN=%s&tocPN=%s&characterNormalization=%s'%(viewMode,queryType,urllib.quote(query),pagesize,pn,tocMode,pn,tocPN,characterNormalization))
+                            pagexml=href.replace('mode=text','mode=texttool&viewMode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s&tocMode=%s&searchPN=%s&tocPN=%s'%(viewMode,queryType,query,pagesize,pn,tocMode,pn,tocPN))
                             hrefNode.nodeValue = pagexml.replace('page-fragment.xql','%s'%selfurl)  
                         if href.startswith('../lt/lemma.xql'):
-                            hrefNode.nodeValue = href.replace('../lt/lemma.xql','%s/template/head_main_query'%(selfurl))        
+                            hrefNode.nodeValue = href.replace('../lt/lemma.xql','%s/template/head_main_lemma_New'%(selfurl))        
                             l.setAttributeNS(None, 'target', '_blank')
                             l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=400, scrollbars=1'); return false;")
@@ -161,12 +108,12 @@
                     if hrefNode:
                         href = hrefNode.nodeValue
-                        hrefNode.nodeValue=href.replace('mode=text','mode=texttool&viewMode=%s&tocMode=%s&tocPN=%s&pn=%s&characterNormalization=%s'%(viewMode,tocMode,tocPN,pn,characterNormalization))             
+                        hrefNode.nodeValue=href.replace('mode=text','mode=texttool&viewMode=%s&tocMode=%s&tocPN=%s&pn=%s'%(viewMode,tocMode,tocPN,pn))             
                         if href.startswith('../lt/lex.xql'):
-                            hrefNode.nodeValue = href.replace('../lt/lex.xql','%s/template/head_main_lex'%selfurl)         
+                            hrefNode.nodeValue = href.replace('../lt/lex.xql','%s/template/head_main_voc'%selfurl)         
                             l.setAttributeNS(None, 'target', '_blank')
                             l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=400, scrollbars=1'); return false;")
                             l.setAttributeNS(None, 'onDblclick', 'popupWin.focus();')
                         if href.startswith('../lt/lemma.xql'):
-                            hrefNode.nodeValue = href.replace('../lt/lemma.xql','%s/template/head_main_lemma'%(selfurl))        
+                            hrefNode.nodeValue = href.replace('../lt/lemma.xql','%s/template/head_main_lemma'%selfurl)        
                             l.setAttributeNS(None, 'target', '_blank')
                             l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=400, scrollbars=1'); return false;")
@@ -174,252 +121,118 @@
                 return serializeNode(pagenode)      
         return "no text here"   
-           
-    def getGisPlaces(self, docinfo=None, pageinfo=None):
-        """ Show all Gis Places of whole Page"""
-        xpath='//place'
-        docpath = docinfo.get('textURLPath',None)
-        if not docpath:
-            return None
-
-        pn = pageinfo['current']
-        hrefList=[]
-        myList= ""
-        text=self.getServerData("xpath.xql", "document=%s&xpath=%s&pn=%s"%(docinfo['textURLPath'],xpath,pn))
-        dom = ET.fromstring(text)
-        result = dom.findall(".//result/resultPage/place")
-        for l in result:
-            href = l.get("id")
-            hrefList.append(href)
-            # WTF: what does this do?
-            myList = ",".join(hrefList)
-        #logging.debug("getGisPlaces :%s"%(myList))                             
-        return myList
-    
-    def getAllGisPlaces (self, docinfo=None, pageinfo=None):
-        """Show all Gis Places of whole Book """
-        xpath ='//echo:place'
-        hrefList=[]
-        myList=""
-        text=self.getServerData("xpath.xql", "document=%s&xpath=%s"%(docinfo['textURLPath'],xpath))
-        dom = ET.fromstring(text)
-        result = dom.findall(".//result/resultPage/place")
-        
-        for l in result:
-            href = l.get("id")
-            hrefList.append(href)
-            # WTF: what does this do?
-            myList = ",".join(hrefList)
-            #logging.debug("getALLGisPlaces :%s"%(myList))
-        return myList
-          
-    def processPageInfo(self, dom, docinfo, pageinfo):
-        """processes page info divs from dom and stores in docinfo and pageinfo"""
-        # assume first second level div is pageMeta
-        alldivs = dom.find("div")
-        
-        if alldivs is None or alldivs.get('class', '') != 'pageMeta':
-            logging.error("processPageInfo: pageMeta div not found!")
-            return
-        
-        for div in alldivs:
-            dc = div.get('class')
-            
-            # pageNumberOrig  
-            if dc == 'pageNumberOrig':
-                pageinfo['pageNumberOrig'] = div.text
-                
-            # pageNumberOrigNorm
-            elif dc == 'pageNumberOrigNorm':
-                pageinfo['pageNumberOrigNorm'] = div.text
-                
-            # pageHeaderTitle
-            elif dc == 'pageHeaderTitle':
-                pageinfo['pageHeaderTitle'] = div.text
-                
-            # numFigureEntries
-            elif dc == 'countFigureEntries':
-                docinfo['numFigureEntries'] = getInt(div.text)
-                
-            # numTocEntries
-            elif dc == 'countTocEntries':
-                # WTF: s1 = int(s)/30+1
-                docinfo['numTocEntries'] = getInt(div.text)
-                
-            # numPlaces
-            elif dc == 'countPlaces':
-                docinfo['numPlaces'] = getInt(div.text)
-                
-            # numTextPages
-            elif dc == 'countPages':
-                np = getInt(div.text)                    
-                if np > 0:
-                    docinfo['numTextPages'] = np
-                    if docinfo.get('numPages', 0) == 0:
-                        # seems to be text-only - update page count
-                        docinfo['numPages'] = np
-                        #pageinfo['end'] = min(pageinfo['end'], np)
-                        pageinfo['numgroups'] = int(np / pageinfo['groupsize'])
-                        if np % pageinfo['groupsize'] > 0:
-                            pageinfo['numgroups'] += 1
-        
-        #logging.debug("processPageInfo: pageinfo=%s"%repr(pageinfo))
-        return
-         
-           
-    def getTextPage(self, mode="text", pn=1, docinfo=None, pageinfo=None):
+                       
+    def getNumPages(self,docinfo=None):
+        """get list of pages from fulltext and put in docinfo"""
+        if 'numPages' in docinfo:
+            # already there
+            return docinfo
+        
+        xquery = '//pb'
+        text = self.getServerData("xquery.xql","document=%s&xquery=%s"%(docinfo['textURLPath'],xquery))
+        #text = self.template.fulltextclient.eval("/mpdl/interface/xquery.xql", "document=%s&xquery=%s"%(docinfo['textURLPath'],xquery))
+        docinfo['numPages'] = text.count("<pb ")
+        return docinfo
+       
+    def getTextPage(self, mode="text", pn=1, docinfo=None, pageinfo=None, highlightQuery=None,sn=None, viewMode=None, tocMode=None, tocPN=None):
         """returns single page from fulltext"""
-        logging.debug("getTextPage mode=%s, pn=%s"%(mode,pn))
-        # check for cached text -- but ideally this shouldn't be called twice
-        if pageinfo.has_key('textPage'):
-            logging.debug("getTextPage: using cached text")
-            return pageinfo['textPage']
-        
         docpath = docinfo['textURLPath']
-        # just checking
-        if pageinfo['current'] != pn:
-            logging.warning("getTextPage: current!=pn!")
-            
-        # stuff for constructing full urls
+        path = docinfo['textURLPath']
         url = docinfo['url']
-        urlmode = docinfo['mode']
-        sn = pageinfo.get('sn', None)
-        highlightQuery = pageinfo.get('highlightQuery', None)
-        tocMode = pageinfo.get('tocMode', None)
-        tocPN = pageinfo.get('tocPN',None)
-        characterNormalization = pageinfo.get('characterNormalization', None)
-        
-        selfurl = docinfo['viewerUrl']
-        
-        if mode == "dict" or mode == "text_dict":
-            # dict is called textPollux in the backend
+        viewMode= pageinfo['viewMode']
+        tocMode = pageinfo['tocMode']
+        tocPN = pageinfo['tocPN']
+        selfurl = self.absolute_url()   
+        if mode == "text_dict":
             textmode = "textPollux"
-        elif not mode:
-            # default is text
-            mode = "text"
-            textmode = "text"
         else:
             textmode = mode
         
-        textParam = "document=%s&mode=%s&pn=%s&characterNormalization=%s"%(docpath,textmode,pn,characterNormalization)
-        if highlightQuery:
-            textParam +="&highlightQuery=%s&sn=%s"%(urllib.quote(highlightQuery),sn)           
-        
-        # fetch the page
+        textParam = "document=%s&mode=%s&pn=%s"%(docpath,textmode,pn)
+        if highlightQuery is not None:
+            textParam +="&highlightQuery=%s&sn=%s"%(highlightQuery,sn)           
+        
         pagexml = self.getServerData("page-fragment.xql",textParam)
-        dom = ET.fromstring(pagexml)
-        # extract additional info
-        self.processPageInfo(dom, docinfo, pageinfo)
-        # page content is in <div class="pageContent">
-        pagediv = None
-        # ElementTree 1.2 in Python 2.6 can't do div[@class='pageContent']
-        # so we look at the second level divs
-        alldivs = dom.findall("div")
-        for div in alldivs:
-            dc = div.get('class')
-            # page content div
-            if dc == 'pageContent':
-                pagediv = div
-                break
-        
+        #pagexml=self.template.fulltextclient.eval("/mpdl/interface/page-fragment.xql", textParam, outputUnicode=False)
+        
+        pagedom = Parse(pagexml)
         # plain text mode
         if mode == "text":
-            # get full url assuming documentViewer is parent
-            selfurl = self.getLink()
-            if pagediv is not None:
-                links = pagediv.findall(".//a")
-                for l in links:
-                    href = l.get('href')
-                    if href and href.startswith('#note-'):
-                        href = href.replace('#note-',"%s#note-"%selfurl)
-                        l.set('href', href)
-
-                return serialize(pagediv)
-            
+            # first div contains text
+            pagedivs = pagedom.xpath("/div")
+            if len(pagedivs) > 0:      
+                pagenode = pagedivs[0]
+                links = pagenode.xpath("//a")
+                for l in links:
+                    hrefNode = l.getAttributeNodeNS(None, u"href")
+                    if hrefNode:
+                        href= hrefNode.nodeValue
+                        if href.startswith('#note-'):
+                            hrefNode.nodeValue = href.replace('#note-',"?url=%s&viewMode=%s&tocMode=%s&tocPN=%s&pn=%s#note-"%(url,viewMode,tocMode,tocPN,pn))
+                return serializeNode(pagenode)
+        if mode == "xml":
+              # first div contains text
+              pagedivs = pagedom.xpath("/div")
+              if len(pagedivs) > 0:
+                  pagenode = pagedivs[0]
+                  return serializeNode(pagenode)
+        if mode == "pureXml":
+              # first div contains text
+              pagedivs = pagedom.xpath("/div")
+              if len(pagedivs) > 0:
+                  pagenode = pagedivs[0]
+                  return serializeNode(pagenode)      
         # text-with-links mode
-        elif mode == "dict":
-            if pagediv is not None:
-                viewerurl = docinfo['viewerUrl']
-                selfurl = self.getLink()
+        if mode == "text_dict":
+            # first div contains text
+            pagedivs = pagedom.xpath("/div")
+            if len(pagedivs) > 0:
+                pagenode = pagedivs[0]
                 # check all a-tags
-                links = pagediv.findall(".//a")
-                for l in links:
-                    href = l.get('href')
-                    
-                    if href:
+                links = pagenode.xpath("//a")
+                for l in links:
+                    hrefNode = l.getAttributeNodeNS(None, u"href")
+                    if hrefNode:
                         # is link with href
-                        if href.startswith('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/lt/wordInfo.xql'):
-                            # is dictionary link - change href (keeping parameters)
-                            l.set('href', href.replace('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/lt/wordInfo.xql','%s/template/viewer_wordinfo'%viewerurl))
-                            # add target to open new page
-                            l.set('target', '_blank')
-                                                          
-                        # TODO: is this needed?
-                        if href.startswith('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/lt/lemma.xql'):
+                        href = hrefNode.nodeValue
+                        if href.startswith('lt/lex.xql'):
+                            # is pollux link
                             selfurl = self.absolute_url()
-                            l.set('href', href.replace('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/lt/lemma.xql','%s/head_main_lemma'%selfurl))
-                            l.set('target', '_blank')
-                            l.set('onclick',"popupWin = window.open(this.href, 'InfoWindow', 'menubar=no, location,width=500,height=600,top=180, left=700, toolbar=no, scrollbars=1'); return false;")
-                            l.set('ondblclick', 'popupWin.focus();')   
-                    
+                            # change href
+                            hrefNode.nodeValue = href.replace('lt/lex.xql','%s/template/head_main_voc'%selfurl)
+                            # add target
+                            l.setAttributeNS(None, 'target', '_blank')
+                            l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=700, scrollbars=1'); return false;")
+                            l.setAttributeNS(None, 'onDblclick', 'popupWin.focus();')      
+                        if href.startswith('lt/lemma.xql'):    
+                            selfurl = self.absolute_url()
+                            hrefNode.nodeValue = href.replace('lt/lemma.xql','%s/template/head_main_lemma'%selfurl)
+                            l.setAttributeNS(None, 'target', '_blank')
+                            l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=700, scrollbars=1'); return false;")
+                            l.setAttributeNS(None, 'onDblclick', 'popupWin.focus();')   
                         if href.startswith('#note-'):
-                            # note link
-                            l.set('href', href.replace('#note-',"%s#note-"%selfurl))
-                              
-                return serialize(pagediv)
-            
-        # xml mode
-        elif mode == "xml":
-            if pagediv is not None:
-                return serialize(pagediv)
-            
-        # pureXml mode
-        elif mode == "pureXml":
-            if pagediv is not None:
-                return serialize(pagediv)
-                  
-        # gis mode
-        elif mode == "gis":
-            name = docinfo['name']
-            if pagediv is not None:
-                # check all a-tags
-                links = pagediv.findall(".//a")
-                for l in links:
-                    href = l.get('href')
-                    if href:
-                        if href.startswith('http://chinagis.mpiwg-berlin.mpg.de'):
-                            l.set('href', href.replace('chinagis_REST/REST/db/chgis/mpdl','chinagis/REST/db/mpdl/%s'%name))
-                            l.set('target', '_blank') 
-                            
-                return serialize(pagediv)
-                    
+                            hrefNode.nodeValue = href.replace('#note-',"?url=%s&viewMode=%s&tocMode=%s&tocPN=%s&pn=%s#note-"%(url,viewMode,tocMode,tocPN,pn))    
+                return serializeNode(pagenode)
         return "no text here"
-    
-    # TODO: should be getWordInfo
-    def getWordInfo(self, word='', language='', display=''):
-        """show information (like dictionaries) about word"""
-        data = self.getServerData("lt/wordInfo.xql","language=%s&word=%s&display=%s&output=html"%(language,urllib.quote(word),urllib.quote(display)))
+
+    def getTranslate(self, query=None, language=None):
+        """translate into another languages"""
+        data = self.getServerData("lt/lex.xql","document=&language="+str(language)+"&query="+url_quote(str(query)))
+        #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lex.xql","document=&language="+str(language)+"&query="+url_quote(str(query)))
         return data
     
-    # WTF: what does this do?
     def getLemma(self, lemma=None, language=None):
         """simular words lemma """
-        data = self.getServerData("lt/lemma.xql","language="+str(language)+"&lemma="+urllib.quote(lemma)+"&output=html")
+        data = self.getServerData("lt/lemma.xql","document=&language="+str(language)+"&lemma="+url_quote(str(lemma)))
+        #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lemma.xql","document=&language="+str(language)+"&lemma="+url_quote(str(lemma)))
         return data
     
-    # WTF: what does this do?
-    def getLemmaQuery(self, query=None, language=None):
+    def getLemmaNew(self, query=None, language=None):
         """simular words lemma """
-        data = self.getServerData("lt/lemma.xql","language="+str(language)+"&query="+urllib.quote(query)+"&output=html")
+        data = self.getServerData("lt/lemma.xql","document=&language="+str(language)+"&lemma="+url_quote(str(query)))
+        #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lemma.xql","document=&language="+str(language)+"&lemma="+url_quote(str(query)))
         return data
-    
-    # WTF: what does this do?
-    def getLex(self, query=None, language=None):
-        #simular words lemma
-        data = self.getServerData("lt/lex.xql","document=&language="+str(language)+"&query="+urllib.quote(query))
-        return data
-
-    # WTF: what does this do?
+
     def getQuery (self,  docinfo=None, pageinfo=None, query=None, queryType=None, pn=1):
-         #number of
+         """number of"""
          docpath = docinfo['textURLPath'] 
          pagesize = pageinfo['queryPageSize']
@@ -430,17 +243,18 @@
          tocDiv = None
          
-         pagexml = self.getServerData("doc-query.xql","document=%s&mode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath, 'text', queryType, urllib.quote(query), pagesize, pn))
+         pagexml = self.getServerData("doc-query.xql","document=%s&mode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath, 'text', queryType, query, pagesize, pn))
+         #pagexml=self.template.fulltextclient.eval("/mpdl/interface/doc-query.xql","document=%s&mode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath, 'text', queryType, query, pagesize, pn) ,outputUnicode=False)
          pagedom = Parse(pagexml)
          numdivs = pagedom.xpath("//div[@class='queryResultHits']")
          tocSearch = int(getTextFromNode(numdivs[0]))
          tc=int((tocSearch/10)+1)
+         logging.debug("documentViewer (gettoc) tc: %s"%(tc))
          return tc
-    
+
     def getToc(self, mode="text", docinfo=None):
-        """loads table of contents and stores XML in docinfo"""
-        logging.debug("getToc mode=%s"%mode)
+        """loads table of contents and stores in docinfo"""
+        logging.debug("documentViewer (gettoc) mode: %s"%(mode))
         if mode == "none":
-            return docinfo
-              
+            return docinfo        
         if 'tocSize_%s'%mode in docinfo:
             # cached toc
@@ -458,87 +272,40 @@
         tocSize = 0
         tocDiv = None
-        # fetch full toc
+        
         pagexml = self.getServerData("doc-query.xql","document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath,queryType, pagesize, pn))
-        dom = ET.fromstring(pagexml)
-        # page content is in <div class="queryResultPage">
-        pagediv = None
-        # ElementTree 1.2 in Python 2.6 can't do div[@class='queryResultPage']
-        alldivs = dom.findall("div")
-        for div in alldivs:
-            dc = div.get('class')
-            # page content div
-            if dc == 'queryResultPage':
-                pagediv = div
-                
-            elif dc == 'queryResultHits':
-                docinfo['tocSize_%s'%mode] = getInt(div.text)
-
-        if pagediv:
-            # store XML in docinfo
-            docinfo['tocXML_%s'%mode] = ET.tostring(pagediv, 'UTF-8')
-
+        #pagexml=self.template.fulltextclient.eval("/mpdl/interface/doc-query.xql", "document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath,queryType,pagesize,pn), outputUnicode=False)
+        # post-processing downloaded xml
+        pagedom = Parse(pagexml)
+        # get number of entries
+        numdivs = pagedom.xpath("//div[@class='queryResultHits']")
+        if len(numdivs) > 0:
+            tocSize = int(getTextFromNode(numdivs[0]))
+        docinfo['tocSize_%s'%mode] = tocSize
         return docinfo
     
-    def getTocPage(self, mode="text", pn=None, start=None, size=None, pageinfo=None, docinfo=None):
+    def getTocPage(self, mode="text", pn=1, pageinfo=None, docinfo=None):
         """returns single page from the table of contents"""
-        logging.debug("getTocPage mode=%s, pn=%s"%(mode,pn))
+        # TODO: this should use the cached TOC
         if mode == "text":
             queryType = "toc"
         else:
             queryType = mode
-            
-        # check for cached TOC
-        if not docinfo.has_key('tocXML_%s'%mode):
-            self.getToc(mode=mode, docinfo=docinfo)
-            
-        tocxml = docinfo.get('tocXML_%s'%mode, None)
-        if not tocxml:
-            logging.error("getTocPage: unable to find tocXML")
-            return "Error: no table of contents!"
-        
-        if size is None:
-            size = pageinfo.get('tocPageSize', 30)
-            
-        if start is None:
-            start = (pn - 1) * size
-
-        fulltoc = ET.fromstring(tocxml)
-        
-        if fulltoc:
-            # paginate
-            first = (start - 1) * 2
-            len = size * 2
-            del fulltoc[:first]
-            del fulltoc[len:]
-            tocdivs = fulltoc
-            
-            # check all a-tags
-            links = tocdivs.findall(".//a")
-            for l in links:
-                href = l.get('href')
-                if href:
-                    # take pn from href
-                    m = re.match(r'page-fragment\.xql.*pn=(\d+)', href)
-                    if m is not None:
-                        # and create new url (assuming parent is documentViewer)
-                        url = self.getLink('pn', m.group(1))
-                        l.set('href', url)
-                    else:
-                        logging.warning("getTocPage: Problem with link=%s"%href)
-                        
-            # fix two-divs-per-row with containing div
-            newtoc = ET.Element('div', {'class':'queryResultPage'})
-            for (d1,d2) in zip(tocdivs[::2],tocdivs[1::2]):
-                e = ET.Element('div',{'class':'tocline'})
-                e.append(d1)
-                e.append(d2)
-                newtoc.append(e)
-                
-            return serialize(newtoc)
-        
-        return "ERROR: no table of contents!"
-    
-    
-    def manage_changeMpdlXmlTextServer(self,title="",serverUrl="http://mpdl-text.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,RESPONSE=None):
+        docpath = docinfo['textURLPath']
+        path = docinfo['textURLPath']       
+        pagesize = pageinfo['tocPageSize']
+        pn = pageinfo['tocPN']
+        url = docinfo['url']
+        selfurl = self.absolute_url()  
+        viewMode=  pageinfo['viewMode']
+        tocMode = pageinfo['tocMode']
+        tocPN = pageinfo['tocPN']  
+        
+        data = self.getServerData("doc-query.xql","document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath,queryType, pagesize, pn))  
+
+        page = data.replace('page-fragment.xql?document=%s'%str(path),'%s?url=%s&viewMode=%s&tocMode=%s&tocPN=%s'%(selfurl,url, viewMode, tocMode, tocPN))
+        text = page.replace('mode=image','mode=texttool')
+        return text
+    
+    def manage_changeMpdlXmlTextServer(self,title="",serverUrl="http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,RESPONSE=None):
         """change settings"""
         self.title=title
@@ -554,6 +321,5 @@
     return pt()
 
-def manage_addMpdlXmlTextServer(self,id,title="",serverUrl="http://mpdl-text.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,RESPONSE=None):
-#def manage_addMpdlXmlTextServer(self,id,title="",serverUrl="http://mpdl-text.mpiwg-berlin.mpg.de:30030/mpdl/interface/",timeout=40,RESPONSE=None):    
+def manage_addMpdlXmlTextServer(self,id,title="",serverUrl="http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,RESPONSE=None):
     """add zogiimage"""
     newObj = MpdlXmlTextServer(id,title,serverUrl,timeout)
@@ -561,4 +327,5 @@
     if RESPONSE is not None:
         RESPONSE.redirect('manage_main')
-        
-        
+
+
+    
