Annotation of documentViewer/documentViewer.py, revision 1.69.2.6

1.18      dwinter     1: 
1.1       dwinter     2: from OFS.Folder import Folder
                      3: from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
1.22      dwinter     4: from Products.PageTemplates.PageTemplateFile import PageTemplateFile 
1.1       dwinter     5: from AccessControl import ClassSecurityInfo
1.8       casties     6: from AccessControl import getSecurityManager
1.1       dwinter     7: from Globals import package_home
                      8: 
1.11      casties     9: from Ft.Xml import EMPTY_NAMESPACE, Parse
1.1       dwinter    10: import os.path
1.7       casties    11: import sys
1.1       dwinter    12: import urllib
1.69.2.5  casties    13: import urllib2
1.20      dwinter    14: import logging
1.43      casties    15: 
1.22      dwinter    16: def logger(txt,method,txt2):
                     17:     """logging"""
                     18:     logging.info(txt+ txt2)
                     19:     
                     20:     
1.4       casties    21: def getInt(number, default=0):
                     22:     """returns always an int (0 in case of problems)"""
                     23:     try:
                     24:         return int(number)
                     25:     except:
1.29      casties    26:         return int(default)
1.4       casties    27: 
1.1       dwinter    28: def getTextFromNode(nodename):
1.18      dwinter    29:     """get the cdata content of a node"""
1.8       casties    30:     if nodename is None:
                     31:         return ""
1.1       dwinter    32:     nodelist=nodename.childNodes
                     33:     rc = ""
                     34:     for node in nodelist:
                     35:         if node.nodeType == node.TEXT_NODE:
                     36:            rc = rc + node.data
                     37:     return rc
                     38: 
1.43      casties    39: def serializeNode(node, encoding='utf-8'):
                     40:     """returns a string containing node as XML"""
                     41:     buf = cStringIO.StringIO()
                     42:     Print(node, stream=buf, encoding=encoding)
                     43:     s = buf.getvalue()
                     44:     buf.close()
                     45:     return s
                     46: 
1.9       casties    47:         
                     48: def getParentDir(path):
                     49:     """returns pathname shortened by one"""
                     50:     return '/'.join(path.split('/')[0:-1])
                     51:         
                     52: 
1.69.2.3  casties    53: def getHttpData(url, data=None, num_tries=3, timeout=10):
                     54:     """returns result from url+data HTTP request"""
                     55:     # we do GET (by appending data to url)
                     56:     if isinstance(data, str) or isinstance(data, unicode):
                     57:         # if data is string then append
                     58:         url = "%s?%s"%(url,data)
                     59:     elif isinstance(data, dict) or isinstance(data, list) or isinstance(data, tuple):
                     60:         # urlencode
                     61:         url = "%s?%s"%(url,urllib.urlencode(data))
                     62:     
                     63:     response = None
                     64:     errmsg = None
                     65:     for cnt in range(num_tries):
                     66:         try:
                     67:             logging.debug("getHttpData(#%s %ss) url=%s"%(cnt+1,timeout,url))
                     68:             if sys.version_info < (2, 6):
                     69:                 # set timeout on socket -- ugly :-(
                     70:                 import socket
                     71:                 socket.setdefaulttimeout(timeout)
                     72:                 response = urllib2.urlopen(url)
                     73:             else:
                     74:                 response = urllib2.urlopen(url,timeout=float(timeout))
                     75:             # check result?
                     76:             break
                     77:         except urllib2.HTTPError, e:
                     78:             logging.error("getHttpData: HTTP error(%s): %s"%(e.code,e))
                     79:             errmsg = str(e)
                     80:             # stop trying
                     81:             break
                     82:         except urllib2.URLError, e:
                     83:             logging.error("getHttpData: URLLIB error(%s): %s"%(e.reason,e))
                     84:             errmsg = str(e)
                     85:             # stop trying
                     86:             #break
                     87: 
                     88:     if response is not None:
                     89:         data = response.read()
                     90:         response.close()
                     91:         return data
                     92:     
                     93:     raise IOError("ERROR fetching HTTP data from %s: %s"%(url,errmsg))
                     94:     #return None
1.1       dwinter    95: 
                     96: 
                     97: 
1.3       casties    98: ##
                     99: ## documentViewer class
                    100: ##
1.69.2.3  casties   101: class documentViewer(Folder):
1.1       dwinter   102:     """document viewer"""
                    103:     meta_type="Document viewer"
                    104:     
                    105:     security=ClassSecurityInfo()
1.3       casties   106:     manage_options=Folder.manage_options+(
1.1       dwinter   107:         {'label':'main config','action':'changeDocumentViewerForm'},
                    108:         )
                    109: 
1.3       casties   110:     # templates and forms
                    111:     viewer_main = PageTemplateFile('zpt/viewer_main', globals())
1.44      casties   112:     toc_thumbs = PageTemplateFile('zpt/toc_thumbs', globals())
                    113:     toc_text = PageTemplateFile('zpt/toc_text', globals())
                    114:     toc_figures = PageTemplateFile('zpt/toc_figures', globals())
1.43      casties   115:     page_main_images = PageTemplateFile('zpt/page_main_images', globals())
                    116:     page_main_text = PageTemplateFile('zpt/page_main_text', globals())
