ILIAS  release_8 Revision v8.24
class.ilLuceneQueryParser.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4/*
5 +-----------------------------------------------------------------------------+
6 | ILIAS open source |
7 +-----------------------------------------------------------------------------+
8 | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
9 | |
10 | This program is free software; you can redistribute it and/or |
11 | modify it under the terms of the GNU General Public License |
12 | as published by the Free Software Foundation; either version 2 |
13 | of the License, or (at your option) any later version. |
14 | |
15 | This program is distributed in the hope that it will be useful, |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | GNU General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU General Public License |
21 | along with this program; if not, write to the Free Software |
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 +-----------------------------------------------------------------------------+
24*/
25
26
36{
37 protected string $query_string;
38 protected string $parsed_query = '';
39
40
45 public function __construct($a_query_string)
46 {
47 $this->query_string = $a_query_string;
48 }
49
54 public function parse(): void
55 {
56 $this->parsed_query = (string) preg_replace_callback(
57 '/(owner:)\s?([A-Za-z0-9_\.\+\*\@!\$\%\~\-]+)/',
58 array($this,'replaceOwnerCallback'),
60 );
61 }
62
66 public function parseAutoWildcard(): void
67 {
68 $this->parsed_query = trim($this->query_string);
69 if (stristr($this->parsed_query, '*')) {
70 return;
71 }
72 if (substr($this->parsed_query, -1) !== '"') {
73 $this->parsed_query .= '*';
74 }
75 }
76
77 public function getQuery(): string
78 {
80 }
81
85 protected function replaceOwnerCallback(array $matches): string
86 {
87 if (isset($matches[2])) {
88 if ($usr_id = ilObjUser::_loginExists($matches[2])) {
89 return $matches[1] . $usr_id;
90 }
91 }
92 return $matches[0];
93 }
94
95
100 public static function validateQuery($a_query): bool
101 {
102 #ilLuceneQueryParser::checkAllowedCharacters($a_query);
103 #ilLuceneQueryParser::checkAsterisk($a_query);
104 #ilLuceneQueryParser::checkAmpersands($a_query);
107 #ilLuceneQueryParser::checkExclamationMark($a_query);
108 #ilLuceneQueryParser::checkQuestionMark($a_query);
110 #ilLuceneQueryParser::checkPlusMinus($a_query);
111 #ilLuceneQueryParser::checkANDORNOT($a_query);
113 #ilLuceneQueryParser::checkColon($a_query);
114 return true;
115 }
116
121 protected static function checkAllowedCharacters(string $query): bool
122 {
123 if (preg_match('/[^\pL0-9_+\-:.()\"*?&§€|!{}\[\]\^~\\@#\/$%\'= ]/u', $query) != 0) {
124 throw new ilLuceneQueryParserException('lucene_err_allowed_characters');
125 }
126 return true;
127 }
128
133 protected static function checkAsterisk(string $query): bool
134 {
135 if (preg_match('/^[\*]*$|[\s]\*|^\*[^\s]/', $query) != 0) {
136 throw new ilLuceneQueryParserException('lucene_err_asterisk');
137 }
138 return true;
139 }
140
145 protected static function checkAmpersands(string $query): bool
146 {
147 if (preg_match('/[&]{2}/', $query) > 0) {
148 if (preg_match('/^([\pL0-9_+\-:.()\"*?&|!{}\[\]\^~\\@#\/$%\'=]+( && )?[\pL0-9_+\-:.()\"*?|!{}\[\]\^~\\@#\/$%\'=]+[ ]*)+$/u', $query) == 0) {
149 throw new ilLuceneQueryParserException('lucene_err_ampersand');
150 }
151 }
152 return true;
153 }
154
159 protected static function checkCaret(string $query): bool
160 {
161 if (preg_match('/[^\\\]\^([^\s]*[^0-9.]+)|[^\\\]\^$/', $query) != 0) {
162 throw new ilLuceneQueryParserException('lucene_err_caret');
163 }
164 return true;
165 }
166
171 protected static function checkSquiggle(string $query): bool
172 {
173 if (preg_match('/[^\\\]*~[^\s]*[^0-9\s]+/', $query, $matches) != 0) {
174 throw new ilLuceneQueryParserException('lucene_err_squiggle');
175 }
176 return true;
177 }
178
183 protected static function checkExclamationMark(string $query): bool
184 {
185 if (preg_match('/^[^!]*$|^([\pL0-9_+\-:.()\"*?&|!{}\[\]\^~\\@#\/$%\'=]+( ! )?[\pL0-9_+\-:.()\"*?&|!{}\[\]\^~\\@#\/$%\'=]+[ ]*)+$/u', $query, $matches) == 0) {
186 throw new ilLuceneQueryParserException('lucene_err_exclamation_mark');
187 }
188 return true;
189 }
190
195 protected static function checkQuestionMark(string $query): bool
196 {
197 if (preg_match('/^(\?)|([^\pL0-9_+\-:.()\"*?&|!{}\[\]\^~\\@#\/$%\'=]\?+)/u', $query, $matches) != 0) {
198 throw new ilLuceneQueryParserException('lucene_err_question_mark');
199 }
200 return true;
201 }
202
207 protected static function checkParenthesis(string $a_query): bool
208 {
209 $hasLft = false;
210 $hasRgt = false;
211
212 $matchLft = 0;
213 $matchRgt = 0;
214
215 $tmp = array();
216
217 if (($matchLft = preg_match_all('/[(]/', $a_query, $tmp)) > 0) {
218 $hasLft = true;
219 }
220 if (($matchRgt = preg_match_all('/[)]/', $a_query, $tmp)) > 0) {
221 $hasRgt = true;
222 }
223
224 if (!$hasLft || !$hasRgt) {
225 return true;
226 }
227
228
229 if (($hasLft && !$hasRgt) || ($hasRgt && !$hasLft)) {
230 throw new ilLuceneQueryParserException('lucene_err_parenthesis_not_closed');
231 }
232
233 if ($matchLft !== $matchRgt) {
234 throw new ilLuceneQueryParserException('lucene_err_parenthesis_not_closed');
235 }
236
237 if (preg_match('/\‍(\s*\‍)/', $a_query) > 0) {
238 throw new ilLuceneQueryParserException('lucene_err_parenthesis_empty');
239 }
240 return true;
241 }
242
248 protected static function checkPlusMinus(string $a_query): bool
249 {
250 if (preg_match('/^[^\n+\-]*$|^([+-]?\s*[\pL0-9_:.()\"*?&|!{}\[\]\^~\\@#\/$%\'=]+[ ]?)+$/u', $a_query) == 0) {
251 throw new ilLuceneQueryParserException('lucene_err_plus_minus');
252 }
253 return true;
254 }
255
261 protected static function checkANDORNOT(string $a_query): bool
262 {
263 if (preg_match('/^([\pL0-9_+\-:.()\"*?&|!{}\[\]\^~\\@\/#$%\'=]+\s*((AND )|(OR )|(AND NOT )|(NOT ))?[\pL0-9_+\-:.()\"*?&|!{}\[\]\^~\\@\/#$%\'=]+[ ]*)+$/u', $a_query) == 0) {
264 throw new ilLuceneQueryParserException('lucene_err_and_or_not');
265 }
266 return true;
267 }
268
274 protected static function checkQuotes(string $a_query): bool
275 {
276 $matches = preg_match_all('/"/', $a_query, $tmp);
277
278 if ($matches == 0) {
279 return true;
280 }
281
282 if (($matches % 2) > 0) {
283 throw new ilLuceneQueryParserException('lucene_err_quotes');
284 }
285
286 if (preg_match('/"\s*"/', $a_query) > 0) {
287 throw new ilLuceneQueryParserException('lucene_err_quotes_not_empty');
288 }
289 return true;
290 }
291
292
297 protected static function checkColon(string $a_query): bool
298 {
299 if (preg_match('/[^\\\\s]:[\s]|[^\\\\s]:$|[\s][^\\]?:|^[^\\\\s]?:/', $a_query) != 0) {
300 throw new ilLuceneQueryParserException('lucene_err_colon');
301 }
302 return true;
303 }
304}
static checkParenthesis(string $a_query)
Check parenthesis.
static checkAllowedCharacters(string $query)
Check allowed characters.
static checkPlusMinus(string $a_query)
Check plus minus.
__construct($a_query_string)
Constructor.
static checkSquiggle(string $query)
Check squiggles.
static checkAmpersands(string $query)
Check ampersands.
static checkColon(string $a_query)
Check colon.
static checkExclamationMark(string $query)
Check exclamation marks (replacement for NOT)
static checkANDORNOT(string $a_query)
Check AND OR NOT.
replaceOwnerCallback(array $matches)
Replace owner callback (preg_replace_callback)
static checkQuotes(string $a_query)
Check quotes.
parseAutoWildcard()
Append asterisk for remote search from global search form field.
static checkQuestionMark(string $query)
Check question mark (wild card single character)
static checkCaret(string $query)
Check carets.
static checkAsterisk(string $query)
Check asterisk.
static _loginExists(string $a_login, int $a_user_id=0)
check if a login name already exists You may exclude a user from the check by giving his user id as 2...
$query