source: documentViewer/documentViewer.py @ 7:75c5208cdf64

modularisierung
Last change on this file since 7:75c5208cdf64 was 7:75c5208cdf64, checked in by casties, 14 years ago

fixed oopsie

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