Annotation of documentViewer/MpdlXmlTextServer.py, revision 1.7

1.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.5       casties     9: import urllib
1.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:         
1.3       casties    24:     def __init__(self,id,title="",serverUrl="http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/", serverName=None, timeout=40):
1.2       casties    25:         """constructor"""
                     26:         self.id=id
                     27:         self.title=title
                     28:         self.timeout = timeout
1.3       casties    29:         if serverName is None:
                     30:             self.serverUrl = serverUrl
                     31:         else:
                     32:             self.serverUrl = "http://%s/mpdl/interface/"%serverName
1.2       casties    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;")
1.6       abukhman   99:                             l.setAttributeNS(None, 'onClick', 'popupWin.focus();')  
1.2       casties   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;")
1.6       abukhman  116:                             l.setAttributeNS(None, 'onClick', 'popupWin.focus();')
1.2       casties   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;")
1.6       abukhman  121:                             l.setAttributeNS(None, 'onClick', 'popupWin.focus();')
1.2       casties   122:                 return serializeNode(pagenode)      
                    123:         return "no text here"   
                    124:                        
1.4       casties   125:     def getNumPages(self, docinfo):
1.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)
1.7     ! abukhman  179:         if mode == "gis":
        !           180:               # first div contains text
        !           181:               pagedivs = pagedom.xpath("/div")
        !           182:               if len(pagedivs) > 0:
        !           183:                   pagenode = pagedivs[0]
        !           184:                   return serializeNode(pagenode)
        !           185:                     
1.2       casties   186:         if mode == "pureXml":
                    187:               # first div contains text
                    188:               pagedivs = pagedom.xpath("/div")
                    189:               if len(pagedivs) > 0:
                    190:                   pagenode = pagedivs[0]
                    191:                   return serializeNode(pagenode)      
                    192:         # text-with-links mode
                    193:         if mode == "text_dict":
                    194:             # first div contains text
                    195:             pagedivs = pagedom.xpath("/div")
                    196:             if len(pagedivs) > 0:
                    197:                 pagenode = pagedivs[0]
                    198:                 # check all a-tags
                    199:                 links = pagenode.xpath("//a")
                    200:                 for l in links:
                    201:                     hrefNode = l.getAttributeNodeNS(None, u"href")
                    202:                     if hrefNode:
                    203:                         # is link with href
                    204:                         href = hrefNode.nodeValue
                    205:                         if href.startswith('lt/lex.xql'):
                    206:                             # is pollux link
                    207:                             selfurl = self.absolute_url()
                    208:                             # change href
                    209:                             hrefNode.nodeValue = href.replace('lt/lex.xql','%s/template/head_main_voc'%selfurl)
                    210:                             # add target
                    211:                             l.setAttributeNS(None, 'target', '_blank')
                    212:                             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  213:                             l.setAttributeNS(None, 'onClick', 'popupWin.focus();')      
1.2       casties   214:                         if href.startswith('lt/lemma.xql'):    
                    215:                             selfurl = self.absolute_url()
                    216:                             hrefNode.nodeValue = href.replace('lt/lemma.xql','%s/template/head_main_lemma'%selfurl)
                    217:                             l.setAttributeNS(None, 'target', '_blank')
                    218:                             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  219:                             l.setAttributeNS(None, 'onClick', 'popupWin.focus();')   
1.2       casties   220:                         if href.startswith('#note-'):
                    221:                             hrefNode.nodeValue = href.replace('#note-',"?url=%s&viewMode=%s&tocMode=%s&tocPN=%s&pn=%s#note-"%(url,viewMode,tocMode,tocPN,pn))    
                    222:                 return serializeNode(pagenode)
                    223:         return "no text here"
                    224: 
                    225:     def getTranslate(self, query=None, language=None):
                    226:         """translate into another languages"""
1.5       casties   227:         data = self.getServerData("lt/lex.xql","document=&language="+str(language)+"&query="+urllib.quote(query))
1.2       casties   228:         #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lex.xql","document=&language="+str(language)+"&query="+url_quote(str(query)))
                    229:         return data
                    230:     
                    231:     def getLemma(self, lemma=None, language=None):
                    232:         """simular words lemma """
1.5       casties   233:         data = self.getServerData("lt/lemma.xql","document=&language="+str(language)+"&lemma="+urllib.quote(lemma))
1.2       casties   234:         #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lemma.xql","document=&language="+str(language)+"&lemma="+url_quote(str(lemma)))
                    235:         return data
                    236:     
                    237:     def getLemmaNew(self, query=None, language=None):
                    238:         """simular words lemma """
