Annotation of documentViewer/documentViewer.py, revision 1.175.2.31

1.1       dwinter     1: from OFS.Folder import Folder
                      2: from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
1.175.2.29  casties     3: from Products.PageTemplates.PageTemplateFile import PageTemplateFile
1.175.2.30  casties     4: from App.ImageFile import ImageFile
                      5: #from Products.ZSimpleFile.ZSimpleFile import ZSimpleFile
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: 
1.175.2.1  casties    10: import xml.etree.ElementTree as ET
                     11: 
1.1       dwinter    12: import os.path
1.7       casties    13: import sys
1.1       dwinter    14: import urllib
1.20      dwinter    15: import logging
1.28      casties    16: import math
1.18      dwinter    17: import urlparse 
1.99      dwinter    18: import re
1.149     abukhman   19: import string
1.117     abukhman   20: 
1.175.2.5  casties    21: from SrvTxtUtils import getInt, getText, getHttpData
                     22: 
1.22      dwinter    23: def logger(txt,method,txt2):
                     24:     """logging"""
                     25:     logging.info(txt+ txt2)
                     26:     
                     27:     
1.170     abukhman   28: def serializeNode(node, encoding="utf-8"):
1.43      casties    29:     """returns a string containing node as XML"""
1.175.2.1  casties    30:     s = ET.tostring(node)
                     31:     
                     32:     # 4Suite:
                     33:     #    stream = cStringIO.StringIO()
                     34:     #    Ft.Xml.Domlette.Print(node, stream=stream, encoding=encoding)
                     35:     #    s = stream.getvalue()
                     36:     #    stream.close()
1.43      casties    37:     return s
                     38: 
1.148     abukhman   39: def browserCheck(self):
                     40:     """check the browsers request to find out the browser type"""
                     41:     bt = {}
                     42:     ua = self.REQUEST.get_header("HTTP_USER_AGENT")
                     43:     bt['ua'] = ua
                     44:     bt['isIE'] = False
                     45:     bt['isN4'] = False
1.165     abukhman   46:     bt['versFirefox']=""
                     47:     bt['versIE']=""
                     48:     bt['versSafariChrome']=""
                     49:     bt['versOpera']=""
                     50:     
1.148     abukhman   51:     if string.find(ua, 'MSIE') > -1:
                     52:         bt['isIE'] = True
                     53:     else:
                     54:         bt['isN4'] = (string.find(ua, 'Mozilla/4.') > -1)
1.165     abukhman   55:     # Safari oder Chrome identification    
                     56:     try:
                     57:         nav = ua[string.find(ua, '('):]
                     58:         nav1=ua[string.find(ua,')'):]
                     59:         nav2=nav1[string.find(nav1,'('):]
                     60:         nav3=nav2[string.find(nav2,')'):]
                     61:         ie = string.split(nav, "; ")[1]
                     62:         ie1 =string.split(nav1, " ")[2]
                     63:         ie2 =string.split(nav3, " ")[1]
                     64:         ie3 =string.split(nav3, " ")[2]
                     65:         if string.find(ie3, "Safari") >-1:
                     66:             bt['versSafariChrome']=string.split(ie2, "/")[1]
                     67:     except: pass
                     68:     # IE identification
1.148     abukhman   69:     try:
                     70:         nav = ua[string.find(ua, '('):]
                     71:         ie = string.split(nav, "; ")[1]
                     72:         if string.find(ie, "MSIE") > -1:
                     73:             bt['versIE'] = string.split(ie, " ")[1]
1.165     abukhman   74:     except:pass
                     75:     # Firefox identification
                     76:     try:
                     77:         nav = ua[string.find(ua, '('):]
                     78:         nav1=ua[string.find(ua,')'):]
                     79:         if string.find(ie1, "Firefox") >-1:
                     80:             nav5= string.split(ie1, "/")[1]
                     81:             logging.debug("FIREFOX: %s"%(nav5))
1.166     abukhman   82:             bt['versFirefox']=nav5[0:3]                   
1.165     abukhman   83:     except:pass
                     84:     #Opera identification
                     85:     try:
                     86:         if string.find(ua,"Opera") >-1:
                     87:             nav = ua[string.find(ua, '('):]
                     88:             nav1=nav[string.find(nav,')'):]
                     89:             bt['versOpera']=string.split(nav1,"/")[2]
                     90:     except:pass
1.148     abukhman   91:     
                     92:     bt['isMac'] = string.find(ua, 'Macintosh') > -1
                     93:     bt['isWin'] = string.find(ua, 'Windows') > -1
                     94:     bt['isIEWin'] = bt['isIE'] and bt['isWin']
                     95:     bt['isIEMac'] = bt['isIE'] and bt['isMac']
                     96:     bt['staticHTML'] = False
                     97: 
                     98:     return bt
1.118     abukhman   99: 
1.175.2.11  casties   100: def getParentPath(path, cnt=1):
                    101:     """returns pathname shortened by cnt"""
                    102:     # make sure path doesn't end with /
                    103:     path = path.rstrip('/')
                    104:     # split by /, shorten, and reassemble
                    105:     return '/'.join(path.split('/')[0:-cnt])
                    106: 
1.175.2.8  casties   107: 
1.3       casties   108: ##
                    109: ## documentViewer class
                    110: ##
                    111: class documentViewer(Folder):
1.1       dwinter   112:     """document viewer"""
                    113:     meta_type="Document viewer"
                    114:     
                    115:     security=ClassSecurityInfo()
1.3       casties   116:     manage_options=Folder.manage_options+(
1.1       dwinter   117:         {'label':'main config','action':'changeDocumentViewerForm'},
                    118:         )
1.175.2.10  casties   119:     
                    120:     metadataService = None
                    121:     """MetaDataFolder instance"""
