Mercurial > hg > documentViewer
annotate MpdlXmlTextServer.py @ 561:9255acc4518d
CLOSED - # 256: display texts from different backends (sandbox)
https://it-dev.mpiwg-berlin.mpg.de/tracs/mpdl-project-software/ticket/256
author | casties |
---|---|
date | Mon, 01 Oct 2012 18:09:01 +0200 |
parents | eabfbad6aeb4 |
children | be21250420be |
rev | line source |
---|---|
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
1 from OFS.SimpleItem import SimpleItem |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
2 from Products.PageTemplates.PageTemplateFile import PageTemplateFile |
455 | 3 |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
4 import xml.etree.ElementTree as ET |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
5 |
455 | 6 import re |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
7 import logging |
134 | 8 import urllib |
511 | 9 import urlparse |
506 | 10 import base64 |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
11 |
458 | 12 from SrvTxtUtils import getInt, getText, getHttpData |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
13 |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
14 def serialize(node): |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
15 """returns a string containing an XML snippet of node""" |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
16 s = ET.tostring(node, 'UTF-8') |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
17 # snip off XML declaration |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
18 if s.startswith('<?xml'): |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
19 i = s.find('?>') |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
20 return s[i+3:] |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
21 |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
22 return s |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
23 |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
24 |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
25 class MpdlXmlTextServer(SimpleItem): |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
26 """TextServer implementation for MPDL-XML eXist server""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
27 meta_type="MPDL-XML TextServer" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
28 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
29 manage_options=( |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
30 {'label':'Config','action':'manage_changeMpdlXmlTextServerForm'}, |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
31 )+SimpleItem.manage_options |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
32 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
33 manage_changeMpdlXmlTextServerForm = PageTemplateFile("zpt/manage_changeMpdlXmlTextServer", globals()) |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
34 |
561
9255acc4518d
CLOSED - # 256: display texts from different backends (sandbox)
casties
parents:
559
diff
changeset
|
35 def __init__(self,id,title="",serverUrl="http://mpdl-text.mpiwg-berlin.mpg.de/mpdl/interface/", serverName=None, timeout=40, repositoryType='production'): |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
36 """constructor""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
37 self.id=id |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
38 self.title=title |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
39 self.timeout = timeout |
561
9255acc4518d
CLOSED - # 256: display texts from different backends (sandbox)
casties
parents:
559
diff
changeset
|
40 self.repositoryType = repositoryType |
132 | 41 if serverName is None: |
42 self.serverUrl = serverUrl | |
43 else: | |
44 self.serverUrl = "http://%s/mpdl/interface/"%serverName | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
45 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
46 def getHttpData(self, url, data=None): |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
47 """returns result from url+data HTTP request""" |
458 | 48 return getHttpData(url,data,timeout=self.timeout) |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
49 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
50 def getServerData(self, method, data=None): |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
51 """returns result from text server for method+data""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
52 url = self.serverUrl+method |
458 | 53 return getHttpData(url,data,timeout=self.timeout) |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
54 |
506 | 55 |
561
9255acc4518d
CLOSED - # 256: display texts from different backends (sandbox)
casties
parents:
559
diff
changeset
|
56 def getRepositoryType(self): |
9255acc4518d
CLOSED - # 256: display texts from different backends (sandbox)
casties
parents:
559
diff
changeset
|
57 """returns the repository type, e.g. 'production'""" |
9255acc4518d
CLOSED - # 256: display texts from different backends (sandbox)
casties
parents:
559
diff
changeset
|
58 return self.repositoryType |
9255acc4518d
CLOSED - # 256: display texts from different backends (sandbox)
casties
parents:
559
diff
changeset
|
59 |
559 | 60 def getTextDownloadUrl(self, type='xml', docinfo=None): |
61 """returns a URL to download the current text""" | |
62 docpath = docinfo.get('textURLPath', None) | |
63 if not docpath: | |
64 return None | |
65 | |
66 docpath = docpath.replace('.xml','.'+type) | |
67 url = '%sgetDoc?doc=%s'%(self.serverUrl.replace('interface/',''), docpath) | |
68 return url | |
69 | |
70 | |
506 | 71 def getPlacesOnPage(self, docinfo=None, pn=None): |
72 """Returns list of GIS places of page pn""" | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
73 docpath = docinfo.get('textURLPath',None) |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
74 if not docpath: |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
75 return None |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
76 |
506 | 77 places=[] |
78 text=self.getServerData("xpath.xql", "document=%s&xpath=//place&pn=%s"%(docpath,pn)) | |
455 | 79 dom = ET.fromstring(text) |
506 | 80 result = dom.findall(".//resultPage/place") |
236 | 81 for l in result: |
506 | 82 id = l.get("id") |
83 name = l.text | |
84 place = {'id': id, 'name': name} | |
85 places.append(place) | |
86 | |
87 return places | |
307 | 88 |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
89 |
517 | 90 def getTextInfo(self, mode='', docinfo=None): |
516 | 91 """reads document info, including page concordance, from text server""" |
518
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
92 logging.debug("getTextInfo mode=%s"%mode) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
93 if mode not in ['toc', 'figures', '']: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
94 mode = '' |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
95 # check cached info |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
96 if mode: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
97 # cached toc-request? |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
98 if 'full_%s'%mode in docinfo: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
99 return docinfo |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
100 |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
101 else: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
102 # no toc-request |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
103 if 'numTextPages' in docinfo: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
104 return docinfo |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
105 |
516 | 106 docpath = docinfo.get('textURLPath', None) |
107 if docpath is None: | |
108 logging.error("getTextInfo: no textURLPath!") | |
109 return docinfo | |
517 | 110 |
516 | 111 # we need to set a result set size |
112 pagesize = 10000 | |
113 pn = 1 | |
518
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
114 # fetch docinfo |
517 | 115 pagexml = self.getServerData("doc-info.xql","document=%s&info=%s&pageSize=%s&pn=%s"%(docpath,mode,pagesize,pn)) |
516 | 116 dom = ET.fromstring(pagexml) |
117 # all info in tag <document> | |
118 doc = dom.find("document") | |
119 if doc is None: | |
120 logging.error("getTextInfo: unable to find document-tag!") | |
121 else: | |
122 # go through all child elements | |
123 for tag in doc: | |
124 name = tag.tag | |
125 # numTextPages | |
126 if name == 'countPages': | |
127 np = getInt(tag.text) | |
128 if np > 0: | |
129 docinfo['numTextPages'] = np | |
130 | |
131 # numFigureEntries | |
132 elif name == 'countFigureEntries': | |
133 docinfo['numFigureEntries'] = getInt(tag.text) | |
134 | |
135 # numTocEntries | |
136 elif name == 'countTocEntries': | |
137 # WTF: s1 = int(s)/30+1 | |
138 docinfo['numTocEntries'] = getInt(tag.text) | |
139 | |
140 # numPlaces | |
141 elif name == 'countPlaces': | |
142 docinfo['numPlaces'] = getInt(tag.text) | |
143 | |
144 # pageNumbers | |
145 elif name == 'pageNumbers': | |
146 # contains tags with page numbers | |
147 # <pn><n>4</n><no>4</no><non/></pn> | |
148 # n=scan number, no=original page no, non=normalized original page no | |
149 # pageNumbers is a dict indexed by scan number | |
150 pages = {} | |
151 for pn in tag: | |
152 page = {} | |
153 n = 0 | |
154 for p in pn: | |
155 if p.tag == 'n': | |
156 n = getInt(p.text) | |
518
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
157 page['pn'] = n |
516 | 158 elif p.tag == 'no': |
159 page['no'] = p.text | |
160 elif p.tag == 'non': | |
161 page['non'] = p.text | |
162 | |
163 if n > 0: | |
164 pages[n] = page | |
165 | |
166 docinfo['pageNumbers'] = pages | |
517 | 167 #logging.debug("got pageNumbers=%s"%repr(pages)) |
516 | 168 |
517 | 169 # toc |
170 elif name == 'toc': | |
518
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
171 # contains tags with table of contents/figures |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
172 # <toc-entry><page>13</page><level>3</level><content>Chapter I</content><level-string>1.</level-string><real-level>1</real-level></toc-entry> |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
173 tocs = [] |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
174 for te in tag: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
175 toc = {} |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
176 for t in te: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
177 if t.tag == 'page': |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
178 toc['pn'] = getInt(t.text) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
179 elif t.tag == 'level': |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
180 toc['level'] = t.text |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
181 elif t.tag == 'content': |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
182 toc['content'] = t.text |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
183 elif t.tag == 'level-string': |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
184 toc['level-string'] = t.text |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
185 elif t.tag == 'real-level': |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
186 toc['real-level'] = t.text |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
187 |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
188 tocs.append(toc) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
189 |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
190 # save as full_toc/full_figures |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
191 docinfo['full_%s'%mode] = tocs |
517 | 192 |
516 | 193 return docinfo |
194 | |
195 | |
455 | 196 def processPageInfo(self, dom, docinfo, pageinfo): |
197 """processes page info divs from dom and stores in docinfo and pageinfo""" | |
469 | 198 # assume first second level div is pageMeta |
199 alldivs = dom.find("div") | |
473 | 200 |
201 if alldivs is None or alldivs.get('class', '') != 'pageMeta': | |
202 logging.error("processPageInfo: pageMeta div not found!") | |
203 return | |
204 | |
455 | 205 for div in alldivs: |
206 dc = div.get('class') | |
207 | |
208 # pageNumberOrig | |
469 | 209 if dc == 'pageNumberOrig': |
455 | 210 pageinfo['pageNumberOrig'] = div.text |
211 | |
212 # pageNumberOrigNorm | |
213 elif dc == 'pageNumberOrigNorm': | |
214 pageinfo['pageNumberOrigNorm'] = div.text | |
215 | |
474 | 216 # pageHeaderTitle |
217 elif dc == 'pageHeaderTitle': | |
218 pageinfo['pageHeaderTitle'] = div.text | |
517 | 219 |
473 | 220 #logging.debug("processPageInfo: pageinfo=%s"%repr(pageinfo)) |
455 | 221 return |
222 | |
388 | 223 |
471 | 224 def getTextPage(self, mode="text", pn=1, docinfo=None, pageinfo=None): |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
225 """returns single page from fulltext""" |
508 | 226 |
455 | 227 logging.debug("getTextPage mode=%s, pn=%s"%(mode,pn)) |
478 | 228 # check for cached text -- but ideally this shouldn't be called twice |
455 | 229 if pageinfo.has_key('textPage'): |
230 logging.debug("getTextPage: using cached text") | |
231 return pageinfo['textPage'] | |
232 | |
530 | 233 docpath = docinfo.get('textURLPath', None) |
234 if not docpath: | |
235 return None | |
236 | |
455 | 237 # just checking |
238 if pageinfo['current'] != pn: | |
239 logging.warning("getTextPage: current!=pn!") | |
240 | |
241 # stuff for constructing full urls | |
478 | 242 selfurl = docinfo['viewerUrl'] |
511 | 243 textParams = {'document': docpath, |
244 'pn': pn} | |
245 if 'characterNormalization' in pageinfo: | |
246 textParams['characterNormalization'] = pageinfo['characterNormalization'] | |
455 | 247 |
508 | 248 if not mode: |
249 # default is dict | |
250 mode = 'text' | |
251 | |
252 modes = mode.split(',') | |
253 # check for multiple layers | |
254 if len(modes) > 1: | |
255 logging.debug("getTextPage: more than one mode=%s"%mode) | |
527 | 256 |
508 | 257 # search mode |
258 if 'search' in modes: | |
259 # add highlighting | |
260 highlightQuery = pageinfo.get('highlightQuery', None) | |
511 | 261 if highlightQuery: |
262 textParams['highlightQuery'] = highlightQuery | |
263 textParams['highlightElement'] = pageinfo.get('highlightElement', '') | |
264 textParams['highlightElementPos'] = pageinfo.get('highlightElementPos', '') | |
508 | 265 |
511 | 266 # ignore mode in the following |
508 | 267 modes.remove('search') |
268 | |
551 | 269 # pundit mode |
270 punditMode = False | |
271 if 'pundit' in modes: | |
272 punditMode = True | |
273 # ignore mode in the following | |
274 modes.remove('pundit') | |
275 | |
508 | 276 # other modes don't combine |
277 if 'dict' in modes: | |
473 | 278 # dict is called textPollux in the backend |
508 | 279 textmode = 'textPollux' |
529 | 280 elif 'xml' in modes: |
281 # xml mode | |
282 textmode = 'xml' | |
283 textParams['characterNormalization'] = 'orig' | |
544 | 284 elif 'gis' in modes: |
285 textmode = 'gis' | |
527 | 286 else: |
508 | 287 # text is default mode |
288 textmode = 'text' | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
289 |
511 | 290 textParams['mode'] = textmode |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
291 |
455 | 292 # fetch the page |
511 | 293 pagexml = self.getServerData("page-fragment.xql",urllib.urlencode(textParams)) |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
294 dom = ET.fromstring(pagexml) |
455 | 295 # extract additional info |
296 self.processPageInfo(dom, docinfo, pageinfo) | |
297 # page content is in <div class="pageContent"> | |
298 pagediv = None | |
299 # ElementTree 1.2 in Python 2.6 can't do div[@class='pageContent'] | |
469 | 300 # so we look at the second level divs |
554 | 301 alldivs = dom.findall('div') |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
302 for div in alldivs: |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
303 dc = div.get('class') |
455 | 304 # page content div |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
305 if dc == 'pageContent': |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
306 pagediv = div |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
307 break |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
308 |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
309 # plain text mode |
508 | 310 if textmode == "text": |
478 | 311 # get full url assuming documentViewer is parent |
312 selfurl = self.getLink() | |
473 | 313 if pagediv is not None: |
551 | 314 if punditMode: |
315 pagediv = self.addPunditAttributes(pagediv, pageinfo, docinfo) | |
316 | |
554 | 317 # fix empty div tags |
318 divs = pagediv.findall('.//div') | |
319 for d in divs: | |
320 if len(d) == 0 and not d.text: | |
321 # make empty divs non-empty | |
322 d.text = ' ' | |
323 | |
551 | 324 # check all a-tags |
554 | 325 links = pagediv.findall('.//a') |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
326 for l in links: |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
327 href = l.get('href') |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
328 if href and href.startswith('#note-'): |
478 | 329 href = href.replace('#note-',"%s#note-"%selfurl) |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
330 l.set('href', href) |
455 | 331 |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
332 return serialize(pagediv) |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
333 |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
334 # text-with-links mode |
508 | 335 elif textmode == "textPollux": |
473 | 336 if pagediv is not None: |
478 | 337 viewerurl = docinfo['viewerUrl'] |
338 selfurl = self.getLink() | |
551 | 339 if punditMode: |
340 pagediv = self.addPunditAttributes(pagediv, pageinfo, docinfo) | |
341 | |
554 | 342 # fix empty div tags |
343 divs = pagediv.findall('.//div') | |
344 for d in divs: | |
345 if len(d) == 0 and not d.text: | |
346 # make empty divs non-empty | |
347 d.text = ' ' | |
348 | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
349 # check all a-tags |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
350 links = pagediv.findall(".//a") |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
351 for l in links: |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
352 href = l.get('href') |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
353 |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
354 if href: |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
355 # is link with href |
511 | 356 linkurl = urlparse.urlparse(href) |
357 #logging.debug("getTextPage: linkurl=%s"%repr(linkurl)) | |
358 if linkurl.path.endswith('GetDictionaryEntries'): | |
359 #TODO: replace wordInfo page | |
478 | 360 # is dictionary link - change href (keeping parameters) |
511 | 361 #l.set('href', href.replace('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/lt/wordInfo.xql','%s/template/viewer_wordinfo'%viewerurl)) |
478 | 362 # add target to open new page |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
363 l.set('target', '_blank') |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
364 |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
365 if href.startswith('#note-'): |
478 | 366 # note link |
367 l.set('href', href.replace('#note-',"%s#note-"%selfurl)) | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
368 |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
369 return serialize(pagediv) |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
370 |
455 | 371 # xml mode |
508 | 372 elif textmode == "xml": |
473 | 373 if pagediv is not None: |
455 | 374 return serialize(pagediv) |
375 | |
544 | 376 # pureXml mode WTF? |
508 | 377 elif textmode == "pureXml": |
473 | 378 if pagediv is not None: |
455 | 379 return serialize(pagediv) |
380 | |
381 # gis mode | |
508 | 382 elif textmode == "gis": |
473 | 383 if pagediv is not None: |
554 | 384 # fix empty div tags |
385 divs = pagediv.findall('.//div') | |
386 for d in divs: | |
387 if len(d) == 0 and not d.text: | |
388 # make empty divs non-empty | |
389 d.text = ' ' | |
390 | |
455 | 391 # check all a-tags |
392 links = pagediv.findall(".//a") | |
506 | 393 # add our URL as backlink |
394 selfurl = self.getLink() | |
395 doc = base64.b64encode(selfurl) | |
455 | 396 for l in links: |
397 href = l.get('href') | |
398 if href: | |
506 | 399 if href.startswith('http://mappit.mpiwg-berlin.mpg.de'): |
400 l.set('href', re.sub(r'doc=[\w+/=]+', 'doc=%s'%doc, href)) | |
401 l.set('target', '_blank') | |
455 | 402 |
403 return serialize(pagediv) | |
404 | |
501 | 405 return None |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
406 |
551 | 407 def addPunditAttributes(self, pagediv, pageinfo, docinfo): |
408 """add about attributes for pundit annotation tool""" | |
409 textid = docinfo.get('DRI', "fn=%s"%docinfo.get('documentPath', '???')) | |
410 pn = pageinfo.get('pn', '1') | |
411 # TODO: use pn as well? | |
412 # check all div-tags | |
413 divs = pagediv.findall(".//div") | |
414 for d in divs: | |
415 id = d.get('id') | |
416 if id: | |
417 d.set('about', "http://echo.mpiwg-berlin.mpg.de/%s/pn=%s/#%s"%(textid,pn,id)) | |
418 cls = d.get('class','') | |
419 cls += ' pundit-content' | |
420 d.set('class', cls.strip()) | |
421 | |
422 return pagediv | |
455 | 423 |
509
9d05befdd462
try to get characterNormalization in search result working.
casties
parents:
508
diff
changeset
|
424 def getSearchResults(self, mode, query=None, pageinfo=None, docinfo=None): |
508 | 425 """loads list of search results and stores XML in docinfo""" |
511 | 426 |
508 | 427 logging.debug("getSearchResults mode=%s query=%s"%(mode, query)) |
428 if mode == "none": | |
429 return docinfo | |
430 | |
511 | 431 cachedQuery = docinfo.get('cachedQuery', None) |
432 if cachedQuery is not None: | |
433 # cached search result | |
434 if cachedQuery == '%s_%s'%(mode,query): | |
435 # same query | |
436 return docinfo | |
437 | |
438 else: | |
439 # different query | |
440 del docinfo['resultSize'] | |
441 del docinfo['resultXML'] | |
508 | 442 |
511 | 443 # cache query |
444 docinfo['cachedQuery'] = '%s_%s'%(mode,query) | |
509
9d05befdd462
try to get characterNormalization in search result working.
casties
parents:
508
diff
changeset
|
445 |
508 | 446 # fetch full results |
511 | 447 docpath = docinfo['textURLPath'] |
508 | 448 params = {'document': docpath, |
449 'mode': 'text', | |
450 'queryType': mode, | |
451 'query': query, | |
452 'queryResultPageSize': 1000, | |
509
9d05befdd462
try to get characterNormalization in search result working.
casties
parents:
508
diff
changeset
|
453 'queryResultPN': 1, |
9d05befdd462
try to get characterNormalization in search result working.
casties
parents:
508
diff
changeset
|
454 'characterNormalization': pageinfo.get('characterNormalization', 'reg')} |
508 | 455 pagexml = self.getServerData("doc-query.xql",urllib.urlencode(params)) |
456 #pagexml = self.getServerData("doc-query.xql","document=%s&mode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s&s=%s&viewMode=%s&characterNormalization=%s&highlightElementPos=%s&highlightElement=%s&highlightQuery=%s"%(docpath, 'text', queryType, urllib.quote(query), pagesize, pn, s, viewMode,characterNormalization, highlightElementPos, highlightElement, urllib.quote(highlightQuery))) | |
457 dom = ET.fromstring(pagexml) | |
458 # page content is in <div class="queryResultPage"> | |
459 pagediv = None | |
460 # ElementTree 1.2 in Python 2.6 can't do div[@class='queryResultPage'] | |
461 alldivs = dom.findall("div") | |
462 for div in alldivs: | |
463 dc = div.get('class') | |
464 # page content div | |
465 if dc == 'queryResultPage': | |
466 pagediv = div | |
467 | |
468 elif dc == 'queryResultHits': | |
511 | 469 docinfo['resultSize'] = getInt(div.text) |
508 | 470 |
510 | 471 if pagediv is not None: |
508 | 472 # store XML in docinfo |
511 | 473 docinfo['resultXML'] = ET.tostring(pagediv, 'UTF-8') |
508 | 474 |
475 return docinfo | |
476 | |
477 | |
478 def getResultsPage(self, mode="text", query=None, pn=None, start=None, size=None, pageinfo=None, docinfo=None): | |
479 """returns single page from the table of contents""" | |
480 logging.debug("getResultsPage mode=%s, pn=%s"%(mode,pn)) | |
516 | 481 # get (cached) result |
482 self.getSearchResults(mode=mode, query=query, pageinfo=pageinfo, docinfo=docinfo) | |
508 | 483 |
511 | 484 resultxml = docinfo.get('resultXML', None) |
508 | 485 if not resultxml: |
486 logging.error("getResultPage: unable to find resultXML") | |
487 return "Error: no result!" | |
488 | |
489 if size is None: | |
511 | 490 size = pageinfo.get('resultPageSize', 10) |
508 | 491 |
492 if start is None: | |
493 start = (pn - 1) * size | |
494 | |
495 fullresult = ET.fromstring(resultxml) | |
496 | |
510 | 497 if fullresult is not None: |
508 | 498 # paginate |
511 | 499 first = start-1 |
508 | 500 len = size |
501 del fullresult[:first] | |
502 del fullresult[len:] | |
503 tocdivs = fullresult | |
504 | |
505 # check all a-tags | |
506 links = tocdivs.findall(".//a") | |
507 for l in links: | |
508 href = l.get('href') | |
509 if href: | |
511 | 510 # assume all links go to pages |
511 linkUrl = urlparse.urlparse(href) | |
512 linkParams = urlparse.parse_qs(linkUrl.query) | |
513 # take some parameters | |
514 params = {'pn': linkParams['pn'], | |
515 'highlightQuery': linkParams.get('highlightQuery',''), | |
516 'highlightElement': linkParams.get('highlightElement',''), | |
517 'highlightElementPos': linkParams.get('highlightElementPos','') | |
518 } | |
519 url = self.getLink(params=params) | |
520 l.set('href', url) | |
508 | 521 |
522 return serialize(tocdivs) | |
523 | |
524 return "ERROR: no results!" | |
525 | |
526 | |
518
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
527 def getToc(self, mode='text', docinfo=None): |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
528 """returns list of table of contents from docinfo""" |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
529 logging.debug("getToc mode=%s"%mode) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
530 if mode == 'text': |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
531 queryType = 'toc' |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
532 else: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
533 queryType = mode |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
534 |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
535 if not 'full_%s'%queryType in docinfo: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
536 # get new toc |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
537 docinfo = self.getTextInfo(queryType, docinfo) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
538 |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
539 return docinfo.get('full_%s'%queryType, []) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
540 |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
541 def getTocPage(self, mode='text', pn=None, start=None, size=None, pageinfo=None, docinfo=None): |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
542 """returns single page from the table of contents""" |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
543 logging.debug("getTocPage mode=%s, pn=%s start=%s size=%s"%(mode,repr(pn),repr(start),repr(size))) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
544 fulltoc = self.getToc(mode=mode, docinfo=docinfo) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
545 if len(fulltoc) < 1: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
546 logging.error("getTocPage: unable to find toc!") |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
547 return "Error: no table of contents!" |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
548 |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
549 if size is None: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
550 size = pageinfo.get('tocPageSize', 30) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
551 |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
552 if start is None: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
553 start = (pn - 1) * size |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
554 |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
555 # paginate |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
556 first = (start - 1) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
557 last = first + size |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
558 tocs = fulltoc[first:last] |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
559 tp = '<div>' |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
560 for toc in tocs: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
561 pageurl = self.getLink('pn', toc['pn']) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
562 tp += '<div class="tocline">' |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
563 tp += '<div class="toc name">[%s %s]</div>'%(toc['level-string'], toc['content']) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
564 tp += '<div class="toc float right page"><a href="%s">Page: %s</a></div>'%(pageurl, toc['pn']) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
565 tp += '</div>\n' |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
566 |
519
9a3cc3732194
uses xml from doc-info.xql for table of contents now.
casties
parents:
518
diff
changeset
|
567 tp += '</div>\n' |
518
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
568 |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
569 return tp |
519
9a3cc3732194
uses xml from doc-info.xql for table of contents now.
casties
parents:
518
diff
changeset
|
570 |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
571 |
561
9255acc4518d
CLOSED - # 256: display texts from different backends (sandbox)
casties
parents:
559
diff
changeset
|
572 def manage_changeMpdlXmlTextServer(self,title="",serverUrl="http://mpdl-text.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,repositoryType=None,RESPONSE=None): |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
573 """change settings""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
574 self.title=title |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
575 self.timeout = timeout |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
576 self.serverUrl = serverUrl |
561
9255acc4518d
CLOSED - # 256: display texts from different backends (sandbox)
casties
parents:
559
diff
changeset
|
577 if repositoryType: |
9255acc4518d
CLOSED - # 256: display texts from different backends (sandbox)
casties
parents:
559
diff
changeset
|
578 self.repositoryType = repositoryType |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
579 if RESPONSE is not None: |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
580 RESPONSE.redirect('manage_main') |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
581 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
582 # management methods |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
583 def manage_addMpdlXmlTextServerForm(self): |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
584 """Form for adding""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
585 pt = PageTemplateFile("zpt/manage_addMpdlXmlTextServer", globals()).__of__(self) |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
586 return pt() |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
587 |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
588 def manage_addMpdlXmlTextServer(self,id,title="",serverUrl="http://mpdl-text.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,RESPONSE=None): |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
589 #def manage_addMpdlXmlTextServer(self,id,title="",serverUrl="http://mpdl-text.mpiwg-berlin.mpg.de:30030/mpdl/interface/",timeout=40,RESPONSE=None): |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
590 """add zogiimage""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
591 newObj = MpdlXmlTextServer(id,title,serverUrl,timeout) |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
592 self.Destination()._setObject(id, newObj) |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
593 if RESPONSE is not None: |
455 | 594 RESPONSE.redirect('manage_main') |
595 | |
596 |