--- documentViewer/documentViewer.py 2010/08/24 12:34:32 1.94 +++ documentViewer/documentViewer.py 2010/11/02 15:32:44 1.151 @@ -5,6 +5,7 @@ from Products.PageTemplates.PageTemplate from AccessControl import ClassSecurityInfo from AccessControl import getSecurityManager from Globals import package_home +from Products.zogiLib.zogiLib import browserCheck from Ft.Xml import EMPTY_NAMESPACE, Parse import Ft.Xml.Domlette @@ -16,6 +17,8 @@ import logging import math import urlparse import cStringIO +import re +import string def logger(txt,method,txt2): """logging""" @@ -48,7 +51,34 @@ def serializeNode(node, encoding='utf-8' buf.close() return s +def browserCheck(self): + """check the browsers request to find out the browser type""" + bt = {} + ua = self.REQUEST.get_header("HTTP_USER_AGENT") + bt['ua'] = ua + bt['isIE'] = False + bt['isN4'] = False + if string.find(ua, 'MSIE') > -1: + bt['isIE'] = True + else: + bt['isN4'] = (string.find(ua, 'Mozilla/4.') > -1) + try: + nav = ua[string.find(ua, '('):] + ie = string.split(nav, "; ")[1] + if string.find(ie, "MSIE") > -1: + bt['versIE'] = string.split(ie, " ")[1] + except: pass + + bt['isMac'] = string.find(ua, 'Macintosh') > -1 + bt['isWin'] = string.find(ua, 'Windows') > -1 + bt['isIEWin'] = bt['isIE'] and bt['isWin'] + bt['isIEMac'] = bt['isIE'] and bt['isMac'] + bt['staticHTML'] = False + + return bt + + def getParentDir(path): """returns pathname shortened by one""" return '/'.join(path.split('/')[0:-1]) @@ -68,7 +98,7 @@ def getHttpData(url, data=None, num_trie errmsg = None for cnt in range(num_tries): try: - #logging.debug("getHttpData(#%s %ss) url=%s"%(cnt+1,timeout,url)) + logging.debug("getHttpData(#%s %ss) url=%s"%(cnt+1,timeout,url)) if sys.version_info < (2, 6): # set timeout on socket -- ugly :-( import socket @@ -79,12 +109,12 @@ def getHttpData(url, data=None, num_trie # check result? break except urllib2.HTTPError, e: - #logging.error("getHttpData: HTTP error(%s): %s"%(e.code,e)) + logging.error("getHttpData: HTTP error(%s): %s"%(e.code,e)) errmsg = str(e) # stop trying break except urllib2.URLError, e: - #logging.error("getHttpData: URLLIB error(%s): %s"%(e.reason,e)) + logging.error("getHttpData: URLLIB error(%s): %s"%(e.reason,e)) errmsg = str(e) # stop trying #break @@ -168,15 +198,35 @@ class documentViewer(Folder): def getQuery(self, **args): """get query""" return self.template.fulltextclient.getQuery(**args) + + def getPDF(self, **args): + """get query""" + return self.template.fulltextclient.getPDF(**args) def getSearch(self, **args): """get search""" return self.template.fulltextclient.getSearch(**args) - + + def getGisPlaces(self, **args): + """get gis places""" + return self.template.fulltextclient.getGisPlaces(**args) + + def getAllGisPlaces(self, **args): + """get all gis places """ + return self.template.fulltextclient.getAllGisPlaces(**args) + + def getOrigPages(self, **args): + """get original page number """ + return self.template.fulltextclient.getOrigPages(**args) + def getNumPages(self, docinfo): """get numpages""" return self.template.fulltextclient.getNumPages(docinfo) - + + def getNumTextPages(self, docinfo): + """get numpages text""" + return self.template.fulltextclient.getNumTextPages(docinfo) + def getTranslate(self, **args): """get translate""" return self.template.fulltextclient.getTranslate(**args) @@ -203,8 +253,8 @@ class documentViewer(Folder): @param viewMode: if images display images, if text display text, default is images (text,images or auto) ''' - #logging.debug("HHHHHHHHHHHHHH:load the rss") - #logger("documentViewer (index)", logging.INFO, "mode: %s url:%s start:%s pn:%s"%(mode,url,start,pn)) + logging.debug("HHHHHHHHHHHHHH:load the rss") + logger("documentViewer (index)", logging.INFO, "mode: %s url:%s start:%s pn:%s"%(mode,url,start,pn)) if not hasattr(self, 'template'): # create template folder if it doesn't exist @@ -214,7 +264,9 @@ 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) + #pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo) + pageinfo = self.getPageinfo(start=start,current=pn, docinfo=docinfo) + ''' ZDES ''' pt = getattr(self.template, 'thumbs_main_rss') if viewMode=="auto": # automodus gewaehlt @@ -226,7 +278,7 @@ 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",tocMode="thumbs",start=None,pn=1,mk=None, query=None, querySearch=None): + def index_html(self,url,mode="texttool",viewMode="auto",tocMode="thumbs",start=None,pn=1,mk=None, query=None, querySearch=None, characterNormalization=""): ''' view it @param mode: defines how to access the document behind url @@ -237,7 +289,7 @@ class documentViewer(Folder): @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)) + logging.debug("documentViewer (index) mode: %s url:%s start:%s pn:%s"%(mode,url,start,pn)) if not hasattr(self, 'template'): # this won't work @@ -259,7 +311,7 @@ class documentViewer(Folder): else: viewMode="images" - pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo,viewMode=viewMode,tocMode=tocMode) + pageinfo = self.getPageinfo(start=start,current=pn, docinfo=docinfo,viewMode=viewMode,tocMode=tocMode) pt = getattr(self.template, 'viewer_main') return pt(docinfo=docinfo,pageinfo=pageinfo,viewMode=viewMode,mk=self.generateMarks(mk)) @@ -273,8 +325,15 @@ class documentViewer(Folder): for m in mk: ret+="mk=%s"%m return ret - - + + + def getBrowser(self): + """getBrowser the version of browser """ + names="" + names = browserCheck(self) + #logging.debug("XXXXXXXXXXXXXXXX: %s"%names) + return names + def findDigilibUrl(self): """try to get the digilib URL from zogilib""" url = self.template.zogilib.getDLBaseUrl() @@ -323,7 +382,7 @@ class documentViewer(Folder): params[param] = str(val) # quote values and assemble into query string - #logging.debug("XYXXXXX: %s"%repr(params.items())) + logging.debug("XYXXXXX: %s"%repr(params.items())) ps = "&".join(["%s=%s"%(k,urllib.quote(v)) for (k, v) in params.items()]) url=self.REQUEST['URL1']+"?"+ps return url @@ -342,21 +401,21 @@ class documentViewer(Folder): def isAccessible(self, docinfo): """returns if access to the resource is granted""" access = docinfo.get('accessType', None) - #logging.debug("documentViewer (accessOK) access type %s"%access) + logging.debug("documentViewer (accessOK) access type %s"%access) if access is not None and access == 'free': - #logging.debug("documentViewer (accessOK) access is free") + logging.debug("documentViewer (accessOK) access is free") return True elif access is None or access in self.authgroups: # only local access -- only logged in users user = getSecurityManager().getUser() - #logging.debug("documentViewer (accessOK) user=%s ip=%s"%(user,self.REQUEST.getClientAddr())) + logging.debug("documentViewer (accessOK) user=%s ip=%s"%(user,self.REQUEST.getClientAddr())) if user is not None: #print "user: ", user return (user.getUserName() != "Anonymous User") else: return False - #logging.error("documentViewer (accessOK) unknown access type %s"%access) + logging.error("documentViewer (accessOK) unknown access type %s"%access) return False @@ -371,7 +430,7 @@ class documentViewer(Folder): infoUrl=self.digilibBaseUrl+"/dirInfo-xml.jsp?mo=dir&fn="+path - #logging.debug("documentViewer (getparamfromdigilib) dirInfo from %s"%(infoUrl)) + logging.debug("documentViewer (getparamfromdigilib) dirInfo from %s"%(infoUrl)) txt = getHttpData(infoUrl) if txt is None: @@ -379,7 +438,7 @@ class documentViewer(Folder): dom = Parse(txt) sizes=dom.xpath("//dir/size") - #logging.debug("documentViewer (getparamfromdigilib) dirInfo:size"%sizes) + logging.debug("documentViewer (getparamfromdigilib) dirInfo:size"%sizes) if sizes: docinfo['numPages'] = int(getTextFromNode(sizes[0])) @@ -390,10 +449,20 @@ class documentViewer(Folder): return docinfo - - def getIndexMeta(self, url): - """returns dom of index.meta document at url""" - dom = None + def getIndexMetaPath(self,url): + """gib nur den Pfad zurueck""" + regexp = re.compile(r".*(experimental|permanent)/(.*)") + regpath = regexp.match(url) + if (regpath==None): + return "" + logging.debug("(getDomFromIndexMeta): URLXAXA: %s"%regpath.group(2)) + return ("/mpiwg/online/"+regpath.group(1)+"/"+regpath.group(2)) + + + + def getIndexMetaUrl(self,url): + """returns utr of index.meta document at url""" + metaUrl = None if url.startswith("http://"): # real URL @@ -404,8 +473,15 @@ class documentViewer(Folder): metaUrl=server+url.replace("/mpiwg/online","") if not metaUrl.endswith("index.meta"): metaUrl += "/index.meta" + + return metaUrl + + def getDomFromIndexMeta(self, url): + """get dom from index meta""" + dom = None + metaUrl = self.getIndexMetaUrl(url) - #logging.debug("(getIndexMeta): METAURL: %s"%metaUrl) + logging.debug("(getDomFromIndexMeta): METAURL: %s"%metaUrl) txt=getHttpData(metaUrl) if txt is None: raise IOError("Unable to read index meta from %s"%(url)) @@ -435,7 +511,7 @@ class documentViewer(Folder): def getAuthinfoFromIndexMeta(self,path,docinfo=None,dom=None,cut=0): """gets authorization info from the index.meta file at path or given by dom""" - #logging.debug("documentViewer (getauthinfofromindexmeta) path: %s"%(path)) + logging.debug("documentViewer (getauthinfofromindexmeta) path: %s"%(path)) access = None @@ -445,7 +521,7 @@ class documentViewer(Folder): if dom is None: for x in range(cut): path=getParentDir(path) - dom = self.getIndexMeta(path) + dom = self.getDomFromIndexMeta(path) acctype = dom.xpath("//access-conditions/access/@type") if acctype and (len(acctype)>0): @@ -459,7 +535,7 @@ class documentViewer(Folder): def getBibinfoFromIndexMeta(self,path,docinfo=None,dom=None,cut=0): """gets bibliographical info from the index.meta file at path or given by dom""" - #logging.debug("documentViewer (getbibinfofromindexmeta) path: %s"%(path)) + logging.debug("documentViewer (getbibinfofromindexmeta) path: %s"%(path)) if docinfo is None: docinfo = {} @@ -467,9 +543,11 @@ class documentViewer(Folder): if dom is None: for x in range(cut): path=getParentDir(path) - dom = self.getIndexMeta(path) + dom = self.getDomFromIndexMeta(path) + + docinfo['indexMetaPath']=self.getIndexMetaPath(path); - #logging.debug("documentViewer (getbibinfofromindexmeta cutted) path: %s"%(path)) + logging.debug("documentViewer (getbibinfofromindexmeta cutted) path: %s"%(path)) # put in all raw bib fields as dict "bib" bib = dom.xpath("//bib/*") if bib and len(bib)>0: @@ -489,6 +567,8 @@ class documentViewer(Folder): bibtype=bibtype.replace("-"," ") # wrong typesiin index meta "-" instead of " " (not wrong! ROC) docinfo['bib_type'] = bibtype bibmap=metaData.generateMappingForType(bibtype) + logging.debug("documentViewer (getbibinfofromindexmeta) bibmap:"+repr(bibmap)) + logging.debug("documentViewer (getbibinfofromindexmeta) bibtype:"+repr(bibtype)) # if there is no mapping bibmap is empty (mapping sometimes has empty fields) if len(bibmap) > 0 and len(bibmap['author'][0]) > 0: try: @@ -500,7 +580,7 @@ class documentViewer(Folder): try: docinfo['year']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['year'][0])[0]) except: pass - #logging.debug("documentViewer (getbibinfofromindexmeta) using mapping for %s"%bibtype) + logging.debug("documentViewer (getbibinfofromindexmeta) using mapping for %s"%bibtype) try: docinfo['lang']=getTextFromNode(dom.xpath("//bib/lang")[0]) except: @@ -508,16 +588,30 @@ class documentViewer(Folder): return docinfo + + def getNameFromIndexMeta(self,path,docinfo=None,dom=None,cut=0): + """gets name info from the index.meta file at path or given by dom""" + if docinfo is None: + docinfo = {} + + if dom is None: + for x in range(cut): + path=getParentDir(path) + dom = self.getDomFromIndexMeta(path) + + docinfo['name']=getTextFromNode(dom.xpath("/resource/name")[0]) + logging.debug("documentViewer docinfo[name] %s"%docinfo['name']) + return docinfo def getDocinfoFromTextTool(self, url, dom=None, docinfo=None): """parse texttool tag in index meta""" - #logging.debug("documentViewer (getdocinfofromtexttool) url: %s" % (url)) + logging.debug("documentViewer (getdocinfofromtexttool) 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: - dom = self.getIndexMeta(url) + dom = self.getDomFromIndexMeta(url) archivePath = None archiveName = None @@ -538,7 +632,7 @@ class documentViewer(Folder): archivePath += "/" + archiveName else: # try to get archive-path from url - #logging.warning("documentViewer (getdocinfofromtexttool) resource/archive-path missing in: %s" % (url)) + logging.warning("documentViewer (getdocinfofromtexttool) resource/archive-path missing in: %s" % (url)) if (not url.startswith('http')): archivePath = url.replace('index.meta', '') @@ -591,10 +685,12 @@ class documentViewer(Folder): docinfo['textURLPath'] = textUrl if not docinfo['imagePath']: # text-only, no page images - docinfo = self.getNumPages(docinfo) + docinfo = self.getNumTextPages(docinfo) presentationUrls = dom.xpath("//texttool/presentation") docinfo = self.getBibinfoFromIndexMeta(url, docinfo=docinfo, dom=dom) # get info von bib tag + docinfo = self.getNameFromIndexMeta(url, docinfo=docinfo, dom=dom) + if presentationUrls and (len(presentationUrls) > 0): # ueberschreibe diese durch presentation informationen # presentation url ergiebt sich ersetzen von index.meta in der url der fuer die Metadaten @@ -632,7 +728,7 @@ class documentViewer(Folder): def getDocinfoFromImagePath(self,path,docinfo=None,cut=0): """path ist the path to the images it assumes that the index.meta file is one level higher.""" - #logging.debug("documentViewer (getdocinfofromimagepath) path: %s"%(path)) + logging.debug("documentViewer (getdocinfofromimagepath) path: %s"%(path)) if docinfo is None: docinfo = {} path=path.replace("/mpiwg/online","") @@ -642,7 +738,7 @@ class documentViewer(Folder): pathorig=path for x in range(cut): path=getParentDir(path) - #logging.debug("documentViewer (getdocinfofromimagepath) PATH:"+path) + logging.debug("documentViewer (getdocinfofromimagepath) PATH:"+path) imageUrl=self.digilibBaseUrl+"/servlet/Scaler?fn="+path docinfo['imageURL'] = imageUrl @@ -654,13 +750,13 @@ class documentViewer(Folder): def getDocinfo(self, mode, url): """returns docinfo depending on mode""" - #logging.debug("documentViewer (getdocinfo) mode: %s, url: %s"%(mode,url)) + logging.debug("documentViewer (getdocinfo) mode: %s, url: %s"%(mode,url)) # look for cached docinfo in session if self.REQUEST.SESSION.has_key('docinfo'): docinfo = self.REQUEST.SESSION['docinfo'] # check if its still current if docinfo is not None and docinfo.get('mode') == mode and docinfo.get('url') == url: - #logging.debug("documentViewer (getdocinfo) docinfo in session: %s"%docinfo) + logging.debug("documentViewer (getdocinfo) docinfo in session: %s"%docinfo) return docinfo # new docinfo docinfo = {'mode': mode, 'url': url} @@ -671,17 +767,19 @@ class documentViewer(Folder): elif mode=="filepath": docinfo = self.getDocinfoFromImagePath(url, docinfo=docinfo,cut=1) else: - #logging.error("documentViewer (getdocinfo) unknown mode: %s!"%mode) + logging.error("documentViewer (getdocinfo) unknown mode: %s!"%mode) raise ValueError("Unknown mode %s! Has to be one of 'texttool','imagepath','filepath'."%(mode)) - #logging.debug("documentViewer (getdocinfo) docinfo: %s"%docinfo) + logging.debug("documentViewer (getdocinfo) docinfo: %s"%docinfo) + #logging.debug("documentViewer (getdocinfo) docinfo: %s"%) self.REQUEST.SESSION['docinfo'] = docinfo return docinfo - def getPageinfo(self, current, start=None, rows=None, cols=None, docinfo=None, viewMode=None, tocMode=None): + def getPageinfo(self, current, start=None, rows=None, cols=None, docinfo=None, viewMode=None, tocMode=None,characterNormalization=""): """returns pageinfo with the given parameters""" pageinfo = {} current = getInt(current) + pageinfo['current'] = current rows = int(rows or self.thumbrows) pageinfo['rows'] = rows @@ -701,9 +799,11 @@ class documentViewer(Folder): pageinfo['numgroups'] += 1 pageinfo['viewMode'] = viewMode pageinfo['tocMode'] = tocMode + #pageinfo ['originalPage'] = self.getOrigPages(docinfo=None, pageinfo=None) pageinfo['characterNormalization'] = self.REQUEST.get('characterNormalization','') - pageinfo['query'] = self.REQUEST.get('query',' ') - pageinfo['queryType'] = self.REQUEST.get('queryType',' ') + pageinfo['query'] = self.REQUEST.get('query','') + #pageinfo['optionsClose']= self.REQUEST.get('optionsClose','') + 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','') @@ -713,6 +813,8 @@ class documentViewer(Folder): toc = int (pageinfo['tocPN']) pageinfo['textPages'] =int (toc) + + if 'tocSize_%s'%tocMode in docinfo: tocSize = int(docinfo['tocSize_%s'%tocMode]) tocPageSize = int(pageinfo['tocPageSize']) @@ -767,7 +869,7 @@ def manage_addDocumentViewerTemplate(sel self._setObject(id, DocumentViewerTemplate(id)) ob = getattr(self, id) txt=file(os.path.join(package_home(globals()),'zpt/viewer_main.zpt'),'r').read() - #logging.info("txt %s:"%txt) + logging.info("txt %s:"%txt) ob.pt_edit(txt,"text/html") if title: ob.pt_setTitle(title)