ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilQueryParser.php
Go to the documentation of this file.
1<?php
2/*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +-----------------------------------------------------------------------------+
22*/
23
35define('QP_COMBINATION_AND','and');
36define('QP_COMBINATION_OR','or');
37
39{
43 const MIN_WORD_LENGTH = 3;
44
45 var $lng = null;
46
49
51 var $quoted_words = array();
52 var $message; // Translated error message
53 var $combination; // combiniation of search words e.g 'and' or 'or'
54 protected $settings = null;
55 protected $wildcards_allowed; // [bool]
56
61 function __construct($a_query_str)
62 {
63 global $lng;
64
65 define('MIN_WORD_LENGTH',self::MIN_WORD_LENGTH);
66
67 $this->lng = $lng;
68
69 $this->query_str = $a_query_str;
70 $this->message = '';
71
72 include_once './Services/Search/classes/class.ilSearchSettings.php';
74
75 if(!$this->setMinWordLength(1))
76 {
78 }
79
80 $this->setAllowedWildcards(false);
81 }
82
83 function setMinWordLength($a_length,$a_force = false)
84 {
85 // Due to a bug in mysql fulltext search queries with min_word_legth < 3
86 // might freeze the system.
87 // Thus min_word_length cannot be set to values < 3 if the mysql fulltext is used.
88 if(!$a_force and $this->settings->enabledIndex() and $a_length < 3)
89 {
90 ilLoggerFactory::getLogger('src')->debug('Disabled min word length');
91 return false;
92 }
93 $this->min_word_length = $a_length;
94 return true;
95 }
97 {
99 }
100
101 function setGlobalMinLength($a_value)
102 {
103 if($a_value !== null)
104 {
105 $a_value = (int)$a_value;
106 if($a_value < 1)
107 {
108 return;
109 }
110 }
111 $this->global_min_length = $a_value;
112 }
113
115 {
117 }
118
119 function setAllowedWildcards($a_value)
120 {
121 $this->wildcards_allowed = (bool)$a_value;
122 }
123
125 {
127 }
128
129 function setMessage($a_msg)
130 {
131 $this->message = $a_msg;
132 }
133 function getMessage()
134 {
135 return $this->message;
136 }
137 function appendMessage($a_msg)
138 {
139 if(strlen($this->getMessage()))
140 {
141 $this->message .= '<br />';
142 }
143 $this->message .= $a_msg;
144 }
145
146 function setCombination($a_combination)
147 {
148 $this->combination = $a_combination;
149 }
150 function getCombination()
151 {
152 return $this->combination;
153 }
154
155 function getQueryString()
156 {
157 return trim($this->query_str);
158 }
159 function getWords()
160 {
161 return $this->words ? $this->words : array();
162 }
163
164 function getQuotedWords($with_quotation = false)
165 {
166 if($with_quotation)
167 {
168 return $this->quoted_words ? $this->quoted_words : array();
169 }
170 else
171 {
172 foreach($this->quoted_words as $word)
173 {
174 $tmp_word[] = str_replace('\"','',$word);
175 }
176 return $tmp_word ? $tmp_word : array();
177 }
178 }
179
181 {
182 $counter = 0;
183 $tmp_str = "";
184 foreach($this->getQuotedWords(true) as $word) {
185 if($counter++)
186 {
187 $tmp_str .= (" ".strtoupper($this->getCombination())." ");
188 }
189 $tmp_str .= $word;
190 }
191 return $tmp_str;
192 }
193 function parse()
194 {
195 $this->words = array();
196
197 #if(!strlen($this->getQueryString()))
198 #{
199 # return false;
200 #}
201
202 $words = explode(' ',trim($this->getQueryString()));
203 foreach($words as $word)
204 {
205 if(!strlen(trim($word)))
206 {
207 continue;
208 }
209
210 if(strlen(trim($word)) < $this->getMinWordLength())
211 {
212 $this->setMessage(sprintf($this->lng->txt('search_minimum_info'), $this->getMinWordLength()));
213 continue;
214 }
215
216 $this->words[] = ilUtil::prepareDBString($word);
217 }
218
219 $fullstr = trim($this->getQueryString());
220 if (!in_array($fullstr, $this->words))
221 {
222 $this->words[] = ilUtil::prepareDBString($fullstr);
223 }
224
225 if(!$this->getAllowedWildcards())
226 {
227 // #14768
228 foreach($this->words as $idx => $word)
229 {
230 if(!stristr($word, '\\'))
231 {
232 $word = str_replace('%', '\%', $word);
233 $word = str_replace('_', '\_', $word);
234 }
235 $this->words[$idx] = $word;
236 }
237 }
238
239 // Parse strings like && 'A "B C D" E' as 'A' && 'B C D' && 'E'
240 // Can be used in LIKE search or fulltext search > MYSQL 4.0
241 $this->__parseQuotation();
242
243 return true;
244 }
245
247 {
248 if(!strlen($this->getQueryString()))
249 {
250 $this->quoted_words[] = '';
251 return false;
252 }
253 $query_str = $this->getQueryString();
254 while(preg_match("/\".*?\"/",$query_str,$matches))
255 {
256 $query_str = str_replace($matches[0],'',$query_str);
257 $this->quoted_words[] = ilUtil::prepareDBString($matches[0]);
258 }
259
260 // Parse the rest
261 $words = explode(' ',trim($query_str));
262 foreach($words as $word)
263 {
264 if(!strlen(trim($word)))
265 {
266 continue;
267 }
268
269 $this->quoted_words[] = ilUtil::prepareDBString($word);
270 }
271
272 if(!$this->getAllowedWildcards())
273 {
274 // #14768
275 foreach($this->quoted_words as $idx => $word)
276 {
277 if(!stristr($word, '\\'))
278 {
279 $word = str_replace('%', '\%', $word);
280 $word = str_replace('_', '\_', $word);
281 }
282 $this->quoted_words[$idx] = $word;
283 }
284 }
285 }
286
287 function validate()
288 {
289 // Words with less than 3 characters
290 if(strlen($this->getMessage()))
291 {
292 return false;
293 }
294 // No search string given
295 if($this->getMinWordLength() and !count($this->getWords()))
296 {
297 $this->setMessage($this->lng->txt('msg_no_search_string'));
298 return false;
299 }
300 // No search string given
301 if($this->getGlobalMinLength() and strlen(str_replace('"', '', $this->getQueryString())) < $this->getGlobalMinLength())
302 {
303 $this->setMessage(sprintf($this->lng->txt('search_minimum_info'), $this->getGlobalMinLength()));
304 return false;
305 }
306
307 return true;
308 }
309}
310?>
sprintf('%.4f', $callTime)
An exception for terminatinating execution or to throw for unit testing.
static getLogger($a_component_id)
Get component logger.
__construct($a_query_str)
Constructor @access public.
setMinWordLength($a_length, $a_force=false)
setCombination($a_combination)
const MIN_WORD_LENGTH
Minimum of characters required for search.
getQuotedWords($with_quotation=false)
setAllowedWildcards($a_value)
setGlobalMinLength($a_value)
static prepareDBString($a_str)
prepare a string for db writing (insert/update)
$counter
settings()
Definition: settings.php:2