1.1       dwinter   122: 
1.3       casties   123:     # templates and forms
1.175.2.25  casties   124:     viewer_text = PageTemplateFile('zpt/viewer_text', globals())
1.175.2.28  casties   125:     viewer_images = PageTemplateFile('zpt/viewer_images', globals())
1.3       casties   126:     viewer_main = PageTemplateFile('zpt/viewer_main', globals())
1.44      casties   127:     toc_thumbs = PageTemplateFile('zpt/toc_thumbs', globals())
                    128:     toc_text = PageTemplateFile('zpt/toc_text', globals())
                    129:     toc_figures = PageTemplateFile('zpt/toc_figures', globals())
1.43      casties   130:     page_main_images = PageTemplateFile('zpt/page_main_images', globals())
1.161     abukhman  131:     page_main_double = PageTemplateFile('zpt/page_main_double', globals())
1.43      casties   132:     page_main_text = PageTemplateFile('zpt/page_main_text', globals())
1.44      casties   133:     page_main_text_dict = PageTemplateFile('zpt/page_main_text_dict', globals())
1.77      abukhman  134:     page_main_gis =PageTemplateFile ('zpt/page_main_gis', globals())
1.48      abukhman  135:     page_main_xml = PageTemplateFile('zpt/page_main_xml', globals())
1.157     abukhman  136:     page_main_pureXml = PageTemplateFile('zpt/page_main_pureXml', globals())
1.3       casties   137:     head_main = PageTemplateFile('zpt/head_main', globals())
1.26      casties   138:     info_xml = PageTemplateFile('zpt/info_xml', globals())
1.175.2.26  casties   139:     # TODO: can this be nicer?
1.175.2.30  casties   140:     docuviewer_css = ImageFile('css/docuviewer.css',globals())
1.70      casties   141:     
                    142:     
1.32      dwinter   143:     thumbs_main_rss = PageTemplateFile('zpt/thumbs_main_rss', globals())
1.3       casties   144: 
1.1       dwinter   145:     
1.45      abukhman  146:     def __init__(self,id,imageScalerUrl=None,textServerName=None,title="",digilibBaseUrl=None,thumbcols=2,thumbrows=5,authgroups="mpiwg"):
1.1       dwinter   147:         """init document viewer"""
                    148:         self.id=id
                    149:         self.title=title
1.4       casties   150:         self.thumbcols = thumbcols
                    151:         self.thumbrows = thumbrows
1.8       casties   152:         # authgroups is list of authorized groups (delimited by ,)
                    153:         self.authgroups = [s.strip().lower() for s in authgroups.split(',')]
1.43      casties   154:         # create template folder so we can always use template.something
                    155:         
                    156:         templateFolder = Folder('template')
                    157:         #self['template'] = templateFolder # Zope-2.12 style
                    158:         self._setObject('template',templateFolder) # old style
                    159:         try:
1.70      casties   160:             import MpdlXmlTextServer
1.71      casties   161:             textServer = MpdlXmlTextServer.MpdlXmlTextServer(id='fulltextclient',serverName=textServerName)
1.43      casties   162:             #templateFolder['fulltextclient'] = xmlRpcClient
1.70      casties   163:             templateFolder._setObject('fulltextclient',textServer)
1.43      casties   164:         except Exception, e:
1.70      casties   165:             logging.error("Unable to create MpdlXmlTextServer for fulltextclient: "+str(e))
1.175.2.10  casties   166:             
1.43      casties   167:         try:
                    168:             from Products.zogiLib.zogiLib import zogiLib
                    169:             zogilib = zogiLib(id="zogilib", title="zogilib for docuviewer", dlServerURL=imageScalerUrl, layout="book")
                    170:             #templateFolder['zogilib'] = zogilib
                    171:             templateFolder._setObject('zogilib',zogilib)
                    172:         except Exception, e:
                    173:             logging.error("Unable to create zogiLib for zogilib: "+str(e))
1.175.2.10  casties   174:             
                    175:         try:
                    176:             # assume MetaDataFolder instance is called metadata 
                    177:             self.metadataService = getattr(self, 'metadata')
                    178:         except Exception, e:
                    179:             logging.error("Unable to find MetaDataFolder 'metadata': "+str(e))
                    180:             
1.175.2.23  casties   181:         if digilibBaseUrl is not None:
                    182:             self.digilibBaseUrl = digilibBaseUrl
                    183:             
1.70      casties   184:         
                    185:     # proxy text server methods to fulltextclient
                    186:     def getTextPage(self, **args):
                    187:         """get page"""
                    188:         return self.template.fulltextclient.getTextPage(**args)
1.171     abukhman  189: 
                    190:     def getOrigPages(self, **args):
                    191:         """get page"""
                    192:         return self.template.fulltextclient.getOrigPages(**args)
1.167     abukhman  193:     
1.171     abukhman  194:     def getOrigPagesNorm(self, **args):
                    195:         """get page"""
                    196:         return self.template.fulltextclient.getOrigPagesNorm(**args)
                    197: 
1.70      casties   198:     def getQuery(self, **args):
1.163     abukhman  199:         """get query in search"""
1.70      casties   200:         return self.template.fulltextclient.getQuery(**args)
1.163     abukhman  201:      
1.70      casties   202:     def getSearch(self, **args):
                    203:         """get search"""
                    204:         return self.template.fulltextclient.getSearch(**args)
1.120     abukhman  205:     
                    206:     def getGisPlaces(self, **args):
1.121     abukhman  207:         """get gis places"""
1.120     abukhman  208:         return self.template.fulltextclient.getGisPlaces(**args)
1.121     abukhman  209:  
                    210:     def getAllGisPlaces(self, **args):
1.122     abukhman  211:         """get all gis places """
                    212:         return self.template.fulltextclient.getAllGisPlaces(**args)
1.163     abukhman  213:        
1.175.2.24  casties   214:     def getWordInfo(self, **args):
1.70      casties   215:         """get translate"""
1.175.2.24  casties   216:         return self.template.fulltextclient.getWordInfo(**args)
1.70      casties   217: 
                    218:     def getLemma(self, **args):
                    219:         """get lemma"""
                    220:         return self.template.fulltextclient.getLemma(**args)
                    221: 
