[227] | 1 | /**********************************************************************
|
---|
| 2 | *
|
---|
| 3 | * browsefilter.cpp --
|
---|
| 4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
| 5 | *
|
---|
[534] | 6 | * A component of the Greenstone digital library software
|
---|
| 7 | * from the New Zealand Digital Library Project at the
|
---|
| 8 | * University of Waikato, New Zealand.
|
---|
[227] | 9 | *
|
---|
[534] | 10 | * This program is free software; you can redistribute it and/or modify
|
---|
| 11 | * it under the terms of the GNU General Public License as published by
|
---|
| 12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | * (at your option) any later version.
|
---|
| 14 | *
|
---|
| 15 | * This program is distributed in the hope that it will be useful,
|
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | * GNU General Public License for more details.
|
---|
| 19 | *
|
---|
| 20 | * You should have received a copy of the GNU General Public License
|
---|
| 21 | * along with this program; if not, write to the Free Software
|
---|
| 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | *
|
---|
[227] | 24 | *********************************************************************/
|
---|
| 25 |
|
---|
| 26 | #include "browsefilter.h"
|
---|
| 27 | #include "fileutil.h"
|
---|
[990] | 28 | #include "gsdltools.h"
|
---|
[227] | 29 |
|
---|
| 30 |
|
---|
| 31 | browsefilterclass::browsefilterclass () {
|
---|
| 32 | gdbmptr = NULL;
|
---|
| 33 |
|
---|
| 34 | // -- onePerQuery StartResults integer
|
---|
| 35 | FilterOption_t filtopt;
|
---|
| 36 | filtopt.name = "StartResults";
|
---|
| 37 | filtopt.type = FilterOption_t::integert;
|
---|
| 38 | filtopt.repeatable = FilterOption_t::onePerQuery;
|
---|
| 39 | filtopt.defaultValue = "1";
|
---|
| 40 | filtopt.validValues.push_back("1");
|
---|
[238] | 41 | filtopt.validValues.push_back("10000");
|
---|
[227] | 42 | filterOptions["StartResults"] = filtopt;
|
---|
| 43 |
|
---|
| 44 | // -- onePerQuery EndResults integer
|
---|
| 45 | filtopt.clear();
|
---|
| 46 | filtopt.name = "EndResults";
|
---|
| 47 | filtopt.type = FilterOption_t::integert;
|
---|
| 48 | filtopt.repeatable = FilterOption_t::onePerQuery;
|
---|
[1256] | 49 | filtopt.defaultValue = "-1";
|
---|
| 50 | filtopt.validValues.push_back("-1");
|
---|
[238] | 51 | filtopt.validValues.push_back("10000");
|
---|
[227] | 52 | filterOptions["EndResults"] = filtopt;
|
---|
| 53 |
|
---|
| 54 | // -- onePerQuery ParentNode string ("" will return the browsing available)
|
---|
| 55 | filtopt.clear();
|
---|
| 56 | filtopt.name = "ParentNode";
|
---|
| 57 | filtopt.type = FilterOption_t::stringt;
|
---|
| 58 | filtopt.repeatable = FilterOption_t::onePerQuery;
|
---|
| 59 | filtopt.defaultValue = "";
|
---|
| 60 | filterOptions["ParentNode"] = filtopt;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | browsefilterclass::~browsefilterclass () {
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | bool browsefilterclass::init (ostream &logout) {
|
---|
| 67 | outconvertclass text_t2ascii;
|
---|
| 68 |
|
---|
| 69 | if (!filterclass::init(logout)) return false;
|
---|
| 70 |
|
---|
| 71 | // get the filename for the database and make sure it exists
|
---|
[2212] | 72 | gdbm_filename = filename_cat(gdbmhome, "collect", collection, "index", "text", collection);
|
---|
[398] | 73 |
|
---|
[990] | 74 | if (littleEndian()) gdbm_filename += ".ldb";
|
---|
| 75 | else gdbm_filename += ".bdb";
|
---|
| 76 |
|
---|
[227] | 77 | if (!file_exists(gdbm_filename)) {
|
---|
| 78 | logout << text_t2ascii
|
---|
[722] | 79 | << "warning: gdbm database \"" //****
|
---|
[227] | 80 | << gdbm_filename << "\" does not exist\n\n";
|
---|
[722] | 81 | // return false; //****
|
---|
[227] | 82 | }
|
---|
| 83 |
|
---|
| 84 | return true;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | void browsefilterclass::filter (const FilterRequest_t &request,
|
---|
| 88 | FilterResponse_t &response,
|
---|
| 89 | comerror_t &err, ostream &logout) {
|
---|
[238] | 90 | int numDocs = 0;
|
---|
[227] | 91 | outconvertclass text_t2ascii;
|
---|
| 92 |
|
---|
| 93 | response.clear ();
|
---|
| 94 | err = noError;
|
---|
| 95 | if (gdbmptr == NULL) {
|
---|
| 96 | // most likely a configuration problem
|
---|
| 97 | logout << text_t2ascii
|
---|
| 98 | << "configuration error: browsefilter contains a null gdbmclass\n\n";
|
---|
| 99 | err = configurationError;
|
---|
| 100 | return;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | // open the database
|
---|
| 104 | gdbmptr->setlogout(&logout);
|
---|
[491] | 105 | if (!gdbmptr->opendatabase (gdbm_filename, GDBM_READER, 100, false)) {
|
---|
[227] | 106 | // most likely a system problem (we have already checked that the
|
---|
| 107 | // gdbm database exists)
|
---|
| 108 | logout << text_t2ascii
|
---|
| 109 | << "system problem: open on gdbm database \""
|
---|
| 110 | << gdbm_filename << "\" failed\n\n";
|
---|
| 111 | err = systemProblem;
|
---|
| 112 | return;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | // get the browse parameters
|
---|
| 116 | int startresults = filterOptions["StartResults"].defaultValue.getint();
|
---|
| 117 | int endresults = filterOptions["EndResults"].defaultValue.getint();
|
---|
| 118 | text_t parentnode = filterOptions["ParentNode"].defaultValue;
|
---|
| 119 | OptionValue_tarray::const_iterator options_here = request.filterOptions.begin();
|
---|
| 120 | OptionValue_tarray::const_iterator options_end = request.filterOptions.end();
|
---|
| 121 | while (options_here != options_end) {
|
---|
| 122 | if ((*options_here).name == "StartResults")
|
---|
| 123 | startresults = (*options_here).value.getint();
|
---|
| 124 | else if ((*options_here).name == "EndResults")
|
---|
| 125 | endresults = (*options_here).value.getint();
|
---|
| 126 | else if ((*options_here).name == "ParentNode")
|
---|
| 127 | parentnode = (*options_here).value;
|
---|
| 128 | else {
|
---|
| 129 | logout << text_t2ascii
|
---|
| 130 | << "warning: unknown browsefilter option \""
|
---|
| 131 | << (*options_here).name
|
---|
| 132 | << "\" ignored.\n\n";
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | options_here++;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[259] | 138 | infodbclass info;
|
---|
| 139 |
|
---|
| 140 | // translate any ".fc", ".pr" etc. stuff in the parentnode
|
---|
| 141 | parentnode = gdbmptr->translate_OID (parentnode, info);
|
---|
| 142 |
|
---|
[227] | 143 | // adjust topmost browsing node
|
---|
| 144 | if (parentnode.empty()) parentnode = "browse";
|
---|
| 145 |
|
---|
| 146 | // get the node
|
---|
| 147 | if ((request.filterResultOptions & FROID) ||
|
---|
| 148 | (request.filterResultOptions & FRmetadata)) {
|
---|
| 149 | if (!gdbmptr->getinfo(parentnode, info)) {
|
---|
| 150 | // didn't find the node
|
---|
| 151 | logout << text_t2ascii
|
---|
| 152 | << "warning: lookup for node \"" << parentnode
|
---|
| 153 | << "\" failed for browsefilter.\n\n";
|
---|
| 154 | } else {
|
---|
| 155 | // found the node
|
---|
| 156 |
|
---|
| 157 | // replace " with the parent node name and split the contains string
|
---|
| 158 | // into the result set
|
---|
[249] | 159 | text_tarray resultset;
|
---|
[227] | 160 | text_t tmptext;
|
---|
| 161 | text_t &contains = info["contains"];
|
---|
| 162 | text_t::iterator contains_here = contains.begin();
|
---|
| 163 | text_t::iterator contains_end = contains.end();
|
---|
| 164 | while (contains_here != contains_end) {
|
---|
| 165 | if (*contains_here == '"') tmptext += parentnode;
|
---|
| 166 | else if (*contains_here == ';') {
|
---|
[249] | 167 | if (!tmptext.empty()) resultset.push_back (tmptext);
|
---|
[227] | 168 | tmptext.clear();
|
---|
| 169 | } else tmptext.push_back(*contains_here);
|
---|
| 170 |
|
---|
| 171 | contains_here++;
|
---|
| 172 | }
|
---|
| 173 | // insert the last result in the set
|
---|
[249] | 174 | if (!tmptext.empty()) resultset.push_back (tmptext);
|
---|
[227] | 175 |
|
---|
[829] | 176 | text_tarray offset_resultset;
|
---|
| 177 | text_t &md_type = info["mdtype"];
|
---|
| 178 | if (!md_type.empty())
|
---|
| 179 | {
|
---|
| 180 | text_t &md_offset = info["mdoffset"];
|
---|
| 181 | if (!md_offset.empty())
|
---|
| 182 | {
|
---|
| 183 | text_t offsettext;
|
---|
| 184 |
|
---|
| 185 | text_t::iterator offset_here = md_offset.begin();
|
---|
| 186 | text_t::iterator offset_end = md_offset.end();
|
---|
| 187 | while (offset_here != offset_end)
|
---|
| 188 | {
|
---|
| 189 | if (*offset_here == ';')
|
---|
| 190 | {
|
---|
| 191 | if (offsettext.empty())
|
---|
| 192 | {
|
---|
| 193 | offset_resultset.push_back ("0");
|
---|
| 194 | }
|
---|
| 195 | else
|
---|
| 196 | {
|
---|
| 197 | offset_resultset.push_back (offsettext);
|
---|
| 198 | }
|
---|
| 199 | offsettext.clear();
|
---|
| 200 | }
|
---|
| 201 | else
|
---|
| 202 | {
|
---|
| 203 | offsettext.push_back(*offset_here);
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | offset_here++;
|
---|
| 207 | }
|
---|
| 208 | // insert the last result in the set
|
---|
[1702] | 209 | if (offsettext.empty())
|
---|
| 210 | {
|
---|
| 211 | offset_resultset.push_back ("0");
|
---|
| 212 | }
|
---|
| 213 | else
|
---|
| 214 | {
|
---|
| 215 | offset_resultset.push_back (offsettext);
|
---|
| 216 | }
|
---|
[829] | 217 | }
|
---|
| 218 | else
|
---|
| 219 | {
|
---|
| 220 | // add 0 offset for each 'contains' entry
|
---|
| 221 | text_tarray::iterator result_here = resultset.begin();
|
---|
| 222 | text_tarray::iterator result_end = resultset.end();
|
---|
| 223 | while (result_here != result_end) {
|
---|
| 224 | offset_resultset.push_back("0");
|
---|
| 225 | result_here++;
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | // do an intersection with the input set
|
---|
| 230 | if (!request.docSet.empty()) {
|
---|
| 231 |
|
---|
| 232 | text_tarray intersect_resultset;
|
---|
| 233 | text_tarray intersect_offset_resultset;
|
---|
| 234 |
|
---|
| 235 | text_tarray::const_iterator resultset_here = resultset.begin();
|
---|
| 236 | text_tarray::const_iterator resultset_end = resultset.end();
|
---|
| 237 | text_tarray::const_iterator offset_resultset_here = offset_resultset.begin();
|
---|
| 238 |
|
---|
| 239 | while (resultset_here != resultset_end) {
|
---|
| 240 | if (in_set (request.docSet, *resultset_here))
|
---|
| 241 | {
|
---|
| 242 | intersect_resultset.push_back (*resultset_here);
|
---|
| 243 | intersect_offset_resultset.push_back (*offset_resultset_here);
|
---|
| 244 | }
|
---|
| 245 | resultset_here ++;
|
---|
| 246 | offset_resultset_here ++;
|
---|
| 247 | }
|
---|
| 248 | resultset = intersect_resultset;
|
---|
| 249 | offset_resultset = intersect_offset_resultset;
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 | else
|
---|
| 253 | {
|
---|
| 254 | // do an intersection with the input set
|
---|
| 255 | if (!request.docSet.empty()) {
|
---|
| 256 | intersect (resultset, request.docSet);
|
---|
| 257 | }
|
---|
[1702] | 258 |
|
---|
| 259 | // add 0 offset for each 'contains' entry
|
---|
| 260 | text_tarray::iterator result_here = resultset.begin();
|
---|
| 261 | text_tarray::iterator result_end = resultset.end();
|
---|
| 262 | while (result_here != result_end) {
|
---|
| 263 | offset_resultset.push_back("0");
|
---|
| 264 | result_here++;
|
---|
| 265 | }
|
---|
[829] | 266 | }
|
---|
| 267 |
|
---|
[227] | 268 | // create the response
|
---|
[238] | 269 | numDocs = resultset.size();
|
---|
[227] | 270 | int resultnum = 1;
|
---|
| 271 | ResultDocInfo_t resultdoc;
|
---|
[249] | 272 | text_tarray::iterator result_here = resultset.begin();
|
---|
| 273 | text_tarray::iterator result_end = resultset.end();
|
---|
[829] | 274 | text_tarray::iterator offset_result_here = offset_resultset.begin();
|
---|
[227] | 275 |
|
---|
| 276 | while (result_here != result_end) {
|
---|
[1256] | 277 | // if endresults is -1 get all results
|
---|
| 278 | if ((endresults != -1) && (resultnum > endresults)) break;
|
---|
[227] | 279 | if (resultnum >= startresults) {
|
---|
| 280 | resultdoc.OID = (*result_here);
|
---|
[829] | 281 | if (!md_type.empty())
|
---|
| 282 | {
|
---|
| 283 | resultdoc.classifier_metadata_type = md_type;
|
---|
| 284 | resultdoc.classifier_metadata_offset = offset_result_here->getint();
|
---|
| 285 | }
|
---|
[227] | 286 | response.docInfo.push_back(resultdoc);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | resultnum++;
|
---|
| 290 | result_here++;
|
---|
[829] | 291 | if (!md_type.empty()) offset_result_here++;
|
---|
[227] | 292 | }
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
| 295 |
|
---|
[238] | 296 | response.numDocs = numDocs;
|
---|
[398] | 297 | response.isApprox = Exact;
|
---|
[227] | 298 | }
|
---|