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