Annotation of documentViewer/documentViewer.py, revision 1.30

1.18      dwinter     1: 
                      2: 
1.1       dwinter     3: from OFS.Folder import Folder
                      4: from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
1.22      dwinter     5: from Products.PageTemplates.PageTemplateFile import PageTemplateFile 
1.1       dwinter     6: from AccessControl import ClassSecurityInfo
1.8       casties     7: from AccessControl import getSecurityManager
1.1       dwinter     8: from Globals import package_home
                      9: 
                     10: from Ft.Xml.Domlette import NonvalidatingReader
                     11: from Ft.Xml.Domlette import PrettyPrint, Print
1.11      casties    12: from Ft.Xml import EMPTY_NAMESPACE, Parse
1.1       dwinter    13: 
                     14: import Ft.Xml.XPath
                     15: 
                     16: import os.path
1.7       casties    17: import sys
1.1       dwinter    18: import cgi
                     19: import urllib
1.20      dwinter    20: import logging
1.28      casties    21: import math
1.22      dwinter    22: 
1.18      dwinter    23: import urlparse 
1.1       dwinter    24: 
1.22      dwinter    25: def logger(txt,method,txt2):
                     26:     """logging"""
                     27:     logging.info(txt+ txt2)
                     28:     
                     29:     
1.4       casties    30: def getInt(number, default=0):
                     31:     """returns always an int (0 in case of problems)"""
                     32:     try:
                     33:         return int(number)
                     34:     except:
1.29      casties    35:         return int(default)
1.4       casties    36: 
1.1       dwinter    37: def getTextFromNode(nodename):
1.18      dwinter    38:     """get the cdata content of a node"""
1.8       casties    39:     if nodename is None:
                     40:         return ""
1.1       dwinter    41:     nodelist=nodename.childNodes
                     42:     rc = ""
                     43:     for node in nodelist:
                     44:         if node.nodeType == node.TEXT_NODE:
                     45:            rc = rc + node.data
                     46:     return rc
                     47: 
1.9       casties    48:         
                     49: def getParentDir(path):
                     50:     """returns pathname shortened by one"""
                     51:     return '/'.join(path.split('/')[0:-1])
                     52:         
                     53: 
1.1       dwinter    54: import socket
                     55: 
1.8       casties    56: def urlopen(url,timeout=2):
1.1       dwinter    57:         """urlopen mit timeout"""
1.8       casties    58:         socket.setdefaulttimeout(timeout)
1.1       dwinter    59:         ret=urllib.urlopen(url)
                     60:         socket.setdefaulttimeout(5)
                     61:         return ret
                     62: 
                     63: 
1.3       casties    64: ##
                     65: ## documentViewer class
                     66: ##
                     67: class documentViewer(Folder):
1.1       dwinter    68:     """document viewer"""
1.20      dwinter    69:     #textViewerUrl="http://127.0.0.1:8080/HFQP/testXSLT/getPage?"
1.18      dwinter    70:     
1.1       dwinter    71:     meta_type="Document viewer"
                     72:     
                     73:     security=ClassSecurityInfo()
1.3       casties    74:     manage_options=Folder.manage_options+(
1.1       dwinter    75:         {'label':'main config','action':'changeDocumentViewerForm'},
                     76:         )
                     77: 
1.3       casties    78:     # templates and forms
                     79:     viewer_main = PageTemplateFile('zpt/viewer_main', globals())
                     80:     thumbs_main = PageTemplateFile('zpt/thumbs_main', globals())
                     81:     image_main = PageTemplateFile('zpt/image_main', globals())
                     82:     head_main = PageTemplateFile('zpt/head_main', globals())
                     83:     docuviewer_css = PageTemplateFile('css/docuviewer.css', globals())
1.26      casties    84:     info_xml = PageTemplateFile('zpt/info_xml', globals())
1.3       casties    85: 
                     86:     security.declareProtected('View management screens','changeDocumentViewerForm')    
                     87:     changeDocumentViewerForm = PageTemplateFile('zpt/changeDocumentViewer', globals())
                     88: 
1.1       dwinter    89:     
1.18      dwinter    90:     def __init__(self,id,imageViewerUrl,textViewerUrl=None,title="",digilibBaseUrl=None,thumbcols=2,thumbrows=10,authgroups="mpiwg"):
1.1       dwinter    91:         """init document viewer"""
                     92:         self.id=id
                     93:         self.title=title
                     94:         self.imageViewerUrl=imageViewerUrl
