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