Annotation of documentViewer/MpdlXmlTextServer.py, revision 1.222

1.2       casties     1: 
                      2: from OFS.SimpleItem import SimpleItem
                      3: from Products.PageTemplates.PageTemplateFile import PageTemplateFile 
                      4: from Ft.Xml import EMPTY_NAMESPACE, Parse
                      5: 
                      6: import sys
                      7: import logging
1.5       casties     8: import urllib
1.2       casties     9: import documentViewer
                     10: from documentViewer import getTextFromNode, serializeNode
                     11: 
                     12: class MpdlXmlTextServer(SimpleItem):
                     13:     """TextServer implementation for MPDL-XML eXist server"""
                     14:     meta_type="MPDL-XML TextServer"
                     15: 
                     16:     manage_options=(
                     17:         {'label':'Config','action':'manage_changeMpdlXmlTextServerForm'},
                     18:        )+SimpleItem.manage_options
                     19:     
                     20:     manage_changeMpdlXmlTextServerForm = PageTemplateFile("zpt/manage_changeMpdlXmlTextServer", globals())
                     21:         
1.3       casties    22:     def __init__(self,id,title="",serverUrl="http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/", serverName=None, timeout=40):
1.2       casties    23:         """constructor"""
                     24:         self.id=id
                     25:         self.title=title
                     26:         self.timeout = timeout
1.3       casties    27:         if serverName is None:
                     28:             self.serverUrl = serverUrl
                     29:         else:
                     30:             self.serverUrl = "http://%s/mpdl/interface/"%serverName
1.2       casties    31:         
                     32:     def getHttpData(self, url, data=None):
                     33:         """returns result from url+data HTTP request"""
                     34:         return documentViewer.getHttpData(url,data,timeout=self.timeout)
                     35:     
                     36:     def getServerData(self, method, data=None):
                     37:         """returns result from text server for method+data"""
                     38:         url = self.serverUrl+method
                     39:         return documentViewer.getHttpData(url,data,timeout=self.timeout)
                     40: 
1.222   ! abukhman   41:     def getSearch(self, pageinfo=None,  docinfo=None,   lemma=None):
1.2       casties    42:         """get search list"""
                     43:         docpath = docinfo['textURLPath'] 
                     44:         url = docinfo['url']
                     45:         pagesize = pageinfo['queryPageSize']
1.222   ! abukhman   46:         pn = pageinfo.get('searchPN',1)
1.2       casties    47:         sn = pageinfo['sn']
                     48:         highlightQuery = pageinfo['highlightQuery']
1.34      abukhman   49:         query =pageinfo['query']
1.2       casties    50:         queryType =pageinfo['queryType']
                     51:         viewMode=  pageinfo['viewMode']
                     52:         tocMode = pageinfo['tocMode']
1.24      abukhman   53:         characterNormalization = pageinfo['characterNormalization']
1.207     abukhman   54:         optionToggle = pageinfo['optionToggle']
1.2       casties    55:         tocPN = pageinfo['tocPN']
                     56:         selfurl = self.absolute_url()
                     57:         
1.207     abukhman   58:         data = self.getServerData("doc-query.xql","document=%s&mode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s&sn=%s&viewMode=%s&characterNormalization=%s&optionToggle=%s&highlightQuery=%s"%(docpath, 'text', queryType, urllib.quote(query), pagesize, pn, sn, viewMode,characterNormalization,optionToggle ,urllib.quote(highlightQuery)))
1.222   ! abukhman   59:                               
1.2       casties    60:         pagexml = data.replace('?document=%s'%str(docpath),'?url=%s'%url)
                     61:         pagedom = Parse(pagexml)
1.222   ! abukhman   62:         
        !            63:         """
        !            64:         pagedivs = pagedom.xpath("//div[@class='queryResultHits']") 
        !            65:         if (pagedivs == pagedom.xpath("//div[@class='queryResultHits']")):
        !            66:             if len(pagedivs)>0:
        !            67:                 docinfo['queryResultHits'] = int(getTextFromNode(pagedivs[0]))
        !            68:                 s = getTextFromNode(pagedivs[0])
        !            69:                 s1 = int(s)/10+1
        !            70:                 try:
        !            71:                     docinfo['queryResultHits'] = int(s1)
        !            72:                     logging.debug("SEARCH ENTRIES: %s"%(s1))
        !            73:                 except:
        !            74:                     docinfo['queryResultHits'] = 0
        !            75:         """