1.44      casties   117:     page_main_text_dict = PageTemplateFile('zpt/page_main_text_dict', globals())
1.48      abukhman  118:     page_main_xml = PageTemplateFile('zpt/page_main_xml', globals())
1.3       casties   119:     head_main = PageTemplateFile('zpt/head_main', globals())
                    120:     docuviewer_css = PageTemplateFile('css/docuviewer.css', globals())
1.26      casties   121:     info_xml = PageTemplateFile('zpt/info_xml', globals())
1.69.2.1  abukhman  122:     
                    123:     
1.32      dwinter   124:     thumbs_main_rss = PageTemplateFile('zpt/thumbs_main_rss', globals())
1.3       casties   125:     security.declareProtected('View management screens','changeDocumentViewerForm')    
                    126:     changeDocumentViewerForm = PageTemplateFile('zpt/changeDocumentViewer', globals())
                    127: 
1.1       dwinter   128:     
1.45      abukhman  129:     def __init__(self,id,imageScalerUrl=None,textServerName=None,title="",digilibBaseUrl=None,thumbcols=2,thumbrows=5,authgroups="mpiwg"):
1.1       dwinter   130:         """init document viewer"""
                    131:         self.id=id
                    132:         self.title=title
1.4       casties   133:         self.thumbcols = thumbcols
                    134:         self.thumbrows = thumbrows
1.8       casties   135:         # authgroups is list of authorized groups (delimited by ,)
                    136:         self.authgroups = [s.strip().lower() for s in authgroups.split(',')]
1.43      casties   137:         # create template folder so we can always use template.something
                    138:         
                    139:         templateFolder = Folder('template')
                    140:         #self['template'] = templateFolder # Zope-2.12 style
                    141:         self._setObject('template',templateFolder) # old style
                    142:         try:
1.69.2.3  casties   143:             import MpdlXmlTextServer
                    144:             textServer = MpdlXmlTextServer(id='fulltextclient')
1.43      casties   145:             #templateFolder['fulltextclient'] = xmlRpcClient
1.69.2.3  casties   146:             templateFolder._setObject('fulltextclient',textServer)
1.43      casties   147:         except Exception, e:
1.69.2.3  casties   148:             logging.error("Unable to create MpdlXmlTextServer for fulltextclient: "+str(e))
1.43      casties   149:         try:
                    150:             from Products.zogiLib.zogiLib import zogiLib
                    151:             zogilib = zogiLib(id="zogilib", title="zogilib for docuviewer", dlServerURL=imageScalerUrl, layout="book")
                    152:             #templateFolder['zogilib'] = zogilib
                    153:             templateFolder._setObject('zogilib',zogilib)
                    154:         except Exception, e:
                    155:             logging.error("Unable to create zogiLib for zogilib: "+str(e))
                    156:         
1.69.2.3  casties   157:         
                    158:     # proxy text server methods to fulltextclient
                    159:     def getTextPage(self, **args):
                    160:         """get page"""
                    161:         return self.template.fulltextclient.getTextPage(**args)
                    162: 
                    163:     def getQuery(self, **args):
                    164:         """get query"""
                    165:         return self.template.fulltextclient.getQuery(**args)
                    166: 
                    167:     def getSearch(self, **args):
                    168:         """get search"""
                    169:         return self.template.fulltextclient.getSearch(**args)
                    170: 
                    171:     def getNumPages(self, **args):
                    172:         """get numpages"""
                    173:         return self.template.fulltextclient.getNumPages(**args)
                    174: 
                    175:     def getTranslate(self, **args):
                    176:         """get translate"""
                    177:         return self.template.fulltextclient.getTranslate(**args)
                    178: 
                    179:     def getLemma(self, **args):
                    180:         """get lemma"""
                    181:         return self.template.fulltextclient.getLemma(**args)
                    182: 
                    183:     def getToc(self, **args):
                    184:         """get toc"""
                    185:         return self.template.fulltextclient.getToc(**args)
                    186: 
                    187:     def getTocPage(self, **args):
                    188:         """get tocpage"""
                    189:         return self.template.fulltextclient.getTocPage(**args)
1.3       casties   190: 
1.69.2.3  casties   191:     
1.32      dwinter   192:     security.declareProtected('View','thumbs_rss')
                    193:     def thumbs_rss(self,mode,url,viewMode="auto",start=None,pn=1):
                    194:         '''
                    195:         view it
                    196:         @param mode: defines how to access the document behind url 
                    197:         @param url: url which contains display information
                    198:         @param viewMode: if images display images, if text display text, default is images (text,images or auto)
                    199:         
                    200:         '''
1.43      casties   201:         logging.debug("HHHHHHHHHHHHHH:load the rss")
1.32      dwinter   202:         logger("documentViewer (index)", logging.INFO, "mode: %s url:%s start:%s pn:%s"%(mode,url,start,pn))
                    203:         
                    204:         if not hasattr(self, 'template'):
                    205:             # create template folder if it doesn't exist
                    206:             self.manage_addFolder('template')
                    207:             
                    208:         if not self.digilibBaseUrl:
                    209:             self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary"
                    210:             
                    211:         docinfo = self.getDocinfo(mode=mode,url=url)
                    212:         pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo)
                    213:         pt = getattr(self.template, 'thumbs_main_rss')
                    214:         
                    215:         if viewMode=="auto": # automodus gewaehlt
                    216:             if docinfo.get("textURL",'') and self.textViewerUrl: #texturl gesetzt und textViewer konfiguriert
                    217:                 viewMode="text"
                    218:             else:
                    219:                 viewMode="images"
                    220:                
                    221:         return pt(docinfo=docinfo,pageinfo=pageinfo,viewMode=viewMode)
                    222:   
