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