1.2       casties    76:         if (queryType=="fulltext")or(queryType=="xpath")or(queryType=="xquery")or(queryType=="fulltextMorphLemma"):   
                     77:             pagedivs = pagedom.xpath("//div[@class='queryResultPage']")
                     78:             if len(pagedivs)>0:
                     79:                 pagenode=pagedivs[0]
                     80:                 links=pagenode.xpath("//a")
                     81:                 for l in links:
                     82:                     hrefNode = l.getAttributeNodeNS(None, u"href")
                     83:                     if hrefNode:
                     84:                         href = hrefNode.nodeValue
                     85:                         if href.startswith('page-fragment.xql'):
                     86:                             selfurl = self.absolute_url()            
1.213     abukhman   87:                             pagexml=href.replace('mode=text','mode=texttool&viewMode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s&tocMode=%s&searchPN=%s&tocPN=%s&optionToggle=%s&characterNormalization=%s'%(viewMode,queryType,urllib.quote(query),pagesize,pn,tocMode,pn,tocPN,optionToggle,characterNormalization))
1.2       casties    88:                             hrefNode.nodeValue = pagexml.replace('page-fragment.xql','%s'%selfurl)                                           
                     89:                 return serializeNode(pagenode)        
                     90:         if (queryType=="fulltextMorph"):
                     91:             pagedivs = pagedom.xpath("//div[@class='queryResult']")
                     92:             if len(pagedivs)>0:
                     93:                 pagenode=pagedivs[0]
                     94:                 links=pagenode.xpath("//a")
                     95:                 for l in links:
                     96:                     hrefNode = l.getAttributeNodeNS(None, u"href")
                     97:                     if hrefNode:
                     98:                         href = hrefNode.nodeValue
                     99:                         if href.startswith('page-fragment.xql'):
                    100:                             selfurl = self.absolute_url()       
1.213     abukhman  101:                             pagexml=href.replace('mode=text','mode=texttool&viewMode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s&tocMode=%s&searchPN=%s&tocPN=%s&optionToggle=%s&characterNormalization=%s'%(viewMode,queryType,urllib.quote(query),pagesize,pn,tocMode,pn,tocPN,optionToggle,characterNormalization))
1.2       casties   102:                             hrefNode.nodeValue = pagexml.replace('page-fragment.xql','%s'%selfurl)  
                    103:                         if href.startswith('../lt/lemma.xql'):
                    104:                             hrefNode.nodeValue = href.replace('../lt/lemma.xql','%s/template/head_main_lemma_New'%(selfurl))        
                    105:                             l.setAttributeNS(None, 'target', '_blank')
                    106:                             l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=400, scrollbars=1'); return false;")
1.6       abukhman  107:                             l.setAttributeNS(None, 'onClick', 'popupWin.focus();')  
1.2       casties   108:                 pagedivs = pagedom.xpath("//div[@class='queryResultMorphExpansion']")                
                    109:                 return serializeNode(pagenode)        
                    110:         if (queryType=="ftIndex")or(queryType=="ftIndexMorph"):
                    111:             pagedivs= pagedom.xpath("//div[@class='queryResultPage']")
                    112:             if len(pagedivs)>0:
                    113:                 pagenode=pagedivs[0]
                    114:                 links=pagenode.xpath("//a")
                    115:                 for l in links:
                    116:                     hrefNode = l.getAttributeNodeNS(None, u"href")
                    117:                     if hrefNode:
                    118:                         href = hrefNode.nodeValue
1.213     abukhman  119:                         hrefNode.nodeValue=href.replace('mode=text','mode=texttool&viewMode=%s&tocMode=%s&tocPN=%s&pn=%s&optionToggle=%s&characterNormalization=%s'%(viewMode,tocMode,tocPN,pn,optionToggle,characterNormalization))             
1.2       casties   120:                         if href.startswith('../lt/lex.xql'):
                    121:                             hrefNode.nodeValue = href.replace('../lt/lex.xql','%s/template/head_main_voc'%selfurl)         
                    122:                             l.setAttributeNS(None, 'target', '_blank')
                    123:                             l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=400, scrollbars=1'); return false;")
