File:  [Repository] / documentViewer / documentViewer.py
Revision 1.69.2.11: download - view: text, annotated - select for diffs - revision graph
Wed Jun 16 19:04:42 2010 UTC (13 years, 11 months ago) by casties
Branches: modularisierung
Diff to: branchpoint 1.69: preferred, unified
fixed oopsie

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

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