ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilLuceneQueryParser Class Reference

Lucene query parser. More...

+ Inheritance diagram for ilLuceneQueryParser:
+ Collaboration diagram for ilLuceneQueryParser:

Public Member Functions

 __construct ($a_query_string)
 Constructor. More...
 
 parse ()
 parse query string More...
 
 parseAutoWildcard ()
 Append asterisk for remote search from global search form field. More...
 
 getQuery ()
 

Static Public Member Functions

static validateQuery ($a_query)
 

Protected Member Functions

 replaceOwnerCallback (array $matches)
 Replace owner callback (preg_replace_callback) More...
 

Static Protected Member Functions

static checkAllowedCharacters (string $query)
 Check allowed characters. More...
 
static checkAsterisk (string $query)
 Check asterisk. More...
 
static checkAmpersands (string $query)
 Check ampersands. More...
 
static checkCaret (string $query)
 Check carets. More...
 
static checkSquiggle (string $query)
 Check squiggles. More...
 
static checkExclamationMark (string $query)
 Check exclamation marks (replacement for NOT) More...
 
static checkQuestionMark (string $query)
 Check question mark (wild card single character) More...
 
static checkParenthesis (string $a_query)
 Check parenthesis. More...
 
static checkPlusMinus (string $a_query)
 Check plus minus. More...
 
static checkANDORNOT (string $a_query)
 Check AND OR NOT. More...
 
static checkQuotes (string $a_query)
 Check quotes. More...
 
static checkColon (string $a_query)
 Check colon. More...
 

Protected Attributes

string $query_string
 
string $parsed_query = ''
 

Detailed Description

Lucene query parser.

Author
Stefan Meyer meyer.nosp@m.@lei.nosp@m.fos.c.nosp@m.om

Definition at line 28 of file class.ilLuceneQueryParser.php.

Constructor & Destructor Documentation

◆ __construct()

ilLuceneQueryParser::__construct (   $a_query_string)

Constructor.

Parameters
stringquery string

Definition at line 38 of file class.ilLuceneQueryParser.php.

39 {
40 $this->query_string = $a_query_string;
41 }

Member Function Documentation

◆ checkAllowedCharacters()

static ilLuceneQueryParser::checkAllowedCharacters ( string  $query)
staticprotected

Check allowed characters.

Exceptions
ilLuceneQueryParserException

Definition at line 114 of file class.ilLuceneQueryParser.php.

114 : bool
115 {
116 if (preg_match('/[^\pL0-9_+\-:.()\"*?&§€|!{}\[\]\^~\\@#\/$%\'= ]/u', $query) != 0) {
117 throw new ilLuceneQueryParserException('lucene_err_allowed_characters');
118 }
119 return true;
120 }

◆ checkAmpersands()

static ilLuceneQueryParser::checkAmpersands ( string  $query)
staticprotected

Check ampersands.

Exceptions
ilLuceneQueryParserException

Definition at line 138 of file class.ilLuceneQueryParser.php.

138 : bool
139 {
140 if (preg_match('/[&]{2}/', $query) > 0) {
141 if (preg_match('/^([\pL0-9_+\-:.()\"*?&|!{}\[\]\^~\\@#\/$%\'=]+( && )?[\pL0-9_+\-:.()\"*?|!{}\[\]\^~\\@#\/$%\'=]+[ ]*)+$/u', $query) == 0) {
142 throw new ilLuceneQueryParserException('lucene_err_ampersand');
143 }
144 }
145 return true;
146 }

◆ checkANDORNOT()

static ilLuceneQueryParser::checkANDORNOT ( string  $a_query)
staticprotected

Check AND OR NOT.

Exceptions
ilLuceneQueryParserException

Definition at line 254 of file class.ilLuceneQueryParser.php.

254 : bool
255 {
256 if (preg_match('/^([\pL0-9_+\-:.()\"*?&|!{}\[\]\^~\\@\/#$%\'=]+\s*((AND )|(OR )|(AND NOT )|(NOT ))?[\pL0-9_+\-:.()\"*?&|!{}\[\]\^~\\@\/#$%\'=]+[ ]*)+$/u', $a_query) == 0) {
257 throw new ilLuceneQueryParserException('lucene_err_and_or_not');
258 }
259 return true;
260 }

◆ checkAsterisk()

static ilLuceneQueryParser::checkAsterisk ( string  $query)
staticprotected

Check asterisk.

Exceptions
ilLuceneQueryParserException

Definition at line 126 of file class.ilLuceneQueryParser.php.

126 : bool
127 {
128 if (preg_match('/^[\*]*$|[\s]\*|^\*[^\s]/', $query) != 0) {
129 throw new ilLuceneQueryParserException('lucene_err_asterisk');
130 }
131 return true;
132 }