1.173     abukhman  222:     def getLemmaQuery(self, **args):
                    223:         """get query"""
                    224:         return self.template.fulltextclient.getLemmaQuery(**args)
                    225: 
                    226:     def getLex(self, **args):
                    227:         """get lex"""
                    228:         return self.template.fulltextclient.getLex(**args)
                    229: 
1.70      casties   230:     def getToc(self, **args):
                    231:         """get toc"""
                    232:         return self.template.fulltextclient.getToc(**args)
                    233: 
                    234:     def getTocPage(self, **args):
                    235:         """get tocpage"""
                    236:         return self.template.fulltextclient.getTocPage(**args)
1.3       casties   237: 
1.70      casties   238:     
1.32      dwinter   239:     security.declareProtected('View','thumbs_rss')
                    240:     def thumbs_rss(self,mode,url,viewMode="auto",start=None,pn=1):
                    241:         '''
                    242:         view it
                    243:         @param mode: defines how to access the document behind url 
                    244:         @param url: url which contains display information
                    245:         @param viewMode: if images display images, if text display text, default is images (text,images or auto)
                    246:         
                    247:         '''
1.95      abukhman  248:         logging.debug("HHHHHHHHHHHHHH:load the rss")
1.175.2.5  casties   249:         logging.debug("documentViewer (index) mode: %s url:%s start:%s pn:%s"%(mode,url,start,pn))
1.32      dwinter   250:         
                    251:         if not hasattr(self, 'template'):
                    252:             # create template folder if it doesn't exist
                    253:             self.manage_addFolder('template')
                    254:             
                    255:         if not self.digilibBaseUrl:
                    256:             self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary"
                    257:             
                    258:         docinfo = self.getDocinfo(mode=mode,url=url)
1.132     abukhman  259:         #pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo)
1.138     abukhman  260:         pageinfo = self.getPageinfo(start=start,current=pn, docinfo=docinfo)
1.126     abukhman  261:         ''' ZDES '''
1.32      dwinter   262:         pt = getattr(self.template, 'thumbs_main_rss')
                    263:         
                    264:         if viewMode=="auto": # automodus gewaehlt
1.159     casties   265:             if docinfo.has_key("textURL") or docinfo.get('textURLPath',None): #texturl gesetzt und textViewer konfiguriert
1.32      dwinter   266:                 viewMode="text"
                    267:             else:
                    268:                 viewMode="images"
                    269:                
                    270:         return pt(docinfo=docinfo,pageinfo=pageinfo,viewMode=viewMode)
1.175.2.12  casties   271: 
1.32      dwinter   272:   
1.3       casties   273:     security.declareProtected('View','index_html')
1.175.2.18  casties   274:     def index_html(self,url,mode="texttool",viewMode="auto",viewType=None,tocMode="thumbs",start=1,pn=1):
1.175.2.11  casties   275:         """
1.175.2.17  casties   276:         view page
1.3       casties   277:         @param url: url which contains display information
1.175.2.17  casties   278:         @param mode: defines how to access the document behind url 
                    279:         @param viewMode: 'images': display images, 'text': display text, default is 'auto'
                    280:         @param viewType: sub-type of viewMode, e.g. 'dict' for viewMode='text'
1.48      abukhman  281:         @param tocMode: type of 'table of contents' for navigation (thumbs, text, figures, none)
1.175.2.11  casties   282:         """
1.3       casties   283:         
1.175.2.21  casties   284:         logging.debug("documentViewer(index_html) mode=%s url=%s viewMode=%s viewType=%s start=%s pn=%s"%(mode,url,viewMode,viewType,start,pn))
1.1       dwinter   285:         
1.3       casties   286:         if not hasattr(self, 'template'):
1.43      casties   287:             # this won't work
                    288:             logging.error("template folder missing!")
                    289:             return "ERROR: template folder missing!"
1.3       casties   290:             
1.43      casties   291:         if not getattr(self, 'digilibBaseUrl', None):
1.71      casties   292:             self.digilibBaseUrl = self.findDigilibUrl() or "http://digilib.mpiwg-berlin.mpg.de/digitallibrary"
1.3       casties   293:             
1.4       casties   294:         docinfo = self.getDocinfo(mode=mode,url=url)
1.47      abukhman  295:         
1.44      casties   296:         if tocMode != "thumbs":
                    297:             # get table of contents
                    298:             docinfo = self.getToc(mode=tocMode, docinfo=docinfo)
1.175.2.3  casties   299: 
1.175.2.17  casties   300:         # auto viewMode: text if there is a text else images
1.175.2.3  casties   301:         if viewMode=="auto": 
                    302:             if docinfo.get('textURL', None) or docinfo.get('textURLPath', None): 
1.175.2.17  casties   303:                 viewMode = "text"
                    304:                 viewType = "dict"
1.21      dwinter   305:             else:
1.175.2.17  casties   306:                 viewMode = "images"
1.44      casties   307:                 
1.175.2.20  casties   308:         elif viewMode == "text_dict":
                    309:             # legacy fix
                    310:             viewMode = "text"
                    311:             viewType = "dict"
                    312:             
1.175.2.21  casties   313:         # stringify viewType
                    314:         if isinstance(viewType, list):
1.175.2.24  casties   315:             logging.debug("index_html: viewType is list:%s"%viewType)
1.175.2.21  casties   316:             viewType = ','.join([t for t in viewType if t])
                    317:                         
1.175.2.18  casties   318:         pageinfo = self.getPageinfo(start=start, current=pn, docinfo=docinfo, viewMode=viewMode, viewType=viewType, tocMode=tocMode)
1.175.2.16  casties   319:                     
1.175.2.21  casties   320:         # get template /template/viewer_$viewMode
                    321:         pt = getattr(self.template, 'viewer_%s'%viewMode, None)
                    322:         if pt is None:
                    323:             logging.error("No template for viewMode=%s!"%viewMode)
                    324:             # TODO: error page?
                    325:             return "No template for viewMode=%s!"%viewMode
                    326:         
1.175.2.3  casties   327:         # and execute with parameters
1.175.2.17  casties   328:         return pt(docinfo=docinfo, pageinfo=pageinfo)
1.1       dwinter   329:   
1.36      dwinter   330:     def generateMarks(self,mk):
                    331:         ret=""