1.3       casties   223:     security.declareProtected('View','index_html')
1.45      abukhman  224:     def index_html(self,url,mode="texttool",viewMode="auto",tocMode="thumbs",start=None,pn=1,mk=None, query=None, querySearch=None):
1.3       casties   225:         '''
                    226:         view it
1.26      casties   227:         @param mode: defines how to access the document behind url 
1.3       casties   228:         @param url: url which contains display information
1.44      casties   229:         @param viewMode: if images display images, if text display text, default is auto (text,images or auto)
1.48      abukhman  230:         @param tocMode: type of 'table of contents' for navigation (thumbs, text, figures, none)
1.49      abukhman  231:         @param querySearch: type of different search modes (fulltext, fulltextMorph, xpath, xquery, ftIndex, ftIndexMorph, fulltextMorphLemma)
1.3       casties   232:         '''
                    233:         
1.43      casties   234:         logging.debug("documentViewer (index) mode: %s url:%s start:%s pn:%s"%(mode,url,start,pn))
1.1       dwinter   235:         
1.3       casties   236:         if not hasattr(self, 'template'):
1.43      casties   237:             # this won't work
                    238:             logging.error("template folder missing!")
                    239:             return "ERROR: template folder missing!"
1.3       casties   240:             
1.43      casties   241:         if not getattr(self, 'digilibBaseUrl', None):
1.3       casties   242:             self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary"
                    243:             
1.4       casties   244:         docinfo = self.getDocinfo(mode=mode,url=url)
1.47      abukhman  245:         
                    246:         
1.44      casties   247:         if tocMode != "thumbs":
                    248:             # get table of contents
                    249:             docinfo = self.getToc(mode=tocMode, docinfo=docinfo)
1.47      abukhman  250:             
1.21      dwinter   251:         if viewMode=="auto": # automodus gewaehlt
1.43      casties   252:             if docinfo.get("textURL",''): #texturl gesetzt und textViewer konfiguriert
1.68      casties   253:                 viewMode="text_dict"
1.21      dwinter   254:             else:
                    255:                 viewMode="images"
1.44      casties   256:                 
1.68      casties   257:         pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo,viewMode=viewMode,tocMode=tocMode)
                    258:         
1.44      casties   259:         pt = getattr(self.template, 'viewer_main')               
1.37      dwinter   260:         return pt(docinfo=docinfo,pageinfo=pageinfo,viewMode=viewMode,mk=self.generateMarks(mk))
1.1       dwinter   261:   
1.36      dwinter   262:     def generateMarks(self,mk):
                    263:         ret=""
1.44      casties   264:         if mk is None:
                    265:             return ""
                    266:        if type(mk) is not ListType:
                    267:            mk=[mk]
1.36      dwinter   268:         for m in mk:
1.37      dwinter   269:             ret+="mk=%s"%m
1.36      dwinter   270:         return ret
1.43      casties   271: 
1.44      casties   272: 
1.43      casties   273:     def findDigilibUrl(self):
                    274:         """try to get the digilib URL from zogilib"""
                    275:         url = self.template.zogilib.getDLBaseUrl()
                    276:         return url
1.67      casties   277: 
                    278:     def getDocumentViewerURL(self):
                    279:         """returns the URL of this instance"""
                    280:         return self.absolute_url()
1.43      casties   281:     
                    282:     def getStyle(self, idx, selected, style=""):
                    283:         """returns a string with the given style and append 'sel' if path == selected."""
                    284:         #logger("documentViewer (getstyle)", logging.INFO, "idx: %s selected: %s style: %s"%(idx,selected,style))
                    285:         if idx == selected:
                    286:             return style + 'sel'
                    287:         else:
                    288:             return style
1.36      dwinter   289:     
1.4       casties   290:     def getLink(self,param=None,val=None):
                    291:         """link to documentviewer with parameter param set to val"""
1.9       casties   292:         params=self.REQUEST.form.copy()
1.4       casties   293:         if param is not None:
1.7       casties   294:             if val is None:
                    295:                 if params.has_key(param):
                    296:                     del params[param]
1.4       casties   297:             else:
1.9       casties   298:                 params[param] = str(val)
1.43      casties   299:                 
                    300:         if params.get("mode", None) == "filepath": #wenn beim erst Aufruf filepath gesetzt wurde aendere das nun zu imagepath
1.38      dwinter   301:                 params["mode"] = "imagepath"
                    302:                 params["url"] = getParentDir(params["url"])
1.7       casties   303:                 
1.9       casties   304:         # quote values and assemble into query string
                    305:         ps = "&".join(["%s=%s"%(k,urllib.quote(v)) for (k, v) in params.items()])
                    306:         url=self.REQUEST['URL1']+"?"+ps
1.4       casties   307:         return url
                    308: 
