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

Last change on this file since 19294 was 18903, checked in by kjdon, 15 years ago

check the existence of collect.cfg to see if a collection name is valid or not, then don't advertise it if not.

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