1.44      casties   332:         if mk is None:
                    333:             return ""
1.73      casties   334:         if not isinstance(mk, list):
1.71      casties   335:             mk=[mk]
1.36      dwinter   336:         for m in mk:
1.37      dwinter   337:             ret+="mk=%s"%m
1.36      dwinter   338:         return ret
1.149     abukhman  339:     
                    340:     
1.148     abukhman  341:     def getBrowser(self):
                    342:         """getBrowser the version of browser """
1.162     casties   343:         bt = browserCheck(self)
1.164     abukhman  344:         logging.debug("BROWSER VERSION: %s"%(bt))
1.162     casties   345:         return bt
1.148     abukhman  346:         
1.43      casties   347:     def findDigilibUrl(self):
                    348:         """try to get the digilib URL from zogilib"""
                    349:         url = self.template.zogilib.getDLBaseUrl()
                    350:         return url
1.175.2.26  casties   351:     
                    352:     def getScalerUrl(self, fn=None, pn=None, dw=100, dh=100, docinfo=None):
                    353:         """returns URL to digilib Scaler with params"""
                    354:         url = None
                    355:         if docinfo is not None:
                    356:             url = docinfo.get('imageURL', None)
                    357:             
                    358:         if url is None:
                    359:             url = "%s/servlet/Scaler?"%self.digilibBaseUrl
                    360:             if fn is None and docinfo is not None:
                    361:                 fn = docinfo.get('imagePath','')
                    362:             
                    363:             url += "fn=%s"%fn
                    364:             
                    365:         if pn:
                    366:             url += "&pn=%s"%pn
                    367:             
                    368:         url += "&dw=%s&dh=%s"%(dw,dh)
                    369:         return url
1.67      casties   370: 
                    371:     def getDocumentViewerURL(self):
                    372:         """returns the URL of this instance"""
                    373:         return self.absolute_url()
1.43      casties   374:     
                    375:     def getStyle(self, idx, selected, style=""):
1.175.2.26  casties   376:         """returns a string with the given style and append 'sel' if idx == selected."""
1.43      casties   377:         #logger("documentViewer (getstyle)", logging.INFO, "idx: %s selected: %s style: %s"%(idx,selected,style))
                    378:         if idx == selected:
                    379:             return style + 'sel'
                    380:         else:
                    381:             return style
1.36      dwinter   382:     
1.175.2.24  casties   383:     def getParams(self, param=None, val=None, params=None, duplicates=None):
1.175.2.16  casties   384:         """returns dict with URL parameters.
                    385:         
                    386:         Takes URL parameters and additionally param=val or dict params.
                    387:         Deletes key if value is None."""
1.162     casties   388:         # copy existing request params
1.175.2.16  casties   389:         newParams=self.REQUEST.form.copy()
1.162     casties   390:         # change single param
1.4       casties   391:         if param is not None:
1.7       casties   392:             if val is None:
1.175.2.16  casties   393:                 if newParams.has_key(param):
                    394:                     del newParams[param]
1.4       casties   395:             else:
1.175.2.16  casties   396:                 newParams[param] = str(val)
1.43      casties   397:                 
1.162     casties   398:         # change more params
                    399:         if params is not None:
1.175.2.24  casties   400:             for (k, v) in params.items():
1.162     casties   401:                 if v is None:
                    402:                     # val=None removes param
1.175.2.16  casties   403:                     if newParams.has_key(k):
                    404:                         del newParams[k]
1.162     casties   405:                         
                    406:                 else:
1.175.2.16  casties   407:                     newParams[k] = v
1.175.2.24  casties   408: 
                    409:         if duplicates:
                    410:             # eliminate lists (coming from duplicate keys)
                    411:             for (k,v) in newParams.items():
                    412:                 if isinstance(v, list):
                    413:                     if duplicates == 'comma':
                    414:                         # make comma-separated list of non-empty entries
                    415:                         newParams[k] = ','.join([t for t in v if t])
                    416:                     elif duplicates == 'first':
                    417:                         # take first non-empty entry
                    418:                         newParams[k] = [t for t in v if t][0]
                    419:         
1.175.2.16  casties   420:         return newParams
                    421:     
1.175.2.24  casties   422:     def getLink(self, param=None, val=None, params=None, baseUrl=None, paramSep='&', duplicates='comma'):
1.175.2.16  casties   423:         """returns URL to documentviewer with parameter param set to val or from dict params"""
1.175.2.24  casties   424:         urlParams = self.getParams(param=param, val=val, params=params, duplicates=duplicates)
1.162     casties   425:         # quote values and assemble into query string (not escaping '/')
1.175.2.21  casties   426:         ps = paramSep.join(["%s=%s"%(k,urllib.quote_plus(unicode(v),'/')) for (k, v) in urlParams.items()])
1.162     casties   427:         if baseUrl is None:
1.175.2.16  casties   428:             baseUrl = self.getDocumentViewerURL()
1.162     casties   429:             
                    430:         url = "%s?%s"%(baseUrl, ps)
1.4       casties   431:         return url
                    432: 
1.175.2.24  casties   433:     def getLinkAmp(self, param=None, val=None, params=None, baseUrl=None, duplicates='comma'):
1.32      dwinter   434:         """link to documentviewer with parameter param set to val"""
1.175.2.24  casties   435:         return self.getLink(param=param, val=val, params=params, baseUrl=baseUrl, paramSep='&', duplicates=duplicates)
1.40      casties   436:     
1.175.2.16  casties   437:     
1.26      casties   438:     def getInfo_xml(self,url,mode):
                    439:         """returns info about the document as XML"""
                    440:         if not self.digilibBaseUrl:
                    441:             self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary"
                    442:         
                    443:         docinfo = self.getDocinfo(mode=mode,url=url)
                    444:         pt = getattr(self.template, 'info_xml')
                    445:         return pt(docinfo=docinfo)
                    446: 
1.9       casties   447:     def isAccessible(self, docinfo):
1.8       casties   448:         """returns if access to the resource is granted"""
                    449:         access = docinfo.get('accessType', None)
