Mercurial > hg > documentViewer
annotate MpdlXmlTextServer.py @ 509:9d05befdd462 elementtree
try to get characterNormalization in search result working.
author | casties |
---|---|
date | Mon, 27 Feb 2012 21:57:18 +0100 |
parents | d5a47f82e755 |
children | 4fb35343d2e7 |
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 |
506 | 9 import base64 |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
10 |
458 | 11 from SrvTxtUtils import getInt, getText, getHttpData |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
12 |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
13 def serialize(node): |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
14 """returns a string containing an XML snippet of node""" |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
15 s = ET.tostring(node, 'UTF-8') |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
16 # snip off XML declaration |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
17 if s.startswith('<?xml'): |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
18 i = s.find('?>') |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
19 return s[i+3:] |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
20 |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
21 return s |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
22 |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
23 |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
24 class MpdlXmlTextServer(SimpleItem): |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
25 """TextServer implementation for MPDL-XML eXist server""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
26 meta_type="MPDL-XML TextServer" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
27 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
28 manage_options=( |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
29 {'label':'Config','action':'manage_changeMpdlXmlTextServerForm'}, |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
30 )+SimpleItem.manage_options |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
31 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
32 manage_changeMpdlXmlTextServerForm = PageTemplateFile("zpt/manage_changeMpdlXmlTextServer", globals()) |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
33 |
455 | 34 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
|
35 """constructor""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
36 self.id=id |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
37 self.title=title |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
38 self.timeout = timeout |
132 | 39 if serverName is None: |
40 self.serverUrl = serverUrl | |
41 else: | |
42 self.serverUrl = "http://%s/mpdl/interface/"%serverName | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
43 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
44 def getHttpData(self, url, data=None): |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
45 """returns result from url+data HTTP request""" |
458 | 46 return getHttpData(url,data,timeout=self.timeout) |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
47 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
48 def getServerData(self, method, data=None): |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
49 """returns result from text server for method+data""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
50 url = self.serverUrl+method |
458 | 51 return getHttpData(url,data,timeout=self.timeout) |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
52 |
506 | 53 |
54 def getPlacesOnPage(self, docinfo=None, pn=None): | |
55 """Returns list of GIS places of page pn""" | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
56 docpath = docinfo.get('textURLPath',None) |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
57 if not docpath: |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
58 return None |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
59 |
506 | 60 places=[] |
61 text=self.getServerData("xpath.xql", "document=%s&xpath=//place&pn=%s"%(docpath,pn)) | |
455 | 62 dom = ET.fromstring(text) |
506 | 63 result = dom.findall(".//resultPage/place") |
236 | 64 for l in result: |
506 | 65 id = l.get("id") |
66 name = l.text | |
67 place = {'id': id, 'name': name} | |
68 places.append(place) | |
69 | |
70 return places | |
307 | 71 |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
72 |
455 | 73 def processPageInfo(self, dom, docinfo, pageinfo): |
74 """processes page info divs from dom and stores in docinfo and pageinfo""" | |
469 | 75 # assume first second level div is pageMeta |
76 alldivs = dom.find("div") | |
473 | 77 |
78 if alldivs is None or alldivs.get('class', '') != 'pageMeta': | |
79 logging.error("processPageInfo: pageMeta div not found!") | |
80 return | |
81 | |
455 | 82 for div in alldivs: |
83 dc = div.get('class') | |
84 | |
85 # pageNumberOrig | |
469 | 86 if dc == 'pageNumberOrig': |
455 | 87 pageinfo['pageNumberOrig'] = div.text |
88 | |
89 # pageNumberOrigNorm | |
90 elif dc == 'pageNumberOrigNorm': | |
91 pageinfo['pageNumberOrigNorm'] = div.text | |
92 | |
474 | 93 # pageHeaderTitle |
94 elif dc == 'pageHeaderTitle': | |
95 pageinfo['pageHeaderTitle'] = div.text | |
96 | |
97 # numFigureEntries | |
455 | 98 elif dc == 'countFigureEntries': |
469 | 99 docinfo['numFigureEntries'] = getInt(div.text) |
455 | 100 |
474 | 101 # numTocEntries |
455 | 102 elif dc == 'countTocEntries': |
103 # WTF: s1 = int(s)/30+1 | |
469 | 104 docinfo['numTocEntries'] = getInt(div.text) |
105 | |
475 | 106 # numPlaces |
107 elif dc == 'countPlaces': | |
108 docinfo['numPlaces'] = getInt(div.text) | |
109 | |
455 | 110 # numTextPages |
111 elif dc == 'countPages': | |
458 | 112 np = getInt(div.text) |
455 | 113 if np > 0: |
114 docinfo['numTextPages'] = np | |
115 if docinfo.get('numPages', 0) == 0: | |
465 | 116 # seems to be text-only - update page count |
117 docinfo['numPages'] = np | |
477 | 118 #pageinfo['end'] = min(pageinfo['end'], np) |
455 | 119 pageinfo['numgroups'] = int(np / pageinfo['groupsize']) |
120 if np % pageinfo['groupsize'] > 0: | |
121 pageinfo['numgroups'] += 1 | |
473 | 122 |
123 #logging.debug("processPageInfo: pageinfo=%s"%repr(pageinfo)) | |
455 | 124 return |
125 | |
388 | 126 |
471 | 127 def getTextPage(self, mode="text", pn=1, docinfo=None, pageinfo=None): |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
128 """returns single page from fulltext""" |
508 | 129 |
455 | 130 logging.debug("getTextPage mode=%s, pn=%s"%(mode,pn)) |
478 | 131 # check for cached text -- but ideally this shouldn't be called twice |
455 | 132 if pageinfo.has_key('textPage'): |
133 logging.debug("getTextPage: using cached text") | |
134 return pageinfo['textPage'] | |
135 | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
136 docpath = docinfo['textURLPath'] |
455 | 137 # just checking |
138 if pageinfo['current'] != pn: | |
139 logging.warning("getTextPage: current!=pn!") | |
140 | |
141 # stuff for constructing full urls | |
142 characterNormalization = pageinfo.get('characterNormalization', None) | |
508 | 143 moreTextParam = '' |
478 | 144 selfurl = docinfo['viewerUrl'] |
455 | 145 |
508 | 146 if not mode: |
147 # default is dict | |
148 mode = 'text' | |
149 | |
150 modes = mode.split(',') | |
151 # check for multiple layers | |
152 if len(modes) > 1: | |
153 logging.debug("getTextPage: more than one mode=%s"%mode) | |
154 | |
155 # search mode | |
156 if 'search' in modes: | |
157 # add highlighting | |
158 highlightQuery = pageinfo.get('highlightQuery', None) | |
159 sn = pageinfo.get('sn', None) | |
160 if highlightQuery and sn: | |
161 moreTextParam +="&highlightQuery=%s&sn=%s"%(urllib.quote(highlightQuery),sn) | |
162 | |
163 # remove mode | |
164 modes.remove('search') | |
165 | |
166 # other modes don't combine | |
167 if 'dict' in modes: | |
473 | 168 # dict is called textPollux in the backend |
508 | 169 textmode = 'textPollux' |
170 elif len(modes) == 0: | |
171 # text is default mode | |
172 textmode = 'text' | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
173 else: |
508 | 174 # just take first mode |
175 textmode = modes[0] | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
176 |
373 | 177 textParam = "document=%s&mode=%s&pn=%s&characterNormalization=%s"%(docpath,textmode,pn,characterNormalization) |
508 | 178 textParam += moreTextParam |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
179 |
455 | 180 # fetch the page |
185 | 181 pagexml = self.getServerData("page-fragment.xql",textParam) |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
182 dom = ET.fromstring(pagexml) |
455 | 183 # extract additional info |
184 self.processPageInfo(dom, docinfo, pageinfo) | |
185 # page content is in <div class="pageContent"> | |
186 pagediv = None | |
187 # ElementTree 1.2 in Python 2.6 can't do div[@class='pageContent'] | |
469 | 188 # so we look at the second level divs |
189 alldivs = dom.findall("div") | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
190 for div in alldivs: |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
191 dc = div.get('class') |
455 | 192 # page content div |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
193 if dc == 'pageContent': |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
194 pagediv = div |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
195 break |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
196 |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
197 # plain text mode |
508 | 198 if textmode == "text": |
478 | 199 # get full url assuming documentViewer is parent |
200 selfurl = self.getLink() | |
473 | 201 if pagediv is not None: |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
202 links = pagediv.findall(".//a") |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
203 for l in links: |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
204 href = l.get('href') |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
205 if href and href.startswith('#note-'): |
478 | 206 href = href.replace('#note-',"%s#note-"%selfurl) |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
207 l.set('href', href) |
455 | 208 |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
209 return serialize(pagediv) |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
210 |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
211 # text-with-links mode |
508 | 212 elif textmode == "textPollux": |
473 | 213 if pagediv is not None: |
478 | 214 viewerurl = docinfo['viewerUrl'] |
215 selfurl = self.getLink() | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
216 # check all a-tags |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
217 links = pagediv.findall(".//a") |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
218 for l in links: |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
219 href = l.get('href') |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
220 |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
221 if href: |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
222 # is link with href |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
223 if href.startswith('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/lt/wordInfo.xql'): |
478 | 224 # is dictionary link - change href (keeping parameters) |
225 l.set('href', href.replace('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/lt/wordInfo.xql','%s/template/viewer_wordinfo'%viewerurl)) | |
226 # add target to open new page | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
227 l.set('target', '_blank') |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
228 |
478 | 229 # TODO: is this needed? |
230 if href.startswith('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/lt/lemma.xql'): | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
231 selfurl = self.absolute_url() |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
232 l.set('href', href.replace('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/lt/lemma.xql','%s/head_main_lemma'%selfurl)) |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
233 l.set('target', '_blank') |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
234 l.set('onclick',"popupWin = window.open(this.href, 'InfoWindow', 'menubar=no, location,width=500,height=600,top=180, left=700, toolbar=no, scrollbars=1'); return false;") |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
235 l.set('ondblclick', 'popupWin.focus();') |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
236 |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
237 if href.startswith('#note-'): |
478 | 238 # note link |
239 l.set('href', href.replace('#note-',"%s#note-"%selfurl)) | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
240 |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
241 return serialize(pagediv) |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
242 |
455 | 243 # xml mode |
508 | 244 elif textmode == "xml": |
473 | 245 if pagediv is not None: |
455 | 246 return serialize(pagediv) |
247 | |
248 # pureXml mode | |
508 | 249 elif textmode == "pureXml": |
473 | 250 if pagediv is not None: |
455 | 251 return serialize(pagediv) |
252 | |
253 # gis mode | |
508 | 254 elif textmode == "gis": |
473 | 255 if pagediv is not None: |
455 | 256 # check all a-tags |
257 links = pagediv.findall(".//a") | |
506 | 258 # add our URL as backlink |
259 selfurl = self.getLink() | |
260 doc = base64.b64encode(selfurl) | |
455 | 261 for l in links: |
262 href = l.get('href') | |
263 if href: | |
506 | 264 if href.startswith('http://mappit.mpiwg-berlin.mpg.de'): |
265 l.set('href', re.sub(r'doc=[\w+/=]+', 'doc=%s'%doc, href)) | |
266 l.set('target', '_blank') | |
455 | 267 |
268 return serialize(pagediv) | |
269 | |
501 | 270 return None |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
271 |
455 | 272 |
509
9d05befdd462
try to get characterNormalization in search result working.
casties
parents:
508
diff
changeset
|
273 def getSearchResults(self, mode, query=None, pageinfo=None, docinfo=None): |
508 | 274 """loads list of search results and stores XML in docinfo""" |
275 logging.debug("getSearchResults mode=%s query=%s"%(mode, query)) | |
276 if mode == "none": | |
277 return docinfo | |
278 | |
279 if 'resultSize_%s_%s'%(mode,query) in docinfo: | |
280 # cached result | |
281 return docinfo | |
282 | |
283 docpath = docinfo['textURLPath'] | |
509
9d05befdd462
try to get characterNormalization in search result working.
casties
parents:
508
diff
changeset
|
284 |
508 | 285 # fetch full results |
286 params = {'document': docpath, | |
287 'mode': 'text', | |
288 'queryType': mode, | |
289 'query': query, | |
290 'queryResultPageSize': 1000, | |
509
9d05befdd462
try to get characterNormalization in search result working.
casties
parents:
508
diff
changeset
|
291 'queryResultPN': 1, |
9d05befdd462
try to get characterNormalization in search result working.
casties
parents:
508
diff
changeset
|
292 'characterNormalization': pageinfo.get('characterNormalization', 'reg')} |
508 | 293 pagexml = self.getServerData("doc-query.xql",urllib.urlencode(params)) |
294 #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))) | |
295 dom = ET.fromstring(pagexml) | |
296 # page content is in <div class="queryResultPage"> | |
297 pagediv = None | |
298 # ElementTree 1.2 in Python 2.6 can't do div[@class='queryResultPage'] | |
299 alldivs = dom.findall("div") | |
300 for div in alldivs: | |
301 dc = div.get('class') | |
302 # page content div | |
303 if dc == 'queryResultPage': | |
304 pagediv = div | |
305 | |
306 elif dc == 'queryResultHits': | |
307 docinfo['resultSize_%s_%s'%(mode,query)] = getInt(div.text) | |
308 | |
309 if pagediv: | |
310 # store XML in docinfo | |
311 docinfo['resultXML_%s_%s'%(mode,query)] = ET.tostring(pagediv, 'UTF-8') | |
312 | |
313 logging.debug("getSearchResults: pagediv=%s"%pagediv) | |
314 return docinfo | |
315 | |
316 | |
317 def getResultsPage(self, mode="text", query=None, pn=None, start=None, size=None, pageinfo=None, docinfo=None): | |
318 """returns single page from the table of contents""" | |
319 logging.debug("getResultsPage mode=%s, pn=%s"%(mode,pn)) | |
320 # check for cached TOC | |
321 #TODO: cache only one search | |
322 if not docinfo.has_key('resultXML_%s_%s'%(mode,query)): | |
509
9d05befdd462
try to get characterNormalization in search result working.
casties
parents:
508
diff
changeset
|
323 self.getSearchResults(mode=mode, query=query, pageinfo=pageinfo, docinfo=docinfo) |
508 | 324 |
325 resultxml = docinfo.get('resultXML_%s_%s'%(mode,query), None) | |
326 if not resultxml: | |
327 logging.error("getResultPage: unable to find resultXML") | |
328 return "Error: no result!" | |
329 | |
330 if size is None: | |
331 size = pageinfo.get('searchResultPageSize', 20) | |
332 | |
333 if start is None: | |
334 start = (pn - 1) * size | |
335 | |
336 fullresult = ET.fromstring(resultxml) | |
337 | |
338 if fullresult: | |
339 # paginate | |
340 first = start | |
341 len = size | |
342 del fullresult[:first] | |
343 del fullresult[len:] | |
344 tocdivs = fullresult | |
345 | |
346 # check all a-tags | |
347 links = tocdivs.findall(".//a") | |
348 for l in links: | |
349 href = l.get('href') | |
350 if href: | |
351 # take pn from href | |
352 m = re.match(r'page-fragment\.xql.*pn=(\d+)', href) | |
353 if m is not None: | |
354 # and create new url (assuming parent is documentViewer) | |
355 #TODO: add highlighting params | |
356 url = self.getLink('pn', m.group(1)) | |
357 l.set('href', url) | |
358 else: | |
359 logging.warning("getResultPage: Problem with link=%s"%href) | |
360 | |
361 # fix two-divs-per-row with containing div | |
362 # newtoc = ET.Element('div', {'class':'queryResultPage'}) | |
363 # for (d1,d2) in zip(tocdivs[::2],tocdivs[1::2]): | |
364 # e = ET.Element('div',{'class':'tocline'}) | |
365 # e.append(d1) | |
366 # e.append(d2) | |
367 # newtoc.append(e) | |
368 | |
369 return serialize(tocdivs) | |
370 | |
371 return "ERROR: no results!" | |
372 | |
373 | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
374 def getToc(self, mode="text", docinfo=None): |
455 | 375 """loads table of contents and stores XML in docinfo""" |
376 logging.debug("getToc mode=%s"%mode) | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
377 if mode == "none": |
455 | 378 return docinfo |
379 | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
380 if 'tocSize_%s'%mode in docinfo: |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
381 # cached toc |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
382 return docinfo |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
383 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
384 docpath = docinfo['textURLPath'] |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
385 # we need to set a result set size |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
386 pagesize = 1000 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
387 pn = 1 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
388 if mode == "text": |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
389 queryType = "toc" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
390 else: |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
391 queryType = mode |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
392 # number of entries in toc |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
393 tocSize = 0 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
394 tocDiv = None |
455 | 395 # fetch full toc |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
396 pagexml = self.getServerData("doc-query.xql","document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath,queryType, pagesize, pn)) |
455 | 397 dom = ET.fromstring(pagexml) |
398 # page content is in <div class="queryResultPage"> | |
399 pagediv = None | |
400 # ElementTree 1.2 in Python 2.6 can't do div[@class='queryResultPage'] | |
401 alldivs = dom.findall("div") | |
402 for div in alldivs: | |
403 dc = div.get('class') | |
404 # page content div | |
405 if dc == 'queryResultPage': | |
406 pagediv = div | |
407 | |
408 elif dc == 'queryResultHits': | |
458 | 409 docinfo['tocSize_%s'%mode] = getInt(div.text) |
455 | 410 |
411 if pagediv: | |
412 # store XML in docinfo | |
413 docinfo['tocXML_%s'%mode] = ET.tostring(pagediv, 'UTF-8') | |
414 | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
415 return docinfo |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
416 |
482
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
417 def getTocPage(self, mode="text", pn=None, start=None, size=None, pageinfo=None, docinfo=None): |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
418 """returns single page from the table of contents""" |
455 | 419 logging.debug("getTocPage mode=%s, pn=%s"%(mode,pn)) |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
420 if mode == "text": |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
421 queryType = "toc" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
422 else: |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
423 queryType = mode |
455 | 424 |
425 # check for cached TOC | |
426 if not docinfo.has_key('tocXML_%s'%mode): | |
427 self.getToc(mode=mode, docinfo=docinfo) | |
428 | |
429 tocxml = docinfo.get('tocXML_%s'%mode, None) | |
430 if not tocxml: | |
431 logging.error("getTocPage: unable to find tocXML") | |
482
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
432 return "Error: no table of contents!" |
455 | 433 |
482
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
434 if size is None: |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
435 size = pageinfo.get('tocPageSize', 30) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
436 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
437 if start is None: |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
438 start = (pn - 1) * size |
455 | 439 |
440 fulltoc = ET.fromstring(tocxml) | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
441 |
455 | 442 if fulltoc: |
443 # paginate | |
482
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
444 first = (start - 1) * 2 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
445 len = size * 2 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
446 del fulltoc[:first] |
456 | 447 del fulltoc[len:] |
455 | 448 tocdivs = fulltoc |
449 | |
450 # check all a-tags | |
451 links = tocdivs.findall(".//a") | |
452 for l in links: | |
453 href = l.get('href') | |
454 if href: | |
455 # take pn from href | |
456 m = re.match(r'page-fragment\.xql.*pn=(\d+)', href) | |
457 if m is not None: | |
476 | 458 # and create new url (assuming parent is documentViewer) |
459 url = self.getLink('pn', m.group(1)) | |
460 l.set('href', url) | |
455 | 461 else: |
462 logging.warning("getTocPage: Problem with link=%s"%href) | |
463 | |
482
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
464 # fix two-divs-per-row with containing div |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
465 newtoc = ET.Element('div', {'class':'queryResultPage'}) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
466 for (d1,d2) in zip(tocdivs[::2],tocdivs[1::2]): |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
467 e = ET.Element('div',{'class':'tocline'}) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
468 e.append(d1) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
469 e.append(d2) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
470 newtoc.append(e) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
471 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
472 return serialize(newtoc) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
473 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
474 return "ERROR: no table of contents!" |
455 | 475 |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
476 |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
477 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
|
478 """change settings""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
479 self.title=title |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
480 self.timeout = timeout |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
481 self.serverUrl = serverUrl |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
482 if RESPONSE is not None: |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
483 RESPONSE.redirect('manage_main') |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
484 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
485 # management methods |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
486 def manage_addMpdlXmlTextServerForm(self): |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
487 """Form for adding""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
488 pt = PageTemplateFile("zpt/manage_addMpdlXmlTextServer", globals()).__of__(self) |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
489 return pt() |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
490 |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
491 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
|
492 #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
|
493 """add zogiimage""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
494 newObj = MpdlXmlTextServer(id,title,serverUrl,timeout) |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
495 self.Destination()._setObject(id, newObj) |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
496 if RESPONSE is not None: |
455 | 497 RESPONSE.redirect('manage_main') |
498 | |
499 |