1.5       casties   239:         data = self.getServerData("lt/lemma.xql","document=&language="+str(language)+"&lemma="+urllib.quote(query))
1.2       casties   240:         #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lemma.xql","document=&language="+str(language)+"&lemma="+url_quote(str(query)))
                    241:         return data
                    242: 
                    243:     def getQuery (self,  docinfo=None, pageinfo=None, query=None, queryType=None, pn=1):
                    244:          """number of"""
                    245:          docpath = docinfo['textURLPath'] 
                    246:          pagesize = pageinfo['queryPageSize']
                    247:          pn = pageinfo['searchPN']
                    248:          query =pageinfo['query']
                    249:          queryType =pageinfo['queryType']
                    250:          tocSearch = 0
                    251:          tocDiv = None
                    252:          
                    253:          pagexml = self.getServerData("doc-query.xql","document=%s&mode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath, 'text', queryType, query, pagesize, pn))
                    254:          #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)
                    255:          pagedom = Parse(pagexml)
                    256:          numdivs = pagedom.xpath("//div[@class='queryResultHits']")
                    257:          tocSearch = int(getTextFromNode(numdivs[0]))
                    258:          tc=int((tocSearch/10)+1)
                    259:          logging.debug("documentViewer (gettoc) tc: %s"%(tc))
                    260:          return tc
                    261: 
                    262:     def getToc(self, mode="text", docinfo=None):
                    263:         """loads table of contents and stores in docinfo"""
                    264:         logging.debug("documentViewer (gettoc) mode: %s"%(mode))
                    265:         if mode == "none":
                    266:             return docinfo        
                    267:         if 'tocSize_%s'%mode in docinfo:
                    268:             # cached toc
                    269:             return docinfo
                    270:         
                    271:         docpath = docinfo['textURLPath']
                    272:         # we need to set a result set size
                    273:         pagesize = 1000
                    274:         pn = 1
                    275:         if mode == "text":
                    276:             queryType = "toc"
                    277:         else:
                    278:             queryType = mode
                    279:         # number of entries in toc
                    280:         tocSize = 0
                    281:         tocDiv = None
                    282:         
                    283:         pagexml = self.getServerData("doc-query.xql","document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath,queryType, pagesize, pn))
                    284:         #pagexml=self.template.fulltextclient.eval("/mpdl/interface/doc-query.xql", "document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath,queryType,pagesize,pn), outputUnicode=False)
                    285:         # post-processing downloaded xml
                    286:         pagedom = Parse(pagexml)
                    287:         # get number of entries
                    288:         numdivs = pagedom.xpath("//div[@class='queryResultHits']")
                    289:         if len(numdivs) > 0:
                    290:             tocSize = int(getTextFromNode(numdivs[0]))
                    291:         docinfo['tocSize_%s'%mode] = tocSize
                    292:         return docinfo
                    293:     
                    294:     def getTocPage(self, mode="text", pn=1, pageinfo=None, docinfo=None):
                    295:         """returns single page from the table of contents"""
                    296:         # TODO: this should use the cached TOC
                    297:         if mode == "text":
                    298:             queryType = "toc"
                    299:         else:
                    300:             queryType = mode
                    301:         docpath = docinfo['textURLPath']
                    302:         path = docinfo['textURLPath']       
                    303:         pagesize = pageinfo['tocPageSize']
                    304:         pn = pageinfo['tocPN']
                    305:         url = docinfo['url']
                    306:         selfurl = self.absolute_url()  
                    307:         viewMode=  pageinfo['viewMode']
                    308:         tocMode = pageinfo['tocMode']
                    309:         tocPN = pageinfo['tocPN']  
                    310:         
                    311:         data = self.getServerData("doc-query.xql","document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath,queryType, pagesize, pn))  
                    312: 
                    313:         page = data.replace('page-fragment.xql?document=%s'%str(path),'%s?url=%s&viewMode=%s&tocMode=%s&tocPN=%s'%(selfurl,url, viewMode, tocMode, tocPN))
                    314:         text = page.replace('mode=image','mode=texttool')
                    315:         return text
                    316:     
                    317:     def manage_changeMpdlXmlTextServer(self,title="",serverUrl="http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,RESPONSE=None):
                    318:         """change settings"""
                    319:         self.title=title
                    320:         self.timeout = timeout
                    321:         self.serverUrl = serverUrl
                    322:         if RESPONSE is not None:
                    323:             RESPONSE.redirect('manage_main')
                    324:         
                    325: # management methods
                    326: def manage_addMpdlXmlTextServerForm(self):
                    327:     """Form for adding"""
                    328:     pt = PageTemplateFile("zpt/manage_addMpdlXmlTextServer", globals()).__of__(self)
                    329:     return pt()
                    330: 
                    331: def manage_addMpdlXmlTextServer(self,id,title="",serverUrl="http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,RESPONSE=None):
                    332:     """add zogiimage"""
                    333:     newObj = MpdlXmlTextServer(id,title,serverUrl,timeout)
                    334:     self.Destination()._setObject(id, newObj)
                    335:     if RESPONSE is not None:
                    336:         RESPONSE.redirect('manage_main')
                    337: 
                    338: 
1.4       casties   339:     

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