[270] | 1 | /**********************************************************************
|
---|
| 2 | *
|
---|
| 3 | * querytools.cpp --
|
---|
| 4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
| 5 | *
|
---|
[533] | 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.
|
---|
[270] | 9 | *
|
---|
[533] | 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 | *
|
---|
[270] | 24 | * $Id: querytools.cpp 533 1999-09-07 04:57:01Z sjboddie $
|
---|
| 25 | *
|
---|
| 26 | *********************************************************************/
|
---|
| 27 |
|
---|
| 28 | /*
|
---|
| 29 | $Log$
|
---|
[533] | 30 | Revision 1.6 1999/09/07 04:56:58 sjboddie
|
---|
| 31 | added GPL notice
|
---|
| 32 |
|
---|
[470] | 33 | Revision 1.5 1999/08/25 04:47:55 sjboddie
|
---|
| 34 | added advanced search option - other minor changes
|
---|
| 35 |
|
---|
[403] | 36 | Revision 1.4 1999/07/19 00:16:58 sjboddie
|
---|
| 37 | no longer display documents that don't match all phrases in query string
|
---|
| 38 |
|
---|
[397] | 39 | Revision 1.3 1999/07/16 00:19:02 sjboddie
|
---|
| 40 | some changes to the way quoted queries are handled
|
---|
| 41 |
|
---|
[349] | 42 | Revision 1.2 1999/07/07 06:12:21 rjmcnab
|
---|
| 43 | Added ability to combine two independant queries.
|
---|
| 44 |
|
---|
[270] | 45 | Revision 1.1 1999/06/15 02:24:06 sjboddie
|
---|
| 46 | moved do_query from queryaction to new querytools module (as do_action
|
---|
| 47 | is also called from documentaction when highlighting text)
|
---|
| 48 |
|
---|
| 49 | */
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | #include "querytools.h"
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | // do_query sets the filter options and makes the protocol call to
|
---|
| 56 | // do a query. The results are returned in response.
|
---|
| 57 | // request.filterResultOptions and request.fields (if required)
|
---|
| 58 | // should be set by the calling function.
|
---|
| 59 |
|
---|
| 60 | bool do_query (FilterRequest_t &request, cgiargsclass &args,
|
---|
[397] | 61 | recptproto *collectproto, FilterResponse_t &response,
|
---|
| 62 | ostream &logout) {
|
---|
[270] | 63 |
|
---|
| 64 | request.filterName = "QueryFilter";
|
---|
| 65 |
|
---|
| 66 | comerror_t err;
|
---|
| 67 | OptionValue_t option;
|
---|
| 68 | text_t formattedstring = args["q"];
|
---|
[470] | 69 | format_querystring (formattedstring, args.getintarg("b"));
|
---|
| 70 |
|
---|
[270] | 71 | option.name = "Term";
|
---|
| 72 | option.value = formattedstring;
|
---|
| 73 | request.filterOptions.push_back (option);
|
---|
| 74 |
|
---|
| 75 | option.name = "QueryType";
|
---|
| 76 | option.value = (args.getintarg("t")) ? "ranked" : "boolean";
|
---|
| 77 | request.filterOptions.push_back (option);
|
---|
| 78 |
|
---|
| 79 | option.name = "Casefold";
|
---|
| 80 | option.value = (args.getintarg("k")) ? "true" : "false";
|
---|
| 81 | request.filterOptions.push_back (option);
|
---|
| 82 |
|
---|
| 83 | option.name = "Stem";
|
---|
| 84 | option.value = (args.getintarg("s")) ? "true" : "false";
|
---|
| 85 | request.filterOptions.push_back (option);
|
---|
| 86 |
|
---|
| 87 | if (!args["h"].empty()) {
|
---|
| 88 | option.name = "Index";
|
---|
| 89 | option.value = args["h"];
|
---|
| 90 | request.filterOptions.push_back (option);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | if (!args["j"].empty()) {
|
---|
| 94 | option.name = "Subcollection";
|
---|
| 95 | option.value = args["j"];
|
---|
| 96 | request.filterOptions.push_back (option);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | if (!args["n"].empty()) {
|
---|
| 100 | option.name = "Language";
|
---|
| 101 | option.value = args["n"];
|
---|
| 102 | request.filterOptions.push_back (option);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[349] | 105 | // fill in the second query if needed
|
---|
| 106 | if (!args["cq2"].empty()) {
|
---|
| 107 | option.name = "CombineQuery";
|
---|
| 108 | option.value = args["cq2"];
|
---|
| 109 | request.filterOptions.push_back (option);
|
---|
| 110 |
|
---|
| 111 | text_t formattedstring2 = args["q2"];
|
---|
[470] | 112 | format_querystring (formattedstring2, args.getintarg("b"));
|
---|
[349] | 113 |
|
---|
| 114 | option.name = "Term";
|
---|
| 115 | option.value = formattedstring2;
|
---|
| 116 | request.filterOptions.push_back (option);
|
---|
| 117 |
|
---|
| 118 | option.name = "QueryType";
|
---|
| 119 | option.value = (args.getintarg("t")) ? "ranked" : "boolean";
|
---|
| 120 | request.filterOptions.push_back (option);
|
---|
| 121 |
|
---|
| 122 | option.name = "Casefold";
|
---|
| 123 | option.value = (args.getintarg("k")) ? "true" : "false";
|
---|
| 124 | request.filterOptions.push_back (option);
|
---|
| 125 |
|
---|
| 126 | option.name = "Stem";
|
---|
| 127 | option.value = (args.getintarg("s")) ? "true" : "false";
|
---|
| 128 | request.filterOptions.push_back (option);
|
---|
| 129 |
|
---|
| 130 | if (!args["h2"].empty()) {
|
---|
| 131 | option.name = "Index";
|
---|
| 132 | option.value = args["h2"];
|
---|
| 133 | request.filterOptions.push_back (option);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | if (!args["j2"].empty()) {
|
---|
| 137 | option.name = "Subcollection";
|
---|
| 138 | option.value = args["j2"];
|
---|
| 139 | request.filterOptions.push_back (option);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | if (!args["n2"].empty()) {
|
---|
| 143 | option.name = "Language";
|
---|
| 144 | option.value = args["n2"];
|
---|
| 145 | request.filterOptions.push_back (option);
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[270] | 149 | option.name = "StartResults";
|
---|
| 150 | option.value = args["r"];
|
---|
| 151 | request.filterOptions.push_back (option);
|
---|
| 152 |
|
---|
| 153 | option.name = "EndResults";
|
---|
| 154 | int arg_m = args.getintarg("m");
|
---|
| 155 | int endresults = args.getintarg("o") + (args.getintarg("r") - 1);
|
---|
| 156 | if (endresults > arg_m) endresults = arg_m;
|
---|
| 157 | option.value = endresults;
|
---|
| 158 | request.filterOptions.push_back (option);
|
---|
| 159 |
|
---|
| 160 | collectproto->filter (args["c"], request, response, err, logout);
|
---|
| 161 |
|
---|
| 162 | if (err != noError) {
|
---|
| 163 | outconvertclass text_t2ascii;
|
---|
| 164 | logout << text_t2ascii
|
---|
| 165 | << "Error: call to QueryFilter failed in queryaction ("
|
---|
| 166 | << get_comerror_string (err) << ")\n";
|
---|
| 167 | return false;
|
---|
| 168 | }
|
---|
| 169 | return true;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[470] | 172 | void format_querystring (text_t &querystring, int querymode) {
|
---|
[270] | 173 | text_t formattedstring;
|
---|
| 174 |
|
---|
| 175 | text_t::const_iterator here = querystring.begin();
|
---|
| 176 | text_t::const_iterator end = querystring.end();
|
---|
| 177 |
|
---|
| 178 | // space is used to insert spaces between Chinese
|
---|
| 179 | // characters. No space is needed before the first
|
---|
| 180 | // Chinese character.
|
---|
| 181 | bool space = false;
|
---|
| 182 |
|
---|
| 183 | // want to remove ()|!& from querystring so boolean queries are just
|
---|
[470] | 184 | // "all the words" queries (unless querymode is advanced)
|
---|
[270] | 185 | while (here != end) {
|
---|
[470] | 186 | if ((querymode == 0) && (*here == '(' || *here == ')' || *here == '|' ||
|
---|
| 187 | *here == '!' || *here == '&')) {
|
---|
[270] | 188 | formattedstring.push_back(' ');
|
---|
| 189 | } else {
|
---|
[397] | 190 | if ((*here >= 0x4e00 && *here <= 0x9fa5) ||
|
---|
| 191 | (*here >= 0xf900 && *here <= 0xfa2d)) {
|
---|
| 192 | // Chinese character
|
---|
| 193 | if (space) formattedstring.push_back (0x200b);
|
---|
| 194 | formattedstring.push_back (*here);
|
---|
| 195 | formattedstring.push_back (0x200b);
|
---|
| 196 | space = true;
|
---|
[270] | 197 | } else {
|
---|
[397] | 198 | // non-Chinese character
|
---|
| 199 | formattedstring.push_back (*here);
|
---|
| 200 | space = false;
|
---|
[270] | 201 | }
|
---|
| 202 | }
|
---|
| 203 | here ++;
|
---|
| 204 | }
|
---|
[397] | 205 | querystring = formattedstring;
|
---|
[270] | 206 | }
|
---|
| 207 |
|
---|
[403] | 208 | void get_phrases (const text_t &querystring, text_tarray &phrases) {
|
---|
| 209 |
|
---|
| 210 | phrases.erase (phrases.begin(), phrases.end());
|
---|
| 211 | if (!querystring.empty()) {
|
---|
| 212 |
|
---|
| 213 | text_t::const_iterator end = querystring.end();
|
---|
| 214 | text_t::const_iterator here = findchar (querystring.begin(), end, '"');
|
---|
| 215 | if (here != end) {
|
---|
| 216 | text_t tmptext;
|
---|
| 217 | bool foundquote = false;
|
---|
| 218 | while (here != end) {
|
---|
| 219 | if (*here == '"') {
|
---|
| 220 | if (foundquote) {
|
---|
| 221 | if (!tmptext.empty()) {
|
---|
| 222 | phrases.push_back(tmptext);
|
---|
| 223 | tmptext.clear();
|
---|
| 224 | }
|
---|
| 225 | foundquote = false;
|
---|
| 226 | } else foundquote = true;
|
---|
| 227 | } else {
|
---|
| 228 | if (foundquote) tmptext.push_back (*here);
|
---|
| 229 | }
|
---|
| 230 | here ++;
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
| 234 | }
|
---|
| 235 |
|
---|