File:  [Repository] / documentViewer / MpdlXmlTextServer.py
Revision 1.26: download - view: text, annotated - select for diffs - revision graph
Tue Aug 24 14:04:21 2010 UTC (13 years, 9 months ago) by abukhman
Branches: MAIN
CVS tags: HEAD
characterNormalization

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

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