◆ checkCaret()

static ilLuceneQueryParser::checkCaret ( string  $query)
staticprotected

Check carets.

Exceptions
ilLuceneQueryParserException

Definition at line 152 of file class.ilLuceneQueryParser.php.

152 : bool
153 {
154 if (preg_match('/[^\\\]\^([^\s]*[^0-9.]+)|[^\\\]\^$/', $query) != 0) {
155 throw new ilLuceneQueryParserException('lucene_err_caret');
156 }
157 return true;
158 }

Referenced by validateQuery().

+ Here is the caller graph for this function:

◆ checkColon()

static ilLuceneQueryParser::checkColon ( string  $a_query)
staticprotected

Check colon.

Exceptions
ilLuceneQueryParserException

Definition at line 290 of file class.ilLuceneQueryParser.php.

290 : bool
291 {
292 if (preg_match('/[^\\\\s]:[\s]|[^\\\\s]:$|[\s][^\\]?:|^[^\\\\s]?:/', $a_query) != 0) {
293 throw new ilLuceneQueryParserException('lucene_err_colon');
294 }
295 return true;
296 }

◆ checkExclamationMark()

static ilLuceneQueryParser::checkExclamationMark ( string  $query)
staticprotected

Check exclamation marks (replacement for NOT)

Exceptions
ilLuceneQueryParserException

Definition at line 176 of file class.ilLuceneQueryParser.php.

176 : bool
177 {
178 if (preg_match('/^[^!]*$|^([\pL0-9_+\-:.()\"*?&|!{}\[\]\^~\\@#\/$%\'=]+( ! )?[\pL0-9_+\-:.()\"*?&|!{}\[\]\^~\\@#\/$%\'=]+[ ]*)+$/u', $query, $matches) == 0) {
179 throw new ilLuceneQueryParserException('lucene_err_exclamation_mark');
180 }
181 return true;
182 }

◆ checkParenthesis()

static ilLuceneQueryParser::checkParenthesis ( string  $a_query)
staticprotected

Check parenthesis.

Exceptions
ilLuceneQueryParserException

Definition at line 200 of file class.ilLuceneQueryParser.php.

200 : bool
201 {
202 $hasLft = false;
203 $hasRgt = false;
204
205 $matchLft = 0;
206 $matchRgt = 0;
207
208 $tmp = array();
209
210 if (($matchLft = preg_match_all('/[(]/', $a_query, $tmp)) > 0) {
211 $hasLft = true;
212 }
213 if (($matchRgt = preg_match_all('/[)]/', $a_query, $tmp)) > 0) {
214 $hasRgt = true;
215 }
216
217 if (!$hasLft || !$hasRgt) {
218 return true;
219 }
220
221
222 if (($hasLft && !$hasRgt) || ($hasRgt && !$hasLft)) {
223 throw new ilLuceneQueryParserException('lucene_err_parenthesis_not_closed');
224 }
225
226 if ($matchLft !== $matchRgt) {
227 throw new ilLuceneQueryParserException('lucene_err_parenthesis_not_closed');
228 }
229
230 if (preg_match('/\‍(\s*\‍)/', $a_query) > 0) {
231 throw new ilLuceneQueryParserException('lucene_err_parenthesis_empty');
232 }
233 return true;
234 }

Referenced by validateQuery().

+ Here is the caller graph for this function:

◆ checkPlusMinus()

static ilLuceneQueryParser::checkPlusMinus ( string  $a_query)
staticprotected

Check plus minus.

Exceptions
ilLuceneQueryParserException

Definition at line 241 of file class.ilLuceneQueryParser.php.

241 : bool
242 {
243 if (preg_match('/^[^\n+\-]*$|^([+-]?\s*[\pL0-9_:.()\"*?&|!{}\[\]\^~\\@#\/$%\'=]+[ ]?)+$/u', $a_query) == 0) {
244 throw new ilLuceneQueryParserException('lucene_err_plus_minus');
245 }
246 return true;
247 }

◆ checkQuestionMark()

static ilLuceneQueryParser::checkQuestionMark ( string  $query)
staticprotected

Check question mark (wild card single character)

Exceptions
ilLuceneQueryParserException

Definition at line 188 of file class.ilLuceneQueryParser.php.

188 : bool
189 {
190 if (preg_match('/^(\?)|([^\pL0-9_+\-:.()\"*?&|!{}\[\]\^~\\@#\/$%\'=]\?+)/u', $query, $matches) != 0) {
191 throw new ilLuceneQueryParserException('lucene_err_question_mark');
192 }
193 return true;
194 }

◆ checkQuotes()

static ilLuceneQueryParser::checkQuotes ( string  $a_query)
staticprotected

Check quotes.

Exceptions
ilLuceneQueryParserException