1.95      abukhman  450:         logging.debug("documentViewer (accessOK) access type %s"%access)
1.175.2.12  casties   451:         if access == 'free':
1.95      abukhman  452:             logging.debug("documentViewer (accessOK) access is free")
1.8       casties   453:             return True
1.175.2.12  casties   454:         
1.17      casties   455:         elif access is None or access in self.authgroups:
1.9       casties   456:             # only local access -- only logged in users
                    457:             user = getSecurityManager().getUser()
1.95      abukhman  458:             logging.debug("documentViewer (accessOK) user=%s ip=%s"%(user,self.REQUEST.getClientAddr()))
1.9       casties   459:             if user is not None:
                    460:                 #print "user: ", user
                    461:                 return (user.getUserName() != "Anonymous User")
                    462:             else:
                    463:                 return False
1.8       casties   464:         
1.95      abukhman  465:         logging.error("documentViewer (accessOK) unknown access type %s"%access)
1.8       casties   466:         return False
1.9       casties   467:     
1.175.2.11  casties   468: 
                    469: 
                    470:     def getDocinfo(self, mode, url):
                    471:         """returns docinfo depending on mode"""
                    472:         logging.debug("getDocinfo: mode=%s, url=%s"%(mode,url))
                    473:         # look for cached docinfo in session
                    474:         if self.REQUEST.SESSION.has_key('docinfo'):
                    475:             docinfo = self.REQUEST.SESSION['docinfo']
                    476:             # check if its still current
                    477:             if docinfo is not None and docinfo.get('mode', None) == mode and docinfo.get('url', None) == url:
                    478:                 logging.debug("getDocinfo: docinfo in session. keys=%s"%docinfo.keys())
                    479:                 return docinfo
                    480:             
                    481:         # new docinfo
                    482:         docinfo = {'mode': mode, 'url': url}
                    483:         # add self url
                    484:         docinfo['viewerUrl'] = self.getDocumentViewerURL()
1.175.2.23  casties   485:         docinfo['digilibBaseUrl'] = self.digilibBaseUrl
1.175.2.11  casties   486:         # get index.meta DOM
                    487:         docUrl = None
                    488:         metaDom = None
                    489:         if mode=="texttool": 
                    490:             # url points to document dir or index.meta
                    491:             metaDom = self.metadataService.getDomFromPathOrUrl(url)
                    492:             docUrl = url.replace('/index.meta', '')
                    493:             if metaDom is None:
                    494:                 raise IOError("Unable to find index.meta for mode=texttool!")
                    495: 
                    496:         elif mode=="imagepath":
                    497:             # url points to folder with images, index.meta optional
                    498:             # asssume index.meta in parent dir
                    499:             docUrl = getParentPath(url)
                    500:             metaDom = self.metadataService.getDomFromPathOrUrl(docUrl)
                    501: 
                    502:         elif mode=="filepath":
                    503:             # url points to image file, index.meta optional
                    504:             # asssume index.meta is two path segments up
                    505:             docUrl = getParentPath(url, 2)
                    506:             metaDom = self.metadataService.getDomFromPathOrUrl(docUrl)
                    507: 
                    508:         else:
                    509:             logging.error("documentViewer (getdocinfo) unknown mode: %s!"%mode)
                    510:             raise ValueError("Unknown mode %s! Has to be one of 'texttool','imagepath','filepath'."%(mode))
                    511:         
                    512:         docinfo['documentUrl'] = docUrl
                    513:         # process index.meta contents
1.175.2.19  casties   514:         if metaDom is not None and metaDom.tag == 'resource':
1.175.2.11  casties   515:             # document directory name and path
                    516:             resource = self.metadataService.getResourceData(dom=metaDom)
                    517:             if resource:
                    518:                 docinfo = self.getDocinfoFromResource(docinfo, resource)
                    519: 
                    520:             # texttool info
                    521:             texttool = self.metadataService.getTexttoolData(dom=metaDom)
                    522:             if texttool:
                    523:                 docinfo = self.getDocinfoFromTexttool(docinfo, texttool)
                    524:             
                    525:             # bib info
                    526:             bib = self.metadataService.getBibData(dom=metaDom)
                    527:             if bib:
                    528:                 docinfo = self.getDocinfoFromBib(docinfo, bib)
1.175.2.12  casties   529:             else:
                    530:                 # no bib - try info.xml
                    531:                 docinfo = self.getDocinfoFromPresentationInfoXml(docinfo)
1.175.2.11  casties   532:                 
                    533:             # auth info
                    534:             access = self.metadataService.getAccessData(dom=metaDom)
                    535:             if access:
                    536:                 docinfo = self.getDocinfoFromAccess(docinfo, access)
                    537: 
1.175.2.13  casties   538:             # attribution info
                    539:             attribution = self.metadataService.getAttributionData(dom=metaDom)
                    540:             if attribution:
                    541:                 logging.debug("getDocinfo: attribution=%s"%repr(attribution))
                    542:                 docinfo['attribution'] = attribution
                    543:                 #docinfo = self.getDocinfoFromAccess(docinfo, access)
                    544: 
                    545:             # copyright info
                    546:             copyright = self.metadataService.getCopyrightData(dom=metaDom)
                    547:             if copyright:
                    548:                 logging.debug("getDocinfo: copyright=%s"%repr(copyright))
                    549:                 docinfo['copyright'] = copyright
                    550:                 #docinfo = self.getDocinfoFromAccess(docinfo, access)
                    551: 
1.175.2.11  casties   552:         # image path
                    553:         if mode != 'texttool':
