Annotation of documentViewer/MpdlXmlTextServer.py, revision 1.158
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:
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: pagesize = pageinfo['queryPageSize']
46: pn = pageinfo['searchPN']
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.2 casties 54: tocPN = pageinfo['tocPN']
55: selfurl = self.absolute_url()
56:
1.41 abukhman 57: 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, urllib.quote(query), pagesize, pn, sn, viewMode,urllib.quote(highlightQuery)))
1.2 casties 58: #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)
59:
60: pagexml = data.replace('?document=%s'%str(docpath),'?url=%s'%url)
61: pagedom = Parse(pagexml)
62: if (queryType=="fulltext")or(queryType=="xpath")or(queryType=="xquery")or(queryType=="fulltextMorphLemma"):
63: pagedivs = pagedom.xpath("//div[@class='queryResultPage']")
64: if len(pagedivs)>0:
65: pagenode=pagedivs[0]
66: links=pagenode.xpath("//a")
67: for l in links:
68: hrefNode = l.getAttributeNodeNS(None, u"href")
69: if hrefNode:
70: href = hrefNode.nodeValue
71: if href.startswith('page-fragment.xql'):
72: selfurl = self.absolute_url()
1.30 abukhman 73: 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,urllib.quote(query),pagesize,pn,tocMode,pn,tocPN))
1.2 casties 74: hrefNode.nodeValue = pagexml.replace('page-fragment.xql','%s'%selfurl)
75: return serializeNode(pagenode)
76: if (queryType=="fulltextMorph"):
77: pagedivs = pagedom.xpath("//div[@class='queryResult']")
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.30 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'%(viewMode,queryType,urllib.quote(query),pagesize,pn,tocMode,pn,tocPN))
1.2 casties 88: hrefNode.nodeValue = pagexml.replace('page-fragment.xql','%s'%selfurl)
89: if href.startswith('../lt/lemma.xql'):
90: hrefNode.nodeValue = href.replace('../lt/lemma.xql','%s/template/head_main_lemma_New'%(selfurl))
91: l.setAttributeNS(None, 'target', '_blank')
92: 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 93: l.setAttributeNS(None, 'onClick', 'popupWin.focus();')
1.2 casties 94: pagedivs = pagedom.xpath("//div[@class='queryResultMorphExpansion']")
95: return serializeNode(pagenode)
96: if (queryType=="ftIndex")or(queryType=="ftIndexMorph"):
97: pagedivs= pagedom.xpath("//div[@class='queryResultPage']")
98: if len(pagedivs)>0:
99: pagenode=pagedivs[0]
100: links=pagenode.xpath("//a")
101: for l in links:
102: hrefNode = l.getAttributeNodeNS(None, u"href")
103: if hrefNode:
104: href = hrefNode.nodeValue
1.18 abukhman 105: hrefNode.nodeValue=href.replace('mode=text','mode=texttool&viewMode=%s&tocMode=%s&tocPN=%s&pn=%s'%(viewMode,tocMode,tocPN,pn))
1.2 casties 106: if href.startswith('../lt/lex.xql'):
107: hrefNode.nodeValue = href.replace('../lt/lex.xql','%s/template/head_main_voc'%selfurl)
108: l.setAttributeNS(None, 'target', '_blank')
109: 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 110: l.setAttributeNS(None, 'onClick', 'popupWin.focus();')
1.2 casties 111: if href.startswith('../lt/lemma.xql'):
112: hrefNode.nodeValue = href.replace('../lt/lemma.xql','%s/template/head_main_lemma'%selfurl)
113: l.setAttributeNS(None, 'target', '_blank')
114: 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 115: l.setAttributeNS(None, 'onClick', 'popupWin.focus();')
1.2 casties 116: return serializeNode(pagenode)
117: return "no text here"
118:
1.58 abukhman 119: """def getNumPages(self, docinfo):
120: ""get list of pages from fulltext and put in docinfo""
1.2 casties 121: if 'numPages' in docinfo:
122: # already there
1.43 abukhman 123: return docinfo
1.2 casties 124: xquery = '//pb'
125: text = self.getServerData("xquery.xql","document=%s&xquery=%s"%(docinfo['textURLPath'],xquery))
126: docinfo['numPages'] = text.count("<pb ")
127: return docinfo
1.58 abukhman 128: """
1.43 abukhman 129: def getNumTextPages (self, docinfo):
130: """get list of pages from fulltext (texts without images) and put in docinfo"""
1.56 abukhman 131: if 'numPages' in docinfo:
1.43 abukhman 132: # allredy there
1.56 abukhman 133: return docinfo
134: xpath ='/count(//pb)'
135: text=self.getServerData("xpath.xql", "document=%s&xpath=%s"%(docinfo['textURLPath'], xpath))
136: dom = Parse(text)
137: result= dom.xpath("//result/resultPage")
138: docinfo['numPages']=int(getTextFromNode(result[0]))
1.43 abukhman 139: return docinfo
1.58 abukhman 140:
1.89 abukhman 141: def getGisPlaces(self, docinfo=None, pageinfo=None):
1.58 abukhman 142: """ Show all Gis Places of whole Page"""
1.100 abukhman 143: xpath='//place'
1.89 abukhman 144: docpath = docinfo['textURLPath']
145: url = docinfo['url']
146: selfurl = self.absolute_url()
1.93 abukhman 147: pn = pageinfo['current']
1.127 abukhman 148: hrefList=[]
1.142 abukhman 149: myList= ""
1.100 abukhman 150: text=self.getServerData("xpath.xql", "document=%s&xpath=%s&pn=%s"%(docinfo['textURLPath'],xpath,pn))
151: dom = Parse(text)
1.101 abukhman 152: result = dom.xpath("//result/resultPage/place")
1.72 abukhman 153: for l in result:
1.86 abukhman 154: hrefNode= l.getAttributeNodeNS(None, u"id")
1.108 abukhman 155: href= hrefNode.nodeValue
1.128 abukhman 156: hrefList.append(href)
1.145 abukhman 157: myList = ",".join(hrefList)
158: logging.debug("getGisPlaces :%s"%(myList))
1.143 abukhman 159: return myList
160:
161: def getAllGisPlaces (self, docinfo=None, pageinfo=None):
162: """Show all Gis Places of whole Book """
163: xpath ='//echo:place'
164: docpath =docinfo['textURLPath']
165: url = docinfo['url']
166: selfurl =self.absolute_url()
167: pn =pageinfo['current']
168: hrefList=[]
169: myList=""
170: text=self.getServerData("xpath.xql", "document=%s&xpath=%s"%(docinfo['textURLPath'],xpath))
171: dom =Parse(text)
172: result = dom.xpath("//result/resultPage/place")
173: for l in result:
174: hrefNode = l.getAttributeNodeNS(None, u"id")
175: href= hrefNode.nodeValue
176: hrefList.append(href)
1.136 abukhman 177: myList = ",".join(hrefList)
1.145 abukhman 178: logging.debug("getALLGisPlaces :%s"%(myList))
179: return myList
180:
1.150 abukhman 181: def getOrigPages (self, docinfo=None, pageinfo=None):
1.146 abukhman 182: """Show original page """
1.148 abukhman 183: docpath = docinfo['textURLPath']
1.147 abukhman 184: #url = docinfo['url']
1.146 abukhman 185: selfurl = self.absolute_url()
186: pn =pageinfo['current']
1.158 ! abukhman 187: pagedivs=""
1.146 abukhman 188: viewMode= pageinfo['viewMode']
1.153 abukhman 189: text = self.getServerData("page-fragment.xql","document=%s&mode=%s&pn=%s"%(docinfo['textURLPath'], 'text', pn))
1.146 abukhman 190: dom =Parse(text)
191: pagedivs = dom.xpath("//div[@class='pageNumberOrig']")
192: logging.debug("getOrigPages :%s"%(pagedivs))
1.157 abukhman 193: original =getTextFromNode(pagedivs[0])
194: logging.debug("XXXXXXXXXXXgetOrigPagesXXXXXXXX :%s"%(original))
195: return original
1.58 abukhman 196:
1.25 abukhman 197: def getTextPage(self, mode="text", pn=1, docinfo=None, pageinfo=None, highlightQuery=None,sn=None, viewMode=None, tocMode=None, tocPN=None, characterNormalization=""):
1.2 casties 198: """returns single page from fulltext"""
199: docpath = docinfo['textURLPath']
200: path = docinfo['textURLPath']
201: url = docinfo['url']
1.73 abukhman 202: name = docinfo['name']
1.2 casties 203: viewMode= pageinfo['viewMode']
204: tocMode = pageinfo['tocMode']
1.20 abukhman 205: characterNormalization=pageinfo['characterNormalization']
1.2 casties 206: tocPN = pageinfo['tocPN']
207: selfurl = self.absolute_url()
208: if mode == "text_dict":
209: textmode = "textPollux"
210: else:
211: textmode = mode
1.19 abukhman 212: #logging.debug("documentViewer (characterNormalization) characterNormalization: %s"%(characterNormalization))
1.26 abukhman 213: textParam = "document=%s&mode=%s&pn=%s&characterNormalization=%s"%(docpath,textmode,pn,characterNormalization)
1.2 casties 214: if highlightQuery is not None:
1.40 abukhman 215: textParam +="&highlightQuery=%s&sn=%s"%(urllib.quote(highlightQuery),sn)
1.2 casties 216:
1.38 abukhman 217: pagexml = self.getServerData("page-fragment.xql",textParam)
1.2 casties 218: #pagexml=self.template.fulltextclient.eval("/mpdl/interface/page-fragment.xql", textParam, outputUnicode=False)
219:
1.39 abukhman 220: pagedom = Parse(pagexml)
1.2 casties 221: # plain text mode
222: if mode == "text":
223: # first div contains text
224: pagedivs = pagedom.xpath("/div")
225: if len(pagedivs) > 0:
226: pagenode = pagedivs[0]
227: links = pagenode.xpath("//a")
228: for l in links:
229: hrefNode = l.getAttributeNodeNS(None, u"href")
230: if hrefNode:
231: href= hrefNode.nodeValue
232: if href.startswith('#note-'):
1.27 abukhman 233: hrefNode.nodeValue = href.replace('#note-',"?url=%s&viewMode=%s&tocMode=%s&tocPN=%s&pn=%s#note-"%(url,viewMode,tocMode,tocPN,pn))
1.2 casties 234: return serializeNode(pagenode)
235: if mode == "xml":
236: # first div contains text
237: pagedivs = pagedom.xpath("/div")
238: if len(pagedivs) > 0:
239: pagenode = pagedivs[0]
240: return serializeNode(pagenode)
1.7 abukhman 241: if mode == "gis":
242: # first div contains text
243: pagedivs = pagedom.xpath("/div")
244: if len(pagedivs) > 0:
245: pagenode = pagedivs[0]
1.28 abukhman 246: links =pagenode.xpath("//a")
247: for l in links:
248: hrefNode =l.getAttributeNodeNS(None, u"href")
249: if hrefNode:
250: href=hrefNode.nodeValue
251: if href.startswith('http://chinagis.mpiwg-berlin.mpg.de'):
1.75 abukhman 252: hrefNode.nodeValue =href.replace('chinagis_REST/REST/db/chgis/mpdl','chinagis/REST/db/mpdl/%s'%name)
1.62 abukhman 253: l.setAttributeNS(None, 'target', '_blank')
1.61 abukhman 254: return serializeNode(pagenode)
1.7 abukhman 255:
1.2 casties 256: if mode == "pureXml":
257: # first div contains text
258: pagedivs = pagedom.xpath("/div")
259: if len(pagedivs) > 0:
260: pagenode = pagedivs[0]
261: return serializeNode(pagenode)
262: # text-with-links mode
263: if mode == "text_dict":
264: # first div contains text
265: pagedivs = pagedom.xpath("/div")
266: if len(pagedivs) > 0:
267: pagenode = pagedivs[0]
268: # check all a-tags
269: links = pagenode.xpath("//a")
270: for l in links:
271: hrefNode = l.getAttributeNodeNS(None, u"href")
272: if hrefNode:
273: # is link with href
274: href = hrefNode.nodeValue
275: if href.startswith('lt/lex.xql'):
276: # is pollux link
277: selfurl = self.absolute_url()
278: # change href
279: hrefNode.nodeValue = href.replace('lt/lex.xql','%s/template/head_main_voc'%selfurl)
280: # add target
281: l.setAttributeNS(None, 'target', '_blank')
282: 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 283: l.setAttributeNS(None, 'onClick', 'popupWin.focus();')
1.2 casties 284: if href.startswith('lt/lemma.xql'):
285: selfurl = self.absolute_url()
286: hrefNode.nodeValue = href.replace('lt/lemma.xql','%s/template/head_main_lemma'%selfurl)
287: l.setAttributeNS(None, 'target', '_blank')
288: 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 289: l.setAttributeNS(None, 'onClick', 'popupWin.focus();')
1.2 casties 290: if href.startswith('#note-'):
1.19 abukhman 291: hrefNode.nodeValue = href.replace('#note-',"?url=%s&viewMode=%s&tocMode=%s&tocPN=%s&pn=%s#note-"%(url,viewMode,tocMode,tocPN,pn))
1.2 casties 292: return serializeNode(pagenode)
293: return "no text here"
294:
295: def getTranslate(self, query=None, language=None):
296: """translate into another languages"""
1.5 casties 297: data = self.getServerData("lt/lex.xql","document=&language="+str(language)+"&query="+urllib.quote(query))
1.2 casties 298: #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lex.xql","document=&language="+str(language)+"&query="+url_quote(str(query)))
299: return data
300:
301: def getLemma(self, lemma=None, language=None):
302: """simular words lemma """
1.5 casties 303: data = self.getServerData("lt/lemma.xql","document=&language="+str(language)+"&lemma="+urllib.quote(lemma))
1.2 casties 304: #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lemma.xql","document=&language="+str(language)+"&lemma="+url_quote(str(lemma)))
305: return data
306:
307: def getLemmaNew(self, query=None, language=None):
308: """simular words lemma """
1.5 casties 309: data = self.getServerData("lt/lemma.xql","document=&language="+str(language)+"&lemma="+urllib.quote(query))
1.2 casties 310: #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lemma.xql","document=&language="+str(language)+"&lemma="+url_quote(str(query)))
311: return data
1.28 abukhman 312:
1.2 casties 313: def getQuery (self, docinfo=None, pageinfo=None, query=None, queryType=None, pn=1):
314: """number of"""
315: docpath = docinfo['textURLPath']
316: pagesize = pageinfo['queryPageSize']
317: pn = pageinfo['searchPN']
1.34 abukhman 318: query =pageinfo['query']
1.2 casties 319: queryType =pageinfo['queryType']
320: tocSearch = 0
321: tocDiv = None
322:
1.32 abukhman 323: 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 324: #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)
325: pagedom = Parse(pagexml)
326: numdivs = pagedom.xpath("//div[@class='queryResultHits']")
327: tocSearch = int(getTextFromNode(numdivs[0]))
328: tc=int((tocSearch/10)+1)
1.23 abukhman 329: logging.debug("documentViewer (gettoc) tc: %s"%(tc))
1.2 casties 330: return tc
331:
332: def getToc(self, mode="text", docinfo=None):
333: """loads table of contents and stores in docinfo"""
1.23 abukhman 334: logging.debug("documentViewer (gettoc) mode: %s"%(mode))
1.2 casties 335: if mode == "none":
336: return docinfo
337: if 'tocSize_%s'%mode in docinfo:
338: # cached toc
339: return docinfo
340:
341: docpath = docinfo['textURLPath']
342: # we need to set a result set size
343: pagesize = 1000
344: pn = 1
345: if mode == "text":
346: queryType = "toc"
347: else:
348: queryType = mode
349: # number of entries in toc
350: tocSize = 0
351: tocDiv = None
352:
353: pagexml = self.getServerData("doc-query.xql","document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath,queryType, pagesize, pn))
354: #pagexml=self.template.fulltextclient.eval("/mpdl/interface/doc-query.xql", "document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath,queryType,pagesize,pn), outputUnicode=False)
355: # post-processing downloaded xml
356: pagedom = Parse(pagexml)
357: # get number of entries
358: numdivs = pagedom.xpath("//div[@class='queryResultHits']")
359: if len(numdivs) > 0:
360: tocSize = int(getTextFromNode(numdivs[0]))
361: docinfo['tocSize_%s'%mode] = tocSize
362: return docinfo
363:
364: def getTocPage(self, mode="text", pn=1, pageinfo=None, docinfo=None):
365: """returns single page from the table of contents"""
366: # TODO: this should use the cached TOC
367: if mode == "text":
368: queryType = "toc"
369: else:
370: queryType = mode
371: docpath = docinfo['textURLPath']
372: path = docinfo['textURLPath']
373: pagesize = pageinfo['tocPageSize']
374: pn = pageinfo['tocPN']
375: url = docinfo['url']
376: selfurl = self.absolute_url()
377: viewMode= pageinfo['viewMode']
1.26 abukhman 378: characterNormalization = pageinfo ['characterNormalization']
1.2 casties 379: tocMode = pageinfo['tocMode']
380: tocPN = pageinfo['tocPN']
381:
1.23 abukhman 382: data = self.getServerData("doc-query.xql","document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s&characterNormalization=%s"%(docpath,queryType, pagesize, pn,characterNormalization))
383: page = data.replace('page-fragment.xql?document=%s'%str(path),'%s?url=%s&viewMode=%s&tocMode=%s&tocPN=%s'%(selfurl,url, viewMode, tocMode, tocPN))
1.2 casties 384: text = page.replace('mode=image','mode=texttool')
1.21 abukhman 385: logging.debug("documentViewer (characterNormalization) characterNormalization: %s"%(characterNormalization))
1.19 abukhman 386: #logging.debug("documentViewer (characterNormalization) text: %s"%(text))
1.2 casties 387: return text
388:
389: def manage_changeMpdlXmlTextServer(self,title="",serverUrl="http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,RESPONSE=None):
390: """change settings"""
391: self.title=title
392: self.timeout = timeout
393: self.serverUrl = serverUrl
394: if RESPONSE is not None:
395: RESPONSE.redirect('manage_main')
396:
397: # management methods
398: def manage_addMpdlXmlTextServerForm(self):
399: """Form for adding"""
400: pt = PageTemplateFile("zpt/manage_addMpdlXmlTextServer", globals()).__of__(self)
401: return pt()
402:
403: def manage_addMpdlXmlTextServer(self,id,title="",serverUrl="http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,RESPONSE=None):
404: """add zogiimage"""
405: newObj = MpdlXmlTextServer(id,title,serverUrl,timeout)
406: self.Destination()._setObject(id, newObj)
407: if RESPONSE is not None:
408: RESPONSE.redirect('manage_main')
409:
410:
1.4 casties 411:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>