Annotation of documentViewer/MpdlXmlTextServer.py, revision 1.2.2.3

1.2.2.2   casties     1: 
                      2: from OFS.SimpleItem import SimpleItem
                      3: from Products.PageTemplates.PageTemplateFile import PageTemplateFile 
                      4: 
                      5: from Ft.Xml import EMPTY_NAMESPACE, Parse
                      6: 
                      7: import sys
                      8: import logging
1.2.2.3 ! casties     9: import urllib
1.2.2.2   casties    10: import documentViewer
                     11: from documentViewer import getTextFromNode, serializeNode
                     12: 
                     13: 
                     14: class MpdlXmlTextServer(SimpleItem):
                     15:     """TextServer implementation for MPDL-XML eXist server"""
                     16:     meta_type="MPDL-XML TextServer"
                     17: 
                     18:     manage_options=(
                     19:         {'label':'Config','action':'manage_changeMpdlXmlTextServerForm'},
                     20:        )+SimpleItem.manage_options
                     21:     
                     22:     manage_changeMpdlXmlTextServerForm = PageTemplateFile("zpt/manage_changeMpdlXmlTextServer", globals())
                     23:         
                     24:     def __init__(self,id,title="",serverUrl="http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/", serverName=None, timeout=40):
                     25:         """constructor"""
                     26:         self.id=id
                     27:         self.title=title
                     28:         self.timeout = timeout
                     29:         if serverName is None:
                     30:             self.serverUrl = serverUrl
                     31:         else:
                     32:             self.serverUrl = "http://%s/mpdl/interface/"%serverName
                     33: 
                     34:         
                     35:     def getHttpData(self, url, data=None):
                     36:         """returns result from url+data HTTP request"""
                     37:         return documentViewer.getHttpData(url,data,timeout=self.timeout)
                     38: 
                     39:     
                     40:     def getServerData(self, method, data=None):
                     41:         """returns result from text server for method+data"""
                     42:         url = self.serverUrl+method
                     43:         return documentViewer.getHttpData(url,data,timeout=self.timeout)
                     44: 
                     45: 
                     46:     def getSearch(self, pn=1, pageinfo=None,  docinfo=None, query=None, queryType=None, lemma=None):
                     47:         """get search list"""
                     48:         docpath = docinfo['textURLPath'] 
                     49:         url = docinfo['url']
                     50:         logging.debug("documentViewer (gettoc) docpath: %s"%(docpath))
                     51:         logging.debug("documentViewer (gettoc) url: %s"%(url))
                     52:         pagesize = pageinfo['queryPageSize']
                     53:         pn = pageinfo['searchPN']
                     54:         sn = pageinfo['sn']
                     55:         highlightQuery = pageinfo['highlightQuery']
                     56:         query =pageinfo['query']
                     57:         queryType =pageinfo['queryType']
                     58:         viewMode=  pageinfo['viewMode']
                     59:         tocMode = pageinfo['tocMode']
                     60:         tocPN = pageinfo['tocPN']
                     61:         selfurl = self.absolute_url()
                     62:         
                     63:         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))
                     64:         #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)                
                     65:         
                     66:         pagexml = data.replace('?document=%s'%str(docpath),'?url=%s'%url)
                     67:         pagedom = Parse(pagexml)
                     68:         if (queryType=="fulltext")or(queryType=="xpath")or(queryType=="xquery")or(queryType=="fulltextMorphLemma"):   
                     69:             pagedivs = pagedom.xpath("//div[@class='queryResultPage']")
                     70:             if len(pagedivs)>0:
                     71:                 pagenode=pagedivs[0]
                     72:                 links=pagenode.xpath("//a")
                     73:                 for l in links:
                     74:                     hrefNode = l.getAttributeNodeNS(None, u"href")
                     75:                     if hrefNode:
                     76:                         href = hrefNode.nodeValue
                     77:                         if href.startswith('page-fragment.xql'):
                     78:                             selfurl = self.absolute_url()            
                     79:                             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))
                     80:                             hrefNode.nodeValue = pagexml.replace('page-fragment.xql','%s'%selfurl)                                           
                     81:                 return serializeNode(pagenode)        
                     82:         if (queryType=="fulltextMorph"):
                     83:             pagedivs = pagedom.xpath("//div[@class='queryResult']")
                     84:             if len(pagedivs)>0:
                     85:                 pagenode=pagedivs[0]
                     86:                 links=pagenode.xpath("//a")
                     87:                 for l in links:
                     88:                     hrefNode = l.getAttributeNodeNS(None, u"href")
                     89:                     if hrefNode:
                     90:                         href = hrefNode.nodeValue
                     91:                         if href.startswith('page-fragment.xql'):
                     92:                             selfurl = self.absolute_url()       
                     93:                             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))
                     94:                             hrefNode.nodeValue = pagexml.replace('page-fragment.xql','%s'%selfurl)  
                     95:                         if href.startswith('../lt/lemma.xql'):
                     96:                             hrefNode.nodeValue = href.replace('../lt/lemma.xql','%s/template/head_main_lemma_New'%(selfurl))        
                     97:                             l.setAttributeNS(None, 'target', '_blank')
                     98:                             l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=400, scrollbars=1'); return false;")
                     99:                             l.setAttributeNS(None, 'onDblclick', 'popupWin.focus();')  
                    100:                 pagedivs = pagedom.xpath("//div[@class='queryResultMorphExpansion']")                
                    101:                 return serializeNode(pagenode)        
                    102:         if (queryType=="ftIndex")or(queryType=="ftIndexMorph"):
                    103:             pagedivs= pagedom.xpath("//div[@class='queryResultPage']")
                    104:             if len(pagedivs)>0:
                    105:                 pagenode=pagedivs[0]
                    106:                 links=pagenode.xpath("//a")
                    107:                 for l in links:
                    108:                     hrefNode = l.getAttributeNodeNS(None, u"href")
                    109:                     if hrefNode:
                    110:                         href = hrefNode.nodeValue
                    111:                         hrefNode.nodeValue=href.replace('mode=text','mode=texttool&viewMode=%s&tocMode=%s&tocPN=%s&pn=%s'%(viewMode,tocMode,tocPN,pn))             
                    112:                         if href.startswith('../lt/lex.xql'):
                    113:                             hrefNode.nodeValue = href.replace('../lt/lex.xql','%s/template/head_main_voc'%selfurl)         
                    114:                             l.setAttributeNS(None, 'target', '_blank')
                    115:                             l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=400, scrollbars=1'); return false;")
                    116:                             l.setAttributeNS(None, 'onDblclick', 'popupWin.focus();')
                    117:                         if href.startswith('../lt/lemma.xql'):
                    118:                             hrefNode.nodeValue = href.replace('../lt/lemma.xql','%s/template/head_main_lemma'%selfurl)        
                    119:                             l.setAttributeNS(None, 'target', '_blank')
                    120:                             l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=400, scrollbars=1'); return false;")
                    121:                             l.setAttributeNS(None, 'onDblclick', 'popupWin.focus();')
                    122:                 return serializeNode(pagenode)      
                    123:         return "no text here"   
                    124:                        
