[22739] | 1 | /**********************************************************************
|
---|
| 2 | *
|
---|
| 3 | * oaiconfig.cpp --
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 2004-2010 The New Zealand Digital Library Project
|
---|
| 6 | *
|
---|
| 7 | * A component of the Greenstone digital library software
|
---|
| 8 | * from the New Zealand Digital Library Project at the
|
---|
| 9 | * University of Waikato, New Zealand.
|
---|
| 10 | *
|
---|
| 11 | * This program is free software; you can redistribute it and/or modify
|
---|
| 12 | * it under the terms of the GNU General Public License as published by
|
---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 14 | * (at your option) any later version.
|
---|
| 15 | *
|
---|
| 16 | * This program is distributed in the hope that it will be useful,
|
---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | * GNU General Public License for more details.
|
---|
| 20 | *
|
---|
| 21 | * You should have received a copy of the GNU General Public License
|
---|
| 22 | * along with this program; if not, write to the Free Software
|
---|
| 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 24 | *
|
---|
| 25 | *********************************************************************/
|
---|
| 26 |
|
---|
[8182] | 27 | #include "oaiconfig.h"
|
---|
| 28 | #include <iostream>
|
---|
[20827] | 29 | #include <stdlib.h>
|
---|
[8182] | 30 | #include "fileutil.h"
|
---|
| 31 |
|
---|
[20589] | 32 |
|
---|
[8182] | 33 | /**
|
---|
[18885] | 34 | * The mapping works as follows in the oai.cfg or collect.cfg file.
|
---|
[8182] | 35 | *
|
---|
| 36 | * A line is in the format oaimapping <collection field> <oai field>
|
---|
| 37 | *
|
---|
| 38 | * The map here is used to look up the "Greenstone" name which is mapped from
|
---|
| 39 | * a given OAI field name, the reverse direction to that given in the
|
---|
| 40 | * Greenstone collect.cfg file. The oairecordaction class instance which
|
---|
| 41 | * produces output for an OAI record information request thus uses the map
|
---|
| 42 | * to work from the field in the collection it has on hand which OAI
|
---|
| 43 | * record name it should use instead.
|
---|
| 44 | *
|
---|
| 45 | * An extension is to be used for this in which the OAI field name in the
|
---|
| 46 | * collect.cfg file can be made specific for a particular record format.
|
---|
| 47 | * This is done using the OAI field name in the format of
|
---|
[18885] | 48 | * <OAI format>.<OAI field name>
|
---|
| 49 | * Thus, an rfc1807 Title field would be referred to as rfc1807.Title
|
---|
[8182] | 50 | *
|
---|
[18885] | 51 | * A collection-level mapping can be provided in oai.cfg by prepending
|
---|
| 52 | * collname to collection field:
|
---|
| 53 | * <collname:field> <oai field>
|
---|
| 54 |
|
---|
[8182] | 55 | * In the absence of a particular format name, the mapping is taken to be
|
---|
| 56 | * universal.
|
---|
| 57 | */
|
---|
| 58 |
|
---|
| 59 | oaiconfig::oaiconfig() : configurable () {
|
---|
[11732] | 60 | this->resumptionSize = -1; // Default = do not use resumption tokens
|
---|
[8182] | 61 | }
|
---|
| 62 |
|
---|
| 63 | oaiconfig::oaiconfig(text_t &gsdlhome, text_t &gsdlcollect)
|
---|
| 64 | {
|
---|
[18885] | 65 | // read main configuration file (oai.cfg) to get oai collections
|
---|
[8311] | 66 | text_t mainconfig = filename_cat(gsdlhome, "etc", "oai.cfg");
|
---|
[8182] | 67 | this->collection = "";
|
---|
[11732] | 68 | this->resumptionSize = -1;
|
---|
[8182] | 69 | this->read_configfile(mainconfig);
|
---|
[11732] | 70 |
|
---|
[8182] | 71 | // then if we've not got a specified collection in the gsdlcollect
|
---|
| 72 | // parameter, read in all the collection's individual configurations
|
---|
| 73 | if (gsdlcollect == "") {
|
---|
[18903] | 74 | text_tarray::iterator start = this->collectList.begin();
|
---|
| 75 | text_tarray::iterator here = this->collectList.end()-1;
|
---|
| 76 | while (here != start) {
|
---|
| 77 | if (!this->configureCollection(gsdlhome, *here)) {
|
---|
| 78 | this->collectList.erase(here);
|
---|
| 79 | }
|
---|
| 80 | --here;
|
---|
[8182] | 81 | }
|
---|
[18903] | 82 | // and do the first one
|
---|
| 83 | if (!this->configureCollection(gsdlhome, *here)) {
|
---|
| 84 | this->collectList.erase(here);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[8182] | 87 | }
|
---|
| 88 | else {
|
---|
[18903] | 89 | // what do we do if this fails?
|
---|
[8182] | 90 | this->configureCollection(gsdlhome, gsdlcollect);
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | oaiconfig::~oaiconfig()
|
---|
| 95 | {
|
---|
| 96 | oaicollectmap::iterator here = this->collectMap.begin();
|
---|
| 97 | oaicollectmap::iterator end = this->collectMap.end();
|
---|
| 98 | while (here != end) {
|
---|
| 99 | delete here->second;
|
---|
[9608] | 100 | ++here;
|
---|
[8182] | 101 | }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[11732] | 104 | int oaiconfig::resumeAfter()
|
---|
| 105 | { return this->resumptionSize;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[8182] | 108 | int oaiconfig::getOAIVersion()
|
---|
| 109 | {
|
---|
| 110 | if (this->oaiVersion == "1.1") {
|
---|
| 111 | return 110;
|
---|
| 112 | }
|
---|
| 113 | return 200;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[18903] | 116 | bool oaiconfig::configureCollection(const text_t &gsdlhome, const text_t &gsdlcollect)
|
---|
[8182] | 117 | {
|
---|
| 118 | text_t cnfgfile = filename_cat(gsdlhome, "collect", gsdlcollect, "etc", "collect.cfg");
|
---|
[18903] | 119 | if (!file_exists(cnfgfile)) {
|
---|
| 120 | return false;
|
---|
| 121 | }
|
---|
[8182] | 122 | this->collection = gsdlcollect;
|
---|
| 123 | this->read_configfile(cnfgfile);
|
---|
[18903] | 124 |
|
---|
| 125 | return true;
|
---|
[8182] | 126 | }
|
---|
| 127 | void oaiconfig::configure (const text_t &key, const text_tarray &cfgline)
|
---|
| 128 | {
|
---|
| 129 | // we've got an oai mapping item, and at least two fields
|
---|
| 130 | if (key == "oaimapping" && cfgline.size() > 1) {
|
---|
| 131 | text_t::const_iterator colonAt;
|
---|
[8303] | 132 | text_t index, name, configCollection;
|
---|
| 133 |
|
---|
| 134 | // Take a default collection as being whatever the collection being configured is...
|
---|
| 135 | configCollection = this->collection;
|
---|
[8182] | 136 |
|
---|
| 137 | // get the name of the (collection) field to map; this may actually
|
---|
| 138 | // be in a colon separated format of the type
|
---|
| 139 | // <collection name>:<field name>
|
---|
| 140 | index = cfgline[0];
|
---|
[20607] | 141 | if ((colonAt = findchar(index.begin(), index.end(), ':')) != index.end()) {
|
---|
[8303] | 142 | configCollection = substr(index.begin(), colonAt);
|
---|
| 143 |
|
---|
| 144 | if (this->collection != "" && configCollection != this->collection) {
|
---|
| 145 | cerr << "Attempt to configure OAI mappings for " << configCollection << " in " << this->collection << endl;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | colonAt += 1;
|
---|
| 149 | index = substr(colonAt, index.end());
|
---|
| 150 | }
|
---|
[8182] | 151 |
|
---|
| 152 | // the second parameter is the metadata field to map the collection
|
---|
| 153 | // field onto. It may be provided with a metadata protocol (which
|
---|
[8276] | 154 | // will be given first and separated by a period or full stop). In
|
---|
| 155 | // the case of format.field name, the splitting is done here.
|
---|
[20607] | 156 | if ((colonAt = findchar(cfgline[1].begin(), cfgline[1].end(), '.')) != cfgline[1].end()) {
|
---|
[8182] | 157 | text_t stub = substr(cfgline[1].begin(), colonAt);
|
---|
| 158 | colonAt += 1;
|
---|
| 159 | name = substr(colonAt, cfgline[1].end());
|
---|
| 160 | index.append(":");
|
---|
| 161 | index.append(stub);
|
---|
| 162 | }
|
---|
| 163 | else {
|
---|
| 164 | name = cfgline[1];
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[8303] | 167 | // now 'index' is in the form <collectionfield>:(formatname)
|
---|
| 168 | // 'name' is simply the fieldname within the format
|
---|
| 169 | // 'configCollection' is the collection to be configured
|
---|
[8182] | 170 |
|
---|
| 171 | // now simply map the field name (index) onto the collection name (name)
|
---|
[8303] | 172 | if (this->collectMap[configCollection] == NULL) {
|
---|
| 173 | this->collectMap[configCollection] = new oaicollectconfig(configCollection);
|
---|
[8182] | 174 | }
|
---|
[8303] | 175 | this->collectMap[configCollection]->fieldMap[index] = name;
|
---|
[8182] | 176 |
|
---|
[8303] | 177 | // cerr << "Mapping " << index << " to " << name << " in " << configCollection << endl;
|
---|
[8182] | 178 |
|
---|
| 179 | // TODO: check that the mapped field is actually in use
|
---|
| 180 | }
|
---|
[8303] | 181 | else if (key == "oaicollection" && cfgline.size() >= 1) {
|
---|
[8182] | 182 | // Configure a collection to be used as part of the OAI archive.
|
---|
| 183 | // This line should read:
|
---|
| 184 | //
|
---|
| 185 | // oaicollection <collectionname>
|
---|
| 186 | //
|
---|
| 187 | // Where <collectionname> is the name of the directory inside the
|
---|
| 188 | // gsdl/collect folder which contains the collection.
|
---|
| 189 | //
|
---|
| 190 | // To configure several collections, merely repeat this line,
|
---|
| 191 | // or alternatively use additional collection names after the
|
---|
| 192 | // first one.
|
---|
| 193 | //
|
---|
[18892] | 194 | // This configuration should only appear in oai.cfg
|
---|
[8182] | 195 | //
|
---|
| 196 | if (this->collection != "") {
|
---|
[18892] | 197 | cerr << "Attempt to configure an oai collection outside of oai.cfg" << endl;
|
---|
[8182] | 198 | cerr << "Configuration attempted in " << this->collection << " collection." << endl;
|
---|
| 199 | exit(1);
|
---|
| 200 | }
|
---|
[9608] | 201 | for (int c = 0; c < cfgline.size(); ++c) {
|
---|
[8303] | 202 | this->collectList.push_back(cfgline[c]);
|
---|
[8182] | 203 | }
|
---|
| 204 | }
|
---|
[18892] | 205 | else if (key == "oaimetadata" && cfgline.size() >= 1) {
|
---|
| 206 | // List of metadata prefixes to suuport
|
---|
| 207 | // This line should read:
|
---|
| 208 | //
|
---|
| 209 | // oaicollection <metadataname> <metadataname>...
|
---|
| 210 | //
|
---|
| 211 | //
|
---|
| 212 | // This configuration should only appear in oai.cfg
|
---|
| 213 | //
|
---|
| 214 | if (this->collection != "") {
|
---|
| 215 | cerr << "Attempt to configure oai metadata outside of oai.cfg" << endl;
|
---|
| 216 | cerr << "Configuration attempted in " << this->collection << " collection." << endl;
|
---|
| 217 | exit(1);
|
---|
| 218 | }
|
---|
| 219 | for (int c = 0; c < cfgline.size(); ++c) {
|
---|
| 220 | // todo: check that the set name is valid
|
---|
| 221 | this->metadataSet.insert(cfgline[c]);
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
[8182] | 224 | else if (key == "oaiinfo" && cfgline.size() >= 1) {
|
---|
| 225 | // Get a piece of information for the oai repository information
|
---|
| 226 | // request. The line should read:
|
---|
| 227 | //
|
---|
| 228 | // oaiinfo <information field name> <value>
|
---|
| 229 | //
|
---|
[18892] | 230 | // This configuration should only be attempted in oai.cfg
|
---|
[8182] | 231 | //
|
---|
| 232 | if (this->collection != "") {
|
---|
[18892] | 233 | cerr << "Attempt to set oai information outside of oai.cfg" << endl;
|
---|
[8182] | 234 | cerr << "Configuration attempted in " << this->collection << " collection." << endl;
|
---|
| 235 | exit(1);
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | // if no second parameter is given, then the first parameter
|
---|
| 239 | if (cfgline.size() == 1) {
|
---|
| 240 | this->infoMap[cfgline[0]] = cfgline[0];
|
---|
| 241 | }
|
---|
| 242 | else {
|
---|
| 243 | this->infoMap[cfgline[0]] = cfgline[1];
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
[22212] | 246 | else if ( key == "oaisetname" || key == "oaisetdescription") {
|
---|
| 247 | text_t coll_name;
|
---|
| 248 | text_t value = "";
|
---|
| 249 | if (this->collection != "") {
|
---|
| 250 | // we are in collect.cfg
|
---|
| 251 | coll_name = this->collection;
|
---|
| 252 | if (cfgline.size() == 1) {
|
---|
| 253 | // just the collection value
|
---|
| 254 | value = cfgline[0];
|
---|
[8182] | 255 | }
|
---|
[22212] | 256 | else if (cfgline.size() == 2) {
|
---|
| 257 | // we have a subset name (eg for classifier)
|
---|
| 258 | coll_name.append(":");
|
---|
| 259 | coll_name.append(cfgline[0]);
|
---|
| 260 | value = cfgline[1];
|
---|
[8182] | 261 | }
|
---|
[22212] | 262 | } else if (cfgline.size() == 2) {
|
---|
| 263 | // oai.cfg, line should be collname, setName
|
---|
| 264 | coll_name = cfgline[0];
|
---|
| 265 | value = cfgline[1];
|
---|
[8182] | 266 | }
|
---|
[22212] | 267 | if (value != "") {
|
---|
| 268 | if (this->collectMap[coll_name] == NULL) {
|
---|
| 269 | this->collectMap[coll_name] = new oaicollectconfig(coll_name);
|
---|
[8182] | 270 | }
|
---|
[22212] | 271 | if (key == "oaisetname") {
|
---|
| 272 | this->collectMap[coll_name]->setName = value;
|
---|
| 273 | } else if (key == "oaisetdescription") {
|
---|
| 274 | this->collectMap[coll_name]->setDescription = value;
|
---|
[8182] | 275 | }
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
[22212] | 278 |
|
---|
| 279 | else if (key == "resumeafter" && cfgline.size() >= 1) {
|
---|
| 280 | this->resumptionSize = cfgline[0].getint();
|
---|
[14284] | 281 | }
|
---|
[8182] | 282 |
|
---|
[22212] | 283 | else if (key == "maintainer") {
|
---|
| 284 | this->maintainer = cfgline[0];
|
---|
[8182] | 285 | }
|
---|
[22212] | 286 | else if (key == "repositoryName") {
|
---|
| 287 | this->repositoryName = cfgline[0];
|
---|
[8182] | 288 | }
|
---|
[22284] | 289 | else if (key == "repositoryId") {
|
---|
| 290 | this->repositoryId = cfgline[0];
|
---|
| 291 | }
|
---|
| 292 | else if (key == "repositoryIdVersion") {
|
---|
| 293 | this->repositoryIdVersion = cfgline[0];
|
---|
| 294 | }
|
---|
[22212] | 295 | else if (key == "baseURL") {
|
---|
| 296 | this->baseURL = cfgline[0];
|
---|
| 297 | }
|
---|
| 298 | else if (key == "baseLibraryURL") {
|
---|
| 299 | this->baseLibraryURL = cfgline[0];
|
---|
[8182] | 300 | }
|
---|
[22212] | 301 | else if (key == "baseDocRoot") {
|
---|
| 302 | this->baseDocRoot = cfgline[0];
|
---|
[14284] | 303 | }
|
---|
[22284] | 304 | else if (key == "oaiversion") {
|
---|
| 305 | this->oaiVersion = cfgline[0];
|
---|
| 306 | }
|
---|
[22212] | 307 |
|
---|
| 308 | }
|
---|
[14284] | 309 |
|
---|
[8182] | 310 | text_t oaiconfig::getMapping(const text_t &collection, const text_t &collectfield)
|
---|
| 311 | {
|
---|
| 312 | if (this->collectMap[collection] == NULL) {
|
---|
| 313 | return "";
|
---|
| 314 | }
|
---|
| 315 | return this->collectMap[collection]->fieldMap[collectfield];
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | /**
|
---|
| 319 | * Get the mapping for a field in a given collection; if no mapping
|
---|
| 320 | * exists, the result will be a blank string.
|
---|
| 321 | */
|
---|
| 322 | text_t oaiconfig::getMapping(const text_t &collection, const text_t &collectfield, const text_t &formatname)
|
---|
| 323 | {
|
---|
| 324 | text_t fullName = collectfield;
|
---|
| 325 | fullName.append(":");
|
---|
| 326 | fullName.append(formatname);
|
---|
| 327 |
|
---|
| 328 | // try the collection-specific options first
|
---|
| 329 | if (this->collectMap[collection] != NULL) {
|
---|
| 330 | // first try the most specific item - this collection, and given that protocol
|
---|
| 331 | if (this->collectMap[collection]->fieldMap.count(fullName) >= 1) {
|
---|
| 332 | return this->collectMap[collection]->fieldMap[fullName];
|
---|
| 333 | }
|
---|
| 334 | // otherwise, fall back to this collection, and all protocols
|
---|
| 335 | else if (this->collectMap[collection]->fieldMap.count(collectfield) >= 1) {
|
---|
| 336 | return this->collectMap[collection]->fieldMap[collectfield];
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | // if no mappings exist, return an empty item
|
---|
| 341 | if (this->collectMap[""] == NULL) {
|
---|
| 342 | return "";
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | // then try generic rules
|
---|
| 346 | if (this->collectMap[""]->fieldMap.count(fullName) >= 1) {
|
---|
| 347 | return this->collectMap[""]->fieldMap[fullName];
|
---|
| 348 | }
|
---|
| 349 | else {
|
---|
| 350 | return this->collectMap[""]->fieldMap[collectfield];
|
---|
| 351 | }
|
---|
| 352 | }
|
---|
[20629] | 353 |
|
---|
[22212] | 354 | text_t oaiconfig::getBaseURL()
|
---|
| 355 | {
|
---|
| 356 | return this->baseURL;
|
---|
| 357 | }
|
---|
| 358 | text_t oaiconfig::getBaseLibraryURL()
|
---|
| 359 | {
|
---|
| 360 | return this->baseLibraryURL;
|
---|
| 361 | }
|
---|
| 362 | text_t oaiconfig::getBaseDocRoot()
|
---|
| 363 | {
|
---|
| 364 | return this->baseDocRoot;
|
---|
| 365 | }
|
---|
| 366 | text_t oaiconfig::getRepositoryName()
|
---|
| 367 | {
|
---|
| 368 | return this->repositoryName;
|
---|
| 369 | }
|
---|
[22284] | 370 | text_t oaiconfig::getRepositoryId()
|
---|
| 371 | {
|
---|
| 372 | return this->repositoryId;
|
---|
| 373 | }
|
---|
| 374 | text_t oaiconfig::getRepositoryIdVersion()
|
---|
| 375 | {
|
---|
| 376 | return this->repositoryIdVersion;
|
---|
| 377 | }
|
---|
[22212] | 378 | text_t oaiconfig::getMaintainer()
|
---|
| 379 | {
|
---|
| 380 | return this->maintainer;
|
---|
| 381 | }
|
---|
[20629] | 382 | text_t oaiconfig::getSetName(const text_t &setSpec)
|
---|
| 383 | {
|
---|
[22212] | 384 | if (this->collectMap[setSpec] == NULL) {
|
---|
| 385 | return "" ;
|
---|
[20629] | 386 | }
|
---|
[22212] | 387 |
|
---|
| 388 | return this->collectMap[setSpec]->setName;
|
---|
| 389 |
|
---|
[20629] | 390 | }
|
---|
| 391 |
|
---|
| 392 | text_t oaiconfig::getSetDescription(const text_t &setSpec)
|
---|
| 393 | {
|
---|
[22212] | 394 | if (this->collectMap[setSpec] == NULL) {
|
---|
| 395 | return "" ;
|
---|
[20629] | 396 | }
|
---|
[22212] | 397 |
|
---|
| 398 | return this->collectMap[setSpec]->setDescription;
|
---|
| 399 | }
|
---|
[20629] | 400 |
|
---|