1.175.2.23  casties   554:             # override image path from texttool with url
1.175.2.12  casties   555:             docinfo['imagePath'] = url.replace('/mpiwg/online/', '', 1)
1.175.2.11  casties   556: 
                    557:         # number of images from digilib
                    558:         if docinfo.get('imagePath', None):
                    559:             docinfo['imageURL'] = self.digilibBaseUrl + "/servlet/Scaler?fn=" + docinfo['imagePath']
                    560:             docinfo = self.getDocinfoFromDigilib(docinfo, docinfo['imagePath'])
                    561: 
                    562:         logging.debug("documentViewer (getdocinfo) docinfo: keys=%s"%docinfo.keys())
                    563:         #logging.debug("documentViewer (getdocinfo) docinfo: %s"%docinfo)
                    564:         # store in session
                    565:         self.REQUEST.SESSION['docinfo'] = docinfo
                    566:         return docinfo
                    567: 
                    568:     def getDocinfoFromResource(self, docinfo, resource):
                    569:         """reads contents of resource element into docinfo"""
                    570:         docName = resource.get('name', None)
                    571:         docinfo['documentName'] = docName
                    572:         docPath = resource.get('archive-path', None)
                    573:         if docPath:
                    574:             # clean up document path
                    575:             if docPath[0] != '/':
                    576:                 docPath = '/' + docPath
                    577:                 
                    578:             if docName and (not docPath.endswith(docName)):
                    579:                 docPath += "/" + docName
                    580:             
                    581:         else:
                    582:             # use docUrl as docPath
                    583:             docUrl = docinfo['documentURL']
                    584:             if not docUrl.startswith('http:'):
                    585:                 docPath = docUrl
1.175.2.12  casties   586:         if docPath:
                    587:             # fix URLs starting with /mpiwg/online
                    588:             docPath = docPath.replace('/mpiwg/online', '', 1)
                    589: 
1.175.2.11  casties   590:         docinfo['documentPath'] = docPath
                    591:         return docinfo
                    592: 
                    593:     def getDocinfoFromTexttool(self, docinfo, texttool):
                    594:         """reads contents of texttool element into docinfo"""
                    595:         # image dir
                    596:         imageDir = texttool.get('image', None)
                    597:         docPath = docinfo.get('documentPath', None)
                    598:         if imageDir and docPath:
                    599:             #print "image: ", imageDir, " archivepath: ", archivePath
                    600:             imageDir = os.path.join(docPath, imageDir)
                    601:             imageDir = imageDir.replace('/mpiwg/online', '', 1)
                    602:             docinfo['imagePath'] = imageDir
                    603:         
                    604:         # old style text URL
                    605:         textUrl = texttool.get('text', None)
                    606:         if textUrl and docPath:
                    607:             if urlparse.urlparse(textUrl)[0] == "": #keine url
                    608:                 textUrl = os.path.join(docPath, textUrl) 
                    609:             
                    610:             docinfo['textURL'] = textUrl
                    611:     
                    612:         # new style text-url-path
                    613:         textUrl = texttool.get('text-url-path', None)
                    614:         if textUrl:
                    615:             docinfo['textURLPath'] = textUrl
1.175.2.15  casties   616:             
                    617:         # page flow
                    618:         docinfo['pageFlow'] = texttool.get('page-flow', 'ltr')
                    619:             
                    620:         # odd pages are left
1.175.2.22  casties   621:         docinfo['oddPage'] = texttool.get('odd-scan-position', 'left')
1.175.2.15  casties   622:             
1.175.2.16  casties   623:         # number of title page (0: not defined)
1.175.2.15  casties   624:         docinfo['titlePage'] = texttool.get('title-scan-no', 0)
1.175.2.11  casties   625:             
                    626:         # old presentation stuff
                    627:         presentation = texttool.get('presentation', None)
                    628:         if presentation and docPath:
1.175.2.12  casties   629:             if presentation.startswith('http:'):
                    630:                 docinfo['presentationUrl'] = presentation
                    631:             else:
                    632:                 docinfo['presentationUrl'] = os.path.join(docPath, presentation)
1.175.2.11  casties   633:             
1.175.2.15  casties   634:         
1.175.2.11  casties   635:         return docinfo
                    636: 
                    637:     def getDocinfoFromBib(self, docinfo, bib):
                    638:         """reads contents of bib element into docinfo"""
1.175.2.12  casties   639:         logging.debug("getDocinfoFromBib bib=%s"%repr(bib))
1.175.2.11  casties   640:         # put all raw bib fields in dict "bib"
                    641:         docinfo['bib'] = bib
                    642:         bibtype = bib.get('@type', None)
                    643:         docinfo['bibType'] = bibtype
                    644:         # also store DC metadata for convenience
                    645:         dc = self.metadataService.getDCMappedData(bib)
                    646:         docinfo['creator'] = dc.get('creator',None)
                    647:         docinfo['title'] = dc.get('title',None)
                    648:         docinfo['date'] = dc.get('date',None)
                    649:         return docinfo
                    650:             
                    651:     def getDocinfoFromAccess(self, docinfo, acc):
                    652:         """reads contents of access element into docinfo"""
                    653:         #TODO: also read resource type
1.175.2.12  casties   654:         logging.debug("getDocinfoFromAccess acc=%s"%repr(acc))
1.175.2.11  casties   655:         try:
1.175.2.12  casties   656:             acctype = acc['@attr']['type']
1.175.2.11  casties   657:             if acctype:
                    658:                 access=acctype
                    659:                 if access in ['group', 'institution']:
                    660:                     access = acc['name'].lower()
                    661:                 
                    662:                 docinfo['accessType'] = access
                    663: 
                    664:         except:
                    665:             pass
                    666:         
                    667:         return docinfo
                    668: 
                    669:     def getDocinfoFromDigilib(self, docinfo, path):
                    670:         infoUrl=self.digilibBaseUrl+"/dirInfo-xml.jsp?mo=dir&fn="+path
                    671:         # fetch data
                    672:         txt = getHttpData(infoUrl)
                    673:         if not txt:
                    674:             logging.error("Unable to get dir-info from %s"%(infoUrl))
                    675:             return docinfo
                    676: 
                    677:         dom = ET.fromstring(txt)
                    678:         size = getText(dom.find("size"))
                    679:         logging.debug("getDocinfoFromDigilib: size=%s"%size)
                    680:         if size:
                    681:             docinfo['numPages'] = int(size)
                    682:         else:
                    683:             docinfo['numPages'] = 0
                    684:             
                    685:         # TODO: produce and keep list of image names and numbers
                    686:         return docinfo
                    687:             
                    688:             
