0
|
1 # -*- coding: utf-8 -*-
|
|
2
|
|
3 #Verbindet Zope mit pubman.
|
|
4
|
|
5
|
|
6 from OFS.SimpleItem import SimpleItem
|
|
7 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
|
|
8 import os.path
|
|
9
|
|
10 from Globals import package_home
|
|
11 import httplib2
|
|
12 import urlparse
|
|
13 import urllib
|
|
14 import re
|
|
15 import xml.etree.ElementTree as ET
|
|
16 import json
|
1
|
17 import logging
|
0
|
18
|
|
19 def zptFile(self, path, orphaned=False):
|
|
20 """returns a page template file from the product"""
|
|
21 if orphaned:
|
|
22 # unusual case
|
|
23 pt=PageTemplateFile(os.path.join(package_home(globals()), path))
|
|
24 else:
|
|
25
|
|
26 pt=PageTemplateFile(os.path.join(package_home(globals()), path)).__of__(self)
|
|
27 return pt
|
|
28
|
|
29 class ZopePubmanConnector(SimpleItem):
|
|
30
|
|
31
|
|
32 connectorString="http://pubman.mpiwg-berlin.mpg.de/search/SearchAndExport?"
|
|
33
|
|
34
|
|
35 meta_type="ZopePubmanConnector"
|
|
36
|
|
37 manage_options= ({'label':'Main Config','action': 'changeMain'},) + SimpleItem.manage_options
|
|
38
|
|
39 def __init__(self,id,title,pubmanURL):
|
|
40 self.id=id
|
|
41 self.title=title
|
|
42 self.pubmanURL=pubmanURL #URL einer pubman instance bzw. einer collection, falls nicht die default collection benutzt werden soll
|
|
43
|
|
44
|
|
45
|
|
46 def changeMain(self,pubmanURL=None,title=None,REQUEST=None,RESPONSE=None):
|
|
47 """change main settings"""
|
|
48 if pubmanURL:
|
|
49 self.pubmanURL=pubmanURL
|
|
50 self.title=title
|
|
51
|
|
52 if RESPONSE is not None:
|
|
53 RESPONSE.redirect('manage_main')
|
|
54
|
|
55
|
|
56 else:
|
|
57 pt=zptFile(self, 'zpt/ChangeZopePubmanConnector.zpt')
|
|
58 return pt()
|
|
59
|
|
60
|
2
|
61 def getPublications(self,personID,limit=None,publicationType=None):
|
0
|
62 """get all publications der personID"""
|
|
63 h = httplib2.Http()
|
1
|
64
|
|
65
|
2
|
66
|
|
67 if publicationType is None:
|
|
68 cn = self.connectorString+"cqlQuery=escidoc.any-identifier=%22"+personID+"%22&"
|
|
69 else:
|
|
70 cn = self.connectorString+"cqlQuery=escidoc.any-identifier=%22"+personID+"%22"
|
|
71 cn +="%20and%20escidoc.publication.type=%22"+publicationType+"%22&"
|
|
72
|
|
73 cn +="exportFormat=APA&outputFormat=snippet&language=all&sortKeys=escidoc.any-dates&sortOrder=descending"
|
|
74 if limit:
|
|
75 cn+="&maximumRecords=%s"%limit
|
1
|
76
|
2
|
77 logging.debug(cn)
|
|
78 resp, content = h.request(cn)
|
|
79
|
3
|
80
|
2
|
81
|
0
|
82 ET.register_namespace("dcterms", "http://purl.org/dc/terms/")
|
|
83
|
|
84 root = ET.fromstring(content)
|
|
85
|
2
|
86 #<escidocItem:item objid="escidoc:630782"
|
|
87
|
0
|
88 citationxpath=".//{http://purl.org/dc/terms/}bibliographicCitation"
|
|
89
|
2
|
90 objxpath=".//{http://www.escidoc.de/schemas/item/0.8}item"
|
|
91 citations=root.findall(objxpath)
|
0
|
92
|
|
93 ret=[]
|
|
94 for citation in citations:
|
2
|
95 objId = citation.get('objid')
|
|
96
|
|
97 text = citation.find(citationxpath)
|
|
98
|
|
99 ret.append((objId,text.text))
|
0
|
100
|
|
101
|
|
102
|
|
103 return ret
|
|
104
|
2
|
105
|
|
106 def search(self,values,exact=False):
|
|
107
|
|
108 """search pubman
|
|
109 @values map mit field->value
|
|
110 @return map mit escidocId -> XML-formatted snippeds
|
|
111 """
|
|
112
|
|
113 fieldToEscidoc={"title":"escidoc.any-title",
|
|
114 "author":"escidoc.publication.any.publication-creator-names",
|
|
115 "any":"escidoc.any-metadata"}
|
|
116
|
|
117
|
|
118 cn = self.connectorString+"cqlQuery=%s&"
|
|
119 cn +="exportFormat=APA&outputFormat=snippet&language=all&sortKeys=escidoc.any-dates&sortOrder=descending"
|
|
120
|
|
121
|
|
122
|
|
123 querys = []
|
|
124 for field in values.keys():
|
|
125
|
|
126 searchField = fieldToEscidoc.get(field,None)
|
|
127 if searchField is None:
|
|
128 logging.debug("search, don't know field: %s"%field)
|
|
129 continue
|
|
130
|
|
131
|
|
132 value = values[field]
|
|
133
|
|
134 if value == '':
|
|
135 continue
|
|
136 logging.debug("%s=%s"%(field,value))
|
|
137 if not exact:
|
|
138 value=value+"*"
|
|
139
|
|
140
|
|
141 querys.append("%s=%%22%s%%22"%(searchField,value))
|
|
142
|
|
143
|
|
144 query=" AND ".join(querys)
|
|
145 h = httplib2.Http()
|
|
146
|
|
147 logging.debug(cn%query)
|
|
148 resp, content = h.request(cn%query)
|
|
149
|
|
150 ET.register_namespace("dcterms", "http://purl.org/dc/terms/")
|
|
151
|
3
|
152 try:
|
|
153 root = ET.fromstring(content)
|
|
154 except:
|
|
155 logging.error("Couldn't parse content of:%s"%(cn%query))
|
|
156 return {}
|
2
|
157 #<escidocItem:item objid="escidoc:630782"
|
|
158
|
|
159 citationxpath=".//{http://purl.org/dc/terms/}bibliographicCitation"
|
|
160
|
|
161 objxpath=".//{http://www.escidoc.de/schemas/item/0.8}item"
|
|
162 citations=root.findall(objxpath)
|
|
163
|
|
164 ret={}
|
|
165 for citation in citations:
|
|
166 objId = citation.get('objid')
|
|
167
|
|
168 text = citation.find(citationxpath)
|
|
169
|
|
170 ret[objId]=text.text
|
|
171
|
|
172
|
|
173
|
|
174 return ret
|
|
175
|
|
176
|
|
177
|
|
178 def getEntryFromPubman(self,escidocid):
|
3
|
179 escidocid=escidocid.lstrip().strip()
|
2
|
180 h = httplib2.Http()
|
|
181 cn = self.connectorString+"cqlQuery=escidoc.objid=%s&"
|
|
182 cn +="exportFormat=APA&outputFormat=snippet&language=all&sortKeys=escidoc.any-dates&sortOrder=descending"
|
|
183
|
|
184 resp, content = h.request(cn%escidocid)
|
|
185 ET.register_namespace("dcterms", "http://purl.org/dc/terms/")
|
3
|
186 logging.debug(cn%escidocid)
|
|
187
|
2
|
188 root = ET.fromstring(content)
|
|
189
|
|
190
|
|
191 citationxpath=".//{http://purl.org/dc/terms/}bibliographicCitation"
|
|
192
|
|
193 citation=root.find(citationxpath)
|
|
194
|
|
195 if citation is not None:
|
|
196
|
|
197 return citation.text
|
|
198
|
|
199 return ""
|
|
200
|
0
|
201 def pubmanConnectorURL(self):
|
|
202 return self.connectorString
|
|
203
|
|
204 def manage_addZopePubmanConnectorForm(self):
|
|
205 """Form for external Links"""
|
|
206 pt=zptFile(self, 'zpt/AddZopePubmanConnector.zpt')
|
|
207 return pt()
|
|
208
|
|
209
|
|
210 def manage_addZopePubmanConnector(self,id,title,pubmanURL,RESPONSE=None):
|
|
211 """Add an external Link"""
|
|
212
|
|
213 newObj=ZopePubmanConnector(id,title,pubmanURL)
|
|
214
|
|
215 self._setObject(id,newObj)
|
|
216
|
|
217
|
|
218 if RESPONSE is not None:
|
|
219 RESPONSE.redirect('manage_main')
|
|
220 |