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 sys |
---|
18 | import cgi |
---|
19 | import urllib |
---|
20 | import zLOG |
---|
21 | |
---|
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 | |
---|
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 | |
---|
48 | ## |
---|
49 | ## documentViewer class |
---|
50 | ## |
---|
51 | class documentViewer(Folder): |
---|
52 | """document viewer""" |
---|
53 | |
---|
54 | meta_type="Document viewer" |
---|
55 | |
---|
56 | security=ClassSecurityInfo() |
---|
57 | manage_options=Folder.manage_options+( |
---|
58 | {'label':'main config','action':'changeDocumentViewerForm'}, |
---|
59 | ) |
---|
60 | |
---|
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 | |
---|
71 | |
---|
72 | def __init__(self,id,imageViewerUrl,title="",digilibBaseUrl=None,thumbcols=2,thumbrows=10): |
---|
73 | """init document viewer""" |
---|
74 | self.id=id |
---|
75 | self.title=title |
---|
76 | self.imageViewerUrl=imageViewerUrl |
---|
77 | if not digilibBaseUrl: |
---|
78 | self.digilibBaseUrl = self.findDigilibUrl() |
---|
79 | else: |
---|
80 | self.digilibBaseUrl = digilibBaseUrl |
---|
81 | self.thumbcols = thumbcols |
---|
82 | self.thumbrows = thumbrows |
---|
83 | # add template folder so we can always use template.something |
---|
84 | self.manage_addFolder('template') |
---|
85 | |
---|
86 | |
---|
87 | security.declareProtected('View','index_html') |
---|
88 | def index_html(self,mode,url,start=None,pn=1): |
---|
89 | ''' |
---|
90 | view it |
---|
91 | @param mode: defines which type of document is behind url |
---|
92 | @param url: url which contains display information |
---|
93 | ''' |
---|
94 | |
---|
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 | |
---|
104 | docinfo = self.getDocinfo(mode=mode,url=url) |
---|
105 | pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo) |
---|
106 | pt = getattr(self.template, 'viewer_main') |
---|
107 | return pt(docinfo=docinfo,pageinfo=pageinfo) |
---|
108 | |
---|
109 | |
---|
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: |
---|
114 | if val is None: |
---|
115 | if params.has_key(param): |
---|
116 | del params[param] |
---|
117 | else: |
---|
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) |
---|
123 | return url |
---|
124 | |
---|
125 | |
---|
126 | def getStyle(self, idx, selected, style=""): |
---|
127 | """returns a string with the given style and append 'sel' if path == selected.""" |
---|
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 |
---|
133 | |
---|
134 | |
---|
135 | def getDirinfoFromDigilib(self,path,docinfo=None): |
---|
136 | """gibt param von dlInfo aus""" |
---|
137 | if docinfo is None: |
---|
138 | docinfo = {} |
---|
139 | |
---|
140 | imageUrl=self.digilibBaseUrl+"/dirInfo-xml.jsp?mo=dir&fn="+path |
---|
141 | |
---|
142 | zLOG.LOG("documentViewer (getparamfromdigilib)", zLOG.INFO, "dirInfo from %s"%(imageUrl)) |
---|
143 | |
---|
144 | try: |
---|
145 | dom = NonvalidatingReader.parseUri(imageUrl) |
---|
146 | except: |
---|
147 | zLOG.LOG("documentViewer (getparamfromdigilib)", zLOG.ERROR, "error reading %s"%(imageUrl)) |
---|
148 | raise IOError("Unable to get dirinfo from %s"%(imageUrl)) |
---|
149 | |
---|
150 | params=dom.xpath("//dir/size") |
---|
151 | zLOG.LOG("documentViewer (getparamfromdigilib)", zLOG.INFO, "dirInfo:size"%params) |
---|
152 | |
---|
153 | if params: |
---|
154 | docinfo['numPages'] = getTextFromNode(params[0]) |
---|
155 | else: |
---|
156 | docinfo['numPages'] = 0 |
---|
157 | |
---|
158 | return docinfo |
---|
159 | |
---|
160 | |
---|
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)) |
---|
164 | |
---|
165 | if docinfo is None: |
---|
166 | docinfo = {} |
---|
167 | |
---|
168 | if dom is None: |
---|
169 | server=self.digilibBaseUrl+"/servlet/Texter?fn=" |
---|
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 |
---|
176 | |
---|
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 |
---|
181 | else: |
---|
182 | bibtype="generic" |
---|
183 | bibtype=bibtype.replace("-"," ") # wrong typesiin index meta "-" instead of " " (not wrong! ROC) |
---|
184 | bibmap=metaData.generateMappingForType(bibtype) |
---|
185 | print "bibmap: ", bibmap, " for: ", bibtype |
---|
186 | if len(bibmap) > 0 and len(bibmap['author'][0]) > 0: |
---|
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]) |
---|
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]) |
---|
204 | raise IOError("Unable to get texttool info from %s"%(url)) |
---|
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",'') |
---|
221 | docinfo=self.getDirinfoFromDigilib(image,docinfo=docinfo) |
---|
222 | docinfo['imagePath'] = image |
---|
223 | docinfo['imageURL'] = self.digilibBaseUrl+"/servlet/Scaler?fn="+image |
---|
224 | |
---|
225 | viewerUrls=dom.xpath("//texttool/digiliburlprefix") |
---|
226 | if viewerUrls and (len(viewerUrls)>0): |
---|
227 | viewerUrl=getTextFromNode(viewerUrls[0]) |
---|
228 | docinfo['viewerURL'] = viewerUrl |
---|
229 | |
---|
230 | textUrls=dom.xpath("//texttool/text") |
---|
231 | if textUrls and (len(textUrls)>0): |
---|
232 | textUrl=getTextFromNode(textUrls[0]) |
---|
233 | docinfo['textURL'] = textUrl |
---|
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 = {} |
---|
244 | path=path.replace("/mpiwg/online","") |
---|
245 | docinfo['imagePath'] = path |
---|
246 | docinfo=self.getDirinfoFromDigilib(path,docinfo=docinfo) |
---|
247 | imageUrl=self.digilibBaseUrl+"/servlet/Scaler?fn="+path |
---|
248 | docinfo['imageURL'] = imageUrl |
---|
249 | |
---|
250 | docinfo = self.getBibinfoFromIndexMeta(path,docinfo=docinfo) |
---|
251 | return docinfo |
---|
252 | |
---|
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 |
---|
275 | |
---|
276 | |
---|
277 | def getPageinfo(self, current, start=None, rows=None, cols=None, docinfo=None): |
---|
278 | """returns pageinfo with the given parameters""" |
---|
279 | pageinfo = {} |
---|
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)) |
---|
289 | pageinfo['start'] = start |
---|
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 | |
---|
298 | return pageinfo |
---|
299 | |
---|
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 |
---|
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 |
---|
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 | |
---|
363 | def changeDocumentViewer(self,imageViewerUrl,title="",digilibBaseUrl=None,thumbrows=2,thumbcols=10,RESPONSE=None): |
---|
364 | """init document viewer""" |
---|
365 | self.title=title |
---|
366 | self.imageViewerUrl=imageViewerUrl |
---|
367 | self.digilibBaseUrl = digilibBaseUrl |
---|
368 | self.thumbrows = thumbrows |
---|
369 | self.thumbcols = thumbcols |
---|
370 | |
---|
371 | if RESPONSE is not None: |
---|
372 | RESPONSE.redirect('manage_main') |
---|
373 | |
---|
374 | |
---|
375 | |
---|
376 | |
---|
377 | # security.declareProtected('View management screens','renameImageForm') |
---|
378 | |
---|
379 | def manage_AddDocumentViewerForm(self): |
---|
380 | """add the viewer form""" |
---|
381 | pt=PageTemplateFile('zpt/addDocumentViewer', globals()).__of__(self) |
---|
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') |
---|
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 | |
---|
425 | |
---|