Mercurial > hg > documentViewer
annotate MpdlXmlTextServer.py @ 510:4fb35343d2e7 elementtree
more search. nicer css.
author | casties |
---|---|
date | Tue, 28 Feb 2012 10:39:21 +0100 |
parents | 9d05befdd462 |
children | 551ca1641a5e |
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 | |
510 | 309 if pagediv is not None: |
508 | 310 # store XML in docinfo |
311 docinfo['resultXML_%s_%s'%(mode,query)] = ET.tostring(pagediv, 'UTF-8') | |
312 | |
313 return docinfo | |
314 | |
315 | |
316 def getResultsPage(self, mode="text", query=None, pn=None, start=None, size=None, pageinfo=None, docinfo=None): | |
317 """returns single page from the table of contents""" | |
318 logging.debug("getResultsPage mode=%s, pn=%s"%(mode,pn)) | |
319 # check for cached TOC | |
320 #TODO: cache only one search | |
321 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
|
322 self.getSearchResults(mode=mode, query=query, pageinfo=pageinfo, docinfo=docinfo) |
508 | 323 |
324 resultxml = docinfo.get('resultXML_%s_%s'%(mode,query), None) | |
325 if not resultxml: | |
326 logging.error("getResultPage: unable to find resultXML") | |
327 return "Error: no result!" | |
328 | |
329 if size is None: | |
330 size = pageinfo.get('searchResultPageSize', 20) | |
331 | |
332 if start is None: | |
333 start = (pn - 1) * size | |
334 | |
335 fullresult = ET.fromstring(resultxml) | |
336 | |
510 | 337 if fullresult is not None: |
508 | 338 # paginate |
339 first = start | |
340 len = size | |
341 del fullresult[:first] | |
342 del fullresult[len:] | |
343 tocdivs = fullresult | |
344 | |
345 # check all a-tags | |
346 links = tocdivs.findall(".//a") | |
347 for l in links: | |
348 href = l.get('href') | |
349 if href: | |
350 # take pn from href | |
351 m = re.match(r'page-fragment\.xql.*pn=(\d+)', href) | |
352 if m is not None: | |
353 # and create new url (assuming parent is documentViewer) | |
354 #TODO: add highlighting params | |
355 url = self.getLink('pn', m.group(1)) | |
356 l.set('href', url) | |
357 else: | |
358 logging.warning("getResultPage: Problem with link=%s"%href) | |
359 | |
360 # fix two-divs-per-row with containing div | |
361 # newtoc = ET.Element('div', {'class':'queryResultPage'}) | |
362 # for (d1,d2) in zip(tocdivs[::2],tocdivs[1::2]): | |
363 # e = ET.Element('div',{'class':'tocline'}) | |
364 # e.append(d1) | |
365 # e.append(d2) | |
366 # newtoc.append(e) | |
367 | |
368 return serialize(tocdivs) | |
369 | |
370 return "ERROR: no results!" | |
371 | |
372 | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
373 def getToc(self, mode="text", docinfo=None): |
455 | 374 """loads table of contents and stores XML in docinfo""" |
375 logging.debug("getToc mode=%s"%mode) | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
376 if mode == "none": |
455 | 377 return docinfo |
378 | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
379 if 'tocSize_%s'%mode in docinfo: |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
380 # cached toc |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
381 return docinfo |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
382 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
383 docpath = docinfo['textURLPath'] |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
384 # we need to set a result set size |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
385 pagesize = 1000 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
386 pn = 1 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
387 if mode == "text": |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
388 queryType = "toc" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
389 else: |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
390 queryType = mode |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
391 # number of entries in toc |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
392 tocSize = 0 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
393 tocDiv = None |
455 | 394 # fetch full toc |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
395 pagexml = self.getServerData("doc-query.xql","document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath,queryType, pagesize, pn)) |
455 | 396 dom = ET.fromstring(pagexml) |
397 # page content is in <div class="queryResultPage"> | |
398 pagediv = None | |
399 # ElementTree 1.2 in Python 2.6 can't do div[@class='queryResultPage'] | |
400 alldivs = dom.findall("div") | |
401 for div in alldivs: | |
402 dc = div.get('class') | |
403 # page content div | |
404 if dc == 'queryResultPage': | |
405 pagediv = div | |
406 | |
407 elif dc == 'queryResultHits': | |
458 | 408 docinfo['tocSize_%s'%mode] = getInt(div.text) |
455 | 409 |
510 | 410 if pagediv is not None: |
455 | 411 # store XML in docinfo |
412 docinfo['tocXML_%s'%mode] = ET.tostring(pagediv, 'UTF-8') | |
413 | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
414 return docinfo |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
415 |
482
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
416 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
|
417 """returns single page from the table of contents""" |
455 | 418 logging.debug("getTocPage mode=%s, pn=%s"%(mode,pn)) |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
419 if mode == "text": |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
420 queryType = "toc" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
421 else: |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
422 queryType = mode |
455 | 423 |
424 # check for cached TOC | |
425 if not docinfo.has_key('tocXML_%s'%mode): | |
426 self.getToc(mode=mode, docinfo=docinfo) | |
427 | |
428 tocxml = docinfo.get('tocXML_%s'%mode, None) | |
429 if not tocxml: | |
430 logging.error("getTocPage: unable to find tocXML") | |
482
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
431 return "Error: no table of contents!" |
455 | 432 |
482
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
433 if size is None: |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
434 size = pageinfo.get('tocPageSize', 30) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
435 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
436 if start is None: |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
437 start = (pn - 1) * size |
455 | 438 |
439 fulltoc = ET.fromstring(tocxml) | |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
440 |
510 | 441 if fulltoc is not None: |
455 | 442 # paginate |
482
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
443 first = (start - 1) * 2 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
444 len = size * 2 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
445 del fulltoc[:first] |
456 | 446 del fulltoc[len:] |
455 | 447 tocdivs = fulltoc |
448 | |
449 # check all a-tags | |
450 links = tocdivs.findall(".//a") | |
451 for l in links: | |
452 href = l.get('href') | |
453 if href: | |
454 # take pn from href | |
455 m = re.match(r'page-fragment\.xql.*pn=(\d+)', href) | |
456 if m is not None: | |
476 | 457 # and create new url (assuming parent is documentViewer) |
458 url = self.getLink('pn', m.group(1)) | |
459 l.set('href', url) | |
455 | 460 else: |
461 logging.warning("getTocPage: Problem with link=%s"%href) | |
462 | |
482
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
463 # fix two-divs-per-row with containing div |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
464 newtoc = ET.Element('div', {'class':'queryResultPage'}) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
465 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
|
466 e = ET.Element('div',{'class':'tocline'}) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
467 e.append(d1) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
468 e.append(d2) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
469 newtoc.append(e) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
470 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
471 return serialize(newtoc) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
472 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
478
diff
changeset
|
473 return "ERROR: no table of contents!" |
455 | 474 |
129
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
475 |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
476 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
|
477 """change settings""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
478 self.title=title |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
479 self.timeout = timeout |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
480 self.serverUrl = serverUrl |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
481 if RESPONSE is not None: |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
482 RESPONSE.redirect('manage_main') |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
483 |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
484 # management methods |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
485 def manage_addMpdlXmlTextServerForm(self): |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
486 """Form for adding""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
487 pt = PageTemplateFile("zpt/manage_addMpdlXmlTextServer", globals()).__of__(self) |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
488 return pt() |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
489 |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
407
diff
changeset
|
490 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
|
491 #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
|
492 """add zogiimage""" |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
493 newObj = MpdlXmlTextServer(id,title,serverUrl,timeout) |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
494 self.Destination()._setObject(id, newObj) |
9404b6c37920
more modular version with separate object MpdlXmlTextServer
casties
parents:
diff
changeset
|
495 if RESPONSE is not None: |
455 | 496 RESPONSE.redirect('manage_main') |
497 | |
498 |