1.32      dwinter   309:     def getLinkAmp(self,param=None,val=None):
                    310:         """link to documentviewer with parameter param set to val"""
                    311:         params=self.REQUEST.form.copy()
                    312:         if param is not None:
                    313:             if val is None:
                    314:                 if params.has_key(param):
                    315:                     del params[param]
                    316:             else:
                    317:                 params[param] = str(val)
                    318:                 
                    319:         # quote values and assemble into query string
1.69.2.2  casties   320:         logging.debug("XYXXXXX: %s"%repr(params.items()))
1.32      dwinter   321:         ps = "&amp;".join(["%s=%s"%(k,urllib.quote(v)) for (k, v) in params.items()])
                    322:         url=self.REQUEST['URL1']+"?"+ps
                    323:         return url
1.40      casties   324:     
1.26      casties   325:     def getInfo_xml(self,url,mode):
                    326:         """returns info about the document as XML"""
                    327: 
                    328:         if not self.digilibBaseUrl:
                    329:             self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary"
                    330:         
                    331:         docinfo = self.getDocinfo(mode=mode,url=url)
                    332:         pt = getattr(self.template, 'info_xml')
                    333:         return pt(docinfo=docinfo)
                    334: 
1.4       casties   335:     
1.9       casties   336:     def isAccessible(self, docinfo):
1.8       casties   337:         """returns if access to the resource is granted"""
                    338:         access = docinfo.get('accessType', None)
1.69.2.2  casties   339:         logging.debug("documentViewer (accessOK) access type %s"%access)
1.17      casties   340:         if access is not None and access == 'free':
1.69.2.2  casties   341:             logging.debug("documentViewer (accessOK) access is free")
1.8       casties   342:             return True
1.17      casties   343:         elif access is None or access in self.authgroups:
1.9       casties   344:             # only local access -- only logged in users
                    345:             user = getSecurityManager().getUser()
                    346:             if user is not None:
                    347:                 #print "user: ", user
                    348:                 return (user.getUserName() != "Anonymous User")
                    349:             else:
                    350:                 return False
1.8       casties   351:         
1.69.2.2  casties   352:         logging.debug("documentViewer (accessOK) unknown access type %s"%access)
1.8       casties   353:         return False
1.9       casties   354:     
1.8       casties   355:                 
1.35      dwinter   356:     def getDirinfoFromDigilib(self,path,docinfo=None,cut=0):
1.6       casties   357:         """gibt param von dlInfo aus"""
1.7       casties   358:         if docinfo is None:
                    359:             docinfo = {}
1.35      dwinter   360:         
                    361:         for x in range(cut):
1.38      dwinter   362:                
1.35      dwinter   363:                 path=getParentDir(path)
1.38      dwinter   364:        
1.13      casties   365:         infoUrl=self.digilibBaseUrl+"/dirInfo-xml.jsp?mo=dir&fn="+path
1.6       casties   366:     
1.69.2.2  casties   367:         logging.debug("documentViewer (getparamfromdigilib) dirInfo from %s"%(infoUrl))
1.6       casties   368:         
1.69.2.3  casties   369:         txt = getHttpData(infoUrl)
                    370:         if txt is None:
1.13      casties   371:             raise IOError("Unable to get dir-info from %s"%(infoUrl))
1.69.2.3  casties   372: 
                    373:         dom = Parse(txt)
1.10      casties   374:         sizes=dom.xpath("//dir/size")
1.69.2.2  casties   375:         logging.debug("documentViewer (getparamfromdigilib) dirInfo:size"%sizes)
1.6       casties   376:         
1.10      casties   377:         if sizes:
                    378:             docinfo['numPages'] = int(getTextFromNode(sizes[0]))
1.7       casties   379:         else:
                    380:             docinfo['numPages'] = 0
1.43      casties   381:             
                    382:         # TODO: produce and keep list of image names and numbers
1.7       casties   383:                         
                    384:         return docinfo
1.8       casties   385:     
1.6       casties   386:             
1.9       casties   387:     def getIndexMeta(self, url):
                    388:         """returns dom of index.meta document at url"""
                    389:         dom = None
1.12      casties   390:         metaUrl = None
1.9       casties   391:         if url.startswith("http://"):
                    392:             # real URL
1.12      casties   393:             metaUrl = url
1.9       casties   394:         else:
                    395:             # online path
                    396:             server=self.digilibBaseUrl+"/servlet/Texter?fn="
1.13      casties   397:             metaUrl=server+url.replace("/mpiwg/online","")
1.9       casties   398:             if not metaUrl.endswith("index.meta"):
                    399:                 metaUrl += "/index.meta"
1.12      casties   400:                 
1.69.2.3  casties   401:         logging.debug("(getIndexMeta): METAURL: %s"%metaUrl)
                    402:         txt=getHttpData(metaUrl)
                    403:         if txt is None:
1.12      casties   404:             raise IOError("Unable to read index meta from %s"%(url))
1.69.2.3  casties   405:         
                    406:         dom = Parse(txt)
1.9       casties   407:         return dom
1.20      dwinter   408:     
                    409:     def getPresentationInfoXML(self, url):
                    410:         """returns dom of info.xml document at url"""
                    411:         dom = None
                    412:         metaUrl = None
                    413:         if url.startswith("http://"):
                    414:             # real URL
                    415:             metaUrl = url
                    416:         else:
                    417:             # online path
                    418:             server=self.digilibBaseUrl+"/servlet/Texter?fn="
                    419:             metaUrl=server+url.replace("/mpiwg/online","")
                    420:         
