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 |
---|
19 | |
---|
20 | def getTextFromNode(nodename): |
---|
21 | nodelist=nodename.childNodes |
---|
22 | rc = "" |
---|
23 | for node in nodelist: |
---|
24 | if node.nodeType == node.TEXT_NODE: |
---|
25 | rc = rc + node.data |
---|
26 | return rc |
---|
27 | |
---|
28 | import socket |
---|
29 | |
---|
30 | def urlopen(url): |
---|
31 | """urlopen mit timeout""" |
---|
32 | socket.setdefaulttimeout(2) |
---|
33 | ret=urllib.urlopen(url) |
---|
34 | socket.setdefaulttimeout(5) |
---|
35 | return ret |
---|
36 | |
---|
37 | def getParamFromDigilib(path,param): |
---|
38 | """gibt param von dlInfo aus""" |
---|
39 | imageUrl=genericDigilib+"/dlInfo-xml.jsp?fn="+path |
---|
40 | |
---|
41 | try: |
---|
42 | dom = NonvalidatingReader.parseUri(imageUrl) |
---|
43 | except: |
---|
44 | return None |
---|
45 | |
---|
46 | |
---|
47 | params=dom.xpath("//document-parameters/parameter[@name='%s']/@value"%param) |
---|
48 | |
---|
49 | if params: |
---|
50 | return params[0].value |
---|
51 | |
---|
52 | def parseUrlTextTool(url): |
---|
53 | """parse index meta""" |
---|
54 | |
---|
55 | try: |
---|
56 | dom = NonvalidatingReader.parseUri(url) |
---|
57 | except: |
---|
58 | zLOG.LOG("documentViewer (parseUrlTexttool)", zLOG.INFO,"%s (%s)"%sys.exc_info()[0:2]) |
---|
59 | return (None,None,None) |
---|
60 | |
---|
61 | archivePaths=dom.xpath("//resource/archive-path") |
---|
62 | |
---|
63 | if archivePaths and (len(archivePaths)>0): |
---|
64 | archivePath=getTextFromNode(archivePaths[0]) |
---|
65 | else: |
---|
66 | archivePath=None |
---|
67 | |
---|
68 | |
---|
69 | images=dom.xpath("//texttool/image") |
---|
70 | |
---|
71 | if images and (len(images)>0): |
---|
72 | image=getTextFromNode(images[0]) |
---|
73 | else: |
---|
74 | image=None |
---|
75 | |
---|
76 | if image and archivePath: |
---|
77 | image=os.path.join(archivePath,image) |
---|
78 | image=image.replace("/mpiwg/online",'') |
---|
79 | pt=getParamFromDigilib(image,'pt') |
---|
80 | |
---|
81 | else: |
---|
82 | image=None |
---|
83 | |
---|
84 | viewerUrls=dom.xpath("//texttool/digiliburlprefix") |
---|
85 | |
---|
86 | if viewerUrls and (len(viewerUrls)>0): |
---|
87 | viewerUrl=getTextFromNode(viewerUrls[0]) |
---|
88 | else: |
---|
89 | viewerUrl=None |
---|
90 | |
---|
91 | |
---|
92 | textUrls=dom.xpath("//texttool/text") |
---|
93 | |
---|
94 | if textUrls and (len(textUrls)>0): |
---|
95 | textUrl=getTextFromNode(textUrls[0]) |
---|
96 | else: |
---|
97 | textUrl=None |
---|
98 | return viewerUrl,(image,pt),textUrl |
---|
99 | |
---|
100 | |
---|
101 | class documentViewer(ZopePageTemplate): |
---|
102 | """document viewer""" |
---|
103 | |
---|
104 | meta_type="Document viewer" |
---|
105 | |
---|
106 | security=ClassSecurityInfo() |
---|
107 | manage_options=ZopePageTemplate.manage_options+( |
---|
108 | {'label':'main config','action':'changeDocumentViewerForm'}, |
---|
109 | ) |
---|
110 | |
---|
111 | _default_content_fn = os.path.join(package_home(globals()),'zpt','documentViewer_template.zpt') |
---|
112 | |
---|
113 | def __init__(self,id,imageViewerUrl,title=""): |
---|
114 | """init document viewer""" |
---|
115 | self.id=id |
---|
116 | self.title=title |
---|
117 | self.imageViewerUrl=imageViewerUrl |
---|
118 | |
---|
119 | security.declareProtected('View management screens','changeDocumentViewerForm') |
---|
120 | def changeDocumentViewerForm(self): |
---|
121 | """change it""" |
---|
122 | pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','changeDocumentViewer.zpt')).__of__(self) |
---|
123 | return pt() |
---|
124 | |
---|
125 | |
---|
126 | def changeDocumentViewer(self,imageViewerUrl,title="",RESPONSE=None): |
---|
127 | """init document viewer""" |
---|
128 | self.title=title |
---|
129 | self.imageViewerUrl=imageViewerUrl |
---|
130 | |
---|
131 | if RESPONSE is not None: |
---|
132 | RESPONSE.redirect('manage_main') |
---|
133 | |
---|
134 | |
---|
135 | def imageLink(self,nr): |
---|
136 | """link hinter den images""" |
---|
137 | paramsTmp=cgi.parse_qs(self.REQUEST['QUERY_STRING']) |
---|
138 | params={} |
---|
139 | for x in paramsTmp.iteritems(): |
---|
140 | params[x[0]]=x[1][0] |
---|
141 | |
---|
142 | params['pn']=nr |
---|
143 | newUrl=self.REQUEST['URL']+"?"+urllib.urlencode(params) |
---|
144 | return newUrl |
---|
145 | |
---|
146 | |
---|
147 | def thumbruler(self,cols,rows,start,maximum): |
---|
148 | """ruler for thumbs""" |
---|
149 | ret="" |
---|
150 | paramsTmp=cgi.parse_qs(self.REQUEST['QUERY_STRING']) |
---|
151 | params={} |
---|
152 | for x in paramsTmp.iteritems(): |
---|
153 | |
---|
154 | if not x[0]=="start": |
---|
155 | params[x[0]]=x[1][0] |
---|
156 | |
---|
157 | newUrlSelect=self.REQUEST['URL']+"?"+urllib.urlencode(params) |
---|
158 | if start>0: |
---|
159 | newStart=max(start-cols*rows,0) |
---|
160 | params['start']=newStart |
---|
161 | newUrl=self.REQUEST['URL']+"?"+urllib.urlencode(params) |
---|
162 | ret+="""<a href="%s">prev</a>"""%newUrl |
---|
163 | |
---|
164 | |
---|
165 | ret+="""<select onChange="location.href='%s&start='+this.options[this.selectedIndex].value" """%newUrlSelect |
---|
166 | nr,rest=divmod(maximum,cols*rows) |
---|
167 | if rest > 0: |
---|
168 | nr+=1 |
---|
169 | for i in range(nr): |
---|
170 | nr=i*cols*rows |
---|
171 | |
---|
172 | if (start >= nr) and (start < nr+cols*rows): |
---|
173 | ret+="""<option value="%s" selected>%s</option>"""%(nr,nr) |
---|
174 | else: |
---|
175 | ret+="""<option value="%s">%s</option>"""%(nr,nr) |
---|
176 | ret+="</select>" |
---|
177 | |
---|
178 | if start<maximum: |
---|
179 | newStart=min(start+cols*rows,maximum) |
---|
180 | params['start']=newStart |
---|
181 | newUrl=self.REQUEST['URL']+"?"+urllib.urlencode(params) |
---|
182 | ret+="""<a href="%s">next</a>"""%newUrl |
---|
183 | |
---|
184 | return ret |
---|
185 | |
---|
186 | def textToolThumb(self,url,start=0): |
---|
187 | """understands the texttool format |
---|
188 | @param url: url to index.meta with texttool tag |
---|
189 | """ |
---|
190 | (viewerUrl,imagepath,textpath)=parseUrlTextTool(url) |
---|
191 | |
---|
192 | imageUrl=genericDigilib+"/servlet/Scaler?fn=%s"%imagepath[0] |
---|
193 | |
---|
194 | pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','thumbs.zpt')).__of__(self) |
---|
195 | return pt(imageUrl=imageUrl,pt=imagepath[1],start=start) |
---|
196 | |
---|
197 | def imagePathThumb(self,path,start=0): |
---|
198 | """path ist the path to the images it is assumes that the index.meta file is one level higher.""" |
---|
199 | |
---|
200 | path=path.replace("/mpiwg/online","") |
---|
201 | pt=getParamFromDigilib(path,'pt') |
---|
202 | imageUrl=genericDigilib+"/servlet/Scaler?fn=%s"%path |
---|
203 | |
---|
204 | pageT=PageTemplateFile(os.path.join(package_home(globals()),'zpt','thumbs.zpt')).__of__(self) |
---|
205 | return pageT(imageUrl=imageUrl,pt=pt,start=start) |
---|
206 | |
---|
207 | def headerFromIndexMeta(self,path): |
---|
208 | """gibt header from index meta out""" |
---|
209 | |
---|
210 | metaData=self.metadata.main.meta.bib |
---|
211 | server="http://foxridge.mpiwg-berlin.mpg.de" |
---|
212 | path="/".join(path.split("/")[0:-1]) |
---|
213 | metaUrl=server+path+"/index.meta" |
---|
214 | |
---|
215 | dom = NonvalidatingReader.parseUri(metaUrl) |
---|
216 | type=dom.xpath("//bib/@type") |
---|
217 | if type and (len(type)>0): |
---|
218 | type=type[0].value |
---|
219 | else: |
---|
220 | type="generic" |
---|
221 | type=type.replace("-"," ")# wrong typesiin index meta "-" instead of " " |
---|
222 | hash=metaData.generateMappingForType(type) |
---|
223 | |
---|
224 | author=getTextFromNode(dom.xpath("//bib/%s"%hash['author'][0])[0]) |
---|
225 | title=getTextFromNode(dom.xpath("//bib/%s"%hash['title'][0])[0]) |
---|
226 | year=getTextFromNode(dom.xpath("//bib/%s"%hash['year'][0])[0]) |
---|
227 | |
---|
228 | return author,title,year |
---|
229 | |
---|
230 | def text(self,mode,url,pn): |
---|
231 | """give text""" |
---|
232 | if mode=="texttool": #index.meta with texttool information |
---|
233 | (viewerUrl,imagepath,textpath)=parseUrlTextTool(url) |
---|
234 | |
---|
235 | print textpath |
---|
236 | try: |
---|
237 | dom = NonvalidatingReader.parseUri(textpath) |
---|
238 | except: |
---|
239 | return None |
---|
240 | |
---|
241 | list=[] |
---|
242 | nodes=dom.xpath("//pb") |
---|
243 | |
---|
244 | node=nodes[int(pn)-1] |
---|
245 | |
---|
246 | p=node |
---|
247 | |
---|
248 | while p.tagName!="p": |
---|
249 | p=p.parentNode |
---|
250 | |
---|
251 | |
---|
252 | endNode=nodes[int(pn)] |
---|
253 | |
---|
254 | |
---|
255 | e=endNode |
---|
256 | |
---|
257 | while e.tagName!="p": |
---|
258 | e=e.parentNode |
---|
259 | |
---|
260 | |
---|
261 | next=node.parentNode |
---|
262 | |
---|
263 | #sammle s |
---|
264 | while next and (next!=endNode.parentNode): |
---|
265 | list.append(next) |
---|
266 | next=next.nextSibling |
---|
267 | list.append(endNode.parentNode) |
---|
268 | |
---|
269 | if p==e:# beide im selben paragraphen |
---|
270 | pass |
---|
271 | # else: |
---|
272 | # next=p |
---|
273 | # while next!=e: |
---|
274 | # print next,e |
---|
275 | # list.append(next) |
---|
276 | # next=next.nextSibling |
---|
277 | # |
---|
278 | # for x in list: |
---|
279 | # PrettyPrint(x) |
---|
280 | # |
---|
281 | # return list |
---|
282 | # |
---|
283 | def image(self,mode,url,pn): |
---|
284 | """give image out""" |
---|
285 | if mode=="texttool": #index.meta with texttool information |
---|
286 | (viewerUrl,imagepath,textpath)=parseUrlTextTool(url) |
---|
287 | if not viewerUrl: |
---|
288 | viewerUrl=self.imageViewerUrl |
---|
289 | url=viewerUrl+"pn=%s&fn=%s"%(pn,imagepath[0]) |
---|
290 | ret="""<iframe height="100%%" width="100%%" src="%s"/>"""%url |
---|
291 | return url |
---|
292 | elif mode=="imagepath": |
---|
293 | url=url.replace("/mpiwg/online","") |
---|
294 | url=self.imageViewerUrl+"pn=%s&fn=%s"%(pn,url) |
---|
295 | ret="""<iframe height="100%%" width="100%%" src="%s"/>"""%url |
---|
296 | return url |
---|
297 | |
---|
298 | |
---|
299 | def thumbs(self,mode,url,start): |
---|
300 | """give thumbs out""" |
---|
301 | if mode=="texttool": #index.meta with texttool information |
---|
302 | return self.textToolThumb(url,int(start)) |
---|
303 | elif mode=="imagepath": |
---|
304 | return self.imagePathThumb(url,int(start)) |
---|
305 | |
---|
306 | security.declareProtected('View','index_html') |
---|
307 | |
---|
308 | |
---|
309 | def index_html(self,mode,url,start=0,pn=1): |
---|
310 | ''' |
---|
311 | view it |
---|
312 | @param mode: defines which type of document is behind url |
---|
313 | @param url: url which contains display information |
---|
314 | ''' |
---|
315 | |
---|
316 | |
---|
317 | pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','documentViewer_template.zpt')).__of__(self) |
---|
318 | return pt(mode=mode,url=url,start=start,pn=pn) |
---|
319 | |
---|
320 | |
---|
321 | |
---|
322 | # security.declareProtected('View management screens','renameImageForm') |
---|
323 | |
---|
324 | def manage_AddDocumentViewerForm(self): |
---|
325 | """add the viewer form""" |
---|
326 | pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addDocumentViewer.zpt')).__of__(self) |
---|
327 | return pt() |
---|
328 | |
---|
329 | def manage_AddDocumentViewer(self,id,imageViewerUrl="",title="",RESPONSE=None): |
---|
330 | """add the viewer""" |
---|
331 | newObj=documentViewer(id,imageViewerUrl,title) |
---|
332 | self._setObject(id,newObj) |
---|
333 | |
---|
334 | if RESPONSE is not None: |
---|
335 | RESPONSE.redirect('manage_main') |
---|
336 | |
---|