ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 {
43  const MIN_WORD_LENGTH = 3;
44 
45  public $lng = null;
46 
47  public $min_word_length = 0;
48  public $global_min_length = null;
49 
50  public $query_str;
51  public $quoted_words = array();
52  public $message; // Translated error message
53  public $combination; // combiniation of search words e.g 'and' or 'or'
54  protected $settings = null;
55  protected $wildcards_allowed; // [bool]
56 
61  public 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)) {
77  }
78 
79  $this->setAllowedWildcards(false);
80  }
81 
82  public function setMinWordLength($a_length, $a_force = false)
83  {
84  // Due to a bug in mysql fulltext search queries with min_word_legth < 3
85  // might freeze the system.
86  // Thus min_word_length cannot be set to values < 3 if the mysql fulltext is used.
87  if (!$a_force and $this->settings->enabledIndex() and $a_length < 3) {
88  ilLoggerFactory::getLogger('src')->debug('Disabled min word length');
89  return false;
90  }
91  $this->min_word_length = $a_length;
92  return true;
93  }
94  public function getMinWordLength()
95  {
97  }
98 
99  public function setGlobalMinLength($a_value)
100  {
101  if ($a_value !== null) {
102  $a_value = (int) $a_value;
103  if ($a_value < 1) {
104  return;
105  }
106  }
107  $this->global_min_length = $a_value;
108  }
109 
110  public function getGlobalMinLength()
111  {
113  }
114 
115  public function setAllowedWildcards($a_value)
116  {
117  $this->wildcards_allowed = (bool) $a_value;
118  }
119 
120  public function getAllowedWildcards()
121  {
123  }
124 
125  public function setMessage($a_msg)
126  {
127  $this->message = $a_msg;
128  }
129  public function getMessage()
130  {
131  return $this->message;
132  }
133  public function appendMessage($a_msg)
134  {
135  if (strlen($this->getMessage())) {
136  $this->message .= '<br />';
137  }
138  $this->message .= $a_msg;
139  }
140 
141  public function setCombination($a_combination)
142  {
143  $this->combination = $a_combination;
144  }
145  public function getCombination()
146  {
147  return $this->combination;
148  }
149 
150  public function getQueryString()
151  {
152  return trim($this->query_str);
153  }
154  public function getWords()
155  {
156  return $this->words ? $this->words : array();
157  }
158 
159  public function getQuotedWords($with_quotation = false)
160  {
161  if ($with_quotation) {
162  return $this->quoted_words ? $this->quoted_words : array();
163  } else {
164  foreach ($this->quoted_words as $word) {
165  $tmp_word[] = str_replace('\"', '', $word);
166  }
167  return $tmp_word ? $tmp_word : array();
168  }
169  }
170 
171  public function getLuceneQueryString()
172  {
173  $counter = 0;
174  $tmp_str = "";
175  foreach ($this->getQuotedWords(true) as $word) {
176  if ($counter++) {
177  $tmp_str .= (" " . strtoupper($this->getCombination()) . " ");
178  }
179  $tmp_str .= $word;
180  }
181  return $tmp_str;
182  }
183  public function parse()
184  {
185  $this->words = array();
186 
187  #if(!strlen($this->getQueryString()))
188  #{
189  # return false;
190  #}
191 
192  $words = explode(' ', trim($this->getQueryString()));
193  foreach ($words as $word) {
194  if (!strlen(trim($word))) {
195  continue;
196  }
197 
198  if (strlen(trim($word)) < $this->getMinWordLength()) {
199  $this->setMessage(sprintf($this->lng->txt('search_minimum_info'), $this->getMinWordLength()));
200  continue;
201  }
202 
203  $this->words[] = ilUtil::prepareDBString($word);
204  }
205 
206  $fullstr = trim($this->getQueryString());
207  if (!in_array($fullstr, $this->words)) {
208  $this->words[] = ilUtil::prepareDBString($fullstr);
209  }
210 
211  if (!$this->getAllowedWildcards()) {
212  // #14768
213  foreach ($this->words as $idx => $word) {
214  if (!stristr($word, '\\')) {
215  $word = str_replace('%', '\%', $word);
216  $word = str_replace('_', '\_', $word);
217  }
218  $this->words[$idx] = $word;
219  }
220  }
221 
222  // Parse strings like && 'A "B C D" E' as 'A' && 'B C D' && 'E'
223  // Can be used in LIKE search or fulltext search > MYSQL 4.0
224  $this->__parseQuotation();
225 
226  return true;
227  }
228 
229  public function __parseQuotation()
230  {
231  if (!strlen($this->getQueryString())) {
232  $this->quoted_words[] = '';
233  return false;
234  }
235  $query_str = $this->getQueryString();
236  while (preg_match("/\".*?\"/", $query_str, $matches)) {
237  $query_str = str_replace($matches[0], '', $query_str);
238  $this->quoted_words[] = ilUtil::prepareDBString($matches[0]);
239  }
240 
241  // Parse the rest
242  $words = explode(' ', trim($query_str));
243  foreach ($words as $word) {
244  if (!strlen(trim($word))) {
245  continue;
246  }
247 
248  $this->quoted_words[] = ilUtil::prepareDBString($word);
249  }
250 
251  if (!$this->getAllowedWildcards()) {
252  // #14768
253  foreach ($this->quoted_words as $idx => $word) {
254  if (!stristr($word, '\\')) {
255  $word = str_replace('%', '\%', $word);
256  $word = str_replace('_', '\_', $word);
257  }
258  $this->quoted_words[$idx] = $word;
259  }
260  }
261  }
262 
263  public function validate()
264  {
265  // Words with less than 3 characters
266  if (strlen($this->getMessage())) {
267  return false;
268  }
269  // No search string given
270  if ($this->getMinWordLength() and !count($this->getWords())) {
271  $this->setMessage($this->lng->txt('msg_no_search_string'));
272  return false;
273  }
274  // No search string given
275  if ($this->getGlobalMinLength() and strlen(str_replace('"', '', $this->getQueryString())) < $this->getGlobalMinLength()) {
276  $this->setMessage(sprintf($this->lng->txt('search_minimum_info'), $this->getGlobalMinLength()));
277  return false;
278  }
279 
280  return true;
281  }
282 }
getQuotedWords($with_quotation=false)
$counter
const MIN_WORD_LENGTH
Minimum of characters required for search.
__construct($a_query_str)
Constructor public.
setGlobalMinLength($a_value)
Create styles array
The data for the language used.
settings()
Definition: settings.php:2
static prepareDBString($a_str)
prepare a string for db writing (insert/update)
setCombination($a_combination)
static getLogger($a_component_id)
Get component logger.
setMinWordLength($a_length, $a_force=false)
setAllowedWildcards($a_value)