1.175.2.12  casties   689:     def getDocinfoFromPresentationInfoXml(self,docinfo):
                    690:         """gets DC-like bibliographical information from the presentation entry in texttools"""
                    691:         url = docinfo.get('presentationUrl', None)
                    692:         if not url:
                    693:             logging.error("getDocinfoFromPresentation: no URL!")
                    694:             return docinfo
                    695:         
                    696:         dom = None
                    697:         metaUrl = None
                    698:         if url.startswith("http://"):
                    699:             # real URL
                    700:             metaUrl = url
                    701:         else:
                    702:             # online path
                    703:             
                    704:             server=self.digilibBaseUrl+"/servlet/Texter?fn="
                    705:             metaUrl=server+url
                    706:         
                    707:         txt=getHttpData(metaUrl)
                    708:         if txt is None:
                    709:             logging.error("Unable to read info.xml from %s"%(url))
                    710:             return docinfo
                    711:             
                    712:         dom = ET.fromstring(txt)
                    713:         docinfo['creator']=getText(dom.find(".//author"))
                    714:         docinfo['title']=getText(dom.find(".//title"))
                    715:         docinfo['date']=getText(dom.find(".//date"))
                    716:         return docinfo
                    717:     
                    718: 
1.175.2.24  casties   719:     def getPageinfo(self, current=None, start=None, rows=None, cols=None, docinfo=None, viewMode=None, viewType=None, tocMode=None):
1.3       casties   720:         """returns pageinfo with the given parameters"""
1.175.2.24  casties   721:         logging.debug("getPageInfo(current=%s, start=%s, rows=%s, cols=%s, viewMode=%s, viewType=%s, tocMode=%s)"%(current,start,rows,cols,viewMode,viewType,tocMode))
1.3       casties   722:         pageinfo = {}
1.175.2.17  casties   723:         pageinfo['viewMode'] = viewMode
                    724:         pageinfo['viewType'] = viewType
                    725:         pageinfo['tocMode'] = tocMode
                    726: 
1.4       casties   727:         current = getInt(current)
                    728:         pageinfo['current'] = current
1.175.2.26  casties   729:         pageinfo['pn'] = current
1.4       casties   730:         rows = int(rows or self.thumbrows)
                    731:         pageinfo['rows'] = rows
                    732:         cols = int(cols or self.thumbcols)
                    733:         pageinfo['cols'] = cols
                    734:         grpsize = cols * rows
                    735:         pageinfo['groupsize'] = grpsize
1.175.2.22  casties   736:         # is start is empty use one around current
1.28      casties   737:         start = getInt(start, default=(math.ceil(float(current)/float(grpsize))*grpsize-(grpsize-1)))
                    738:         # int(current / grpsize) * grpsize +1))
1.3       casties   739:         pageinfo['start'] = start
1.175.2.26  casties   740:         
1.175.2.16  casties   741:         np = int(docinfo.get('numPages', 0))
                    742:         if np == 0:
                    743:             # numPages unknown - maybe we can get it from text page
                    744:             if docinfo.get('textURLPath', None):
                    745:                 # cache text page as well
1.175.2.26  casties   746:                 pageinfo['textPage'] = self.getTextPage(mode=viewType, pn=current, docinfo=docinfo, pageinfo=pageinfo)
1.175.2.16  casties   747:                 np = int(docinfo.get('numPages', 0))
                    748:                 
                    749:         pageinfo['numgroups'] = int(np / grpsize)
                    750:         if np % grpsize > 0:
                    751:             pageinfo['numgroups'] += 1
1.175.2.22  casties   752: 
                    753:         pageFlowLtr = docinfo.get('pageFlow', 'ltr') != 'rtl'
                    754:         oddScanLeft = docinfo.get('oddPage', 'left') != 'right'
                    755:         # add zeroth page for two columns
                    756:         pageZero = (cols == 2 and (pageFlowLtr != oddScanLeft))
                    757:         pageinfo['pageZero'] = pageZero
1.175.2.26  casties   758:         pageinfo['pageBatch'] = self.getPageBatch(start=start, rows=rows, cols=cols, pageFlowLtr=pageFlowLtr, pageZero=pageZero, minIdx=1, maxIdx=np)
1.175.2.7  casties   759:                 
1.175.2.26  casties   760:         # TODO: do we need this here?
1.161     abukhman  761:         pageinfo['characterNormalization'] = self.REQUEST.get('characterNormalization','reg')
1.156     abukhman  762:         pageinfo['query'] = self.REQUEST.get('query','') 
1.146     abukhman  763:         pageinfo['queryType'] = self.REQUEST.get('queryType','')
1.45      abukhman  764:         pageinfo['querySearch'] =self.REQUEST.get('querySearch', 'fulltext')
1.146     abukhman  765:         pageinfo['highlightQuery'] = self.REQUEST.get('highlightQuery','')
1.175.2.22  casties   766:         pageinfo['tocPageSize'] = getInt(self.REQUEST.get('tocPageSize', 30))
                    767:         pageinfo['queryPageSize'] = getInt(self.REQUEST.get('queryPageSize', 10))
                    768:         pageinfo['tocPN'] = getInt(self.REQUEST.get('tocPN', '1'))
                    769:         pageinfo['searchPN'] = getInt(self.REQUEST.get('searchPN','1'))
1.90      abukhman  770:         
1.175.2.22  casties   771:         # limit tocPN
1.48      abukhman  772:         if 'tocSize_%s'%tocMode in docinfo:
1.175.2.22  casties   773:             tocSize = docinfo['tocSize_%s'%tocMode]
                    774:             tocPageSize = pageinfo['tocPageSize']
1.69      abukhman  775:             # cached toc           
1.48      abukhman  776:             if tocSize%tocPageSize>0:
                    777:                 tocPages=tocSize/tocPageSize+1
                    778:             else:
                    779:                 tocPages=tocSize/tocPageSize