1.6       abukhman  124:                             l.setAttributeNS(None, 'onClick', 'popupWin.focus();')
1.2       casties   125:                         if href.startswith('../lt/lemma.xql'):
                    126:                             hrefNode.nodeValue = href.replace('../lt/lemma.xql','%s/template/head_main_lemma'%selfurl)        
                    127:                             l.setAttributeNS(None, 'target', '_blank')
                    128:                             l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=400, scrollbars=1'); return false;")
1.6       abukhman  129:                             l.setAttributeNS(None, 'onClick', 'popupWin.focus();')
1.2       casties   130:                 return serializeNode(pagenode)      
                    131:         return "no text here"   
                    132:                        
1.222   ! abukhman  133:            
1.89      abukhman  134:     def getGisPlaces(self, docinfo=None, pageinfo=None):
1.58      abukhman  135:         """ Show all Gis Places of whole Page"""
1.100     abukhman  136:         xpath='//place'
1.214     casties   137:         docpath = docinfo.get('textURLPath',None)
                    138:         if not docpath:
                    139:             return None
                    140: 
1.89      abukhman  141:         url = docinfo['url']
                    142:         selfurl = self.absolute_url()
1.93      abukhman  143:         pn = pageinfo['current']
1.127     abukhman  144:         hrefList=[]
1.142     abukhman  145:         myList= ""
1.100     abukhman  146:         text=self.getServerData("xpath.xql", "document=%s&xpath=%s&pn=%s"%(docinfo['textURLPath'],xpath,pn))
                    147:         dom = Parse(text)
1.101     abukhman  148:         result = dom.xpath("//result/resultPage/place")
1.72      abukhman  149:         for l in result:
1.86      abukhman  150:             hrefNode= l.getAttributeNodeNS(None, u"id")
1.108     abukhman  151:             href= hrefNode.nodeValue
1.128     abukhman  152:             hrefList.append(href)
1.145     abukhman  153:             myList = ",".join(hrefList)
                    154:         logging.debug("getGisPlaces :%s"%(myList))                             
1.143     abukhman  155:         return myList
                    156:     
                    157:     def getAllGisPlaces (self, docinfo=None, pageinfo=None):
                    158:         """Show all Gis Places of whole Book """
                    159:         xpath ='//echo:place'
                    160:         docpath =docinfo['textURLPath']
                    161:         url = docinfo['url']
                    162:         selfurl =self.absolute_url()
                    163:         pn =pageinfo['current']
                    164:         hrefList=[]
                    165:         myList=""
                    166:         text=self.getServerData("xpath.xql", "document=%s&xpath=%s"%(docinfo['textURLPath'],xpath))
                    167:         dom =Parse(text)
                    168:         result = dom.xpath("//result/resultPage/place")
1.205     abukhman  169:         
1.143     abukhman  170:         for l in result:
                    171:             hrefNode = l.getAttributeNodeNS(None, u"id")
                    172:             href= hrefNode.nodeValue
                    173:             hrefList.append(href)
1.136     abukhman  174:             myList = ",".join(hrefList)
1.145     abukhman  175:             logging.debug("getALLGisPlaces :%s"%(myList))
                    176:         return myList
1.222   ! abukhman  177:           
1.215     abukhman  178:            
1.222   ! abukhman  179:     def getTextPage(self, docinfo=None, pageinfo=None):
1.2       casties   180:         """returns single page from fulltext"""
                    181:         docpath = docinfo['textURLPath']
                    182:         path = docinfo['textURLPath']
1.222   ! abukhman  183:         url = docinfo.get('url',None)
        !           184:         name = docinfo.get('name',None)
        !           185:         pn =pageinfo['current']
        !           186:         #viewMode= pageinfo['viewMode']
1.196     abukhman  187:         sn = pageinfo['sn']
1.187     abukhman  188:         highlightQuery = pageinfo['highlightQuery']
1.222   ! abukhman  189:         mode = pageinfo ['viewMode']
1.2       casties   190:         tocMode = pageinfo['tocMode']
1.20      abukhman  191:         characterNormalization=pageinfo['characterNormalization']
1.2       casties   192:         tocPN = pageinfo['tocPN']
                    193:         selfurl = self.absolute_url()   
                    194:         if mode == "text_dict":
                    195:             textmode = "textPollux"
                    196:         else:
                    197:             textmode = mode