1.18      dwinter    95:         self.textViewerUrl=textViewerUrl
                     96:         
1.4       casties    97:         if not digilibBaseUrl:
1.3       casties    98:             self.digilibBaseUrl = self.findDigilibUrl()
1.4       casties    99:         else:
                    100:             self.digilibBaseUrl = digilibBaseUrl
                    101:         self.thumbcols = thumbcols
                    102:         self.thumbrows = thumbrows
1.8       casties   103:         # authgroups is list of authorized groups (delimited by ,)
                    104:         self.authgroups = [s.strip().lower() for s in authgroups.split(',')]
1.3       casties   105:         # add template folder so we can always use template.something
                    106:         self.manage_addFolder('template')
                    107: 
                    108: 
                    109:     security.declareProtected('View','index_html')
1.21      dwinter   110:     def index_html(self,mode,url,viewMode="auto",start=None,pn=1):
1.3       casties   111:         '''
                    112:         view it
1.26      casties   113:         @param mode: defines how to access the document behind url 
1.3       casties   114:         @param url: url which contains display information
1.26      casties   115:         @param viewMode: if images display images, if text display text, default is images (text,images or auto)
1.18      dwinter   116:         
1.3       casties   117:         '''
                    118:         
1.22      dwinter   119:         logger("documentViewer (index)", logging.INFO, "mode: %s url:%s start:%s pn:%s"%(mode,url,start,pn))
1.1       dwinter   120:         
1.3       casties   121:         if not hasattr(self, 'template'):
                    122:             # create template folder if it doesn't exist
                    123:             self.manage_addFolder('template')
                    124:             
                    125:         if not self.digilibBaseUrl:
                    126:             self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary"
                    127:             
1.4       casties   128:         docinfo = self.getDocinfo(mode=mode,url=url)
                    129:         pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo)
1.3       casties   130:         pt = getattr(self.template, 'viewer_main')
1.21      dwinter   131:         
                    132:         if viewMode=="auto": # automodus gewaehlt
                    133:             if docinfo.get("textURL",'') and self.textViewerUrl: #texturl gesetzt und textViewer konfiguriert
                    134:                 viewMode="text"
                    135:             else:
                    136:                 viewMode="images"
1.22      dwinter   137:                
1.18      dwinter   138:         return pt(docinfo=docinfo,pageinfo=pageinfo,viewMode=viewMode)
1.1       dwinter   139:   
                    140:   
1.4       casties   141:     def getLink(self,param=None,val=None):
                    142:         """link to documentviewer with parameter param set to val"""
1.9       casties   143:         params=self.REQUEST.form.copy()
1.4       casties   144:         if param is not None:
1.7       casties   145:             if val is None:
                    146:                 if params.has_key(param):
                    147:                     del params[param]
1.4       casties   148:             else:
1.9       casties   149:                 params[param] = str(val)
1.7       casties   150:                 
1.9       casties   151:         # quote values and assemble into query string
                    152:         ps = "&".join(["%s=%s"%(k,urllib.quote(v)) for (k, v) in params.items()])
                    153:         url=self.REQUEST['URL1']+"?"+ps
1.4       casties   154:         return url
                    155: 
1.26      casties   156: 
                    157:     def getInfo_xml(self,url,mode):
                    158:         """returns info about the document as XML"""
                    159: 
                    160:         if not self.digilibBaseUrl:
                    161:             self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary"
                    162:         
                    163:         docinfo = self.getDocinfo(mode=mode,url=url)
                    164:         pt = getattr(self.template, 'info_xml')
                    165:         return pt(docinfo=docinfo)
                    166: 
1.4       casties   167:     
1.3       casties   168:     def getStyle(self, idx, selected, style=""):
1.4       casties   169:         """returns a string with the given style and append 'sel' if path == selected."""
1.22      dwinter   170:         #logger("documentViewer (getstyle)", logging.INFO, "idx: %s selected: %s style: %s"%(idx,selected,style))
1.3       casties   171:         if idx == selected:
                    172:             return style + 'sel'
                    173:         else:
1.9       casties   174:             return style
1.26      casties   175: 
1.2       dwinter   176:         
1.9       casties   177:     def isAccessible(self, docinfo):
1.8       casties   178:         """returns if access to the resource is granted"""
                    179:         access = docinfo.get('accessType', None)
