Mercurial > hg > documentViewer
annotate MpdlXmlTextServer.py @ 559:eabfbad6aeb4
"extended" layer for index view and some bugfixes.
author | casties |
---|---|
date | Fri, 28 Sep 2012 18:50:59 +0200 |
parents | c56bc63436de |
children | 9255acc4518d |
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 |
455 | 35 def __init__(self,id,title="",serverUrl="http://mpdl-text.mpiwg-berlin.mpg.de/mpdl/interface/", serverName=None, timeout=40): |
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 |
132 | 40 if serverName is None: |
41 self.serverUrl = serverUrl | |
42 else: | |
43 self.serverUrl = "http://%s/mpdl/interface/"%serverName | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
44 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
45 def getHttpData(self, url, data=None): |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
46 """returns result from url+data HTTP request""" |
458 | 47 return getHttpData(url,data,timeout=self.timeout) |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
48 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
49 def getServerData(self, method, data=None): |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
50 """returns result from text server for method+data""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
51 url = self.serverUrl+method |
458 | 52 return getHttpData(url,data,timeout=self.timeout) |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
53 |
506 | 54 |
559 | 55 def getTextDownloadUrl(self, type='xml', docinfo=None): |
56 """returns a URL to download the current text""" | |
57 docpath = docinfo.get('textURLPath', None) | |
58 if not docpath: | |
59 return None | |
60 | |
61 docpath = docpath.replace('.xml','.'+type) | |
62 url = '%sgetDoc?doc=%s'%(self.serverUrl.replace('interface/',''), docpath) | |
63 return url | |
64 | |
65 | |
506 | 66 def getPlacesOnPage(self, docinfo=None, pn=None): |
67 """Returns list of GIS places of page pn""" | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
68 docpath = docinfo.get('textURLPath',None) |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
69 if not docpath: |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
70 return None |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
71 |
506 | 72 places=[] |
73 text=self.getServerData("xpath.xql", "document=%s&xpath=//place&pn=%s"%(docpath,pn)) | |
455 | 74 dom = ET.fromstring(text) |
506 | 75 result = dom.findall(".//resultPage/place") |
236 | 76 for l in result: |
506 | 77 id = l.get("id") |
78 name = l.text | |
79 place = {'id': id, 'name': name} | |
80 places.append(place) | |
81 | |
82 return places | |
307 | 83 |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
84 |
517 | 85 def getTextInfo(self, mode='', docinfo=None): |
516 | 86 """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
|
87 logging.debug("getTextInfo mode=%s"%mode) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
88 if mode not in ['toc', 'figures', '']: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
89 mode = '' |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
90 # check cached info |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
91 if mode: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
92 # cached toc-request? |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
93 if 'full_%s'%mode 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 |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
96 else: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
97 # no toc-request |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
98 if 'numTextPages' 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 |
516 | 101 docpath = docinfo.get('textURLPath', None) |
102 if docpath is None: | |
103 logging.error("getTextInfo: no textURLPath!") | |
104 return docinfo | |
517 | 105 |
516 | 106 # we need to set a result set size |
107 pagesize = 10000 | |
108 pn = 1 | |
518
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
109 # fetch docinfo |
517 | 110 pagexml = self.getServerData("doc-info.xql","document=%s&info=%s&pageSize=%s&pn=%s"%(docpath,mode,pagesize,pn)) |
516 | 111 dom = ET.fromstring(pagexml) |
112 # all info in tag <document> | |
113 doc = dom.find("document") | |
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 # just checking |
233 if pageinfo['current'] != pn: | |
234 logging.warning("getTextPage: current!=pn!") | |
235 | |
236 # stuff for constructing full urls | |
478 | 237 selfurl = docinfo['viewerUrl'] |
511 | 238 textParams = {'document': docpath, |
239 'pn': pn} | |
240 if 'characterNormalization' in pageinfo: | |
241 textParams['characterNormalization'] = pageinfo['characterNormalization'] | |
455 | 242 |
508 | 243 if not mode: |
244 # default is dict | |
245 mode = 'text' | |
246 | |
247 modes = mode.split(',') | |
248 # check for multiple layers | |
249 if len(modes) > 1: | |
250 logging.debug("getTextPage: more than one mode=%s"%mode) | |
527 | 251 |
508 | 252 # search mode |
253 if 'search' in modes: | |
254 # add highlighting | |
255 highlightQuery = pageinfo.get('highlightQuery', None) | |
511 | 256 if highlightQuery: |
257 textParams['highlightQuery'] = highlightQuery | |
258 textParams['highlightElement'] = pageinfo.get('highlightElement', '') | |
259 textParams['highlightElementPos'] = pageinfo.get('highlightElementPos', '') | |
508 | 260 |
511 | 261 # ignore mode in the following |
508 | 262 modes.remove('search') |
263 | |
551 | 264 # pundit mode |
265 punditMode = False | |
266 if 'pundit' in modes: | |
267 punditMode = True | |
268 # ignore mode in the following | |
269 modes.remove('pundit') | |
270 | |
508 | 271 # other modes don't combine |
272 if 'dict' in modes: | |
473 | 273 # dict is called textPollux in the backend |
508 | 274 textmode = 'textPollux' |
529 | 275 elif 'xml' in modes: |
276 # xml mode | |
277 textmode = 'xml' | |
278 textParams['characterNormalization'] = 'orig' | |
544 | 279 elif 'gis' in modes: |
280 textmode = 'gis' | |
527 | 281 else: |
508 | 282 # text is default mode |
283 textmode = 'text' | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
284 |
511 | 285 textParams['mode'] = textmode |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
286 |
455 | 287 # fetch the page |
511 | 288 pagexml = self.getServerData("page-fragment.xql",urllib.urlencode(textParams)) |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
289 dom = ET.fromstring(pagexml) |
455 | 290 # extract additional info |
291 self.processPageInfo(dom, docinfo, pageinfo) | |
292 # page content is in <div class="pageContent"> | |
293 pagediv = None | |
294 # ElementTree 1.2 in Python 2.6 can't do div[@class='pageContent'] | |
469 | 295 # so we look at the second level divs |
554 | 296 alldivs = dom.findall('div') |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
297 for div in alldivs: |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
298 dc = div.get('class') |
455 | 299 # page content div |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
300 if dc == 'pageContent': |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
301 pagediv = div |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
302 break |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
303 |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
304 # plain text mode |
508 | 305 if textmode == "text": |
478 | 306 # get full url assuming documentViewer is parent |
307 selfurl = self.getLink() | |
473 | 308 if pagediv is not None: |
551 | 309 if punditMode: |
310 pagediv = self.addPunditAttributes(pagediv, pageinfo, docinfo) | |
311 | |
554 | 312 # fix empty div tags |
313 divs = pagediv.findall('.//div') | |
314 for d in divs: | |
315 if len(d) == 0 and not d.text: | |
316 # make empty divs non-empty | |
317 d.text = ' ' | |
318 | |
551 | 319 # check all a-tags |
554 | 320 links = pagediv.findall('.//a') |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
321 for l in links: |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
322 href = l.get('href') |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
323 if href and href.startswith('#note-'): |
478 | 324 href = href.replace('#note-',"%s#note-"%selfurl) |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
325 l.set('href', href) |
455 | 326 |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
327 return serialize(pagediv) |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
328 |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
329 # text-with-links mode |
508 | 330 elif textmode == "textPollux": |
473 | 331 if pagediv is not None: |
478 | 332 viewerurl = docinfo['viewerUrl'] |
333 selfurl = self.getLink() | |
551 | 334 if punditMode: |
335 pagediv = self.addPunditAttributes(pagediv, pageinfo, docinfo) | |
336 | |
554 | 337 # fix empty div tags |
338 divs = pagediv.findall('.//div') | |
339 for d in divs: | |
340 if len(d) == 0 and not d.text: | |
341 # make empty divs non-empty | |
342 d.text = ' ' | |
343 | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
344 # check all a-tags |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
345 links = pagediv.findall(".//a") |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
346 for l in links: |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
347 href = l.get('href') |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
348 |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
349 if href: |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
350 # is link with href |
511 | 351 linkurl = urlparse.urlparse(href) |
352 #logging.debug("getTextPage: linkurl=%s"%repr(linkurl)) | |
353 if linkurl.path.endswith('GetDictionaryEntries'): | |
354 #TODO: replace wordInfo page | |
478 | 355 # is dictionary link - change href (keeping parameters) |
511 | 356 #l.set('href', href.replace('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/lt/wordInfo.xql','%s/template/viewer_wordinfo'%viewerurl)) |
478 | 357 # add target to open new page |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
358 l.set('target', '_blank') |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
359 |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
360 if href.startswith('#note-'): |
478 | 361 # note link |
362 l.set('href', href.replace('#note-',"%s#note-"%selfurl)) | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
363 |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
364 return serialize(pagediv) |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
365 |
455 | 366 # xml mode |
508 | 367 elif textmode == "xml": |
473 | 368 if pagediv is not None: |
455 | 369 return serialize(pagediv) |
370 | |
544 | 371 # pureXml mode WTF? |
508 | 372 elif textmode == "pureXml": |
473 | 373 if pagediv is not None: |
455 | 374 return serialize(pagediv) |
375 | |
376 # gis mode | |
508 | 377 elif textmode == "gis": |
473 | 378 if pagediv is not None: |
554 | 379 # fix empty div tags |
380 divs = pagediv.findall('.//div') | |
381 for d in divs: | |
382 if len(d) == 0 and not d.text: | |
383 # make empty divs non-empty | |
384 d.text = ' ' | |
385 | |
455 | 386 # check all a-tags |
387 links = pagediv.findall(".//a") | |
506 | 388 # add our URL as backlink |
389 selfurl = self.getLink() | |
390 doc = base64.b64encode(selfurl) | |
455 | 391 for l in links: |
392 href = l.get('href') | |
393 if href: | |
506 | 394 if href.startswith('http://mappit.mpiwg-berlin.mpg.de'): |
395 l.set('href', re.sub(r'doc=[\w+/=]+', 'doc=%s'%doc, href)) | |
396 l.set('target', '_blank') | |
455 | 397 |
398 return serialize(pagediv) | |
399 | |
501 | 400 return None |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
401 |
551 | 402 def addPunditAttributes(self, pagediv, pageinfo, docinfo): |
403 """add about attributes for pundit annotation tool""" | |
404 textid = docinfo.get('DRI', "fn=%s"%docinfo.get('documentPath', '???')) | |
405 pn = pageinfo.get('pn', '1') | |
406 # TODO: use pn as well? | |
407 # check all div-tags | |
408 divs = pagediv.findall(".//div") | |
409 for d in divs: | |
410 id = d.get('id') | |
411 if id: | |
412 d.set('about', "http://echo.mpiwg-berlin.mpg.de/%s/pn=%s/#%s"%(textid,pn,id)) | |
413 cls = d.get('class','') | |
414 cls += ' pundit-content' | |
415 d.set('class', cls.strip()) | |
416 | |
417 return pagediv | |
455 | 418 |
509
9d05befdd462
try to get characterNormalization in search result working.
casties
parents:
508
diff
changeset
|
419 def getSearchResults(self, mode, query=None, pageinfo=None, docinfo=None): |
508 | 420 """loads list of search results and stores XML in docinfo""" |
511 | 421 |
508 | 422 logging.debug("getSearchResults mode=%s query=%s"%(mode, query)) |
423 if mode == "none": | |
424 return docinfo | |
425 | |
511 | 426 cachedQuery = docinfo.get('cachedQuery', None) |
427 if cachedQuery is not None: | |
428 # cached search result | |
429 if cachedQuery == '%s_%s'%(mode,query): | |
430 # same query | |
431 return docinfo | |
432 | |
433 else: | |
434 # different query | |
435 del docinfo['resultSize'] | |
436 del docinfo['resultXML'] | |
508 | 437 |
511 | 438 # cache query |
439 docinfo['cachedQuery'] = '%s_%s'%(mode,query) | |
509
9d05befdd462
try to get characterNormalization in search result working.
casties
parents:
508
diff
changeset
|
440 |
508 | 441 # fetch full results |
511 | 442 docpath = docinfo['textURLPath'] |
508 | 443 params = {'document': docpath, |
444 'mode': 'text', | |
445 'queryType': mode, | |
446 'query': query, | |
447 'queryResultPageSize': 1000, | |
509
9d05befdd462
try to get characterNormalization in search result working.
casties
parents:
508
diff
changeset
|
448 'queryResultPN': 1, |
9d05befdd462
try to get characterNormalization in search result working.
casties
parents:
508
diff
changeset
|
449 'characterNormalization': pageinfo.get('characterNormalization', 'reg')} |
508 | 450 pagexml = self.getServerData("doc-query.xql",urllib.urlencode(params)) |
451 #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))) | |
452 dom = ET.fromstring(pagexml) | |
453 # page content is in <div class="queryResultPage"> | |
454 pagediv = None | |
455 # ElementTree 1.2 in Python 2.6 can't do div[@class='queryResultPage'] | |
456 alldivs = dom.findall("div") | |
457 for div in alldivs: | |
458 dc = div.get('class') | |
459 # page content div | |
460 if dc == 'queryResultPage': | |
461 pagediv = div | |
462 | |
463 elif dc == 'queryResultHits': | |
511 | 464 docinfo['resultSize'] = getInt(div.text) |
508 | 465 |
510 | 466 if pagediv is not None: |
508 | 467 # store XML in docinfo |
511 | 468 docinfo['resultXML'] = ET.tostring(pagediv, 'UTF-8') |
508 | 469 |
470 return docinfo | |
471 | |
472 | |
473 def getResultsPage(self, mode="text", query=None, pn=None, start=None, size=None, pageinfo=None, docinfo=None): | |
474 """returns single page from the table of contents""" | |
475 logging.debug("getResultsPage mode=%s, pn=%s"%(mode,pn)) | |
516 | 476 # get (cached) result |
477 self.getSearchResults(mode=mode, query=query, pageinfo=pageinfo, docinfo=docinfo) | |
508 | 478 |
511 | 479 resultxml = docinfo.get('resultXML', None) |
508 | 480 if not resultxml: |
481 logging.error("getResultPage: unable to find resultXML") | |
482 return "Error: no result!" | |
483 | |
484 if size is None: | |
511 | 485 size = pageinfo.get('resultPageSize', 10) |
508 | 486 |
487 if start is None: | |
488 start = (pn - 1) * size | |
489 | |
490 fullresult = ET.fromstring(resultxml) | |
491 | |
510 | 492 if fullresult is not None: |
508 | 493 # paginate |
511 | 494 first = start-1 |
508 | 495 len = size |
496 del fullresult[:first] | |
497 del fullresult[len:] | |
498 tocdivs = fullresult | |
499 | |
500 # check all a-tags | |
501 links = tocdivs.findall(".//a") | |
502 for l in links: | |
503 href = l.get('href') | |
504 if href: | |
511 | 505 # assume all links go to pages |
506 linkUrl = urlparse.urlparse(href) | |
507 linkParams = urlparse.parse_qs(linkUrl.query) | |
508 # take some parameters | |
509 params = {'pn': linkParams['pn'], | |
510 'highlightQuery': linkParams.get('highlightQuery',''), | |
511 'highlightElement': linkParams.get('highlightElement',''), | |
512 'highlightElementPos': linkParams.get('highlightElementPos','') | |
513 } | |
514 url = self.getLink(params=params) | |
515 l.set('href', url) | |
508 | 516 |
517 return serialize(tocdivs) | |
518 | |
519 return "ERROR: no results!" | |
520 | |
521 | |
518
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
522 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
|
523 """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
|
524 logging.debug("getToc mode=%s"%mode) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
525 if mode == 'text': |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
526 queryType = 'toc' |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
527 else: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
528 queryType = mode |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
529 |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
530 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
|
531 # get new toc |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
532 docinfo = self.getTextInfo(queryType, docinfo) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
533 |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
534 return docinfo.get('full_%s'%queryType, []) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
535 |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
536 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
|
537 """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
|
538 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
|
539 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
|
540 if len(fulltoc) < 1: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
541 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
|
542 return "Error: no table of contents!" |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
543 |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
544 if size is None: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
545 size = pageinfo.get('tocPageSize', 30) |
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 if start is None: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
548 start = (pn - 1) * size |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
549 |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
550 # paginate |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
551 first = (start - 1) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
552 last = first + size |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
553 tocs = fulltoc[first:last] |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
554 tp = '<div>' |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
555 for toc in tocs: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
556 pageurl = self.getLink('pn', toc['pn']) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
557 tp += '<div class="tocline">' |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
558 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
|
559 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
|
560 tp += '</div>\n' |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
561 |
519
9a3cc3732194
uses xml from doc-info.xql for table of contents now.
casties
parents:
518
diff
changeset
|
562 tp += '</div>\n' |
518
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
563 |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
517
diff
changeset
|
564 return tp |
519
9a3cc3732194
uses xml from doc-info.xql for table of contents now.
casties
parents:
518
diff
changeset
|
565 |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
566 |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
567 def manage_changeMpdlXmlTextServer(self,title="",serverUrl="http://mpdl-text.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,RESPONSE=None): |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
568 """change settings""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
569 self.title=title |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
570 self.timeout = timeout |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
571 self.serverUrl = serverUrl |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
572 if RESPONSE is not None: |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
573 RESPONSE.redirect('manage_main') |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
574 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
575 # management methods |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
576 def manage_addMpdlXmlTextServerForm(self): |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
577 """Form for adding""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
578 pt = PageTemplateFile("zpt/manage_addMpdlXmlTextServer", globals()).__of__(self) |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
579 return pt() |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
580 |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
581 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
|
582 #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
|
583 """add zogiimage""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
584 newObj = MpdlXmlTextServer(id,title,serverUrl,timeout) |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
585 self.Destination()._setObject(id, newObj) |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
586 if RESPONSE is not None: |
455 | 587 RESPONSE.redirect('manage_main') |
588 | |
589 |