1.2.2.3 ! casties   125:     def getNumPages(self, docinfo):
1.2.2.2   casties   126:         """get list of pages from fulltext and put in docinfo"""
                    127:         if 'numPages' in docinfo:
                    128:             # already there
                    129:             return docinfo
                    130:         
                    131:         xquery = '//pb'
                    132:         text = self.getServerData("xquery.xql","document=%s&xquery=%s"%(docinfo['textURLPath'],xquery))
                    133:         #text = self.template.fulltextclient.eval("/mpdl/interface/xquery.xql", "document=%s&xquery=%s"%(docinfo['textURLPath'],xquery))
                    134:         docinfo['numPages'] = text.count("<pb ")
                    135:         return docinfo
                    136:        
                    137:     def getTextPage(self, mode="text", pn=1, docinfo=None, pageinfo=None, highlightQuery=None,sn=None, viewMode=None, tocMode=None, tocPN=None):
                    138:         """returns single page from fulltext"""
                    139:         docpath = docinfo['textURLPath']
                    140:         path = docinfo['textURLPath']
                    141:         url = docinfo['url']
                    142:         viewMode= pageinfo['viewMode']
                    143:         tocMode = pageinfo['tocMode']
                    144:         tocPN = pageinfo['tocPN']
                    145:         selfurl = self.absolute_url()   
                    146:         if mode == "text_dict":
                    147:             textmode = "textPollux"
                    148:         else:
                    149:             textmode = mode
                    150:         
                    151:         textParam = "document=%s&mode=%s&pn=%s"%(docpath,textmode,pn)
                    152:         if highlightQuery is not None:
                    153:             textParam +="&highlightQuery=%s&sn=%s"%(highlightQuery,sn)           
                    154:         
                    155:         pagexml = self.getServerData("page-fragment.xql",textParam)
                    156:         #pagexml=self.template.fulltextclient.eval("/mpdl/interface/page-fragment.xql", textParam, outputUnicode=False)
                    157:         
                    158:         pagedom = Parse(pagexml)
                    159:         # plain text mode
                    160:         if mode == "text":
                    161:             # first div contains text
                    162:             pagedivs = pagedom.xpath("/div")
                    163:             if len(pagedivs) > 0:      
                    164:                 pagenode = pagedivs[0]
                    165:                 links = pagenode.xpath("//a")
                    166:                 for l in links:
                    167:                     hrefNode = l.getAttributeNodeNS(None, u"href")
                    168:                     if hrefNode:
                    169:                         href= hrefNode.nodeValue
                    170:                         if href.startswith('#note-'):
                    171:                             hrefNode.nodeValue = href.replace('#note-',"?url=%s&viewMode=%s&tocMode=%s&tocPN=%s&pn=%s#note-"%(url,viewMode,tocMode,tocPN,pn))
                    172:                 return serializeNode(pagenode)
                    173:         if mode == "xml":
                    174:               # first div contains text
                    175:               pagedivs = pagedom.xpath("/div")
                    176:               if len(pagedivs) > 0:
                    177:                   pagenode = pagedivs[0]
                    178:                   return serializeNode(pagenode)
                    179:         if mode == "pureXml":
                    180:               # first div contains text
                    181:               pagedivs = pagedom.xpath("/div")
                    182:               if len(pagedivs) > 0:
                    183:                   pagenode = pagedivs[0]
                    184:                   return serializeNode(pagenode)      
                    185:         # text-with-links mode
                    186:         if mode == "text_dict":
                    187:             # first div contains text
                    188:             pagedivs = pagedom.xpath("/div")
                    189:             if len(pagedivs) > 0:
                    190:                 pagenode = pagedivs[0]
                    191:                 # check all a-tags
                    192:                 links = pagenode.xpath("//a")
                    193:                 for l in links:
                    194:                     hrefNode = l.getAttributeNodeNS(None, u"href")
                    195:                     if hrefNode:
                    196:                         # is link with href
                    197:                         href = hrefNode.nodeValue
                    198:                         if href.startswith('lt/lex.xql'):
                    199:                             # is pollux link
                    200:                             selfurl = self.absolute_url()
                    201:                             # change href
                    202:                             hrefNode.nodeValue = href.replace('lt/lex.xql','%s/template/head_main_voc'%selfurl)
                    203:                             # add target
                    204:                             l.setAttributeNS(None, 'target', '_blank')
                    205:                             l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=700, scrollbars=1'); return false;")
                    206:                             l.setAttributeNS(None, 'onDblclick', 'popupWin.focus();')      
                    207:                         if href.startswith('lt/lemma.xql'):    
                    208:                             selfurl = self.absolute_url()
                    209:                             hrefNode.nodeValue = href.replace('lt/lemma.xql','%s/template/head_main_lemma'%selfurl)
                    210:                             l.setAttributeNS(None, 'target', '_blank')
                    211:                             l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=700, scrollbars=1'); return false;")
                    212:                             l.setAttributeNS(None, 'onDblclick', 'popupWin.focus();')   
                    213:                         if href.startswith('#note-'):
                    214:                             hrefNode.nodeValue = href.replace('#note-',"?url=%s&viewMode=%s&tocMode=%s&tocPN=%s&pn=%s#note-"%(url,viewMode,tocMode,tocPN,pn))    
                    215:                 return serializeNode(pagenode)
                    216:         return "no text here"
                    217: 
                    218:     def getTranslate(self, query=None, language=None):
                    219:         """translate into another languages"""
