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