source: documentViewer/MpdlXmlTextServer.py @ 156:c94e206ba32c

Last change on this file since 156:c94e206ba32c was 156:c94e206ba32c, checked in by abukhman, 14 years ago

characterNormalization

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