1.222   ! abukhman  198:         
1.193     abukhman  199:         textParam = "document=%s&mode=%s&pn=%s&characterNormalization=%s"%(docpath,textmode,pn,characterNormalization)
1.190     abukhman  200:         if highlightQuery is not None:
1.196     abukhman  201:             textParam +="&highlightQuery=%s&sn=%s"%(urllib.quote(highlightQuery),sn)           
1.222   ! abukhman  202:         
1.38      abukhman  203:         pagexml = self.getServerData("page-fragment.xql",textParam)
1.222   ! abukhman  204:         dom = Parse(pagexml)
        !           205:         
        !           206:         #original Pages
        !           207:         pagedivs = dom.xpath("//div[@class='pageNumberOrig']")
        !           208:         if pagedivs == dom.xpath("//div[@class='pageNumberOrig']"):
        !           209:             if len(pagedivs)>0:
        !           210:                 docinfo['pageNumberOrig']= getTextFromNode(pagedivs[0])
        !           211:                 logging.debug("ORIGINAL PAGES: %s"%(docinfo['pageNumberOrig']))
        !           212:                           
        !           213:         #figureEntries
        !           214:         pagedivs = dom.xpath("//div[@class='countFigureEntries']")
        !           215:         if pagedivs == dom.xpath("//div[@class='countFigureEntries']"):
        !           216:             if len(pagedivs)>0:
        !           217:                 docinfo['countFigureEntries'] = getTextFromNode(pagedivs[0])
        !           218:                 s = getTextFromNode(pagedivs[0])
        !           219:                 if s=='0':
        !           220:                     try:
        !           221:                         docinfo['countFigureEntries'] = int(s)
        !           222:                         logging.debug("FIGURE ENTRIES: %s"%(s))
        !           223:                     except:
        !           224:                         docinfo['countFigureEntries'] = 0
        !           225:                 else:
        !           226:                     s1 = int(s)/30+1
        !           227:                     try:
        !           228:                         docinfo['countFigureEntries'] = int(s1)
        !           229:                         logging.debug("FIGURE ENTRIES: %s"%(s1))
        !           230:                     except:
        !           231:                         docinfo['countFigureEntries'] = 0      
        !           232:         
        !           233:         #allPlaces
        !           234:         pagedivs = dom.xpath("//div[@class='countPlaces']")
        !           235:         if pagedivs == dom.xpath("//div[@class='countPlaces']"):
        !           236:             if len(pagedivs)>0:
        !           237:                 docinfo['countPlaces']= getTextFromNode(pagedivs[0])
        !           238:                 s = getTextFromNode(pagedivs[0])
        !           239:                 try:
        !           240:                     docinfo['countPlaces'] = int(s)
        !           241:                     logging.debug("PLACES HERE: %s"%(s))
        !           242:                 except:
        !           243:                     docinfo['countPlaces'] = 0
        !           244:         
        !           245:         #tocEntries
        !           246:         pagedivs = dom.xpath("//div[@class='countTocEntries']")
        !           247:         if pagedivs == dom.xpath("//div[@class='countTocEntries']"):
        !           248:             if len(pagedivs)>0:
        !           249:                 docinfo['countTocEntries'] = int(getTextFromNode(pagedivs[0]))
        !           250:                 s = getTextFromNode(pagedivs[0])
        !           251:                 if s=='0':
        !           252:                     try:
        !           253:                         docinfo['countTocEntries'] = int(s)
        !           254:                         logging.debug("TEXT ENTRIES: %s"%(s))
        !           255:                     except:
        !           256:                         docinfo['countTocEntries'] = 0
        !           257:                 else:
        !           258:                     s1 = int(s)/30+1
        !           259:                     try:
        !           260:                         docinfo['countTocEntries'] = int(s1)
        !           261:                         logging.debug("TEXT ENTRIES: %s"%(s1))
        !           262:                     except:
        !           263:                         docinfo['countTocEntries'] = 0
        !           264:         
        !           265:         #numTextPages
        !           266:         pagedivs = dom.xpath("//div[@class='countPages']")
        !           267:         if pagedivs == dom.xpath("//div[@class='countPages']"):
        !           268:             if len(pagedivs)>0:
        !           269:                 docinfo['numPages'] = getTextFromNode(pagedivs[0])
        !           270:                 s = getTextFromNode(pagedivs[0])
        !           271:                 
        !           272:                 try:
        !           273:                     docinfo['numPages'] = int(s)
        !           274:                     logging.debug("PAGE NUMBER: %s"%(s))
        !           275:                 
        !           276:                     np = docinfo['numPages']
        !           277:                     pageinfo['end'] = min(pageinfo['end'], np)
        !           278:                     pageinfo['numgroups'] = int(np / pageinfo['groupsize'])
        !           279:                     if np % pageinfo['groupsize'] > 0:
        !           280:                         pageinfo['numgroups'] += 1                
        !           281:                 except:
        !           282:                     docinfo['numPages'] = 0
        !           283:                   
        !           284:         else:
        !           285:          #no full text -- init to 0
        !           286:             docinfo['pageNumberOrig'] = 0
        !           287:             docinfo['countFigureEntries'] = 0
        !           288:             docinfo['countPlaces'] = 0
        !           289:             docinfo['countTocEntries'] = 0
        !           290:             docinfo['numPages'] = 0
        !           291:         #return docinfo
