--- documentViewer/documentViewer.py 2010/03/19 11:42:40 1.43 +++ documentViewer/documentViewer.py 2010/05/20 09:28:36 1.55 @@ -2,6 +2,7 @@ from OFS.Folder import Folder from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate from Products.PageTemplates.PageTemplateFile import PageTemplateFile +from Products.PythonScripts.standard import url_quote from AccessControl import ClassSecurityInfo from AccessControl import getSecurityManager from Globals import package_home @@ -10,6 +11,9 @@ from Ft.Xml.Domlette import Nonvalidatin from Ft.Xml.Domlette import PrettyPrint, Print from Ft.Xml import EMPTY_NAMESPACE, Parse +from xml.dom.minidom import parse, parseString + + import Ft.Xml.XPath import cStringIO @@ -87,10 +91,13 @@ class documentViewer(Folder): # templates and forms viewer_main = PageTemplateFile('zpt/viewer_main', globals()) - thumbs_main = PageTemplateFile('zpt/thumbs_main', globals()) - image_main = PageTemplateFile('zpt/image_main', globals()) # obsolete! + toc_thumbs = PageTemplateFile('zpt/toc_thumbs', globals()) + toc_text = PageTemplateFile('zpt/toc_text', globals()) + toc_figures = PageTemplateFile('zpt/toc_figures', globals()) page_main_images = PageTemplateFile('zpt/page_main_images', globals()) page_main_text = PageTemplateFile('zpt/page_main_text', globals()) + page_main_text_dict = PageTemplateFile('zpt/page_main_text_dict', globals()) + page_main_xml = PageTemplateFile('zpt/page_main_xml', globals()) head_main = PageTemplateFile('zpt/head_main', globals()) docuviewer_css = PageTemplateFile('css/docuviewer.css', globals()) info_xml = PageTemplateFile('zpt/info_xml', globals()) @@ -100,7 +107,7 @@ class documentViewer(Folder): changeDocumentViewerForm = PageTemplateFile('zpt/changeDocumentViewer', globals()) - def __init__(self,id,imageScalerUrl=None,textServerName=None,title="",digilibBaseUrl=None,thumbcols=2,thumbrows=10,authgroups="mpiwg"): + def __init__(self,id,imageScalerUrl=None,textServerName=None,title="",digilibBaseUrl=None,thumbcols=2,thumbrows=5,authgroups="mpiwg"): """init document viewer""" self.id=id self.title=title @@ -161,13 +168,14 @@ class documentViewer(Folder): return pt(docinfo=docinfo,pageinfo=pageinfo,viewMode=viewMode) security.declareProtected('View','index_html') - def index_html(self,url,mode="texttool",viewMode="auto",start=None,pn=1,mk=None): + def index_html(self,url,mode="texttool",viewMode="auto",tocMode="thumbs",start=None,pn=1,mk=None, query=None, querySearch=None): ''' view it @param mode: defines how to access the document behind url @param url: url which contains display information - @param viewMode: if images display images, if text display text, default is images (text,images or auto) - + @param viewMode: if images display images, if text display text, default is auto (text,images or auto) + @param tocMode: type of 'table of contents' for navigation (thumbs, text, figures, none) + @param querySearch: type of different search modes (fulltext, fulltextMorph, xpath, xquery, ftIndex, ftIndexMorph, fulltextMorphLemma) ''' logging.debug("documentViewer (index) mode: %s url:%s start:%s pn:%s"%(mode,url,start,pn)) @@ -181,28 +189,34 @@ class documentViewer(Folder): self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary" docinfo = self.getDocinfo(mode=mode,url=url) - pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo) - pt = getattr(self.template, 'viewer_main') + + + if tocMode != "thumbs": + # get table of contents + docinfo = self.getToc(mode=tocMode, docinfo=docinfo) + + pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo,viewMode=viewMode,tocMode=tocMode) if viewMode=="auto": # automodus gewaehlt if docinfo.get("textURL",''): #texturl gesetzt und textViewer konfiguriert viewMode="text" else: viewMode="images" - + + pt = getattr(self.template, 'viewer_main') return pt(docinfo=docinfo,pageinfo=pageinfo,viewMode=viewMode,mk=self.generateMarks(mk)) def generateMarks(self,mk): ret="" - if mk is None: - return "" - - if type(mk) is not ListType: - mk=[mk] + if mk is None: + return "" + if type(mk) is not ListType: + mk=[mk] for m in mk: ret+="mk=%s"%m return ret + def findDigilibUrl(self): """try to get the digilib URL from zogilib""" url = self.template.zogilib.getDLBaseUrl() @@ -343,7 +357,7 @@ class documentViewer(Folder): dom = Parse(txt) break except: - logger("ERROR documentViewer (getIndexMata)", logging.INFO,"%s (%s)"%sys.exc_info()[0:2]) + logger("ERROR documentViewer (getIndexMeta)", logging.INFO,"%s (%s)"%sys.exc_info()[0:2]) if dom is None: raise IOError("Unable to read index meta from %s"%(url)) @@ -362,7 +376,6 @@ class documentViewer(Folder): # online path server=self.digilibBaseUrl+"/servlet/Texter?fn=" metaUrl=server+url.replace("/mpiwg/online","") - for cnt in range(num_retries): try: @@ -461,7 +474,6 @@ class documentViewer(Folder): logger("documentViewer (getdocinfofromtexttool)", logging.INFO, "url: %s" % (url)) if docinfo is None: docinfo = {} - if docinfo.get('lang', None) is None: docinfo['lang'] = '' # default keine Sprache gesetzt if dom is None: @@ -534,7 +546,10 @@ class documentViewer(Folder): textUrls = dom.xpath("//texttool/text-url-path") if textUrls and (len(textUrls) > 0): textUrl = getTextFromNode(textUrls[0]) - docinfo['textURLPath'] = textUrl + docinfo['textURLPath'] = textUrl + if not docinfo['imagePath']: + # text-only, no page images + docinfo = self.getNumPages(docinfo) #im moment einfach auf eins setzen, navigation ueber die thumbs geht natuerlich nicht presentationUrls = dom.xpath("//texttool/presentation") docinfo = self.getBibinfoFromIndexMeta(url, docinfo=docinfo, dom=dom) # get info von bib tag @@ -547,15 +562,12 @@ class documentViewer(Folder): presentationUrl = url.replace('index.meta', presentationPath) else: presentationUrl = url + "/" + presentationPath - docinfo = self.getNumPages(docinfo) #im moment einfach auf eins setzen, navigation ueber die thumbs geht natuerlich nicht + docinfo = self.getBibinfoFromTextToolPresentation(presentationUrl, docinfo=docinfo, dom=dom) docinfo = self.getAuthinfoFromIndexMeta(url, docinfo=docinfo, dom=dom) # get access info return docinfo - - - def getBibinfoFromTextToolPresentation(self,url,docinfo=None,dom=None): @@ -618,14 +630,14 @@ class documentViewer(Folder): docinfo = self.getDocinfoFromImagePath(url, docinfo=docinfo,cut=1) else: logger("documentViewer (getdocinfo)", logging.ERROR,"unknown mode!") - raise ValueError("Unknown mode %s"%(mode)) + raise ValueError("Unknown mode %s! Has to be one of 'texttool','imagepath','filepath'."%(mode)) logger("documentViewer (getdocinfo)", logging.INFO,"docinfo: %s"%docinfo) self.REQUEST.SESSION['docinfo'] = docinfo return docinfo - def getPageinfo(self, current, start=None, rows=None, cols=None, docinfo=None): + def getPageinfo(self, current, start=None, rows=None, cols=None, docinfo=None, viewMode=None, tocMode=None): """returns pageinfo with the given parameters""" pageinfo = {} current = getInt(current) @@ -640,17 +652,131 @@ class documentViewer(Folder): # int(current / grpsize) * grpsize +1)) pageinfo['start'] = start pageinfo['end'] = start + grpsize - if docinfo is not None: + if (docinfo is not None) and ('numPages' in docinfo): np = int(docinfo['numPages']) pageinfo['end'] = min(pageinfo['end'], np) pageinfo['numgroups'] = int(np / grpsize) if np % grpsize > 0: pageinfo['numgroups'] += 1 + + + pageinfo['viewMode'] = viewMode + pageinfo['tocMode'] = tocMode + pageinfo['query'] = self.REQUEST.get('query',' ') + pageinfo['queryType'] = self.REQUEST.get('queryType',' ') + pageinfo['querySearch'] =self.REQUEST.get('querySearch', 'fulltext') + + pageinfo['textPN'] = self.REQUEST.get('textPN','1') + pageinfo['highlightQuery'] = self.REQUEST.get('highlightQuery','') + pageinfo['tocPageSize'] = self.REQUEST.get('tocPageSize', '30') + pageinfo['queryPageSize'] =self.REQUEST.get('queryPageSize', '10') + pageinfo['tocPN'] = self.REQUEST.get('tocPN', '1') + toc = int (pageinfo['tocPN']) + pageinfo['textPages'] =int (toc) + + if 'tocSize_%s'%tocMode in docinfo: + tocSize = int(docinfo['tocSize_%s'%tocMode]) + tocPageSize = int(pageinfo['tocPageSize']) + # cached toc + + if tocSize%tocPageSize>0: + tocPages=tocSize/tocPageSize+1 + else: + tocPages=tocSize/tocPageSize + pageinfo['tocPN'] = min (tocPages,toc) + + pageinfo['searchPN'] =self.REQUEST.get('searchPN','1') + pageinfo['sn'] =self.REQUEST.get('sn','1') return pageinfo - - + def getSearch(self, pn=1, pageinfo=None, docinfo=None, query=None, queryType=None): + """get search list""" + docpath = docinfo['textURLPath'] + url = docinfo['url'] + logging.debug("documentViewer (gettoc) docpath: %s"%(docpath)) + logging.debug("documentViewer (gettoc) url: %s"%(url)) + pagesize = pageinfo['queryPageSize'] + pn = pageinfo['searchPN'] + sn = pageinfo['sn'] + highlightQuery = pageinfo['highlightQuery'] + query =pageinfo['query'] + queryType =pageinfo['queryType'] + viewMode= pageinfo['viewMode'] + tocMode = pageinfo['tocMode'] + tocPN = pageinfo['tocPN'] + selfurl = self.absolute_url() + logging.debug("documentViewer (gettoc) /mpdl/interface/doc-query.xql?document=%s&mode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s&sn=%s&viewMode=%s"%(docpath, 'text', queryType, query, pagesize, pn, sn, viewMode)) + page=self.template.fulltextclient.eval("/mpdl/interface/doc-query.xql","document=%s&mode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s&sn=%s&viewMode=%s"%(docpath, 'text', queryType, query, pagesize, pn, sn, viewMode) ,outputUnicode=False) + pagexml = page.replace('?document=%s'%str(docpath),'?url=%s'%url) + pagedom = Parse(pagexml) + if (queryType=="fulltext")or(queryType=="xpath")or(queryType=="xquery")or(queryType=="fulltextMorphLemma"): + pagedivs = pagedom.xpath("//div[@class='queryResultPage']") + if len(pagedivs)>0: + pagenode=pagedivs[0] + links=pagenode.xpath("//a") + for l in links: + hrefNode = l.getAttributeNodeNS(None, u"href") + if hrefNode: + href = hrefNode.nodeValue + if href.startswith('page-fragment.xql'): + selfurl = self.absolute_url() + #l.setAttributeNS(None, "span class = 'hit highlight'", "background-color: #77DD77;") + pagexml=href.replace('mode=text','mode=texttool&viewMode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s&tocMode=%s&searchPN=%s&tocPN=%s'%(viewMode,queryType,query,pagesize,pn,tocMode,pn,tocPN)) + hrefNode.nodeValue = pagexml.replace('page-fragment.xql','%s'%selfurl) + l.setAttributeNS(None, "onClick", "id='highlighting'") + logging.debug("documentViewer (gettoc) highlightQuery: %s"%(highlightQuery)) + l.setAttributeNS(None, "highlightQuery", "id='highlighting'") + return serializeNode(pagenode) + + if (queryType=="fulltextMorph"): + pagedivs = pagedom.xpath("//div[@class='queryResult']") + + if len(pagedivs)>0: + pagenode=pagedivs[0] + links=pagenode.xpath("//a") + for l in links: + hrefNode = l.getAttributeNodeNS(None, u"href") + if hrefNode: + href = hrefNode.nodeValue + if href.startswith('page-fragment.xql'): + selfurl = self.absolute_url() + pagexml=href.replace('mode=text','mode=texttool&viewMode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s&tocMode=%s&searchPN=%s&tocPN=%s'%(viewMode,queryType,query,pagesize,pn,tocMode,pn,tocPN)) + hrefNode.nodeValue = pagexml.replace('page-fragment.xql','%s'%selfurl) + if href.startswith('../lt/lemma.xql'): + selfurl = self.absolute_url() + hrefNode.nodeValue = href.replace('lt/lemma.xql','%s/template/head_main_lemma'%selfurl) + l.setAttributeNS(None, 'target', '_blank') + l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=400, scrollbars=1'); return false;") + l.setAttributeNS(None, 'onDblclick', 'popupWin.focus();') + return serializeNode(pagenode) + + if (queryType=="ftIndex")or(queryType=="ftIndexMorph"): + pagedivs= pagedom.xpath("//div[@class='queryResultPage']") + if len(pagedivs)>0: + pagenode=pagedivs[0] + links=pagenode.xpath("//a") + for l in links: + hrefNode = l.getAttributeNodeNS(None, u"href") + if hrefNode: + href = hrefNode.nodeValue + hrefNode.nodeValue=href.replace('mode=text','mode=texttool&viewMode=%s&tocMode=%s&tocPN=%s&pn=%s'%(viewMode,tocMode,tocPN,pn)) + + if href.startswith('../lt/lex.xql'): + # selfurl = self.absolute_url() + hrefNode.nodeValue = href.replace('../lt/lex.xql','%s/template/head_main_voc'%selfurl) + l.setAttributeNS(None, 'target', '_blank') + l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=400, scrollbars=1'); return false;") + l.setAttributeNS(None, 'onDblclick', 'popupWin.focus();') + if href.startswith('../lt/lemma.xql'): + #selfurl = self.absolute_url() + hrefNode.nodeValue = href.replace('../lt/lemma.xql','%s/template/head_main_lemma'%selfurl) + l.setAttributeNS(None, 'target', '_blank') + l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=400, scrollbars=1'); return false;") + l.setAttributeNS(None, 'onDblclick', 'popupWin.focus();') + return serializeNode(pagenode) + return "no text here" + def getNumPages(self,docinfo=None): """get list of pages from fulltext and put in docinfo""" xquery = '//pb' @@ -659,21 +785,47 @@ class documentViewer(Folder): docinfo['numPages'] = text.count(" 0: + pagenode = pagedivs[0] + links = pagenode.xpath("//a") + for l in links: + hrefNode = l.getAttributeNodeNS(None, u"href") + if hrefNode: + href= hrefNode.nodeValue + if href.startswith('#note-0006-01'): + selfurl = self.absolute_url() + hrefNode.nodeValue = href.replace("href='#note-'",'xaxa/%s/'%selfurl) return serializeNode(pagenode) - + if mode == "xml": + # first div contains text + pagedivs = pagedom.xpath("/div") + if len(pagedivs) > 0: + pagenode = pagedivs[0] + return serializeNode(pagenode) # text-with-links mode - if mode == "textPollux": + if mode == "text_dict": # first div contains text pagedivs = pagedom.xpath("/div") if len(pagedivs) > 0: @@ -689,15 +841,119 @@ class documentViewer(Folder): # is pollux link selfurl = self.absolute_url() # change href - hrefNode.nodeValue = href.replace('lt/lex.xql','%s/head_main_voc'%selfurl) + hrefNode.nodeValue = href.replace('lt/lex.xql','%s/template/head_main_voc'%selfurl) # add target l.setAttributeNS(None, 'target', '_blank') + l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=700, scrollbars=1'); return false;") + l.setAttributeNS(None, 'onDblclick', 'popupWin.focus();') + + if href.startswith('lt/lemma.xql'): + selfurl = self.absolute_url() + hrefNode.nodeValue = href.replace('lt/lemma.xql','%s/template/head_main_lemma'%selfurl) + l.setAttributeNS(None, 'target', '_blank') + l.setAttributeNS(None, 'onClick',"popupWin = window.open(this.href, 'contacts', 'location,width=500,height=600,top=180, left=700, scrollbars=1'); return false;") + l.setAttributeNS(None, 'onDblclick', 'popupWin.focus();') return serializeNode(pagenode) return "no text here" + def getTranslate(self, query=None, language=None): + """translate into another languages""" + pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lex.xql","document=&language="+str(language)+"&query="+url_quote(str(query))) + return pagexml + + def getLemma(self, lemma=None, language=None): + """simular words lemma """ + pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lemma.xql","document=&language="+str(language)+"&lemma="+url_quote(str(lemma))) + #pagexml=self.template.fulltextclient.eval("/mpdl/interface/lt/lemma.xql","lemma=%s&language=%s"%(lemma,language),outputUnicode=False) + return pagexml + + def getQuery (self, docinfo=None, pageinfo=None, query=None, queryType=None, pn=1): + """number of""" + docpath = docinfo['textURLPath'] + pagesize = pageinfo['queryPageSize'] + pn = pageinfo['searchPN'] + query =pageinfo['query'] + queryType =pageinfo['queryType'] + + tocSearch = 0 + tocDiv = None + pagexml=self.template.fulltextclient.eval("/mpdl/interface/doc-query.xql","document=%s&mode=%s&queryType=%s&query=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath, 'text', queryType, query, pagesize, pn) ,outputUnicode=False) + + pagedom = Parse(pagexml) + numdivs = pagedom.xpath("//div[@class='queryResultHits']") + tocSearch = int(getTextFromNode(numdivs[0])) + tc=int((tocSearch/10)+1) + logging.debug("documentViewer (gettoc) tc: %s"%(tc)) + return tc + + def getToc(self, mode="text", docinfo=None): + """loads table of contents and stores in docinfo""" + logging.debug("documentViewer (gettoc) mode: %s"%(mode)) + if 'tocSize_%s'%mode in docinfo: + # cached toc + return docinfo + docpath = docinfo['textURLPath'] + # we need to set a result set size + pagesize = 1000 + pn = 1 + if mode == "text": + queryType = "toc" + else: + queryType = mode + # number of entries in toc + tocSize = 0 + tocDiv = None + pagexml=self.template.fulltextclient.eval("/mpdl/interface/doc-query.xql", "document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath,queryType,pagesize,pn), outputUnicode=False) + # post-processing downloaded xml + pagedom = Parse(pagexml) + # get number of entries + numdivs = pagedom.xpath("//div[@class='queryResultHits']") + if len(numdivs) > 0: + tocSize = int(getTextFromNode(numdivs[0])) + # div contains text + #pagedivs = pagedom.xpath("//div[@class='queryResultPage']") + #if len(pagedivs) > 0: + # tocDiv = pagedivs[0] + + docinfo['tocSize_%s'%mode] = tocSize + #docinfo['tocDiv_%s'%mode] = tocDiv + return docinfo + + def getTocPage(self, mode="text", pn=1, pageinfo=None, docinfo=None): + """returns single page from the table of contents""" + # TODO: this should use the cached TOC + if mode == "text": + queryType = "toc" + else: + queryType = mode + docpath = docinfo['textURLPath'] + path = docinfo['textURLPath'] + #logging.debug("documentViewer (gettoc) pathNomer: %s"%(pathNomer)) + pagesize = pageinfo['tocPageSize'] + pn = pageinfo['tocPN'] + url = docinfo['url'] + selfurl = self.absolute_url() + viewMode= pageinfo['viewMode'] + tocMode = pageinfo['tocMode'] + tocPN = pageinfo['tocPN'] + + pagexml=self.template.fulltextclient.eval("/mpdl/interface/doc-query.xql", "document=%s&queryType=%s&queryResultPageSize=%s&queryResultPN=%s"%(docpath,queryType, pagesize, pn), outputUnicode=False) + page = pagexml.replace('page-fragment.xql?document=%s'%str(path),'%s?url=%s&viewMode=%s&tocMode=%s&tocPN=%s'%(selfurl,url, viewMode, tocMode, tocPN)) + text = page.replace('mode=image','mode=texttool') + return text + # post-processing downloaded xml + #pagedom = Parse(text) + # div contains text + #pagedivs = pagedom.xpath("//div[@class='queryResultPage']") + #if len(pagedivs) > 0: + # pagenode = pagedivs[0] + # return serializeNode(pagenode) + #else: + # return "No TOC!" + - def changeDocumentViewer(self,title="",digilibBaseUrl=None,thumbrows=2,thumbcols=10,authgroups='mpiwg',RESPONSE=None): + def changeDocumentViewer(self,title="",digilibBaseUrl=None,thumbrows=2,thumbcols=5,authgroups='mpiwg',RESPONSE=None): """init document viewer""" self.title=title self.digilibBaseUrl = digilibBaseUrl