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