1.2       casties   292:         
                    293:         # plain text mode
                    294:         if mode == "text":
                    295:             # first div contains text
1.222   ! abukhman  296:             pagedivs = dom.xpath("/div")
1.2       casties   297:             if len(pagedivs) > 0:      
                    298:                 pagenode = pagedivs[0]
                    299:                 links = pagenode.xpath("//a")
                    300:                 for l in links:
                    301:                     hrefNode = l.getAttributeNodeNS(None, u"href")
                    302:                     if hrefNode:
                    303:                         href= hrefNode.nodeValue
                    304:                         if href.startswith('#note-'):
1.27      abukhman  305:                             hrefNode.nodeValue = href.replace('#note-',"?url=%s&viewMode=%s&tocMode=%s&tocPN=%s&pn=%s#note-"%(url,viewMode,tocMode,tocPN,pn))
1.2       casties   306:                 return serializeNode(pagenode)
                    307:         if mode == "xml":
                    308:               # first div contains text
1.222   ! abukhman  309:               pagedivs = dom.xpath("/div")
1.2       casties   310:               if len(pagedivs) > 0:
                    311:                   pagenode = pagedivs[0]
                    312:                   return serializeNode(pagenode)
1.7       abukhman  313:         if mode == "gis":
                    314:               # first div contains text
1.222   ! abukhman  315:               pagedivs = dom.xpath("/div")
1.7       abukhman  316:               if len(pagedivs) > 0:
                    317:                   pagenode = pagedivs[0]
1.28      abukhman  318:                   links =pagenode.xpath("//a")
                    319:                   for l in links:
                    320:                       hrefNode =l.getAttributeNodeNS(None, u"href")
                    321:                       if hrefNode:
                    322:                           href=hrefNode.nodeValue
                    323:                           if href.startswith('http://chinagis.mpiwg-berlin.mpg.de'):
1.75      abukhman  324:                               hrefNode.nodeValue =href.replace('chinagis_REST/REST/db/chgis/mpdl','chinagis/REST/db/mpdl/%s'%name)
1.62      abukhman  325:                               l.setAttributeNS(None, 'target', '_blank') 
1.61      abukhman  326:                   return serializeNode(pagenode)
1.7       abukhman  327:                     
1.2       casties   328:         if mode == "pureXml":
                    329:               # first div contains text
1.222   ! abukhman  330:               pagedivs = dom.xpath("/div")
1.2       casties   331:               if len(pagedivs) > 0:
                    332:                   pagenode = pagedivs[0]
                    333:                   return serializeNode(pagenode)      
                    334:         # text-with-links mode
                    335:         if mode == "text_dict":
                    336:             # first div contains text