1.22      dwinter   180:         logger("documentViewer (accessOK)", logging.INFO, "access type %s"%access)
1.17      casties   181:         if access is not None and access == 'free':
1.22      dwinter   182:             logger("documentViewer (accessOK)", logging.INFO, "access is free")
1.8       casties   183:             return True
1.17      casties   184:         elif access is None or access in self.authgroups:
1.9       casties   185:             # only local access -- only logged in users
                    186:             user = getSecurityManager().getUser()
                    187:             if user is not None:
                    188:                 #print "user: ", user
                    189:                 return (user.getUserName() != "Anonymous User")
                    190:             else:
                    191:                 return False
1.8       casties   192:         
1.22      dwinter   193:         logger("documentViewer (accessOK)", logging.INFO, "unknown access type %s"%access)
1.8       casties   194:         return False
1.9       casties   195:     
1.8       casties   196:                 
1.7       casties   197:     def getDirinfoFromDigilib(self,path,docinfo=None):
1.6       casties   198:         """gibt param von dlInfo aus"""
1.13      casties   199:         num_retries = 3
1.7       casties   200:         if docinfo is None:
                    201:             docinfo = {}
                    202:             
1.13      casties   203:         infoUrl=self.digilibBaseUrl+"/dirInfo-xml.jsp?mo=dir&fn="+path
1.6       casties   204:     
1.22      dwinter   205:         logger("documentViewer (getparamfromdigilib)", logging.INFO, "dirInfo from %s"%(infoUrl))
1.6       casties   206:         
1.13      casties   207:         for cnt in range(num_retries):
1.9       casties   208:             try:
1.13      casties   209:                 # dom = NonvalidatingReader.parseUri(imageUrl)
                    210:                 txt=urllib.urlopen(infoUrl).read()
                    211:                 dom = Parse(txt)
1.9       casties   212:                 break
                    213:             except:
1.22      dwinter   214:                 logger("documentViewer (getdirinfofromdigilib)", logging.ERROR, "error reading %s (try %d)"%(infoUrl,cnt))
1.9       casties   215:         else:
1.13      casties   216:             raise IOError("Unable to get dir-info from %s"%(infoUrl))
1.6       casties   217:         
1.10      casties   218:         sizes=dom.xpath("//dir/size")
1.22      dwinter   219:         logger("documentViewer (getparamfromdigilib)", logging.INFO, "dirInfo:size"%sizes)
1.6       casties   220:         
1.10      casties   221:         if sizes:
                    222:             docinfo['numPages'] = int(getTextFromNode(sizes[0]))
1.7       casties   223:         else:
                    224:             docinfo['numPages'] = 0
                    225:                         
                    226:         return docinfo
1.8       casties   227:     
1.6       casties   228:             
1.9       casties   229:     def getIndexMeta(self, url):
                    230:         """returns dom of index.meta document at url"""
1.12      casties   231:         num_retries = 3
1.9       casties   232:         dom = None
1.12      casties   233:         metaUrl = None
1.9       casties   234:         if url.startswith("http://"):
                    235:             # real URL
1.12      casties   236:             metaUrl = url
1.9       casties   237:         else:
                    238:             # online path
                    239:             server=self.digilibBaseUrl+"/servlet/Texter?fn="
1.13      casties   240:             metaUrl=server+url.replace("/mpiwg/online","")
1.9       casties   241:             if not metaUrl.endswith("index.meta"):
                    242:                 metaUrl += "/index.meta"
1.18      dwinter   243:         print metaUrl
1.13      casties   244:         for cnt in range(num_retries):
1.9       casties   245:             try:
1.12      casties   246:                 # patch dirk encoding fehler treten dann nicht mehr auf
1.11      casties   247:                 # dom = NonvalidatingReader.parseUri(metaUrl)
1.12      casties   248:                 txt=urllib.urlopen(metaUrl).read()
                    249:                 dom = Parse(txt)
1.13      casties   250:                 break
1.9       casties   251:             except:
1.22      dwinter   252:                 logger("ERROR documentViewer (getIndexMata)", logging.INFO,"%s (%s)"%sys.exc_info()[0:2])
1.12      casties   253:                 
                    254:         if dom is None:
                    255:             raise IOError("Unable to read index meta from %s"%(url))
