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