ILIAS  Release_4_0_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 
44 
46  var $quoted_words = array();
47  var $message; // Translated error message
48  var $combination; // combiniation of search words e.g 'and' or 'or'
49  protected $settings = null;
50 
55  function ilQueryParser($a_query_str)
56  {
57  global $lng;
58 
59  define(MIN_WORD_LENGTH,3);
60 
61  $this->lng =& $lng;
62 
63  $this->query_str = $a_query_str;
64  $this->message = '';
65 
66  include_once './Services/Search/classes/class.ilSearchSettings.php';
67  $this->settings = ilSearchSettings::getInstance();
68 
69  if(!$this->setMinWordLength(1))
70  {
71  $this->setMinWordLength(MIN_WORD_LENGTH);
72  }
73  }
74 
75  function setMinWordLength($a_length,$a_force = false)
76  {
77  // Due to a bug in mysql fulltext search queries with min_word_legth < 3
78  // might freeze the system.
79  // Thus min_word_length cannot be set to values < 3 if the mysql fulltext is used.
80  if(!$a_force and $this->settings->enabledIndex() and $a_length < 3)
81  {
82  $GLOBALS['ilLog']->write(__METHOD__.': Disabled min word length');
83  return false;
84  }
85  $this->min_word_length = $a_length;
86  return true;
87  }
88  function getMinWordLength()
89  {
91  }
92 
93  function setMessage($a_msg)
94  {
95  $this->message = $a_msg;
96  }
97  function getMessage()
98  {
99  return $this->message;
100  }
101  function appendMessage($a_msg)
102  {
103  if(strlen($this->getMessage()))
104  {
105  $this->message .= '<br />';
106  }
107  $this->message .= $a_msg;
108  }
109 
110  function setCombination($a_combination)
111  {
112  $this->combination = $a_combination;
113  }
114  function getCombination()
115  {
116  return $this->combination;
117  }
118 
119  function getQueryString()
120  {
121  return trim($this->query_str);
122  }
123  function getWords()
124  {
125  return $this->words ? $this->words : array();
126  }
127 
128  function getQuotedWords($with_quotation = false)
129  {
130  if($with_quotation)
131  {
132  return $this->quoted_words ? $this->quoted_words : array();
133  }
134  else
135  {
136  foreach($this->quoted_words as $word)
137  {
138  $tmp_word[] = str_replace('\"','',$word);
139  }
140  return $tmp_word ? $tmp_word : array();
141  }
142  }
143 
145  {
146  $counter = 0;
147  $tmp_str = "";
148  foreach($this->getQuotedWords(true) as $word) {
149  if($counter++)
150  {
151  $tmp_str .= (" ".strtoupper($this->getCombination())." ");
152  }
153  $tmp_str .= $word;
154  }
155  return $tmp_str;
156  }
157  function parse()
158  {
159  $this->words = array();
160 
161  #if(!strlen($this->getQueryString()))
162  #{
163  # return false;
164  #}
165 
166  $words = explode(' ',trim($this->getQueryString()));
167  foreach($words as $word)
168  {
169  if(!strlen(trim($word)))
170  {
171  continue;
172  }
173 
174  if(strlen(trim($word)) < $this->getMinWordLength())
175  {
176  $this->setMessage($this->lng->txt('search_minimum_three'));
177  continue;
178  }
179  $this->words[] = ilUtil::prepareDBString($word);
180  }
181 
182  $fullstr = trim($this->getQueryString());
183  if (!in_array($fullstr, $this->words))
184  {
185  $this->words[] = ilUtil::prepareDBString($fullstr);
186  }
187 
188  // Parse strings like && 'A "B C D" E' as 'A' && 'B C D' && 'E'
189  // Can be used in LIKE search or fulltext search > MYSQL 4.0
190  $this->__parseQuotation();
191 
192  return true;
193  }
194 
195  function __parseQuotation()
196  {
197  if(!strlen($this->getQueryString()))
198  {
199  $this->quoted_words[] = '';
200  return false;
201  }
202  $query_str = $this->getQueryString();
203  while(preg_match("/\".*?\"/",$query_str,$matches))
204  {
205  $query_str = str_replace($matches[0],'',$query_str);
206  $this->quoted_words[] = ilUtil::prepareDBString($matches[0]);
207  }
208 
209  // Parse the rest
210  $words = explode(' ',trim($query_str));
211  foreach($words as $word)
212  {
213  if(!strlen(trim($word)))
214  {
215  continue;
216  }
217  $this->quoted_words[] = ilUtil::prepareDBString($word);
218  }
219 
220  }
221 
222  function validate()
223  {
224  // Words with less than 3 characters
225  if(strlen($this->getMessage()))
226  {
227  return false;
228  }
229  // No search string given
230  if($this->getMinWordLength() and !count($this->getWords()))
231  {
232  $this->setMessage($this->lng->txt('msg_no_search_string'));
233  return false;
234  }
235 
236  return true;
237  }
238 }
239 ?>