1.9       casties   256:                  
                    257:         return dom
1.20      dwinter   258:     
                    259:     def getPresentationInfoXML(self, url):
                    260:         """returns dom of info.xml document at url"""
                    261:         num_retries = 3
                    262:         dom = None
                    263:         metaUrl = None
                    264:         if url.startswith("http://"):
                    265:             # real URL
                    266:             metaUrl = url
                    267:         else:
                    268:             # online path
                    269:             server=self.digilibBaseUrl+"/servlet/Texter?fn="
                    270:             metaUrl=server+url.replace("/mpiwg/online","")
                    271:            
                    272:         
                    273:         for cnt in range(num_retries):
                    274:             try:
                    275:                 # patch dirk encoding fehler treten dann nicht mehr auf
                    276:                 # dom = NonvalidatingReader.parseUri(metaUrl)
                    277:                 txt=urllib.urlopen(metaUrl).read()
                    278:                 dom = Parse(txt)
                    279:                 break
                    280:             except:
1.22      dwinter   281:                 logger("ERROR documentViewer (getPresentationInfoXML)", logging.INFO,"%s (%s)"%sys.exc_info()[0:2])
1.20      dwinter   282:                 
                    283:         if dom is None:
                    284:             raise IOError("Unable to read infoXMLfrom %s"%(url))
                    285:                  
                    286:         return dom
1.9       casties   287:                         
                    288:         
1.8       casties   289:     def getAuthinfoFromIndexMeta(self,path,docinfo=None,dom=None):
1.9       casties   290:         """gets authorization info from the index.meta file at path or given by dom"""
1.22      dwinter   291:         logger("documentViewer (getauthinfofromindexmeta)", logging.INFO,"path: %s"%(path))
1.8       casties   292:         
                    293:         access = None
                    294:         
                    295:         if docinfo is None:
                    296:             docinfo = {}
                    297:             
                    298:         if dom is None:
1.9       casties   299:             dom = self.getIndexMeta(getParentDir(path))
1.18      dwinter   300:        
1.8       casties   301:         acctype = dom.xpath("//access-conditions/access/@type")
                    302:         if acctype and (len(acctype)>0):
                    303:             access=acctype[0].value
1.9       casties   304:             if access in ['group', 'institution']:
1.8       casties   305:                 access = getTextFromNode(dom.xpath("//access-conditions/access/name")[0]).lower()
                    306:             
                    307:         docinfo['accessType'] = access
                    308:         return docinfo
1.6       casties   309:     
1.8       casties   310:         
1.3       casties   311:     def getBibinfoFromIndexMeta(self,path,docinfo=None,dom=None):
1.9       casties   312:         """gets bibliographical info from the index.meta file at path or given by dom"""
1.27      casties   313:         logging.debug("documentViewer (getbibinfofromindexmeta) path: %s"%(path))
1.2       dwinter   314:         
1.3       casties   315:         if docinfo is None:
                    316:             docinfo = {}
                    317:             
                    318:         if dom is None:
1.9       casties   319:             dom = self.getIndexMeta(getParentDir(path))
                    320:             
1.27      casties   321:         # put in all raw bib fields as dict "bib"
                    322:         bib = dom.xpath("//bib/*")
                    323:         if bib and len(bib)>0:
                    324:             bibinfo = {}
                    325:             for e in bib:
                    326:                 bibinfo[e.localName] = getTextFromNode(e)
                    327:             docinfo['bib'] = bibinfo
                    328:         
                    329:         # extract some fields (author, title, year) according to their mapping
1.4       casties   330:         metaData=self.metadata.main.meta.bib
                    331:         bibtype=dom.xpath("//bib/@type")
                    332:         if bibtype and (len(bibtype)>0):
                    333:             bibtype=bibtype[0].value
1.2       dwinter   334:         else:
1.4       casties   335:             bibtype="generic"
1.27      casties   336:             
1.4       casties   337:         bibtype=bibtype.replace("-"," ") # wrong typesiin index meta "-" instead of " " (not wrong! ROC)
1.27      casties   338:         docinfo['bib_type'] = bibtype
1.4       casties   339:         bibmap=metaData.generateMappingForType(bibtype)
1.8       casties   340:         # if there is no mapping bibmap is empty (mapping sometimes has empty fields)
1.7       casties   341:         if len(bibmap) > 0 and len(bibmap['author'][0]) > 0:
1.30    ! casties   342:             try:
        !           343:                 docinfo['author']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['author'][0])[0])
        !           344:             except: pass
        !           345:             try:
        !           346:                 docinfo['title']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['title'][0])[0])
        !           347:             except: pass
        !           348:             try:
        !           349:                 docinfo['year']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['year'][0])[0])
        !           350:             except: pass
