source: gsdl/trunk/runtime-src/src/oaiservr/oaiconfig.cpp@ 18860

Last change on this file since 18860 was 18860, checked in by kjdon, 12 years ago

added new field to collection - baseDocRoot. Equivalent to _httpprefix_ but oaiserver doesn't use macros. Used to find location of oai2.xsl - stylesheet to pretty display responses

  • Property svn:keywords set to Author Date Id Revision
File size: 11.3 KB
Line 
1#include "oaiconfig.h"
2
3#include <algorithm>
4#include <iostream>
5
6#include "fileutil.h"
7
8/**
9 * The mapping works as follows in the collect.cfg file.
10 *
11 * A line is in the format oaimapping <collection field> <oai field>
12 *
13 * The map here is used to look up the "Greenstone" name which is mapped from
14 * a given OAI field name, the reverse direction to that given in the
15 * Greenstone collect.cfg file. The oairecordaction class instance which
16 * produces output for an OAI record information request thus uses the map
17 * to work from the field in the collection it has on hand which OAI
18 * record name it should use instead.
19 *
20 * An extension is to be used for this in which the OAI field name in the
21 * collect.cfg file can be made specific for a particular record format.
22 * This is done using the OAI field name in the format of
23 * <OAI format>:<OAI field name>
24 * Thus, an rfc1807 Title field would be referred to as rfc1807:Title
25 *
26 * A collection-level mapping is not needed - to configure the behaviour
27 * of a collection, ensure that you place the appropriate configuration in
28 * its etc/collect.cfg file - the oaimapping stuff IS NOT picked up from
29 * the central main.cfg file at the moment.
30 *
31 * In the absence of a particular format name, the mapping is taken to be
32 * universal.
33 */
34
35oaiconfig::oaiconfig() : configurable () {
36 this->resumptionSize = -1; // Default = do not use resumption tokens
37}
38
39oaiconfig::oaiconfig(text_t &gsdlhome, text_t &gsdlcollect)
40{
41 // read main configuration file to get oai collections
42 text_t mainconfig = filename_cat(gsdlhome, "etc", "oai.cfg");
43 this->collection = "";
44 this->resumptionSize = -1;
45 this->read_configfile(mainconfig);
46
47 // then if we've not got a specified collection in the gsdlcollect
48 // parameter, read in all the collection's individual configurations
49 if (gsdlcollect == "") {
50 text_tarray::iterator here = this->collectList.begin();
51 text_tarray::iterator end = this->collectList.end();
52 while (here != end) {
53 this->configureCollection(gsdlhome, *here);
54 ++here;
55 }
56 }
57 else {
58 this->configureCollection(gsdlhome, gsdlcollect);
59 }
60}
61
62oaiconfig::~oaiconfig()
63{
64 oaicollectmap::iterator here = this->collectMap.begin();
65 oaicollectmap::iterator end = this->collectMap.end();
66 while (here != end) {
67 delete here->second;
68 ++here;
69 }
70}
71
72int oaiconfig::resumeAfter()
73{ return this->resumptionSize;
74}
75
76int oaiconfig::getOAIVersion()
77{
78 if (this->oaiVersion == "1.1") {
79 return 110;
80 }
81 return 200;
82}
83
84void oaiconfig::configureCollection(const text_t &gsdlhome, const text_t &gsdlcollect)
85{
86 text_t cnfgfile = filename_cat(gsdlhome, "collect", gsdlcollect, "etc", "collect.cfg");
87 this->collection = gsdlcollect;
88 this->read_configfile(cnfgfile);
89}
90
91void oaiconfig::configure (const text_t &key, const text_tarray &cfgline)
92{
93 // we've got an oai mapping item, and at least two fields
94 if (key == "oaimapping" && cfgline.size() > 1) {
95 text_t::const_iterator colonAt;
96 text_t index, name, configCollection;
97
98 // Take a default collection as being whatever the collection being configured is...
99 configCollection = this->collection;
100
101 // get the name of the (collection) field to map; this may actually
102 // be in a colon separated format of the type
103 // <collection name>:<field name>
104 index = cfgline[0];
105 if ((colonAt = find(index.begin(), index.end(), ':')) != index.end()) {
106 configCollection = substr(index.begin(), colonAt);
107
108 if (this->collection != "" && configCollection != this->collection) {
109 cerr << "Attempt to configure OAI mappings for " << configCollection << " in " << this->collection << endl;
110 }
111
112 colonAt += 1;
113 index = substr(colonAt, index.end());
114 }
115
116 // the second parameter is the metadata field to map the collection
117 // field onto. It may be provided with a metadata protocol (which
118 // will be given first and separated by a period or full stop). In
119 // the case of format.field name, the splitting is done here.
120 if ((colonAt = find(cfgline[1].begin(), cfgline[1].end(), '.')) != cfgline[1].end()) {
121 text_t stub = substr(cfgline[1].begin(), colonAt);
122 colonAt += 1;
123 name = substr(colonAt, cfgline[1].end());
124 index.append(":");
125 index.append(stub);
126 }
127 else {
128 name = cfgline[1];
129 }
130
131 // now 'index' is in the form <collectionfield>:(formatname)
132 // 'name' is simply the fieldname within the format
133 // 'configCollection' is the collection to be configured
134
135 // now simply map the field name (index) onto the collection name (name)
136 if (this->collectMap[configCollection] == NULL) {
137 this->collectMap[configCollection] = new oaicollectconfig(configCollection);
138 }
139 this->collectMap[configCollection]->fieldMap[index] = name;
140
141 // cerr << "Mapping " << index << " to " << name << " in " << configCollection << endl;
142
143 // TODO: check that the mapped field is actually in use
144 }
145 else if (key == "oaicollection" && cfgline.size() >= 1) {
146 // Configure a collection to be used as part of the OAI archive.
147 // This line should read:
148 //
149 // oaicollection <collectionname>
150 //
151 // Where <collectionname> is the name of the directory inside the
152 // gsdl/collect folder which contains the collection.
153 //
154 // To configure several collections, merely repeat this line,
155 // or alternatively use additional collection names after the
156 // first one.
157 //
158 // This configuration should only appear in main.cfg
159 //
160 if (this->collection != "") {
161 cerr << "Attempt to configure an oai collection outside of main.cfg" << endl;
162 cerr << "Configuration attempted in " << this->collection << " collection." << endl;
163 exit(1);
164 }
165 for (int c = 0; c < cfgline.size(); ++c) {
166 this->collectList.push_back(cfgline[c]);
167 }
168 }
169 else if (key == "oaiinfo" && cfgline.size() >= 1) {
170 // Get a piece of information for the oai repository information
171 // request. The line should read:
172 //
173 // oaiinfo <information field name> <value>
174 //
175 // This configuration should only be attempted in main.cfg
176 //
177 if (this->collection != "") {
178 cerr << "Attempt to set oai information outside of main.cfg" << endl;
179 cerr << "Configuration attempted in " << this->collection << " collection." << endl;
180 exit(1);
181 }
182
183 // if no second parameter is given, then the first parameter
184 if (cfgline.size() == 1) {
185 this->infoMap[cfgline[0]] = cfgline[0];
186 }
187 else {
188 this->infoMap[cfgline[0]] = cfgline[1];
189 }
190 }
191 else if (key == "oaiversion" && cfgline.size() >= 1) {
192 this->oaiVersion = cfgline[0];
193 }
194 else if (key == "resumeafter" && cfgline.size() >= 1) {
195 this->resumptionSize = cfgline[0].getint();
196 }
197 // get and note a maintainer item to support the Identify Verb of OAI
198 else if (key == "maintainer" && cfgline.size() >= 1) {
199 int line = 0;
200
201 // TODO: exhaustive checks for empty or default values of maintainer
202 while (line < cfgline.size()) {
203 if (cfgline[line] != "NULL" &&
204 cfgline[line] != "") {
205 // do something
206 break;
207 }
208 else {
209 ++line;
210 }
211 }
212
213 // Only try to set the configuration if we have a legitimate value ...
214 if (line < cfgline.size()) {
215 // ensure we have a map to write to
216 if (this->collectMap[this->collection] == NULL) {
217 this->collectMap[this->collection] = new oaicollectconfig(this->collection);
218 }
219 this->collectMap[this->collection]->maintainer = cfgline[line];
220 }
221 }
222 else if (key == "repositoryName" && cfgline.size() >= 1) {
223 int line = 0;
224
225 // TODO: exhaustive checks for empty or default values of repositoryName
226 while (line < cfgline.size()) {
227 if (cfgline[line] != "NULL" &&
228 cfgline[line] != "") {
229 // do something
230 break;
231 }
232 else {
233 ++line;
234 }
235 }
236
237 // Only try to set the configuration if we have a legitimate value ...
238 if (line < cfgline.size()) {
239 // ensure we have a map to write to
240 if (this->collectMap[this->collection] == NULL) {
241 this->collectMap[this->collection] = new oaicollectconfig(this->collection);
242 }
243 this->collectMap[this->collection]->repositoryName = cfgline[line];
244 }
245 }
246 else if (key == "baseURL" && cfgline.size() >= 1) {
247 int line = 0;
248
249 while (line < cfgline.size()) {
250 if (cfgline[line] != "NULL" &&
251 cfgline[line] != "") {
252 // do something
253 break;
254 }
255 else {
256 ++line;
257 }
258 }
259
260 // Only try to set the configuration if we have a legitimate value ...
261 if (line < cfgline.size()) {
262 // ensure we have a map to write to
263 if (this->collectMap[this->collection] == NULL) {
264 this->collectMap[this->collection] = new oaicollectconfig(this->collection);
265 }
266 this->collectMap[this->collection]->baseURL = cfgline[line];
267 }
268 }
269 else if (key == "baseDocRoot" && cfgline.size() >= 1) {
270 int line = 0;
271
272 while (line < cfgline.size()) {
273 if (cfgline[line] != "NULL" &&
274 cfgline[line] != "") {
275 // do something
276 break;
277 }
278 else {
279 ++line;
280 }
281 }
282
283 // Only try to set the configuration if we have a legitimate value ...
284 if (line < cfgline.size()) {
285 // ensure we have a map to write to
286 if (this->collectMap[this->collection] == NULL) {
287 this->collectMap[this->collection] = new oaicollectconfig(this->collection);
288 }
289 this->collectMap[this->collection]->baseDocRoot = cfgline[line];
290 }
291 }
292}
293
294/**
295 * TODO: store all field values in a map per collection
296 */
297text_t oaiconfig::getCollectionConfig(const text_t &collection, const text_t &field)
298{
299 if (this->collectMap[collection] == NULL) {
300 return "";
301 }
302 if (field == "maintainer") {
303 return this->collectMap[collection]->maintainer;
304 }
305
306 if (field == "repositoryName") {
307 return this->collectMap[collection]->repositoryName;
308 }
309
310 if (field == "baseURL") {
311 return this->collectMap[collection]->baseURL;
312 }
313
314 if (field == "baseDocRoot") {
315 return this->collectMap[collection]->baseDocRoot;
316 }
317
318 return "";
319}
320
321text_t oaiconfig::getMapping(const text_t &collection, const text_t &collectfield)
322{
323 if (this->collectMap[collection] == NULL) {
324 return "";
325 }
326 return this->collectMap[collection]->fieldMap[collectfield];
327}
328
329/**
330 * Get the mapping for a field in a given collection; if no mapping
331 * exists, the result will be a blank string.
332 */
333text_t oaiconfig::getMapping(const text_t &collection, const text_t &collectfield, const text_t &formatname)
334{
335 text_t fullName = collectfield;
336 fullName.append(":");
337 fullName.append(formatname);
338
339 // try the collection-specific options first
340 if (this->collectMap[collection] != NULL) {
341 // first try the most specific item - this collection, and given that protocol
342 if (this->collectMap[collection]->fieldMap.count(fullName) >= 1) {
343 return this->collectMap[collection]->fieldMap[fullName];
344 }
345 // otherwise, fall back to this collection, and all protocols
346 else if (this->collectMap[collection]->fieldMap.count(collectfield) >= 1) {
347 return this->collectMap[collection]->fieldMap[collectfield];
348 }
349 }
350
351 // if no mappings exist, return an empty item
352 if (this->collectMap[""] == NULL) {
353 return "";
354 }
355
356 // then try generic rules
357 if (this->collectMap[""]->fieldMap.count(fullName) >= 1) {
358 return this->collectMap[""]->fieldMap[fullName];
359 }
360 else {
361 return this->collectMap[""]->fieldMap[collectfield];
362 }
363}
Note: See TracBrowser for help on using the repository browser.