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 |
|
---|
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 get_phrases (const text_t &querystring, text_tarray &phrases) {
|
---|
188 |
|
---|
189 | phrases.erase (phrases.begin(), phrases.end());
|
---|
190 | if (!querystring.empty()) {
|
---|
191 |
|
---|
192 | text_t::const_iterator end = querystring.end();
|
---|
193 | text_t::const_iterator here = findchar (querystring.begin(), end, '"');
|
---|
194 | if (here != end) {
|
---|
195 | text_t tmptext;
|
---|
196 | bool foundquote = false;
|
---|
197 | while (here != end) {
|
---|
198 | if (*here == '"') {
|
---|
199 | if (foundquote) {
|
---|
200 | if (!tmptext.empty()) {
|
---|
201 | phrases.push_back(tmptext);
|
---|
202 | tmptext.clear();
|
---|
203 | }
|
---|
204 | foundquote = false;
|
---|
205 | } else foundquote = true;
|
---|
206 | } else {
|
---|
207 | if (foundquote) tmptext.push_back (*here);
|
---|
208 | }
|
---|
209 | here ++;
|
---|
210 | }
|
---|
211 | }
|
---|
212 | }
|
---|
213 | }
|
---|