ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
35 define('QP_COMBINATION_AND','and');
36 define('QP_COMBINATION_OR','or');
37 
39 {
40  var $lng = null;
41 
43  var $global_min_length = null;
44 
45 
47  var $quoted_words = array();
48  var $message; // Translated error message
49  var $combination; // combiniation of search words e.g 'and' or 'or'
50  protected $settings = null;
51  protected $wildcards_allowed; // [bool]
52 
57  function ilQueryParser($a_query_str)
58  {
59  global $lng;
60 
61  define(MIN_WORD_LENGTH,3);
62 
63  $this->lng =& $lng;
64 
65  $this->query_str = $a_query_str;
66  $this->message = '';
67 
68  include_once './Services/Search/classes/class.ilSearchSettings.php';
69  $this->settings = ilSearchSettings::getInstance();
70 
71  if(!$this->setMinWordLength(1))
72  {
73  $this->setMinWordLength(MIN_WORD_LENGTH);
74  }
75 
76  $this->setAllowedWildcards(false);
77  }
78 
79  function setMinWordLength($a_length,$a_force = false)
80  {
81  // Due to a bug in mysql fulltext search queries with min_word_legth < 3
82  // might freeze the system.
83  // Thus min_word_length cannot be set to values < 3 if the mysql fulltext is used.
84  if(!$a_force and $this->settings->enabledIndex() and $a_length < 3)
85  {
86  $GLOBALS['ilLog']->write(__METHOD__.': Disabled min word length');
87  return false;
88  }
89  $this->min_word_length = $a_length;
90  return true;
91  }
92  function getMinWordLength()
93  {
95  }
96 
97  function setGlobalMinLength($a_value)
98  {
99  if($a_value !== null)
100  {
101  $a_value = (int)$a_value;
102  if($a_value < 1)
103  {
104  return;
105  }
106  }
107  $this->global_min_length = $a_value;
108  }
109 
111  {
113  }
114 
115  function setAllowedWildcards($a_value)
116  {
117  $this->wildcards_allowed = (bool)$a_value;
118  }
119 
121  {
123  }
124 
125  function setMessage($a_msg)
126  {
127  $this->message = $a_msg;
128  }
129  function getMessage()
130  {
131  return $this->message;
132  }
133  function appendMessage($a_msg)
134  {
135  if(strlen($this->getMessage()))
136  {
137  $this->message .= '<br />';
138  }
139  $this->message .= $a_msg;
140  }
141 
142  function setCombination($a_combination)
143  {
144  $this->combination = $a_combination;
145  }
146  function getCombination()
147  {
148  return $this->combination;
149  }
150 
151  function getQueryString()
152  {
153  return trim($this->query_str);
154  }
155  function getWords()
156  {
157  return $this->words ? $this->words : array();
158  }
159 
160  function getQuotedWords($with_quotation = false)
161  {
162  if($with_quotation)
163  {
164  return $this->quoted_words ? $this->quoted_words : array();
165  }
166  else
167  {
168  foreach($this->quoted_words as $word)
169  {
170  $tmp_word[] = str_replace('\"','',$word);
171  }
172  return $tmp_word ? $tmp_word : array();
173  }
174  }
175 
177  {
178  $counter = 0;
179  $tmp_str = "";
180  foreach($this->getQuotedWords(true) as $word) {
181  if($counter++)
182  {
183  $tmp_str .= (" ".strtoupper($this->getCombination())." ");
184  }
185  $tmp_str .= $word;
186  }
187  return $tmp_str;
188  }
189  function parse()
190  {
191  $this->words = array();
192 
193  #if(!strlen($this->getQueryString()))
194  #{
195  # return false;
196  #}
197 
198  $words = explode(' ',trim($this->getQueryString()));
199  foreach($words as $word)
200  {
201  if(!strlen(trim($word)))
202  {
203  continue;
204  }
205 
206  if(strlen(trim($word)) < $this->getMinWordLength())
207  {
208  $this->setMessage(sprintf($this->lng->txt('search_minimum_info'), $this->getMinWordLength()));
209  continue;
210  }
211  $this->words[] = ilUtil::prepareDBString($word);
212  }
213 
214  $fullstr = trim($this->getQueryString());
215  if (!in_array($fullstr, $this->words))
216  {
217  $this->words[] = ilUtil::prepareDBString($fullstr);
218  }
219 
220  if(!$this->getAllowedWildcards())
221  {
222  // #14768
223  foreach($this->words as $idx => $word)
224  {
225  if(!stristr($word, '\\'))
226  {
227  $word = str_replace('%', '\%', $word);
228  $word = str_replace('_', '\_', $word);
229  }
230  $this->words[$idx] = $word;
231  }
232  }
233 
234  // Parse strings like && 'A "B C D" E' as 'A' && 'B C D' && 'E'
235  // Can be used in LIKE search or fulltext search > MYSQL 4.0
236  $this->__parseQuotation();
237 
238  return true;
239  }
240 
241  function __parseQuotation()
242  {
243  if(!strlen($this->getQueryString()))
244  {
245  $this->quoted_words[] = '';
246  return false;
247  }
248  $query_str = $this->getQueryString();
249  while(preg_match("/\".*?\"/",$query_str,$matches))
250  {
251  $query_str = str_replace($matches[0],'',$query_str);
252  $this->quoted_words[] = ilUtil::prepareDBString($matches[0]);
253  }
254 
255  // Parse the rest
256  $words = explode(' ',trim($query_str));
257  foreach($words as $word)
258  {
259  if(!strlen(trim($word)))
260  {
261  continue;
262  }
263  $this->quoted_words[] = ilUtil::prepareDBString($word);
264  }
265 
266  if(!$this->getAllowedWildcards())
267  {
268  // #14768
269  foreach($this->quoted_words as $idx => $word)
270  {
271  if(!stristr($word, '\\'))
272  {
273  $word = str_replace('%', '\%', $word);
274  $word = str_replace('_', '\_', $word);
275  }
276  $this->quoted_words[$idx] = $word;
277  }
278  }
279  }
280 
281  function validate()
282  {
283  // Words with less than 3 characters
284  if(strlen($this->getMessage()))
285  {
286  return false;
287  }
288  // No search string given
289  if($this->getMinWordLength() and !count($this->getWords()))
290  {
291  $this->setMessage($this->lng->txt('msg_no_search_string'));
292  return false;
293  }
294  // No search string given
295  if($this->getGlobalMinLength() and strlen(str_replace('"', '', $this->getQueryString())) < $this->getGlobalMinLength())
296  {
297  $this->setMessage(sprintf($this->lng->txt('search_minimum_info'), $this->getGlobalMinLength()));
298  return false;
299  }
300 
301  return true;
302  }
303 }
304 ?>