1.27      casties   351:             logging.debug("documentViewer (getbibinfofromindexmeta) using mapping for %s"%bibtype)
1.22      dwinter   352:             try:
                    353:                 docinfo['lang']=getTextFromNode(dom.xpath("//bib/lang")[0])
                    354:             except:
                    355:                 docinfo['lang']=''
1.27      casties   356: 
1.3       casties   357:         return docinfo
                    358: 
                    359:         
1.8       casties   360:     def getDocinfoFromTextTool(self,url,dom=None,docinfo=None):
1.3       casties   361:        """parse texttool tag in index meta"""
1.22      dwinter   362:        logger("documentViewer (getdocinfofromtexttool)", logging.INFO,"url: %s"%(url))
1.3       casties   363:        if docinfo is None:
                    364:            docinfo = {}
                    365:            
1.22      dwinter   366:        if docinfo.get('lang',None) is None:
                    367:            docinfo['lang']='' # default keine Sprache gesetzt
1.8       casties   368:        if dom is None:
1.9       casties   369:            dom = self.getIndexMeta(url)
1.8       casties   370:        
1.16      casties   371:        archivePath = None
                    372:        archiveName = None
                    373: 
1.8       casties   374:        archiveNames=dom.xpath("//resource/name")
                    375:        if archiveNames and (len(archiveNames)>0):
                    376:            archiveName=getTextFromNode(archiveNames[0])
1.16      casties   377:        else:
1.22      dwinter   378:            logger("documentViewer (getdocinfofromtexttool)", logging.WARNING,"resource/name missing in: %s"%(url))
1.3       casties   379:        
                    380:        archivePaths=dom.xpath("//resource/archive-path")
                    381:        if archivePaths and (len(archivePaths)>0):
                    382:            archivePath=getTextFromNode(archivePaths[0])
1.8       casties   383:            # clean up archive path
                    384:            if archivePath[0] != '/':
                    385:                archivePath = '/' + archivePath
1.16      casties   386:            if archiveName and (not archivePath.endswith(archiveName)):
1.8       casties   387:                archivePath += "/" + archiveName
1.3       casties   388:        else:
1.16      casties   389:            # try to get archive-path from url
1.22      dwinter   390:            logger("documentViewer (getdocinfofromtexttool)", logging.WARNING,"resource/archive-path missing in: %s"%(url))
1.16      casties   391:            if (not url.startswith('http')):
                    392:                archivePath = url.replace('index.meta', '')
                    393:                
                    394:        if archivePath is None:
                    395:            # we balk without archive-path
                    396:            raise IOError("Missing archive-path (for text-tool) in %s"%(url))
1.3       casties   397:        
1.9       casties   398:        imageDirs=dom.xpath("//texttool/image")
                    399:        if imageDirs and (len(imageDirs)>0):
                    400:            imageDir=getTextFromNode(imageDirs[0])
1.3       casties   401:        else:
1.22      dwinter   402:            # we balk with no image tag / not necessary anymore because textmode is now standard
                    403:            #raise IOError("No text-tool info in %s"%(url))
                    404:            imageDir=""
                    405:            docinfo['numPages']=1 # im moment einfach auf eins setzen, navigation ueber die thumbs geht natuerlich nicht
                    406:        
                    407:            docinfo['imagePath'] = "" # keine Bilder
                    408:            docinfo['imageURL'] = ""
                    409: 
1.9       casties   410:        if imageDir and archivePath:
                    411:            #print "image: ", imageDir, " archivepath: ", archivePath
                    412:            imageDir=os.path.join(archivePath,imageDir)
                    413:            imageDir=imageDir.replace("/mpiwg/online",'')
                    414:            docinfo=self.getDirinfoFromDigilib(imageDir,docinfo=docinfo)
                    415:            docinfo['imagePath'] = imageDir
                    416:            docinfo['imageURL'] = self.digilibBaseUrl+"/servlet/Scaler?fn="+imageDir