1.175.2.7  casties   780:                 
1.175.2.22  casties   781:             pageinfo['tocPN'] = min(tocPages,pageinfo['tocPN'])
1.175.2.7  casties   782:             
1.3       casties   783:         return pageinfo
1.175.2.7  casties   784: 
1.175.2.10  casties   785: 
1.175.2.27  casties   786:     def getPageBatch(self, start=1, rows=10, cols=2, pageFlowLtr=True, pageZero=False, minIdx=1, maxIdx=0):
1.175.2.26  casties   787:         """returns dict with array of page informations for one screenfull of thumbnails"""
1.175.2.27  casties   788:         batch = {}
1.175.2.26  casties   789:         grpsize = rows * cols
1.175.2.22  casties   790:         if maxIdx == 0:
1.175.2.26  casties   791:             maxIdx = start + grpsize
1.175.2.22  casties   792: 
1.175.2.27  casties   793:         nb = int(math.ceil(maxIdx / float(grpsize)))
                    794:         # list of all batch start and end points
                    795:         batches = []
                    796:         if pageZero:
                    797:             ofs = 0
                    798:         else:
                    799:             ofs = 1
                    800:             
                    801:         for i in range(nb):
                    802:             s = i * grpsize + ofs
                    803:             e = min((i + 1) * grpsize + ofs - 1, maxIdx)
                    804:             batches.append({'start':s, 'end':e})
                    805:             
                    806:         batch['batches'] = batches
                    807: 
1.175.2.22  casties   808:         pages = []
                    809:         if pageZero and start == 1:
                    810:             # correct beginning
                    811:             idx = 0
                    812:         else:
                    813:             idx = start
                    814:             
                    815:         for r in range(rows):
                    816:             row = []
                    817:             for c in range(cols):
                    818:                 if idx < minIdx or idx > maxIdx:
                    819:                     page = {'idx':None}
                    820:                 else:
                    821:                     page = {'idx':idx}
                    822:                     
                    823:                 idx += 1
                    824:                 if pageFlowLtr:
                    825:                     row.append(page)
                    826:                 else:
                    827:                     row.insert(0, page) 
                    828:                 
                    829:             pages.append(row)
                    830:             
1.175.2.26  casties   831:         if start > 1:
                    832:             batch['prevStart'] = max(start - grpsize, 1)
                    833:         else:
                    834:             batch['prevStart'] = None
                    835:             
                    836:         if start + grpsize < maxIdx:
                    837:             batch['nextStart'] = start + grpsize
                    838:         else:
                    839:             batch['nextStart'] = None
                    840: 
                    841:         batch['pages'] = pages
1.175.2.27  casties   842:         return batch
                    843:         
                    844:     def getBatch(self, start=1, size=10, end=0, data=None, fullData=True):
                    845:         """returns dict with information for one screenfull of data."""
                    846:         batch = {}
                    847:         if end == 0:
                    848:             end = start + size                    
                    849:             
                    850:         nb = int(math.ceil(end / float(size)))
                    851:         # list of all batch start and end points
                    852:         batches = []
                    853:         for i in range(nb):
                    854:             s = i * size + 1
                    855:             e = min((i + 1) * size, end)
                    856:             batches.append({'start':s, 'end':e})
                    857:             
                    858:         batch['batches'] = batches
                    859:         # list of elements in this batch
                    860:         this = []
                    861:         j = 0
                    862:         for i in range(start, min(start+size, end)):
                    863:             if data:
                    864:                 if fullData:
                    865:                     d = data[i]
                    866:                 else:
                    867:                     d = data[j]
                    868:                     j += 1
                    869:             
                    870:             else:
                    871:                 d = i+1
                    872:                 
                    873:             this.append(d)
                    874:             
                    875:         batch['this'] = this
                    876:         if start > 1:
                    877:             batch['prevStart'] = max(start - size, 1)
                    878:         else:
                    879:             batch['prevStart'] = None
                    880:             
                    881:         if start + size < end:
                    882:             batch['nextStart'] = start + size
                    883:         else:
                    884:             batch['nextStart'] = None
                    885:         
1.175.2.26  casties   886:         return batch
1.175.2.22  casties   887:         
                    888: 
1.175.2.10  casties   889:     security.declareProtected('View management screens','changeDocumentViewerForm')    
                    890:     changeDocumentViewerForm = PageTemplateFile('zpt/changeDocumentViewer', globals())
1.3       casties   891:     
1.175.2.7  casties   892:     def changeDocumentViewer(self,title="",digilibBaseUrl=None,thumbrows=2,thumbcols=5,authgroups='mpiwg',RESPONSE=None):
1.3       casties   893:         """init document viewer"""
                    894:         self.title=title
                    895:         self.digilibBaseUrl = digilibBaseUrl
1.4       casties   896:         self.thumbrows = thumbrows
                    897:         self.thumbcols = thumbcols
1.8       casties   898:         self.authgroups = [s.strip().lower() for s in authgroups.split(',')]
1.175.2.10  casties   899:         try:
                    900:             # assume MetaDataFolder instance is called metadata 
                    901:             self.metadataService = getattr(self, 'metadata')
                    902:         except Exception, e:
                    903:             logging.error("Unable to find MetaDataFolder 'metadata': "+str(e))
                    904: 
1.3       casties   905:         if RESPONSE is not None:
                    906:             RESPONSE.redirect('manage_main')
1.1       dwinter   907:         
                    908: def manage_AddDocumentViewerForm(self):
                    909:     """add the viewer form"""
1.3       casties   910:     pt=PageTemplateFile('zpt/addDocumentViewer', globals()).__of__(self)
1.1       dwinter   911:     return pt()
                    912:   
1.43      casties   913: def manage_AddDocumentViewer(self,id,imageScalerUrl="",textServerName="",title="",RESPONSE=None):
1.1       dwinter   914:     """add the viewer"""
1.43      casties   915:     newObj=documentViewer(id,imageScalerUrl=imageScalerUrl,title=title,textServerName=textServerName)
1.1       dwinter   916:     self._setObject(id,newObj)
                    917:     
                    918:     if RESPONSE is not None:
                    919:         RESPONSE.redirect('manage_main')

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