1.69.2.3  casties   421:         txt=getHttpData(metaUrl)
                    422:         if txt is None:
1.20      dwinter   423:             raise IOError("Unable to read infoXMLfrom %s"%(url))
1.69.2.3  casties   424:             
                    425:         dom = Parse(txt)
1.20      dwinter   426:         return dom
1.9       casties   427:                         
                    428:         
1.33      dwinter   429:     def getAuthinfoFromIndexMeta(self,path,docinfo=None,dom=None,cut=0):
1.9       casties   430:         """gets authorization info from the index.meta file at path or given by dom"""
1.69.2.2  casties   431:         logging.debug("documentViewer (getauthinfofromindexmeta) path: %s"%(path))
1.8       casties   432:         
                    433:         access = None
                    434:         
                    435:         if docinfo is None:
                    436:             docinfo = {}
                    437:             
                    438:         if dom is None:
1.38      dwinter   439:             for x in range(cut):
1.33      dwinter   440:                 path=getParentDir(path)
                    441:             dom = self.getIndexMeta(path)
1.18      dwinter   442:        
1.8       casties   443:         acctype = dom.xpath("//access-conditions/access/@type")
                    444:         if acctype and (len(acctype)>0):
                    445:             access=acctype[0].value
1.9       casties   446:             if access in ['group', 'institution']:
1.8       casties   447:                 access = getTextFromNode(dom.xpath("//access-conditions/access/name")[0]).lower()
                    448:             
                    449:         docinfo['accessType'] = access
                    450:         return docinfo
1.6       casties   451:     
1.8       casties   452:         
1.33      dwinter   453:     def getBibinfoFromIndexMeta(self,path,docinfo=None,dom=None,cut=0):
1.9       casties   454:         """gets bibliographical info from the index.meta file at path or given by dom"""
1.27      casties   455:         logging.debug("documentViewer (getbibinfofromindexmeta) path: %s"%(path))
1.2       dwinter   456:         
1.3       casties   457:         if docinfo is None:
                    458:             docinfo = {}
1.38      dwinter   459:         
1.3       casties   460:         if dom is None:
1.38      dwinter   461:             for x in range(cut):
1.33      dwinter   462:                 path=getParentDir(path)
                    463:             dom = self.getIndexMeta(path)
1.39      dwinter   464:         
                    465:         logging.debug("documentViewer (getbibinfofromindexmeta cutted) path: %s"%(path))
1.27      casties   466:         # put in all raw bib fields as dict "bib"
                    467:         bib = dom.xpath("//bib/*")
                    468:         if bib and len(bib)>0:
                    469:             bibinfo = {}
                    470:             for e in bib:
                    471:                 bibinfo[e.localName] = getTextFromNode(e)
                    472:             docinfo['bib'] = bibinfo
                    473:         
                    474:         # extract some fields (author, title, year) according to their mapping
1.4       casties   475:         metaData=self.metadata.main.meta.bib
                    476:         bibtype=dom.xpath("//bib/@type")
                    477:         if bibtype and (len(bibtype)>0):
                    478:             bibtype=bibtype[0].value
1.2       dwinter   479:         else:
1.4       casties   480:             bibtype="generic"
1.27      casties   481:             
1.4       casties   482:         bibtype=bibtype.replace("-"," ") # wrong typesiin index meta "-" instead of " " (not wrong! ROC)
1.27      casties   483:         docinfo['bib_type'] = bibtype
1.4       casties   484:         bibmap=metaData.generateMappingForType(bibtype)
1.8       casties   485:         # if there is no mapping bibmap is empty (mapping sometimes has empty fields)
1.7       casties   486:         if len(bibmap) > 0 and len(bibmap['author'][0]) > 0:
1.30      casties   487:             try:
                    488:                 docinfo['author']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['author'][0])[0])
                    489:             except: pass
                    490:             try:
                    491:                 docinfo['title']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['title'][0])[0])
                    492:             except: pass
                    493:             try:
                    494:                 docinfo['year']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['year'][0])[0])
                    495:             except: pass
1.27      casties   496:             logging.debug("documentViewer (getbibinfofromindexmeta) using mapping for %s"%bibtype)
1.22      dwinter   497:             try:
                    498:                 docinfo['lang']=getTextFromNode(dom.xpath("//bib/lang")[0])
                    499:             except:
                    500:                 docinfo['lang']=''
1.27      casties   501: 
1.3       casties   502:         return docinfo
1.42      abukhman  503:     
                    504:     
1.43      casties   505:     def getDocinfoFromTextTool(self, url, dom=None, docinfo=None):
                    506:         """parse texttool tag in index meta"""
1.69.2.2  casties   507:         logging.debug("documentViewer (getdocinfofromtexttool) url: %s" % (url))
1.43      casties   508:         if docinfo is None:
                    509:            docinfo = {}
                    510:         if docinfo.get('lang', None) is None:
                    511:             docinfo['lang'] = '' # default keine Sprache gesetzt
                    512:         if dom is None:
                    513:             dom = self.getIndexMeta(url)
                    514:         
                    515:         archivePath = None
                    516:         archiveName = None
                    517:     
                    518:         archiveNames = dom.xpath("//resource/name")
                    519:         if archiveNames and (len(archiveNames) > 0):
                    520:             archiveName = getTextFromNode(archiveNames[0])
                    521:         else:
1.69.2.2  casties   522:             logging.warning("documentViewer (getdocinfofromtexttool) resource/name missing in: %s" % (url))
1.43      casties   523:         
                    524:         archivePaths = dom.xpath("//resource/archive-path")
                    525:         if archivePaths and (len(archivePaths) > 0):
                    526:             archivePath = getTextFromNode(archivePaths[0])
                    527:             # clean up archive path
                    528:             if archivePath[0] != '/':
                    529:                 archivePath = '/' + archivePath
                    530:             if archiveName and (not archivePath.endswith(archiveName)):
                    531:                 archivePath += "/" + archiveName
                    532:         else:
                    533:             # try to get archive-path from url
1.69.2.2  casties   534:             logging.warning("documentViewer (getdocinfofromtexttool) resource/archive-path missing in: %s" % (url))
1.43      casties   535:             if (not url.startswith('http')):
                    536:                 archivePath = url.replace('index.meta', '')
                    537:                 
                    538:         if archivePath is None:
                    539:             # we balk without archive-path
                    540:             raise IOError("Missing archive-path (for text-tool) in %s" % (url))
                    541:         
                    542:         imageDirs = dom.xpath("//texttool/image")
                    543:         if imageDirs and (len(imageDirs) > 0):
                    544:             imageDir = getTextFromNode(imageDirs[0])
                    545:             
                    546:         else:
                    547:             # we balk with no image tag / not necessary anymore because textmode is now standard
                    548:             #raise IOError("No text-tool info in %s"%(url))
                    549:             imageDir = ""
                    550:             #xquery="//pb"  
                    551:             docinfo['imagePath'] = "" # keine Bilder
                    552:             docinfo['imageURL'] = ""
                    553:             
                    554:         if imageDir and archivePath:
                    555:             #print "image: ", imageDir, " archivepath: ", archivePath
                    556:             imageDir = os.path.join(archivePath, imageDir)
                    557:             imageDir = imageDir.replace("/mpiwg/online", '')
                    558:             docinfo = self.getDirinfoFromDigilib(imageDir, docinfo=docinfo)
                    559:             docinfo['imagePath'] = imageDir
                    560:             
                    561:             docinfo['imageURL'] = self.digilibBaseUrl + "/servlet/Scaler?fn=" + imageDir
                    562:             
                    563:         viewerUrls = dom.xpath("//texttool/digiliburlprefix")
                    564:         if viewerUrls and (len(viewerUrls) > 0):
                    565:             viewerUrl = getTextFromNode(viewerUrls[0])
                    566:             docinfo['viewerURL'] = viewerUrl
                    567:                    
                    568:         textUrls = dom.xpath("//texttool/text")
                    569:         if textUrls and (len(textUrls) > 0):
                    570:             textUrl = getTextFromNode(textUrls[0])
                    571:             if urlparse.urlparse(textUrl)[0] == "": #keine url
                    572:                 textUrl = os.path.join(archivePath, textUrl) 
                    573:             # fix URLs starting with /mpiwg/online
                    574:             if textUrl.startswith("/mpiwg/online"):
                    575:                 textUrl = textUrl.replace("/mpiwg/online", '', 1)
                    576:             
                    577:             docinfo['textURL'] = textUrl
                    578:     
                    579:         textUrls = dom.xpath("//texttool/text-url-path")
                    580:         if textUrls and (len(textUrls) > 0):
                    581:             textUrl = getTextFromNode(textUrls[0])
1.51      casties   582:             docinfo['textURLPath'] = textUrl
                    583:             if not docinfo['imagePath']:
                    584:                 # text-only, no page images
                    585:                 docinfo = self.getNumPages(docinfo) #im moment einfach auf eins setzen, navigation ueber die thumbs geht natuerlich nicht    
1.43      casties   586:          
                    587:         presentationUrls = dom.xpath("//texttool/presentation")
                    588:         docinfo = self.getBibinfoFromIndexMeta(url, docinfo=docinfo, dom=dom)   # get info von bib tag
                    589:         
                    590:         if presentationUrls and (len(presentationUrls) > 0): # ueberschreibe diese durch presentation informationen 
                    591:              # presentation url ergiebt sich ersetzen von index.meta in der url der fuer die Metadaten
                    592:              # durch den relativen Pfad auf die presentation infos
                    593:             presentationPath = getTextFromNode(presentationUrls[0])
                    594:             if url.endswith("index.meta"): 
                    595:                 presentationUrl = url.replace('index.meta', presentationPath)
                    596:             else:
                    597:                 presentationUrl = url + "/" + presentationPath
1.51      casties   598:                 
1.43      casties   599:             docinfo = self.getBibinfoFromTextToolPresentation(presentationUrl, docinfo=docinfo, dom=dom)
                    600:     
                    601:         docinfo = self.getAuthinfoFromIndexMeta(url, docinfo=docinfo, dom=dom)   # get access info
1.3       casties   602:         
1.43      casties   603:         return docinfo
1.3       casties   604:    
1.20      dwinter   605:    
                    606:     def getBibinfoFromTextToolPresentation(self,url,docinfo=None,dom=None):
                    607:         """gets the bibliographical information from the preseantion entry in texttools
                    608:         """
                    609:         dom=self.getPresentationInfoXML(url)