1.3       casties   417:            
                    418:        viewerUrls=dom.xpath("//texttool/digiliburlprefix")
                    419:        if viewerUrls and (len(viewerUrls)>0):
                    420:            viewerUrl=getTextFromNode(viewerUrls[0])
1.7       casties   421:            docinfo['viewerURL'] = viewerUrl
1.3       casties   422:                   
                    423:        textUrls=dom.xpath("//texttool/text")
                    424:        if textUrls and (len(textUrls)>0):
                    425:            textUrl=getTextFromNode(textUrls[0])
1.19      dwinter   426:            if urlparse.urlparse(textUrl)[0]=="": #keine url
                    427:                textUrl=os.path.join(archivePath,textUrl) 
                    428: 
1.7       casties   429:            docinfo['textURL'] = textUrl
1.20      dwinter   430:    
                    431:        presentationUrls=dom.xpath("//texttool/presentation")
1.22      dwinter   432:        docinfo = self.getBibinfoFromIndexMeta(url,docinfo=docinfo,dom=dom)   # get info von bib tag
                    433:        
                    434:        if presentationUrls and (len(presentationUrls)>0): # ueberschreibe diese durch presentation informationen 
1.30    ! casties   435:             # presentation url ergiebt sich ersetzen von index.meta in der url der fuer die Metadaten
1.20      dwinter   436:             # durch den relativen Pfad auf die presentation infos
                    437:            presentationUrl=url.replace('index.meta',getTextFromNode(presentationUrls[0]))
                    438:            docinfo = self.getBibinfoFromTextToolPresentation(presentationUrl,docinfo=docinfo,dom=dom)
1.22      dwinter   439: 
1.25      casties   440:        docinfo = self.getAuthinfoFromIndexMeta(url,docinfo=docinfo,dom=dom)   # get access info
1.3       casties   441:        return docinfo
                    442:    
1.20      dwinter   443:    
                    444:     def getBibinfoFromTextToolPresentation(self,url,docinfo=None,dom=None):
                    445:         """gets the bibliographical information from the preseantion entry in texttools
                    446:         """
                    447:         dom=self.getPresentationInfoXML(url)
1.29      casties   448:         try:
                    449:             docinfo['author']=getTextFromNode(dom.xpath("//author")[0])
                    450:         except:
                    451:             pass
                    452:         try:
                    453:             docinfo['title']=getTextFromNode(dom.xpath("//title")[0])
                    454:         except:
                    455:             pass
                    456:         try:
                    457:             docinfo['year']=getTextFromNode(dom.xpath("//date")[0])
                    458:         except:
                    459:             pass
1.20      dwinter   460:         return docinfo
                    461:     
1.3       casties   462:     def getDocinfoFromImagePath(self,path,docinfo=None):
                    463:         """path ist the path to the images it assumes that the index.meta file is one level higher."""
1.22      dwinter   464:         logger("documentViewer (getdocinfofromimagepath)", logging.INFO,"path: %s"%(path))
1.3       casties   465:         if docinfo is None:
                    466:             docinfo = {}
1.6       casties   467:         path=path.replace("/mpiwg/online","")
1.3       casties   468:         docinfo['imagePath'] = path
1.7       casties   469:         docinfo=self.getDirinfoFromDigilib(path,docinfo=docinfo)
                    470:         imageUrl=self.digilibBaseUrl+"/servlet/Scaler?fn="+path
1.3       casties   471:         docinfo['imageURL'] = imageUrl
                    472:         
                    473:         docinfo = self.getBibinfoFromIndexMeta(path,docinfo=docinfo)
1.8       casties   474:         docinfo = self.getAuthinfoFromIndexMeta(path,docinfo=docinfo)
1.3       casties   475:         return docinfo
                    476:     
1.2       dwinter   477:     
1.3       casties   478:     def getDocinfo(self, mode, url):
                    479:         """returns docinfo depending on mode"""
1.22      dwinter   480:         logger("documentViewer (getdocinfo)", logging.INFO,"mode: %s, url: %s"%(mode,url))
1.3       casties   481:         # look for cached docinfo in session
1.21      dwinter   482:         if self.REQUEST.SESSION.has_key('docinfo'):
1.3       casties   483:             docinfo = self.REQUEST.SESSION['docinfo']
                    484:             # check if its still current
                    485:             if docinfo is not None and docinfo.get('mode') == mode and docinfo.get('url') == url:
1.22      dwinter   486:                 logger("documentViewer (getdocinfo)", logging.INFO,"docinfo in session: %s"%docinfo)
1.3       casties   487:                 return docinfo
                    488:         # new docinfo
                    489:         docinfo = {'mode': mode, 'url': url}
                    490:         if mode=="texttool": #index.meta with texttool information
                    491:             docinfo = self.getDocinfoFromTextTool(url, docinfo=docinfo)
                    492:         elif mode=="imagepath":
                    493:             docinfo = self.getDocinfoFromImagePath(url, docinfo=docinfo)
                    494:         else:
1.22      dwinter   495:             logger("documentViewer (getdocinfo)", logging.ERROR,"unknown mode!")
1.10      casties   496:             raise ValueError("Unknown mode %s"%(mode))
                    497:                         
1.22      dwinter   498:         logger("documentViewer (getdocinfo)", logging.INFO,"docinfo: %s"%docinfo)
1.3       casties   499:         self.REQUEST.SESSION['docinfo'] = docinfo
                    500:         return docinfo
1.2       dwinter   501:         
                    502:         
1.4       casties   503:     def getPageinfo(self, current, start=None, rows=None, cols=None, docinfo=None):
1.3       casties   504:         """returns pageinfo with the given parameters"""
                    505:         pageinfo = {}
1.4       casties   506:         current = getInt(current)
                    507:         pageinfo['current'] = current
                    508:         rows = int(rows or self.thumbrows)
                    509:         pageinfo['rows'] = rows
                    510:         cols = int(cols or self.thumbcols)
                    511:         pageinfo['cols'] = cols
                    512:         grpsize = cols * rows
                    513:         pageinfo['groupsize'] = grpsize
1.28      casties   514:         start = getInt(start, default=(math.ceil(float(current)/float(grpsize))*grpsize-(grpsize-1)))
                    515:         # int(current / grpsize) * grpsize +1))
1.3       casties   516:         pageinfo['start'] = start
1.4       casties   517:         pageinfo['end'] = start + grpsize
                    518:         if docinfo is not None:
                    519:             np = int(docinfo['numPages'])
                    520:             pageinfo['end'] = min(pageinfo['end'], np)
                    521:             pageinfo['numgroups'] = int(np / grpsize)
                    522:             if np % grpsize > 0:
                    523:                 pageinfo['numgroups'] += 1
                    524:                 
1.3       casties   525:         return pageinfo
                    526:                 
1.1       dwinter   527:     def text(self,mode,url,pn):
                    528:         """give text"""
                    529:         if mode=="texttool": #index.meta with texttool information
                    530:             (viewerUrl,imagepath,textpath)=parseUrlTextTool(url)
                    531:         
1.9       casties   532:         #print textpath
1.1       dwinter   533:         try:
                    534:             dom = NonvalidatingReader.parseUri(textpath)
                    535:         except:
                    536:             return None
                    537:     
                    538:         list=[]
                    539:         nodes=dom.xpath("//pb")
                    540: 
                    541:         node=nodes[int(pn)-1]
                    542:         
                    543:         p=node
                    544:         
                    545:         while p.tagName!="p":
                    546:             p=p.parentNode
                    547:         
                    548:         
                    549:         endNode=nodes[int(pn)]
                    550:         
                    551:         
                    552:         e=endNode
                    553:         
                    554:         while e.tagName!="p":
                    555:             e=e.parentNode
                    556:         
                    557:         
                    558:         next=node.parentNode
                    559:         
                    560:         #sammle s
                    561:         while next and (next!=endNode.parentNode):
                    562:             list.append(next)    
                    563:             next=next.nextSibling    
                    564:         list.append(endNode.parentNode)
                    565:         
                    566:         if p==e:# beide im selben paragraphen
1.2       dwinter   567:             pass
                    568: #    else:
                    569: #            next=p
                    570: #            while next!=e:
                    571: #                print next,e
                    572: #                list.append(next)
                    573: #                next=next.nextSibling
                    574: #            
                    575: #        for x in list:
                    576: #            PrettyPrint(x)
                    577: #
                    578: #        return list