1.222   ! abukhman  337:             pagedivs = dom.xpath("/div")
1.2       casties   338:             if len(pagedivs) > 0:
                    339:                 pagenode = pagedivs[0]
                    340:                 # check all a-tags
                    341:                 links = pagenode.xpath("//a")
                    342:                 for l in links:
                    343:                     hrefNode = l.getAttributeNodeNS(None, u"href")
                    344:                     if hrefNode:
                    345:                         # is link with href
                    346:                         href = hrefNode.nodeValue
                    347:                         if href.startswith('lt/lex.xql'):
                    348:                             # is pollux link
                    349:                             selfurl = self.absolute_url()
                    350:                             # change href
                    351:                             hrefNode.nodeValue = href.replace('lt/lex.xql','%s/template/head_main_voc'%selfurl)
                    352:                             # add target
                    353:                             l.setAttributeNS(None, 'target', '_blank')
                    354:                             l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=700, scrollbars=1'); return false;")
1.6       abukhman  355:                             l.setAttributeNS(None, 'onClick', 'popupWin.focus();')      
1.2       casties   356:                         if href.startswith('lt/lemma.xql'):    
                    357:                             selfurl = self.absolute_url()
                    358:                             hrefNode.nodeValue = href.replace('lt/lemma.xql','%s/template/head_main_lemma'%selfurl)
                    359:                             l.setAttributeNS(None, 'target', '_blank')
                    360:                             l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=700, scrollbars=1'); return false;")
1.6       abukhman  361:                             l.setAttributeNS(None, 'onClick', 'popupWin.focus();')   
1.2       casties   362:                         if href.startswith('#note-'):
1.19      abukhman  363:                             hrefNode.nodeValue = href.replace('#note-',"?url=%s&viewMode=%s&tocMode=%s&tocPN=%s&pn=%s#note-"%(url,viewMode,tocMode,tocPN,pn))    
1.2       casties   364:                 return serializeNode(pagenode)
                    365:         return "no text here"
                    366: 
                    367:     def getTranslate(self, query=None, language=None):
                    368:         """translate into another languages"""
1.5       casties   369:         data = self.getServerData("lt/lex.xql","document=&language="+str(language)+"&query="+urllib.quote(query))
1.2       casties   370:         #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lex.xql","document=&language="+str(language)+"&query="+url_quote(str(query)))
                    371:         return data
                    372:     
                    373:     def getLemma(self, lemma=None, language=None):
                    374:         """simular words lemma """
1.5       casties   375:         data = self.getServerData("lt/lemma.xql","document=&language="+str(language)+"&lemma="+urllib.quote(lemma))
1.2       casties   376:         #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lemma.xql","document=&language="+str(language)+"&lemma="+url_quote(str(lemma)))
                    377:         return data
                    378:     
                    379:     def getLemmaNew(self, query=None, language=None):
                    380:         """simular words lemma """
1.5       casties   381:         data = self.getServerData("lt/lemma.xql","document=&language="+str(language)+"&lemma="+urllib.quote(query))
1.2       casties   382:         #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lemma.xql","document=&language="+str(language)+"&lemma="+url_quote(str(query)))
                    383:         return data
1.28      abukhman  384:     
1.206     abukhman  385:     def getQuery (self,  docinfo=None, pageinfo=None, query=None, queryType=None, pn=1, optionToggle=None):
1.222   ! abukhman  386:          #number of
1.2       casties   387:          docpath = docinfo['textURLPath'] 
                    388:          pagesize = pageinfo['queryPageSize']
                    389:          pn = pageinfo['searchPN']
1.34      abukhman  390:          query =pageinfo['query']
1.2       casties   391:          queryType =pageinfo['queryType']
                    392:          tocSearch = 0
                    393:          tocDiv = None
                    394:          
1.32      abukhman  395:          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))
1.2       casties   396:          pagedom = Parse(pagexml)
                    397:          numdivs = pagedom.xpath("//div[@class='queryResultHits']")
                    398:          tocSearch = int(getTextFromNode(numdivs[0]))
1.204     abukhman  399:          logging.debug("documentViewer (gettoc) tocSearch: %s"%(tocSearch))
1.2       casties   400:          tc=int((tocSearch/10)+1)
1.23      abukhman  401:          logging.debug("documentViewer (gettoc) tc: %s"%(tc))
1.2       casties   402:          return tc
1.222   ! abukhman  403:     
1.2       casties   404:     def getToc(self, mode="text", docinfo=None):
                    405:         """loads table of contents and stores in docinfo"""