1.29      casties   610:         try:
                    611:             docinfo['author']=getTextFromNode(dom.xpath("//author")[0])
                    612:         except:
                    613:             pass
                    614:         try:
                    615:             docinfo['title']=getTextFromNode(dom.xpath("//title")[0])
                    616:         except:
                    617:             pass
                    618:         try:
                    619:             docinfo['year']=getTextFromNode(dom.xpath("//date")[0])
                    620:         except:
                    621:             pass
1.20      dwinter   622:         return docinfo
                    623:     
1.33      dwinter   624:     def getDocinfoFromImagePath(self,path,docinfo=None,cut=0):
1.3       casties   625:         """path ist the path to the images it assumes that the index.meta file is one level higher."""
1.69.2.2  casties   626:         logging.debug("documentViewer (getdocinfofromimagepath) path: %s"%(path))
1.3       casties   627:         if docinfo is None:
                    628:             docinfo = {}
1.6       casties   629:         path=path.replace("/mpiwg/online","")
1.3       casties   630:         docinfo['imagePath'] = path
1.35      dwinter   631:         docinfo=self.getDirinfoFromDigilib(path,docinfo=docinfo,cut=cut)
1.38      dwinter   632:         
1.39      dwinter   633:         pathorig=path
1.38      dwinter   634:         for x in range(cut):       
                    635:                 path=getParentDir(path)
1.69.2.2  casties   636:         logging.debug("documentViewer (getdocinfofromimagepath) PATH:"+path)
1.7       casties   637:         imageUrl=self.digilibBaseUrl+"/servlet/Scaler?fn="+path
1.3       casties   638:         docinfo['imageURL'] = imageUrl
                    639:         
1.39      dwinter   640:         #path ist the path to the images it assumes that the index.meta file is one level higher.
                    641:         docinfo = self.getBibinfoFromIndexMeta(pathorig,docinfo=docinfo,cut=cut+1)
                    642:         docinfo = self.getAuthinfoFromIndexMeta(pathorig,docinfo=docinfo,cut=cut+1)
1.3       casties   643:         return docinfo
                    644:     
1.2       dwinter   645:     
1.3       casties   646:     def getDocinfo(self, mode, url):
                    647:         """returns docinfo depending on mode"""
1.69.2.2  casties   648:         logging.debug("documentViewer (getdocinfo) mode: %s, url: %s"%(mode,url))
1.3       casties   649:         # look for cached docinfo in session
1.21      dwinter   650:         if self.REQUEST.SESSION.has_key('docinfo'):
1.3       casties   651:             docinfo = self.REQUEST.SESSION['docinfo']
                    652:             # check if its still current
                    653:             if docinfo is not None and docinfo.get('mode') == mode and docinfo.get('url') == url:
1.69.2.2  casties   654:                 logging.debug("documentViewer (getdocinfo) docinfo in session: %s"%docinfo)
1.3       casties   655:                 return docinfo
                    656:         # new docinfo
                    657:         docinfo = {'mode': mode, 'url': url}
                    658:         if mode=="texttool": #index.meta with texttool information
                    659:             docinfo = self.getDocinfoFromTextTool(url, docinfo=docinfo)
                    660:         elif mode=="imagepath":
                    661:             docinfo = self.getDocinfoFromImagePath(url, docinfo=docinfo)
1.33      dwinter   662:         elif mode=="filepath":
1.37      dwinter   663:             docinfo = self.getDocinfoFromImagePath(url, docinfo=docinfo,cut=1)
1.3       casties   664:         else:
1.69.2.2  casties   665:             logging.error("documentViewer (getdocinfo) unknown mode: %s!"%mode)
1.44      casties   666:             raise ValueError("Unknown mode %s! Has to be one of 'texttool','imagepath','filepath'."%(mode))
1.10      casties   667:                         
1.69.2.2  casties   668:         logging.debug("documentViewer (getdocinfo) docinfo: %s"%docinfo)
1.3       casties   669:         self.REQUEST.SESSION['docinfo'] = docinfo
                    670:         return docinfo
1.69      abukhman  671:                
1.44      casties   672:     def getPageinfo(self, current, start=None, rows=None, cols=None, docinfo=None, viewMode=None, tocMode=None):
1.3       casties   673:         """returns pageinfo with the given parameters"""
                    674:         pageinfo = {}
1.4       casties   675:         current = getInt(current)
                    676:         pageinfo['current'] = current
                    677:         rows = int(rows or self.thumbrows)
                    678:         pageinfo['rows'] = rows
                    679:         cols = int(cols or self.thumbcols)
                    680:         pageinfo['cols'] = cols
                    681:         grpsize = cols * rows
                    682:         pageinfo['groupsize'] = grpsize
1.28      casties   683:         start = getInt(start, default=(math.ceil(float(current)/float(grpsize))*grpsize-(grpsize-1)))
                    684:         # int(current / grpsize) * grpsize +1))
1.3       casties   685:         pageinfo['start'] = start
1.4       casties   686:         pageinfo['end'] = start + grpsize
1.44      casties   687:         if (docinfo is not None) and ('numPages' in docinfo):
1.4       casties   688:             np = int(docinfo['numPages'])
                    689:             pageinfo['end'] = min(pageinfo['end'], np)
                    690:             pageinfo['numgroups'] = int(np / grpsize)
                    691:             if np % grpsize > 0:
