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, urllib.quote(query), pagesize, pn, sn, viewMode,urllib.quote(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,urllib.quote(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,urllib.quote(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: xquery = '//pb'
127: text = self.getServerData("xquery.xql","document=%s&xquery=%s"%(docinfo['textURLPath'],xquery))
128: docinfo['numPages'] = text.count("<pb ")
129: return docinfo
130: """
131: def getNumTextPages (self, docinfo):
132: """get list of pages from fulltext (texts without images) and put in docinfo"""
133: if 'numPages' in docinfo:
134: # allredy there
135: return docinfo
136: xpath ='/count(//pb)'
137: text=self.getServerData("xpath.xql", "document=%s&xpath=%s"%(docinfo['textURLPath'], xpath))
138: dom = Parse(text)
139: result= dom.xpath("//result/resultPage")
140: docinfo['numPages']=int(getTextFromNode(result[0]))
141: return docinfo
142:
143: def getGisPlaces(self, docinfo=None, pageinfo=None):
144: """ Show all Gis Places of whole Page"""
145: xpath='//place'
146: docpath = docinfo['textURLPath']
147: url = docinfo['url']
148: selfurl = self.absolute_url()
149: pn = pageinfo['current']
150:
151: text=self.getServerData("xpath.xql", "document=%s&xpath=%s&pn=%s"%(docinfo['textURLPath'],xpath,pn))
152: dom = Parse(text)
153: result = dom.xpath("//result/resultPage/place")
154: for l in result:
155: hrefNode= l.getAttributeNodeNS(None, u"id")
156: href= hrefNode.nodeValue
157: href = ",".join(href)
158: logging.debug("getGisPlaces :%s"%(href))
159: #logging.debug("getGisPlaces :%s"%(href))
160: #if href.startswith('id='):
161: #hrefNode.nodeValue = href.replace('id=',"?")
162: #logging.debug("documentViewer getGisPlaces (characterNormalization) hrefNode.nodeValue:%s"%(hrefNode.nodeValue))
163: logging.debug("getGisPlaces :%s"%(href))
164: return href
165:
166: def getTextPage(self, mode="text", pn=1, docinfo=None, pageinfo=None, highlightQuery=None,sn=None, viewMode=None, tocMode=None, tocPN=None, characterNormalization=""):
167: """returns single page from fulltext"""
168: docpath = docinfo['textURLPath']
169: path = docinfo['textURLPath']
170: url = docinfo['url']
171: name = docinfo['name']
172: viewMode= pageinfo['viewMode']
173: tocMode = pageinfo['tocMode']
174: characterNormalization=pageinfo['characterNormalization']
175: tocPN = pageinfo['tocPN']
176: selfurl = self.absolute_url()
177: if mode == "text_dict":
178: textmode = "textPollux"
179: else:
180: textmode = mode
181: #logging.debug("documentViewer (characterNormalization) characterNormalization: %s"%(characterNormalization))
182: textParam = "document=%s&mode=%s&pn=%s&characterNormalization=%s"%(docpath,textmode,pn,characterNormalization)
183: if highlightQuery is not None:
184: textParam +="&highlightQuery=%s&sn=%s"%(urllib.quote(highlightQuery),sn)
185:
186: pagexml = self.getServerData("page-fragment.xql",textParam)
187: #pagexml=self.template.fulltextclient.eval("/mpdl/interface/page-fragment.xql", textParam, outputUnicode=False)
188:
189: pagedom = Parse(pagexml)
190: # plain text mode
191: if mode == "text":
192: # first div contains text
193: pagedivs = pagedom.xpath("/div")
194: if len(pagedivs) > 0:
195: pagenode = pagedivs[0]
196: links = pagenode.xpath("//a")
197: for l in links:
198: hrefNode = l.getAttributeNodeNS(None, u"href")
199: if hrefNode:
200: href= hrefNode.nodeValue
201: if href.startswith('#note-'):
202: hrefNode.nodeValue = href.replace('#note-',"?url=%s&viewMode=%s&tocMode=%s&tocPN=%s&pn=%s#note-"%(url,viewMode,tocMode,tocPN,pn))
203: return serializeNode(pagenode)
204: if mode == "xml":
205: # first div contains text
206: pagedivs = pagedom.xpath("/div")
207: if len(pagedivs) > 0:
208: pagenode = pagedivs[0]
209: return serializeNode(pagenode)
210: if mode == "gis":
211: # first div contains text
212: pagedivs = pagedom.xpath("/div")
213: if len(pagedivs) > 0:
214: pagenode = pagedivs[0]
215: links =pagenode.xpath("//a")
216: for l in links:
217: hrefNode =l.getAttributeNodeNS(None, u"href")
218: if hrefNode:
219: href=hrefNode.nodeValue
220: if href.startswith('http://chinagis.mpiwg-berlin.mpg.de'):
221: hrefNode.nodeValue =href.replace('chinagis_REST/REST/db/chgis/mpdl','chinagis/REST/db/mpdl/%s'%name)
222: l.setAttributeNS(None, 'target', '_blank')
223: return serializeNode(pagenode)
224:
225: if mode == "pureXml":
226: # first div contains text
227: pagedivs = pagedom.xpath("/div")
228: if len(pagedivs) > 0:
229: pagenode = pagedivs[0]
230: return serializeNode(pagenode)
231: # text-with-links mode
232: if mode == "text_dict":
233: # first div contains text
234: pagedivs = pagedom.xpath("/div")
235: if len(pagedivs) > 0:
236: pagenode = pagedivs[0]
237: # check all a-tags
238: links = pagenode.xpath("//a")
239: for l in links:
240: hrefNode = l.getAttributeNodeNS(None, u"href")
241: if hrefNode:
242: # is link with href
243: href = hrefNode.nodeValue
244: if href.startswith('lt/lex.xql'):
245: # is pollux link
246: selfurl = self.absolute_url()
247: # change href
248: hrefNode.nodeValue = href.replace('lt/lex.xql','%s/template/head_main_voc'%selfurl)
249: # add target
250: l.setAttributeNS(None, 'target', '_blank')
251: l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=700, scrollbars=1'); return false;")
252: l.setAttributeNS(None, 'onClick', 'popupWin.focus();')
253: if href.startswith('lt/lemma.xql'):
254: selfurl = self.absolute_url()
255: hrefNode.nodeValue = href.replace('lt/lemma.xql','%s/template/head_main_lemma'%selfurl)
256: l.setAttributeNS(None, 'target', '_blank')
257: l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=700, scrollbars=1'); return false;")
258: l.setAttributeNS(None, 'onClick', 'popupWin.focus();')
259: if href.startswith('#note-'):
260: hrefNode.nodeValue = href.replace('#note-',"?url=%s&viewMode=%s&tocMode=%s&tocPN=%s&pn=%s#note-"%(url,viewMode,tocMode,tocPN,pn))
261: return serializeNode(pagenode)
262: return "no text here"
263:
264: def getTranslate(self, query=None, language=None):
265: """translate into another languages"""
266: data = self.getServerData("lt/lex.xql","document=&language="+str(language)+"&query="+urllib.quote(query))
267: #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lex.xql","document=&language="+str(language)+"&query="+url_quote(str(query)))
268: return data
269:
270: def getLemma(self, lemma=None, language=None):
271: """simular words lemma """
272: data = self.getServerData("lt/lemma.xql","document=&language="+str(language)+"&lemma="+urllib.quote(lemma))
273: #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lemma.xql","document=&language="+str(language)+"&lemma="+url_quote(str(lemma)))
274: return data
275:
276: def getLemmaNew(self, query=None, language=None):
277: """simular words lemma """
278: data = self.getServerData("lt/lemma.xql","document=&language="+str(language)+"&lemma="+urllib.quote(query))
279: #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lemma.xql","document=&language="+str(language)+"&lemma="+url_quote(str(query)))
280: return data
281:
282: def getQuery (self, docinfo=None, pageinfo=None, query=None, queryType=None, pn=1):
283: """number of"""
284: docpath = docinfo['textURLPath']
285: pagesize = pageinfo['queryPageSize']
286: pn = pageinfo['searchPN']
287: query =pageinfo['query']
288: queryType =pageinfo['queryType']
289: tocSearch = 0
290: tocDiv = None
291:
292: 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))
293: #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)
294: pagedom = Parse(pagexml)
295: numdivs = pagedom.xpath("//div[@class='queryResultHits']")
296: tocSearch = int(getTextFromNode(numdivs[0]))
297: tc=int((tocSearch/10)+1)
298: logging.debug("documentViewer (gettoc) tc: %s"%(tc))
299: return tc
300:
301: def getToc(self, mode="text", docinfo=None):
302: """loads table of contents and stores in docinfo"""
303: logging.debug("documentViewer (gettoc) mode: %s"%(mode))
304: if mode == "none":
305: return docinfo
306: if 'tocSize_%s'%mode in docinfo:
307: # cached toc
308: return docinfo
309:
310: docpath = docinfo['textURLPath']
311: # we need to set a result set size
312: pagesize = 1000
313: pn = 1
314: if mode == "text":
315: queryType = "toc"
316: else:
317: queryType = mode
318: # number of entries in toc
319: tocSize = 0
320: tocDiv = None
321:
322: pagexml = self.getServerData("doc-query.xql","document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath,queryType, pagesize, pn))
323: #pagexml=self.template.fulltextclient.eval("/mpdl/interface/doc-query.xql", "document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath,queryType,pagesize,pn), outputUnicode=False)
324: # post-processing downloaded xml
325: pagedom = Parse(pagexml)
326: # get number of entries
327: numdivs = pagedom.xpath("//div[@class='queryResultHits']")
328: if len(numdivs) > 0:
329: tocSize = int(getTextFromNode(numdivs[0]))
330: docinfo['tocSize_%s'%mode] = tocSize
331: return docinfo
332:
333: def getTocPage(self, mode="text", pn=1, pageinfo=None, docinfo=None):
334: """returns single page from the table of contents"""
335: # TODO: this should use the cached TOC
336: if mode == "text":
337: queryType = "toc"
338: else:
339: queryType = mode
340: docpath = docinfo['textURLPath']
341: path = docinfo['textURLPath']
342: pagesize = pageinfo['tocPageSize']
343: pn = pageinfo['tocPN']
344: url = docinfo['url']
345: selfurl = self.absolute_url()
346: viewMode= pageinfo['viewMode']
347: characterNormalization = pageinfo ['characterNormalization']
348: tocMode = pageinfo['tocMode']
349: tocPN = pageinfo['tocPN']
350:
351: data = self.getServerData("doc-query.xql","document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s&characterNormalization=%s"%(docpath,queryType, pagesize, pn,characterNormalization))
352: page = data.replace('page-fragment.xql?document=%s'%str(path),'%s?url=%s&viewMode=%s&tocMode=%s&tocPN=%s'%(selfurl,url, viewMode, tocMode, tocPN))
353: text = page.replace('mode=image','mode=texttool')
354: logging.debug("documentViewer (characterNormalization) characterNormalization: %s"%(characterNormalization))
355: #logging.debug("documentViewer (characterNormalization) text: %s"%(text))
356: return text
357:
358: def manage_changeMpdlXmlTextServer(self,title="",serverUrl="http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,RESPONSE=None):
359: """change settings"""
360: self.title=title
361: self.timeout = timeout
362: self.serverUrl = serverUrl
363: if RESPONSE is not None:
364: RESPONSE.redirect('manage_main')
365:
366: # management methods
367: def manage_addMpdlXmlTextServerForm(self):
368: """Form for adding"""
369: pt = PageTemplateFile("zpt/manage_addMpdlXmlTextServer", globals()).__of__(self)
370: return pt()
371:
372: def manage_addMpdlXmlTextServer(self,id,title="",serverUrl="http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,RESPONSE=None):
373: """add zogiimage"""
374: newObj = MpdlXmlTextServer(id,title,serverUrl,timeout)
375: self.Destination()._setObject(id, newObj)
376: if RESPONSE is not None:
377: RESPONSE.redirect('manage_main')
378:
379:
380:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>