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