Definition at line 267 of file class.ilLuceneQueryParser.php.

267 : bool
268 {
269 $matches = preg_match_all('/"/', $a_query, $tmp);
270
271 if ($matches == 0) {
272 return true;
273 }
274
275 if (($matches % 2) > 0) {
276 throw new ilLuceneQueryParserException('lucene_err_quotes');
277 }
278
279 if (preg_match('/"\s*"/', $a_query) > 0) {
280 throw new ilLuceneQueryParserException('lucene_err_quotes_not_empty');
281 }
282 return true;
283 }

Referenced by validateQuery().

+ Here is the caller graph for this function:

◆ checkSquiggle()

static ilLuceneQueryParser::checkSquiggle ( string  $query)
staticprotected

Check squiggles.

Exceptions
ilLuceneQueryParserException

Definition at line 164 of file class.ilLuceneQueryParser.php.

164 : bool
165 {
166 if (preg_match('/[^\\\]*~[^\s]*[^0-9\s]+/', $query, $matches) != 0) {
167 throw new ilLuceneQueryParserException('lucene_err_squiggle');
168 }
169 return true;
170 }

Referenced by validateQuery().

+ Here is the caller graph for this function:

◆ getQuery()

ilLuceneQueryParser::getQuery ( )

Definition at line 70 of file class.ilLuceneQueryParser.php.

70 : string
71 {
73 }

References $parsed_query.

◆ parse()

ilLuceneQueryParser::parse ( )

parse query string

Returns
void

Reimplemented in ilMailLuceneQueryParser.

Definition at line 47 of file class.ilLuceneQueryParser.php.

47 : void
48 {
49 $this->parsed_query = (string) preg_replace_callback(
50 '/(owner:)\s?([A-Za-z0-9_\.\+\*\@!\$\%\~\-]+)/',
51 array($this,'replaceOwnerCallback'),
53 );
54 }

References $query_string.

◆ parseAutoWildcard()

ilLuceneQueryParser::parseAutoWildcard ( )

Append asterisk for remote search from global search form field.

Definition at line 59 of file class.ilLuceneQueryParser.php.

59 : void
60 {
61 $this->parsed_query = trim($this->query_string);
62 if (stristr($this->parsed_query, '*')) {
63 return;
64 }
65 if (substr($this->parsed_query, -1) !== '"') {
66 $this->parsed_query .= '*';
67 }
68 }

◆ replaceOwnerCallback()

ilLuceneQueryParser::replaceOwnerCallback ( array  $matches)
protected

Replace owner callback (preg_replace_callback)

Definition at line 78 of file class.ilLuceneQueryParser.php.

78 : string
79 {
80 if (isset($matches[2])) {
81 if ($usr_id = ilObjUser::_loginExists($matches[2])) {
82 return $matches[1] . $usr_id;
83 }
84 }
85 return $matches[0];
86 }
static _loginExists(string $a_login, int $a_user_id=0)

References ilObjUser\_loginExists().

+ Here is the call graph for this function:

◆ validateQuery()

static ilLuceneQueryParser::validateQuery (   $a_query)
static
Exceptions
ilLuceneQueryParserException
Todo:
add multi byte query validation.

Definition at line 93 of file class.ilLuceneQueryParser.php.

93 : bool
94 {
95 #ilLuceneQueryParser::checkAllowedCharacters($a_query);
96 #ilLuceneQueryParser::checkAsterisk($a_query);
97 #ilLuceneQueryParser::checkAmpersands($a_query);
100 #ilLuceneQueryParser::checkExclamationMark($a_query);
101 #ilLuceneQueryParser::checkQuestionMark($a_query);
103 #ilLuceneQueryParser::checkPlusMinus($a_query);
104 #ilLuceneQueryParser::checkANDORNOT($a_query);
106 #ilLuceneQueryParser::checkColon($a_query);
107 return true;
108 }
static checkParenthesis(string $a_query)
Check parenthesis.
static checkSquiggle(string $query)
Check squiggles.
static checkQuotes(string $a_query)
Check quotes.
static checkCaret(string $query)
Check carets.

References checkCaret(), checkParenthesis(), checkQuotes(), and checkSquiggle().

Referenced by ilLuceneQueryInputGUI\checkInput(), ilSearchLuceneQueryParserTest\testFailedParenthesis(), and ilSearchLuceneQueryParserTest\testValidation().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $parsed_query

string ilLuceneQueryParser::$parsed_query = ''
protected

Definition at line 31 of file class.ilLuceneQueryParser.php.

Referenced by getQuery().

◆ $query_string

string ilLuceneQueryParser::$query_string
protected

Definition at line 30 of file class.ilLuceneQueryParser.php.

Referenced by ilMailLuceneQueryParser\parse(), and parse().


The documentation for this class was generated from the following file: