Changeset 511:551ca1641a5e in documentViewer for MpdlXmlTextServer.py


Ignore:
Timestamp:
Feb 28, 2012, 5:21:59 PM (12 years ago)
Author:
casties
Branch:
elementtree
Message:

more cleanup.
search really works now.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • MpdlXmlTextServer.py

    r510 r511  
    77import logging
    88import urllib
     9import urlparse
    910import base64
    1011
     
    140141           
    141142        # stuff for constructing full urls
    142         characterNormalization = pageinfo.get('characterNormalization', None)
    143         moreTextParam = ''
    144143        selfurl = docinfo['viewerUrl']
     144        textParams = {'document': docpath,
     145                      'pn': pn}
     146        if 'characterNormalization' in pageinfo:
     147            textParams['characterNormalization'] = pageinfo['characterNormalization']
    145148       
    146149        if not mode:
     
    157160            # add highlighting
    158161            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
     162            if highlightQuery:
     163                textParams['highlightQuery'] = highlightQuery
     164                textParams['highlightElement'] = pageinfo.get('highlightElement', '')
     165                textParams['highlightElementPos'] = pageinfo.get('highlightElementPos', '')
     166               
     167            # ignore mode in the following
    164168            modes.remove('search')
    165169                           
     
    175179            textmode = modes[0]
    176180       
    177         textParam = "document=%s&mode=%s&pn=%s&characterNormalization=%s"%(docpath,textmode,pn,characterNormalization)
    178         textParam += moreTextParam
     181        textParams['mode'] = textmode
    179182       
    180183        # fetch the page
    181         pagexml = self.getServerData("page-fragment.xql",textParam)
     184        pagexml = self.getServerData("page-fragment.xql",urllib.urlencode(textParams))
    182185        dom = ET.fromstring(pagexml)
    183186        # extract additional info
     
    221224                    if href:
    222225                        # is link with href
    223                         if href.startswith('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/lt/wordInfo.xql'):
     226                        linkurl = urlparse.urlparse(href)
     227                        #logging.debug("getTextPage: linkurl=%s"%repr(linkurl))
     228                        if linkurl.path.endswith('GetDictionaryEntries'):
     229                            #TODO: replace wordInfo page
    224230                            # 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))
     231                            #l.set('href', href.replace('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/lt/wordInfo.xql','%s/template/viewer_wordinfo'%viewerurl))
    226232                            # add target to open new page
    227233                            l.set('target', '_blank')
    228234                                                         
    229235                        # TODO: is this needed?
    230                         if href.startswith('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/lt/lemma.xql'):
    231                             selfurl = self.absolute_url()
    232                             l.set('href', href.replace('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/lt/lemma.xql','%s/head_main_lemma'%selfurl))
    233                             l.set('target', '_blank')
    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;")
    235                             l.set('ondblclick', 'popupWin.focus();')   
     236#                        if href.startswith('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/lt/lemma.xql'):
     237#                            selfurl = self.absolute_url()
     238#                            l.set('href', href.replace('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/lt/lemma.xql','%s/head_main_lemma'%selfurl))
     239#                            l.set('target', '_blank')
     240#                            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;")
     241#                            l.set('ondblclick', 'popupWin.focus();')   
    236242                   
    237243                        if href.startswith('#note-'):
     
    273279    def getSearchResults(self, mode, query=None, pageinfo=None, docinfo=None):
    274280        """loads list of search results and stores XML in docinfo"""
     281       
    275282        logging.debug("getSearchResults mode=%s query=%s"%(mode, query))
    276283        if mode == "none":
    277284            return docinfo
    278285             
    279         if 'resultSize_%s_%s'%(mode,query) in docinfo:
    280             # cached result
    281             return docinfo
    282        
     286        cachedQuery = docinfo.get('cachedQuery', None)
     287        if cachedQuery is not None:
     288            # cached search result
     289            if cachedQuery == '%s_%s'%(mode,query):
     290                # same query
     291                return docinfo
     292           
     293            else:
     294                # different query
     295                del docinfo['resultSize']
     296                del docinfo['resultXML']
     297       
     298        # cache query
     299        docinfo['cachedQuery'] = '%s_%s'%(mode,query)
     300       
     301        # fetch full results
    283302        docpath = docinfo['textURLPath']
    284        
    285         # fetch full results
    286303        params = {'document': docpath,
    287304                  'mode': 'text',
     
    305322               
    306323            elif dc == 'queryResultHits':
    307                 docinfo['resultSize_%s_%s'%(mode,query)] = getInt(div.text)
     324                docinfo['resultSize'] = getInt(div.text)
    308325
    309326        if pagediv is not None:
    310327            # store XML in docinfo
    311             docinfo['resultXML_%s_%s'%(mode,query)] = ET.tostring(pagediv, 'UTF-8')
     328            docinfo['resultXML'] = ET.tostring(pagediv, 'UTF-8')
    312329
    313330        return docinfo
     
    317334        """returns single page from the table of contents"""
    318335        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)):
     336        # check for cached result
     337        if not 'resultXML' in docinfo:
    322338            self.getSearchResults(mode=mode, query=query, pageinfo=pageinfo, docinfo=docinfo)
    323339           
    324         resultxml = docinfo.get('resultXML_%s_%s'%(mode,query), None)
     340        resultxml = docinfo.get('resultXML', None)
    325341        if not resultxml:
    326342            logging.error("getResultPage: unable to find resultXML")
     
    328344       
    329345        if size is None:
    330             size = pageinfo.get('searchResultPageSize', 20)
     346            size = pageinfo.get('resultPageSize', 10)
    331347           
    332348        if start is None:
     
    337353        if fullresult is not None:
    338354            # paginate
    339             first = start
     355            first = start-1
    340356            len = size
    341357            del fullresult[:first]
     
    348364                href = l.get('href')
    349365                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)
     366                    # assume all links go to pages
     367                    linkUrl = urlparse.urlparse(href)
     368                    linkParams = urlparse.parse_qs(linkUrl.query)
     369                    # take some parameters
     370                    params = {'pn': linkParams['pn'],
     371                              'highlightQuery': linkParams.get('highlightQuery',''),
     372                              'highlightElement': linkParams.get('highlightElement',''),
     373                              'highlightElementPos': linkParams.get('highlightElementPos','')
     374                              }
     375                    url = self.getLink(params=params)
     376                    l.set('href', url)
    359377                       
    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                
    368378            return serialize(tocdivs)
    369379       
Note: See TracChangeset for help on using the changeset viewer.