1.23      abukhman  406:         logging.debug("documentViewer (gettoc) mode: %s"%(mode))
1.2       casties   407:         if mode == "none":
                    408:             return docinfo        
                    409:         if 'tocSize_%s'%mode in docinfo:
                    410:             # cached toc
                    411:             return docinfo
                    412:         
                    413:         docpath = docinfo['textURLPath']
                    414:         # we need to set a result set size
                    415:         pagesize = 1000
                    416:         pn = 1
                    417:         if mode == "text":
                    418:             queryType = "toc"
                    419:         else:
                    420:             queryType = mode
                    421:         # number of entries in toc
                    422:         tocSize = 0
                    423:         tocDiv = None
                    424:         
                    425:         pagexml = self.getServerData("doc-query.xql","document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath,queryType, pagesize, pn))
1.222   ! abukhman  426:         
1.2       casties   427:         # post-processing downloaded xml
                    428:         pagedom = Parse(pagexml)
                    429:         # get number of entries
                    430:         numdivs = pagedom.xpath("//div[@class='queryResultHits']")
                    431:         if len(numdivs) > 0:
                    432:             tocSize = int(getTextFromNode(numdivs[0]))
                    433:         docinfo['tocSize_%s'%mode] = tocSize
                    434:         return docinfo
                    435:     
                    436:     def getTocPage(self, mode="text", pn=1, pageinfo=None, docinfo=None):
                    437:         """returns single page from the table of contents"""
                    438:         # TODO: this should use the cached TOC
                    439:         if mode == "text":
                    440:             queryType = "toc"
                    441:         else:
                    442:             queryType = mode
                    443:         docpath = docinfo['textURLPath']
                    444:         path = docinfo['textURLPath']       
                    445:         pagesize = pageinfo['tocPageSize']
                    446:         pn = pageinfo['tocPN']
                    447:         url = docinfo['url']
                    448:         selfurl = self.absolute_url()  
                    449:         viewMode=  pageinfo['viewMode']
1.26      abukhman  450:         characterNormalization = pageinfo ['characterNormalization']
1.206     abukhman  451:         optionToggle =pageinfo ['optionToggle']
1.2       casties   452:         tocMode = pageinfo['tocMode']
                    453:         tocPN = pageinfo['tocPN']  
                    454:         
1.213     abukhman  455:         data = self.getServerData("doc-query.xql","document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s&characterNormalization=regPlusNorm&optionToggle=1"%(docpath,queryType, pagesize, pn))  
                    456:         page = data.replace('page-fragment.xql?document=%s'%str(path),'%s?url=%s&viewMode=%s&tocMode=%s&tocPN=%s&optionToggle=1'%(selfurl,url, viewMode, tocMode, tocPN))
1.2       casties   457:         text = page.replace('mode=image','mode=texttool')
1.222   ! abukhman  458:         #logging.debug("documentViewer (characterNormalization) characterNormalization: %s"%(characterNormalization))
        !           459:         
1.2       casties   460:         return text
                    461:     
                    462:     def manage_changeMpdlXmlTextServer(self,title="",serverUrl="http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,RESPONSE=None):
                    463:         """change settings"""
                    464:         self.title=title
                    465:         self.timeout = timeout
                    466:         self.serverUrl = serverUrl
                    467:         if RESPONSE is not None:
                    468:             RESPONSE.redirect('manage_main')
                    469:         
                    470: # management methods
                    471: def manage_addMpdlXmlTextServerForm(self):
                    472:     """Form for adding"""
                    473:     pt = PageTemplateFile("zpt/manage_addMpdlXmlTextServer", globals()).__of__(self)
                    474:     return pt()
                    475: 
                    476: def manage_addMpdlXmlTextServer(self,id,title="",serverUrl="http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,RESPONSE=None):
                    477:     """add zogiimage"""
                    478:     newObj = MpdlXmlTextServer(id,title,serverUrl,timeout)
                    479:     self.Destination()._setObject(id, newObj)
                    480:     if RESPONSE is not None:
                    481:         RESPONSE.redirect('manage_main')
                    482: 
                    483: 
1.4       casties   484:     

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>