0
|
1 from OFS.Folder import Folder
|
|
2 from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
|
|
3 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
|
|
4 from AccessControl import ClassSecurityInfo
|
32
|
5 from AccessControl import getSecurityManager
|
0
|
6 from Globals import package_home
|
|
7
|
|
8 from Ft.Xml.Domlette import NonvalidatingReader
|
|
9 from Ft.Xml.Domlette import PrettyPrint, Print
|
|
10 from Ft.Xml import EMPTY_NAMESPACE
|
|
11
|
|
12 import Ft.Xml.XPath
|
|
13
|
|
14 import os.path
|
31
|
15 import sys
|
0
|
16 import cgi
|
|
17 import urllib
|
22
|
18 import zLOG
|
0
|
19
|
25
|
20 def getInt(number, default=0):
|
|
21 """returns always an int (0 in case of problems)"""
|
|
22 try:
|
|
23 return int(number)
|
|
24 except:
|
|
25 return default
|
|
26
|
0
|
27 def getTextFromNode(nodename):
|
32
|
28 if nodename is None:
|
|
29 return ""
|
0
|
30 nodelist=nodename.childNodes
|
|
31 rc = ""
|
|
32 for node in nodelist:
|
|
33 if node.nodeType == node.TEXT_NODE:
|
|
34 rc = rc + node.data
|
|
35 return rc
|
|
36
|
35
|
37
|
|
38 def getParentDir(path):
|
|
39 """returns pathname shortened by one"""
|
|
40 return '/'.join(path.split('/')[0:-1])
|
|
41
|
|
42
|
0
|
43 import socket
|
|
44
|
32
|
45 def urlopen(url,timeout=2):
|
0
|
46 """urlopen mit timeout"""
|
32
|
47 socket.setdefaulttimeout(timeout)
|
0
|
48 ret=urllib.urlopen(url)
|
|
49 socket.setdefaulttimeout(5)
|
|
50 return ret
|
|
51
|
|
52
|
22
|
53 ##
|
|
54 ## documentViewer class
|
|
55 ##
|
|
56 class documentViewer(Folder):
|
0
|
57 """document viewer"""
|
|
58
|
|
59 meta_type="Document viewer"
|
|
60
|
|
61 security=ClassSecurityInfo()
|
22
|
62 manage_options=Folder.manage_options+(
|
0
|
63 {'label':'main config','action':'changeDocumentViewerForm'},
|
|
64 )
|
|
65
|
22
|
66 # templates and forms
|
|
67 viewer_main = PageTemplateFile('zpt/viewer_main', globals())
|
|
68 thumbs_main = PageTemplateFile('zpt/thumbs_main', globals())
|
|
69 image_main = PageTemplateFile('zpt/image_main', globals())
|
|
70 head_main = PageTemplateFile('zpt/head_main', globals())
|
|
71 docuviewer_css = PageTemplateFile('css/docuviewer.css', globals())
|
|
72
|
|
73 security.declareProtected('View management screens','changeDocumentViewerForm')
|
|
74 changeDocumentViewerForm = PageTemplateFile('zpt/changeDocumentViewer', globals())
|
|
75
|
0
|
76
|
32
|
77 def __init__(self,id,imageViewerUrl,title="",digilibBaseUrl=None,thumbcols=2,thumbrows=10,authgroups="mpiwg"):
|
0
|
78 """init document viewer"""
|
|
79 self.id=id
|
|
80 self.title=title
|
|
81 self.imageViewerUrl=imageViewerUrl
|
25
|
82 if not digilibBaseUrl:
|
22
|
83 self.digilibBaseUrl = self.findDigilibUrl()
|
25
|
84 else:
|
|
85 self.digilibBaseUrl = digilibBaseUrl
|
|
86 self.thumbcols = thumbcols
|
|
87 self.thumbrows = thumbrows
|
32
|
88 # authgroups is list of authorized groups (delimited by ,)
|
|
89 self.authgroups = [s.strip().lower() for s in authgroups.split(',')]
|
22
|
90 # add template folder so we can always use template.something
|
|
91 self.manage_addFolder('template')
|
|
92
|
|
93
|
|
94 security.declareProtected('View','index_html')
|
25
|
95 def index_html(self,mode,url,start=None,pn=1):
|
22
|
96 '''
|
|
97 view it
|
|
98 @param mode: defines which type of document is behind url
|
|
99 @param url: url which contains display information
|
|
100 '''
|
0
|
101
|
22
|
102 zLOG.LOG("documentViewer (index)", zLOG.INFO, "mode: %s url:%s start:%s pn:%s"%(mode,url,start,pn))
|
|
103
|
|
104 if not hasattr(self, 'template'):
|
|
105 # create template folder if it doesn't exist
|
|
106 self.manage_addFolder('template')
|
|
107
|
|
108 if not self.digilibBaseUrl:
|
|
109 self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary"
|
|
110
|
25
|
111 docinfo = self.getDocinfo(mode=mode,url=url)
|
|
112 pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo)
|
22
|
113 pt = getattr(self.template, 'viewer_main')
|
|
114 return pt(docinfo=docinfo,pageinfo=pageinfo)
|
0
|
115
|
|
116
|
25
|
117 def getLink(self,param=None,val=None):
|
|
118 """link to documentviewer with parameter param set to val"""
|
35
|
119 params=self.REQUEST.form.copy()
|
25
|
120 if param is not None:
|
31
|
121 if val is None:
|
|
122 if params.has_key(param):
|
|
123 del params[param]
|
25
|
124 else:
|
35
|
125 params[param] = str(val)
|
31
|
126
|
35
|
127 # quote values and assemble into query string
|
|
128 ps = "&".join(["%s=%s"%(k,urllib.quote(v)) for (k, v) in params.items()])
|
|
129 url=self.REQUEST['URL1']+"?"+ps
|
25
|
130 return url
|
|
131
|
0
|
132
|
22
|
133 def getStyle(self, idx, selected, style=""):
|
25
|
134 """returns a string with the given style and append 'sel' if path == selected."""
|
22
|
135 #zLOG.LOG("documentViewer (getstyle)", zLOG.INFO, "idx: %s selected: %s style: %s"%(idx,selected,style))
|
|
136 if idx == selected:
|
|
137 return style + 'sel'
|
|
138 else:
|
35
|
139 return style
|
0
|
140
|
35
|
141
|
|
142 def isAccessible(self, docinfo):
|
32
|
143 """returns if access to the resource is granted"""
|
|
144 access = docinfo.get('accessType', None)
|
|
145 if access is None:
|
35
|
146 # no information - no access
|
|
147 #TODO: check
|
32
|
148 return True
|
|
149 elif access == 'free':
|
|
150 return True
|
35
|
151 elif access in self.authgroups:
|
|
152 # only local access -- only logged in users
|
|
153 user = getSecurityManager().getUser()
|
|
154 if user is not None:
|
|
155 #print "user: ", user
|
|
156 return (user.getUserName() != "Anonymous User")
|
|
157 else:
|
|
158 return False
|
32
|
159
|
35
|
160 zLOG.LOG("documentViewer (accessOK)", zLOG.INFO, "unknown access type %s"%access)
|
32
|
161 return False
|
35
|
162
|
32
|
163
|
31
|
164 def getDirinfoFromDigilib(self,path,docinfo=None):
|
29
|
165 """gibt param von dlInfo aus"""
|
31
|
166 if docinfo is None:
|
|
167 docinfo = {}
|
|
168
|
29
|
169 imageUrl=self.digilibBaseUrl+"/dirInfo-xml.jsp?mo=dir&fn="+path
|
|
170
|
31
|
171 zLOG.LOG("documentViewer (getparamfromdigilib)", zLOG.INFO, "dirInfo from %s"%(imageUrl))
|
29
|
172
|
35
|
173 for cnt in (1,2,3):
|
|
174 try:
|
|
175 dom = NonvalidatingReader.parseUri(imageUrl)
|
|
176 break
|
|
177 except:
|
|
178 zLOG.LOG("documentViewer (getdirinfofromdigilib)", zLOG.ERROR, "error reading %s (try %d)"%(imageUrl,cnt))
|
|
179 else:
|
31
|
180 raise IOError("Unable to get dirinfo from %s"%(imageUrl))
|
29
|
181
|
31
|
182 params=dom.xpath("//dir/size")
|
|
183 zLOG.LOG("documentViewer (getparamfromdigilib)", zLOG.INFO, "dirInfo:size"%params)
|
29
|
184
|
|
185 if params:
|
32
|
186 docinfo['numPages'] = int(getTextFromNode(params[0]))
|
31
|
187 else:
|
|
188 docinfo['numPages'] = 0
|
|
189
|
|
190 return docinfo
|
32
|
191
|
29
|
192
|
35
|
193 def getIndexMeta(self, url):
|
|
194 """returns dom of index.meta document at url"""
|
|
195 dom = None
|
|
196 if url.startswith("http://"):
|
|
197 # real URL
|
|
198 try:
|
|
199 dom = NonvalidatingReader.parseUri(url)
|
|
200 except:
|
|
201 zLOG.LOG("documentViewer (getIndexMata)", zLOG.INFO,"%s (%s)"%sys.exc_info()[0:2])
|
|
202 raise IOError("Unable to get info from %s"%(url))
|
|
203 else:
|
|
204 # online path
|
|
205 server=self.digilibBaseUrl+"/servlet/Texter?fn="
|
|
206 metaUrl=server+url
|
|
207 if not metaUrl.endswith("index.meta"):
|
|
208 metaUrl += "/index.meta"
|
|
209 try:
|
|
210 dom = NonvalidatingReader.parseUri(metaUrl)
|
|
211 except:
|
|
212 zLOG.LOG("documentViewer (getIndexMata)", zLOG.INFO,"%s (%s)"%sys.exc_info()[0:2])
|
|
213 raise IOError("Unable to get info from %s"%(url))
|
|
214
|
|
215 return dom
|
|
216
|
|
217
|
32
|
218 def getAuthinfoFromIndexMeta(self,path,docinfo=None,dom=None):
|
35
|
219 """gets authorization info from the index.meta file at path or given by dom"""
|
32
|
220 zLOG.LOG("documentViewer (getbibinfofromindexmeta)", zLOG.INFO,"path: %s"%(path))
|
|
221
|
|
222 access = None
|
|
223
|
|
224 if docinfo is None:
|
|
225 docinfo = {}
|
|
226
|
|
227 if dom is None:
|
35
|
228 dom = self.getIndexMeta(getParentDir(path))
|
32
|
229
|
|
230 acctype = dom.xpath("//access-conditions/access/@type")
|
|
231 if acctype and (len(acctype)>0):
|
|
232 access=acctype[0].value
|
35
|
233 if access in ['group', 'institution']:
|
32
|
234 access = getTextFromNode(dom.xpath("//access-conditions/access/name")[0]).lower()
|
|
235
|
|
236 docinfo['accessType'] = access
|
|
237 return docinfo
|
29
|
238
|
32
|
239
|
22
|
240 def getBibinfoFromIndexMeta(self,path,docinfo=None,dom=None):
|
35
|
241 """gets bibliographical info from the index.meta file at path or given by dom"""
|
22
|
242 zLOG.LOG("documentViewer (getbibinfofromindexmeta)", zLOG.INFO,"path: %s"%(path))
|
20
|
243
|
22
|
244 if docinfo is None:
|
|
245 docinfo = {}
|
|
246
|
|
247 if dom is None:
|
35
|
248 dom = self.getIndexMeta(getParentDir(path))
|
|
249
|
25
|
250 metaData=self.metadata.main.meta.bib
|
|
251 bibtype=dom.xpath("//bib/@type")
|
|
252 if bibtype and (len(bibtype)>0):
|
|
253 bibtype=bibtype[0].value
|
20
|
254 else:
|
25
|
255 bibtype="generic"
|
|
256 bibtype=bibtype.replace("-"," ") # wrong typesiin index meta "-" instead of " " (not wrong! ROC)
|
|
257 bibmap=metaData.generateMappingForType(bibtype)
|
35
|
258 #print "bibmap: ", bibmap, " for: ", bibtype
|
32
|
259 # if there is no mapping bibmap is empty (mapping sometimes has empty fields)
|
31
|
260 if len(bibmap) > 0 and len(bibmap['author'][0]) > 0:
|
25
|
261 docinfo['author']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['author'][0])[0])
|
|
262 docinfo['title']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['title'][0])[0])
|
|
263 docinfo['year']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['year'][0])[0])
|
22
|
264
|
|
265 return docinfo
|
|
266
|
|
267
|
32
|
268 def getDocinfoFromTextTool(self,url,dom=None,docinfo=None):
|
22
|
269 """parse texttool tag in index meta"""
|
|
270 zLOG.LOG("documentViewer (getdocinfofromtexttool)", zLOG.INFO,"url: %s"%(url))
|
|
271 if docinfo is None:
|
|
272 docinfo = {}
|
|
273
|
32
|
274 if dom is None:
|
35
|
275 dom = self.getIndexMeta(url)
|
32
|
276
|
|
277 archiveNames=dom.xpath("//resource/name")
|
|
278 if archiveNames and (len(archiveNames)>0):
|
|
279 archiveName=getTextFromNode(archiveNames[0])
|
22
|
280
|
|
281 archivePaths=dom.xpath("//resource/archive-path")
|
|
282 if archivePaths and (len(archivePaths)>0):
|
|
283 archivePath=getTextFromNode(archivePaths[0])
|
32
|
284 # clean up archive path
|
|
285 if archivePath[0] != '/':
|
|
286 archivePath = '/' + archivePath
|
|
287 if not archivePath.endswith(archiveName):
|
|
288 archivePath += "/" + archiveName
|
22
|
289 else:
|
|
290 archivePath=None
|
|
291
|
35
|
292 imageDirs=dom.xpath("//texttool/image")
|
|
293 if imageDirs and (len(imageDirs)>0):
|
|
294 imageDir=getTextFromNode(imageDirs[0])
|
22
|
295 else:
|
35
|
296 imageDir=None
|
22
|
297
|
35
|
298 if imageDir and archivePath:
|
|
299 #print "image: ", imageDir, " archivepath: ", archivePath
|
|
300 imageDir=os.path.join(archivePath,imageDir)
|
|
301 imageDir=imageDir.replace("/mpiwg/online",'')
|
|
302 docinfo=self.getDirinfoFromDigilib(imageDir,docinfo=docinfo)
|
|
303 docinfo['imagePath'] = imageDir
|
|
304 docinfo['imageURL'] = self.digilibBaseUrl+"/servlet/Scaler?fn="+imageDir
|
22
|
305
|
|
306 viewerUrls=dom.xpath("//texttool/digiliburlprefix")
|
|
307 if viewerUrls and (len(viewerUrls)>0):
|
|
308 viewerUrl=getTextFromNode(viewerUrls[0])
|
31
|
309 docinfo['viewerURL'] = viewerUrl
|
22
|
310
|
|
311 textUrls=dom.xpath("//texttool/text")
|
|
312 if textUrls and (len(textUrls)>0):
|
|
313 textUrl=getTextFromNode(textUrls[0])
|
31
|
314 docinfo['textURL'] = textUrl
|
22
|
315
|
|
316 docinfo = self.getBibinfoFromIndexMeta(url,docinfo=docinfo,dom=dom)
|
32
|
317 docinfo = self.getAuthinfoFromIndexMeta(url,docinfo=docinfo,dom=dom)
|
22
|
318 return docinfo
|
|
319
|
|
320
|
|
321 def getDocinfoFromImagePath(self,path,docinfo=None):
|
|
322 """path ist the path to the images it assumes that the index.meta file is one level higher."""
|
|
323 zLOG.LOG("documentViewer (getdocinfofromimagepath)", zLOG.INFO,"path: %s"%(path))
|
|
324 if docinfo is None:
|
|
325 docinfo = {}
|
29
|
326 path=path.replace("/mpiwg/online","")
|
22
|
327 docinfo['imagePath'] = path
|
31
|
328 docinfo=self.getDirinfoFromDigilib(path,docinfo=docinfo)
|
|
329 imageUrl=self.digilibBaseUrl+"/servlet/Scaler?fn="+path
|
22
|
330 docinfo['imageURL'] = imageUrl
|
|
331
|
|
332 docinfo = self.getBibinfoFromIndexMeta(path,docinfo=docinfo)
|
32
|
333 docinfo = self.getAuthinfoFromIndexMeta(path,docinfo=docinfo)
|
22
|
334 return docinfo
|
20
|
335
|
22
|
336
|
|
337 def getDocinfo(self, mode, url):
|
|
338 """returns docinfo depending on mode"""
|
|
339 zLOG.LOG("documentViewer (getdocinfo)", zLOG.INFO,"mode: %s, url: %s"%(mode,url))
|
|
340 # look for cached docinfo in session
|
|
341 if self.REQUEST.SESSION.has_key('docinfo'):
|
|
342 docinfo = self.REQUEST.SESSION['docinfo']
|
|
343 # check if its still current
|
|
344 if docinfo is not None and docinfo.get('mode') == mode and docinfo.get('url') == url:
|
|
345 zLOG.LOG("documentViewer (getdocinfo)", zLOG.INFO,"docinfo in session: %s"%docinfo)
|
|
346 return docinfo
|
|
347 # new docinfo
|
|
348 docinfo = {'mode': mode, 'url': url}
|
|
349 if mode=="texttool": #index.meta with texttool information
|
|
350 docinfo = self.getDocinfoFromTextTool(url, docinfo=docinfo)
|
|
351 elif mode=="imagepath":
|
|
352 docinfo = self.getDocinfoFromImagePath(url, docinfo=docinfo)
|
|
353 else:
|
|
354 zLOG.LOG("documentViewer (getdocinfo)", zLOG.ERROR,"unknown mode!")
|
|
355 zLOG.LOG("documentViewer (getdocinfo)", zLOG.INFO,"docinfo: %s"%docinfo)
|
|
356 self.REQUEST.SESSION['docinfo'] = docinfo
|
|
357 return docinfo
|
20
|
358
|
|
359
|
25
|
360 def getPageinfo(self, current, start=None, rows=None, cols=None, docinfo=None):
|
22
|
361 """returns pageinfo with the given parameters"""
|
|
362 pageinfo = {}
|
25
|
363 current = getInt(current)
|
|
364 pageinfo['current'] = current
|
|
365 rows = int(rows or self.thumbrows)
|
|
366 pageinfo['rows'] = rows
|
|
367 cols = int(cols or self.thumbcols)
|
|
368 pageinfo['cols'] = cols
|
|
369 grpsize = cols * rows
|
|
370 pageinfo['groupsize'] = grpsize
|
|
371 start = getInt(start, default=(int(current / grpsize) * grpsize +1))
|
22
|
372 pageinfo['start'] = start
|
25
|
373 pageinfo['end'] = start + grpsize
|
|
374 if docinfo is not None:
|
|
375 np = int(docinfo['numPages'])
|
|
376 pageinfo['end'] = min(pageinfo['end'], np)
|
|
377 pageinfo['numgroups'] = int(np / grpsize)
|
|
378 if np % grpsize > 0:
|
|
379 pageinfo['numgroups'] += 1
|
|
380
|
22
|
381 return pageinfo
|
|
382
|
0
|
383 def text(self,mode,url,pn):
|
|
384 """give text"""
|
|
385 if mode=="texttool": #index.meta with texttool information
|
|
386 (viewerUrl,imagepath,textpath)=parseUrlTextTool(url)
|
|
387
|
35
|
388 #print textpath
|
0
|
389 try:
|
|
390 dom = NonvalidatingReader.parseUri(textpath)
|
|
391 except:
|
|
392 return None
|
|
393
|
|
394 list=[]
|
|
395 nodes=dom.xpath("//pb")
|
|
396
|
|
397 node=nodes[int(pn)-1]
|
|
398
|
|
399 p=node
|
|
400
|
|
401 while p.tagName!="p":
|
|
402 p=p.parentNode
|
|
403
|
|
404
|
|
405 endNode=nodes[int(pn)]
|
|
406
|
|
407
|
|
408 e=endNode
|
|
409
|
|
410 while e.tagName!="p":
|
|
411 e=e.parentNode
|
|
412
|
|
413
|
|
414 next=node.parentNode
|
|
415
|
|
416 #sammle s
|
|
417 while next and (next!=endNode.parentNode):
|
|
418 list.append(next)
|
|
419 next=next.nextSibling
|
|
420 list.append(endNode.parentNode)
|
|
421
|
|
422 if p==e:# beide im selben paragraphen
|
20
|
423 pass
|
|
424 # else:
|
|
425 # next=p
|
|
426 # while next!=e:
|
|
427 # print next,e
|
|
428 # list.append(next)
|
|
429 # next=next.nextSibling
|
|
430 #
|
|
431 # for x in list:
|
|
432 # PrettyPrint(x)
|
|
433 #
|
|
434 # return list
|
22
|
435 #
|
|
436
|
|
437 def findDigilibUrl(self):
|
|
438 """try to get the digilib URL from zogilib"""
|
|
439 url = self.imageViewerUrl[:-1] + "/getScalerUrl"
|
|
440 try:
|
|
441 scaler = urlopen(url).read()
|
|
442 return scaler.replace("/servlet/Scaler?", "")
|
|
443 except:
|
|
444 return None
|
|
445
|
32
|
446 def changeDocumentViewer(self,imageViewerUrl,title="",digilibBaseUrl=None,thumbrows=2,thumbcols=10,authgroups='mpiwg',RESPONSE=None):
|
22
|
447 """init document viewer"""
|
|
448 self.title=title
|
|
449 self.imageViewerUrl=imageViewerUrl
|
|
450 self.digilibBaseUrl = digilibBaseUrl
|
25
|
451 self.thumbrows = thumbrows
|
|
452 self.thumbcols = thumbcols
|
32
|
453 self.authgroups = [s.strip().lower() for s in authgroups.split(',')]
|
22
|
454 if RESPONSE is not None:
|
|
455 RESPONSE.redirect('manage_main')
|
0
|
456
|
|
457
|
|
458
|
|
459
|
|
460 # security.declareProtected('View management screens','renameImageForm')
|
|
461
|
|
462 def manage_AddDocumentViewerForm(self):
|
|
463 """add the viewer form"""
|
22
|
464 pt=PageTemplateFile('zpt/addDocumentViewer', globals()).__of__(self)
|
0
|
465 return pt()
|
|
466
|
|
467 def manage_AddDocumentViewer(self,id,imageViewerUrl="",title="",RESPONSE=None):
|
|
468 """add the viewer"""
|
|
469 newObj=documentViewer(id,imageViewerUrl,title)
|
|
470 self._setObject(id,newObj)
|
|
471
|
|
472 if RESPONSE is not None:
|
|
473 RESPONSE.redirect('manage_main')
|
22
|
474
|
|
475
|
|
476 ##
|
|
477 ## DocumentViewerTemplate class
|
|
478 ##
|
|
479 class DocumentViewerTemplate(ZopePageTemplate):
|
|
480 """Template for document viewer"""
|
|
481 meta_type="DocumentViewer Template"
|
|
482
|
|
483
|
|
484 def manage_addDocumentViewerTemplateForm(self):
|
|
485 """Form for adding"""
|
|
486 pt=PageTemplateFile('zpt/addDocumentViewerTemplate', globals()).__of__(self)
|
|
487 return pt()
|
|
488
|
|
489 def manage_addDocumentViewerTemplate(self, id='viewer_main', title=None, text=None,
|
|
490 REQUEST=None, submit=None):
|
|
491 "Add a Page Template with optional file content."
|
|
492
|
|
493 self._setObject(id, DocumentViewerTemplate(id))
|
|
494 ob = getattr(self, id)
|
|
495 ob.pt_edit(open(os.path.join(package_home(globals()),'zpt/viewer_main.zpt')).read(),None)
|
|
496 if title:
|
|
497 ob.pt_setTitle(title)
|
|
498 try:
|
|
499 u = self.DestinationURL()
|
|
500 except AttributeError:
|
|
501 u = REQUEST['URL1']
|
|
502
|
|
503 u = "%s/%s" % (u, urllib.quote(id))
|
|
504 REQUEST.RESPONSE.redirect(u+'/manage_main')
|
|
505 return ''
|
|
506
|
|
507
|
0
|
508 |