1.3       casties   579: #
                    580: 
                    581:     def findDigilibUrl(self):
                    582:         """try to get the digilib URL from zogilib"""
                    583:         url = self.imageViewerUrl[:-1] + "/getScalerUrl"
1.20      dwinter   584:         #print urlparse.urlparse(url)[0]
                    585:         #print urlparse.urljoin(self.absolute_url(),url)
                    586:         logging.info("finddigiliburl: %s"%urlparse.urlparse(url)[0])
                    587:         logging.info("finddigiliburl: %s"%urlparse.urljoin(self.absolute_url(),url))
                    588:         
1.3       casties   589:         try:
1.18      dwinter   590:             if urlparse.urlparse(url)[0]=='': #relative path
                    591:                 url=urlparse.urljoin(self.absolute_url()+"/",url)
                    592:                 
1.3       casties   593:             scaler = urlopen(url).read()
                    594:             return scaler.replace("/servlet/Scaler?", "")
                    595:         except:
                    596:             return None
                    597:     
1.18      dwinter   598:     def changeDocumentViewer(self,imageViewerUrl,textViewerUrl,title="",digilibBaseUrl=None,thumbrows=2,thumbcols=10,authgroups='mpiwg',RESPONSE=None):
1.3       casties   599:         """init document viewer"""
                    600:         self.title=title
                    601:         self.imageViewerUrl=imageViewerUrl
1.18      dwinter   602:         self.textViewerUrl=textViewerUrl
1.3       casties   603:         self.digilibBaseUrl = digilibBaseUrl
1.4       casties   604:         self.thumbrows = thumbrows
                    605:         self.thumbcols = thumbcols
1.8       casties   606:         self.authgroups = [s.strip().lower() for s in authgroups.split(',')]
1.3       casties   607:         if RESPONSE is not None:
                    608:             RESPONSE.redirect('manage_main')
1.1       dwinter   609:     
                    610:     
                    611:         
                    612:         
                    613: #    security.declareProtected('View management screens','renameImageForm')
                    614: 
                    615: def manage_AddDocumentViewerForm(self):
                    616:     """add the viewer form"""
1.3       casties   617:     pt=PageTemplateFile('zpt/addDocumentViewer', globals()).__of__(self)
1.1       dwinter   618:     return pt()
                    619:   
1.18      dwinter   620: def manage_AddDocumentViewer(self,id,imageViewerUrl="",textViewerUrl="",title="",RESPONSE=None):
1.1       dwinter   621:     """add the viewer"""
1.18      dwinter   622:     newObj=documentViewer(id,imageViewerUrl,title=title,textViewerUrl=textViewerUrl)
1.1       dwinter   623:     self._setObject(id,newObj)
                    624:     
                    625:     if RESPONSE is not None:
                    626:         RESPONSE.redirect('manage_main')
1.3       casties   627: 
                    628: 
                    629: ##
                    630: ## DocumentViewerTemplate class
                    631: ##
                    632: class DocumentViewerTemplate(ZopePageTemplate):
                    633:     """Template for document viewer"""
                    634:     meta_type="DocumentViewer Template"
                    635: 
                    636: 
                    637: def manage_addDocumentViewerTemplateForm(self):
                    638:     """Form for adding"""
                    639:     pt=PageTemplateFile('zpt/addDocumentViewerTemplate', globals()).__of__(self)
                    640:     return pt()
                    641: 
                    642: def manage_addDocumentViewerTemplate(self, id='viewer_main', title=None, text=None,
                    643:                            REQUEST=None, submit=None):
                    644:     "Add a Page Template with optional file content."
                    645: 
                    646:     self._setObject(id, DocumentViewerTemplate(id))
                    647:     ob = getattr(self, id)
1.23      dwinter   648:     txt=file(os.path.join(package_home(globals()),'zpt/viewer_main.zpt'),'r').read()
                    649:     logging.info("txt %s:"%txt)
                    650:     ob.pt_edit(txt,"text/html")
1.3       casties   651:     if title:
                    652:         ob.pt_setTitle(title)
                    653:     try:
                    654:         u = self.DestinationURL()
                    655:     except AttributeError:
                    656:         u = REQUEST['URL1']
                    657:         
                    658:     u = "%s/%s" % (u, urllib.quote(id))
                    659:     REQUEST.RESPONSE.redirect(u+'/manage_main')
                    660:     return ''
                    661: 
                    662: 
1.14      casties   663:     

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