1.69      abukhman  692:                 pageinfo['numgroups'] += 1        
1.44      casties   693:         pageinfo['viewMode'] = viewMode
                    694:         pageinfo['tocMode'] = tocMode
1.45      abukhman  695:         pageinfo['query'] = self.REQUEST.get('query',' ')
                    696:         pageinfo['queryType'] = self.REQUEST.get('queryType',' ')
                    697:         pageinfo['querySearch'] =self.REQUEST.get('querySearch', 'fulltext')
1.48      abukhman  698:         pageinfo['textPN'] = self.REQUEST.get('textPN','1')
1.55      abukhman  699:         pageinfo['highlightQuery'] = self.REQUEST.get('highlightQuery','')
1.45      abukhman  700:         pageinfo['tocPageSize'] = self.REQUEST.get('tocPageSize', '30')
1.54      abukhman  701:         pageinfo['queryPageSize'] =self.REQUEST.get('queryPageSize', '10')
1.44      casties   702:         pageinfo['tocPN'] = self.REQUEST.get('tocPN', '1')
1.48      abukhman  703:         toc = int (pageinfo['tocPN'])
                    704:         pageinfo['textPages'] =int (toc)
                    705:         
                    706:         if 'tocSize_%s'%tocMode in docinfo:
                    707:             tocSize = int(docinfo['tocSize_%s'%tocMode])
                    708:             tocPageSize = int(pageinfo['tocPageSize'])
1.69      abukhman  709:             # cached toc           
1.48      abukhman  710:             if tocSize%tocPageSize>0:
                    711:                 tocPages=tocSize/tocPageSize+1
                    712:             else:
                    713:                 tocPages=tocSize/tocPageSize
1.69      abukhman  714:             pageinfo['tocPN'] = min (tocPages,toc)                    
1.45      abukhman  715:         pageinfo['searchPN'] =self.REQUEST.get('searchPN','1')
1.59      abukhman  716:         pageinfo['sn'] =self.REQUEST.get('sn','')
1.3       casties   717:         return pageinfo
                    718:     
1.69      abukhman  719: def changeDocumentViewer(self,title="",digilibBaseUrl=None,thumbrows=2,thumbcols=5,authgroups='mpiwg',RESPONSE=None):
1.3       casties   720:         """init document viewer"""
                    721:         self.title=title
                    722:         self.digilibBaseUrl = digilibBaseUrl
1.4       casties   723:         self.thumbrows = thumbrows
                    724:         self.thumbcols = thumbcols
1.8       casties   725:         self.authgroups = [s.strip().lower() for s in authgroups.split(',')]
1.3       casties   726:         if RESPONSE is not None:
                    727:             RESPONSE.redirect('manage_main')
1.1       dwinter   728:         
                    729: def manage_AddDocumentViewerForm(self):
                    730:     """add the viewer form"""
1.3       casties   731:     pt=PageTemplateFile('zpt/addDocumentViewer', globals()).__of__(self)
1.1       dwinter   732:     return pt()
                    733:   
1.43      casties   734: def manage_AddDocumentViewer(self,id,imageScalerUrl="",textServerName="",title="",RESPONSE=None):
1.1       dwinter   735:     """add the viewer"""
1.43      casties   736:     newObj=documentViewer(id,imageScalerUrl=imageScalerUrl,title=title,textServerName=textServerName)
1.1       dwinter   737:     self._setObject(id,newObj)
                    738:     
                    739:     if RESPONSE is not None:
                    740:         RESPONSE.redirect('manage_main')
1.3       casties   741: 
                    742: ## DocumentViewerTemplate class
                    743: class DocumentViewerTemplate(ZopePageTemplate):
                    744:     """Template for document viewer"""
                    745:     meta_type="DocumentViewer Template"
                    746: 
                    747: 
                    748: def manage_addDocumentViewerTemplateForm(self):
                    749:     """Form for adding"""
                    750:     pt=PageTemplateFile('zpt/addDocumentViewerTemplate', globals()).__of__(self)
                    751:     return pt()
                    752: 
                    753: def manage_addDocumentViewerTemplate(self, id='viewer_main', title=None, text=None,
                    754:                            REQUEST=None, submit=None):
                    755:     "Add a Page Template with optional file content."
                    756: 
                    757:     self._setObject(id, DocumentViewerTemplate(id))
                    758:     ob = getattr(self, id)
1.23      dwinter   759:     txt=file(os.path.join(package_home(globals()),'zpt/viewer_main.zpt'),'r').read()
                    760:     logging.info("txt %s:"%txt)
                    761:     ob.pt_edit(txt,"text/html")
1.3       casties   762:     if title:
                    763:         ob.pt_setTitle(title)
                    764:     try:
                    765:         u = self.DestinationURL()
                    766:     except AttributeError:
                    767:         u = REQUEST['URL1']
                    768:         
                    769:     u = "%s/%s" % (u, urllib.quote(id))
                    770:     REQUEST.RESPONSE.redirect(u+'/manage_main')
                    771:     return ''
                    772: 
                    773: 
1.14      casties   774:     

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>