1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * querytools.cpp --
|
---|
4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
5 | *
|
---|
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.
|
---|
9 | *
|
---|
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 | *
|
---|
24 | *********************************************************************/
|
---|
25 |
|
---|
26 | #include "querytools.h"
|
---|
27 | #include <ctype.h>
|
---|
28 |
|
---|
29 | // request.filterResultOptions and request.fields (if required) should
|
---|
30 | // be set from the calling code
|
---|
31 | void set_queryfilter_options (FilterRequest_t &request, const text_t &querystring,
|
---|
32 | cgiargsclass &args) {
|
---|
33 |
|
---|
34 | request.filterName = "QueryFilter";
|
---|
35 |
|
---|
36 | OptionValue_t option;
|
---|
37 |
|
---|
38 | option.name = "Term";
|
---|
39 | option.value = querystring;
|
---|
40 | request.filterOptions.push_back (option);
|
---|
41 |
|
---|
42 | option.name = "QueryType";
|
---|
43 | option.value = (args.getintarg("t")) ? "ranked" : "boolean";
|
---|
44 | request.filterOptions.push_back (option);
|
---|
45 |
|
---|
46 | option.name = "Casefold";
|
---|
47 | option.value = (args.getintarg("k")) ? "true" : "false";
|
---|
48 | request.filterOptions.push_back (option);
|
---|
49 |
|
---|
50 | option.name = "Stem";
|
---|
51 | option.value = (args.getintarg("s")) ? "true" : "false";
|
---|
52 | request.filterOptions.push_back (option);
|
---|
53 |
|
---|
54 | if (!args["h"].empty()) {
|
---|
55 | option.name = "Index";
|
---|
56 | option.value = args["h"];
|
---|
57 | request.filterOptions.push_back (option);
|
---|
58 | }
|
---|
59 |
|
---|
60 | if (!args["j"].empty()) {
|
---|
61 | option.name = "Subcollection";
|
---|
62 | option.value = args["j"];
|
---|
63 | request.filterOptions.push_back (option);
|
---|
64 | }
|
---|
65 |
|
---|
66 | if (!args["n"].empty()) {
|
---|
67 | option.name = "Language";
|
---|
68 | option.value = args["n"];
|
---|
69 | request.filterOptions.push_back (option);
|
---|
70 | }
|
---|
71 |
|
---|
72 | if (!args["g"].empty()) { // granularity for mgpp
|
---|
73 | option.name = "Level";
|
---|
74 | option.value = args["g"];
|
---|
75 | request.filterOptions.push_back (option);
|
---|
76 | }
|
---|
77 |
|
---|
78 | set_more_queryfilter_options (request, args);
|
---|
79 | }
|
---|
80 |
|
---|
81 | void set_queryfilter_options (FilterRequest_t &request, const text_t &querystring1,
|
---|
82 | const text_t &querystring2, cgiargsclass &args) {
|
---|
83 |
|
---|
84 | set_queryfilter_options (request, querystring1, args);
|
---|
85 |
|
---|
86 | // fill in the second query if needed
|
---|
87 | if (!args["cq2"].empty()) {
|
---|
88 | OptionValue_t option;
|
---|
89 |
|
---|
90 | option.name = "CombineQuery";
|
---|
91 | option.value = args["cq2"];
|
---|
92 | request.filterOptions.push_back (option);
|
---|
93 |
|
---|
94 | option.name = "Term";
|
---|
95 | option.value = querystring2;
|
---|
96 | request.filterOptions.push_back (option);
|
---|
97 |
|
---|
98 | option.name = "QueryType";
|
---|
99 | option.value = (args.getintarg("t")) ? "ranked" : "boolean";
|
---|
100 | request.filterOptions.push_back (option);
|
---|
101 |
|
---|
102 | option.name = "Casefold";
|
---|
103 | option.value = (args.getintarg("k")) ? "true" : "false";
|
---|
104 | request.filterOptions.push_back (option);
|
---|
105 |
|
---|
106 | option.name = "Stem";
|
---|
107 | option.value = (args.getintarg("s")) ? "true" : "false";
|
---|
108 | request.filterOptions.push_back (option);
|
---|
109 |
|
---|
110 | if (!args["h2"].empty()) {
|
---|
111 | option.name = "Index";
|
---|
112 | option.value = args["h2"];
|
---|
113 | request.filterOptions.push_back (option);
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (!args["j2"].empty()) {
|
---|
117 | option.name = "Subcollection";
|
---|
118 | option.value = args["j2"];
|
---|
119 | request.filterOptions.push_back (option);
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (!args["n2"].empty()) {
|
---|
123 | option.name = "Language";
|
---|
124 | option.value = args["n2"];
|
---|
125 | request.filterOptions.push_back (option);
|
---|
126 | }
|
---|
127 | }
|
---|
128 | set_more_queryfilter_options (request, args);
|
---|
129 | }
|
---|
130 |
|
---|
131 | void set_more_queryfilter_options (FilterRequest_t &request, cgiargsclass &args) {
|
---|
132 |
|
---|
133 | OptionValue_t option;
|
---|
134 | int arg_m = args.getintarg("m");
|
---|
135 |
|
---|
136 | option.name = "Maxdocs";
|
---|
137 | option.value = arg_m;
|
---|
138 | request.filterOptions.push_back (option);
|
---|
139 |
|
---|
140 | // option.name = "StartResults";
|
---|
141 | // option.value = args["r"];
|
---|
142 | // request.filterOptions.push_back (option);
|
---|
143 |
|
---|
144 | // option.name = "EndResults";
|
---|
145 | // int endresults = args.getintarg("o") + (args.getintarg("r") - 1);
|
---|
146 | // if ((endresults > arg_m) && (arg_m != -1)) endresults = arg_m;
|
---|
147 | // option.value = endresults;
|
---|
148 | // request.filterOptions.push_back (option);
|
---|
149 | }
|
---|
150 |
|
---|
151 | void format_querystring (text_t &querystring, int querymode) {
|
---|
152 | text_t formattedstring;
|
---|
153 |
|
---|
154 | text_t::const_iterator here = querystring.begin();
|
---|
155 | text_t::const_iterator end = querystring.end();
|
---|
156 |
|
---|
157 | // space is used to insert spaces between Chinese
|
---|
158 | // characters. No space is needed before the first
|
---|
159 | // Chinese character.
|
---|
160 | bool space = false;
|
---|
161 |
|
---|
162 | // want to remove ()|!& from querystring so boolean queries are just
|
---|
163 | // "all the words" queries (unless querymode is advanced)
|
---|
164 | while (here != end) {
|
---|
165 | if ((querymode == 0) && (*here == '(' || *here == ')' || *here == '|' ||
|
---|
166 | *here == '!' || *here == '&')) {
|
---|
167 | formattedstring.push_back(' ');
|
---|
168 | } else {
|
---|
169 | if ((*here >= 0x4e00 && *here <= 0x9fa5) ||
|
---|
170 | (*here >= 0xf900 && *here <= 0xfa2d)) {
|
---|
171 | // Chinese character
|
---|
172 | if (space) formattedstring.push_back (0x200b);
|
---|
173 | formattedstring.push_back (*here);
|
---|
174 | formattedstring.push_back (0x200b);
|
---|
175 | space = true;
|
---|
176 | } else {
|
---|
177 | // non-Chinese character
|
---|
178 | formattedstring.push_back (*here);
|
---|
179 | space = false;
|
---|
180 | }
|
---|
181 | }
|
---|
182 | here ++;
|
---|
183 | }
|
---|
184 | querystring = formattedstring;
|
---|
185 | }
|
---|
186 |
|
---|
187 | void add_ands(text_t& querystring, int querytype)
|
---|
188 | {
|
---|
189 |
|
---|
190 | if(querytype==0)
|
---|
191 | {
|
---|
192 | text_t formattedstring;
|
---|
193 |
|
---|
194 | bool spacelast = false;
|
---|
195 |
|
---|
196 | text_t::const_iterator here = querystring.begin();
|
---|
197 | text_t::const_iterator end = querystring.end();
|
---|
198 |
|
---|
199 |
|
---|
200 | querystring = formattedstring;
|
---|
201 |
|
---|
202 | while(here!=end && isspace((*here)))
|
---|
203 | here++;
|
---|
204 |
|
---|
205 | if(here==end)
|
---|
206 | return;
|
---|
207 |
|
---|
208 | while(here!=end)
|
---|
209 | {
|
---|
210 | if(isspace((*here)))
|
---|
211 | {
|
---|
212 | while(here!=end && isspace((*here)))
|
---|
213 | here++;
|
---|
214 | if(here!=end)
|
---|
215 | formattedstring.appendcstr(" AND ");
|
---|
216 | }
|
---|
217 | if(!isspace((*here)))
|
---|
218 | {
|
---|
219 | formattedstring.push_back((*here));
|
---|
220 | }
|
---|
221 | here++;
|
---|
222 | }
|
---|
223 | querystring = formattedstring;
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 |
|
---|
229 | void add_dates(text_t &querystring, int startdate, int enddate,
|
---|
230 | int startbc, int endbc)
|
---|
231 | {
|
---|
232 | if(startdate)
|
---|
233 | {
|
---|
234 | int querystringis = 0;
|
---|
235 | text_t::const_iterator here = querystring.begin();
|
---|
236 | text_t::const_iterator end = querystring.end();
|
---|
237 | while(here!=end)
|
---|
238 | {
|
---|
239 | if(!(isspace((*here)))){
|
---|
240 | here = end;
|
---|
241 | querystringis = 1;
|
---|
242 | }
|
---|
243 | else
|
---|
244 | here++;
|
---|
245 | }
|
---|
246 | //converting BCE dates
|
---|
247 | if(startbc && startdate > 0)
|
---|
248 | {
|
---|
249 | startdate *= -1;
|
---|
250 | }
|
---|
251 | if(endbc && enddate > 0)
|
---|
252 | {
|
---|
253 | enddate *= -1;
|
---|
254 | }
|
---|
255 | if(enddate != 0 && enddate<startdate)
|
---|
256 | {
|
---|
257 | cout<<"enddate too small"<<endl;
|
---|
258 | return;
|
---|
259 | }
|
---|
260 | if(querystringis)
|
---|
261 | querystring.appendcstr(" AND");
|
---|
262 | if(!enddate)
|
---|
263 | {
|
---|
264 | querystring.appendcstr(" [");
|
---|
265 | querystring.appendint(startdate);
|
---|
266 | querystring.appendcstr("]:Date");
|
---|
267 | }
|
---|
268 | else{
|
---|
269 | int nextdate = startdate;
|
---|
270 | querystring.appendcstr(" (");
|
---|
271 | while(nextdate<=enddate)
|
---|
272 | {
|
---|
273 | if(nextdate!=0)
|
---|
274 | {
|
---|
275 | querystring.appendcstr(" [");
|
---|
276 | querystring.appendint(nextdate);
|
---|
277 | querystring.appendcstr("]:Date");
|
---|
278 | }
|
---|
279 | nextdate++;
|
---|
280 | }
|
---|
281 | querystring.appendcstr(" )");
|
---|
282 | }
|
---|
283 | }
|
---|
284 | }
|
---|
285 | void get_phrases (const text_t &querystring, text_tarray &phrases) {
|
---|
286 |
|
---|
287 | phrases.erase (phrases.begin(), phrases.end());
|
---|
288 | if (!querystring.empty()) {
|
---|
289 |
|
---|
290 | text_t::const_iterator end = querystring.end();
|
---|
291 | text_t::const_iterator here = findchar (querystring.begin(), end, '"');
|
---|
292 | if (here != end) {
|
---|
293 | text_t tmptext;
|
---|
294 | bool foundquote = false;
|
---|
295 | while (here != end) {
|
---|
296 | if (*here == '"') {
|
---|
297 | if (foundquote) {
|
---|
298 | if (!tmptext.empty()) {
|
---|
299 | phrases.push_back(tmptext);
|
---|
300 | tmptext.clear();
|
---|
301 | }
|
---|
302 | foundquote = false;
|
---|
303 | } else foundquote = true;
|
---|
304 | } else {
|
---|
305 | if (foundquote) tmptext.push_back (*here);
|
---|
306 | }
|
---|
307 | here ++;
|
---|
308 | }
|
---|
309 | }
|
---|
310 | }
|
---|
311 | }
|
---|