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