1.2.2.3 ! casties   220:         data = self.getServerData("lt/lex.xql","document=&language="+str(language)+"&query="+urllib.quote(query))
1.2.2.2   casties   221:         #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lex.xql","document=&language="+str(language)+"&query="+url_quote(str(query)))
                    222:         return data
                    223:     
                    224:     def getLemma(self, lemma=None, language=None):
                    225:         """simular words lemma """
1.2.2.3 ! casties   226:         data = self.getServerData("lt/lemma.xql","document=&language="+str(language)+"&lemma="+urllib.quote(lemma))
1.2.2.2   casties   227:         #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lemma.xql","document=&language="+str(language)+"&lemma="+url_quote(str(lemma)))
                    228:         return data
                    229:     
                    230:     def getLemmaNew(self, query=None, language=None):
                    231:         """simular words lemma """
1.2.2.3 ! casties   232:         data = self.getServerData("lt/lemma.xql","document=&language="+str(language)+"&lemma="+urllib.quote(query))
1.2.2.2   casties   233:         #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lemma.xql","document=&language="+str(language)+"&lemma="+url_quote(str(query)))
                    234:         return data
                    235: 
                    236:     def getQuery (self,  docinfo=None, pageinfo=None, query=None, queryType=None, pn=1):
                    237:          """number of"""
                    238:          docpath = docinfo['textURLPath'] 
                    239:          pagesize = pageinfo['queryPageSize']
                    240:          pn = pageinfo['searchPN']
                    241:          query =pageinfo['query']
                    242:          queryType =pageinfo['queryType']
                    243:          tocSearch = 0
                    244:          tocDiv = None
                    245:          
                    246:          pagexml = self.getServerData("doc-query.xql","document=%s&mode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath, 'text', queryType, query, pagesize, pn))
                    247:          #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)
                    248:          pagedom = Parse(pagexml)
                    249:          numdivs = pagedom.xpath("//div[@class='queryResultHits']")
                    250:          tocSearch = int(getTextFromNode(numdivs[0]))
                    251:          tc=int((tocSearch/10)+1)
                    252:          logging.debug("documentViewer (gettoc) tc: %s"%(tc))
                    253:          return tc
                    254: 
                    255:     def getToc(self, mode="text", docinfo=None):
                    256:         """loads table of contents and stores in docinfo"""
                    257:         logging.debug("documentViewer (gettoc) mode: %s"%(mode))
                    258:         if mode == "none":
                    259:             return docinfo        
                    260:         if 'tocSize_%s'%mode in docinfo:
                    261:             # cached toc
                    262:             return docinfo
                    263:         
                    264:         docpath = docinfo['textURLPath']
                    265:         # we need to set a result set size
                    266:         pagesize = 1000
                    267:         pn = 1
                    268:         if mode == "text":
                    269:             queryType = "toc"
                    270:         else:
                    271:             queryType = mode
                    272:         # number of entries in toc
                    273:         tocSize = 0
                    274:         tocDiv = None
                    275:         
                    276:         pagexml = self.getServerData("doc-query.xql","document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath,queryType, pagesize, pn))
                    277:         #pagexml=self.template.fulltextclient.eval("/mpdl/interface/doc-query.xql", "document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath,queryType,pagesize,pn), outputUnicode=False)
                    278:         # post-processing downloaded xml
                    279:         pagedom = Parse(pagexml)
                    280:         # get number of entries
                    281:         numdivs = pagedom.xpath("//div[@class='queryResultHits']")
                    282:         if len(numdivs) > 0:
                    283:             tocSize = int(getTextFromNode(numdivs[0]))
                    284:         docinfo['tocSize_%s'%mode] = tocSize
                    285:         return docinfo
                    286:     
                    287:     def getTocPage(self, mode="text", pn=1, pageinfo=None, docinfo=None):
                    288:         """returns single page from the table of contents"""
                    289:         # TODO: this should use the cached TOC
                    290:         if mode == "text":
                    291:             queryType = "toc"
                    292:         else:
                    293:             queryType = mode
                    294:         docpath = docinfo['textURLPath']
                    295:         path = docinfo['textURLPath']       
                    296:         pagesize = pageinfo['tocPageSize']
                    297:         pn = pageinfo['tocPN']
                    298:         url = docinfo['url']
                    299:         selfurl = self.absolute_url()  
                    300:         viewMode=  pageinfo['viewMode']
                    301:         tocMode = pageinfo['tocMode']
                    302:         tocPN = pageinfo['tocPN']  
                    303:         
                    304:         data = self.getServerData("doc-query.xql","document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath,queryType, pagesize, pn))  
                    305: 
                    306:         page = data.replace('page-fragment.xql?document=%s'%str(path),'%s?url=%s&viewMode=%s&tocMode=%s&tocPN=%s'%(selfurl,url, viewMode, tocMode, tocPN))
                    307:         text = page.replace('mode=image','mode=texttool')
                    308:         return text
                    309:     
                    310:     def manage_changeMpdlXmlTextServer(self,title="",serverUrl="http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,RESPONSE=None):
                    311:         """change settings"""
                    312:         self.title=title
                    313:         self.timeout = timeout
                    314:         self.serverUrl = serverUrl
                    315:         if RESPONSE is not None:
                    316:             RESPONSE.redirect('manage_main')
                    317:         
                    318: # management methods
                    319: def manage_addMpdlXmlTextServerForm(self):
                    320:     """Form for adding"""
                    321:     pt = PageTemplateFile("zpt/manage_addMpdlXmlTextServer", globals()).__of__(self)
                    322:     return pt()
                    323: 
                    324: def manage_addMpdlXmlTextServer(self,id,title="",serverUrl="http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,RESPONSE=None):
                    325:     """add zogiimage"""
                    326:     newObj = MpdlXmlTextServer(id,title,serverUrl,timeout)
                    327:     self.Destination()._setObject(id, newObj)
                    328:     if RESPONSE is not None:
                    329:         RESPONSE.redirect('manage_main')
                    330: 
                    331: 
1.2.2.3 ! casties   332:     

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