ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
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 35 of file class.ilLuceneQueryParser.php.

Constructor & Destructor Documentation

◆ __construct()

ilLuceneQueryParser::__construct (   $a_query_string)

Constructor.

Parameters
stringquery string

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

46  {
47  $this->query_string = $a_query_string;
48  }

Member Function Documentation

◆ checkAllowedCharacters()

static ilLuceneQueryParser::checkAllowedCharacters ( string  $query)
staticprotected

Check allowed characters.

Exceptions
ilLuceneQueryParserException

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

121  : bool
122  {
123  if (preg_match('/[^\pL0-9_+\-:.()\"*?&§€|!{}\[\]\^~\\@#\/$%\'= ]/u', $query) != 0) {
124  throw new ilLuceneQueryParserException('lucene_err_allowed_characters');
125  }
126  return true;
127  }
$query

◆ checkAmpersands()

static ilLuceneQueryParser::checkAmpersands ( string  $query)
staticprotected

Check ampersands.

Exceptions
ilLuceneQueryParserException

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

145  : 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  }
$query

◆ checkANDORNOT()

static ilLuceneQueryParser::checkANDORNOT ( string  $a_query)
staticprotected

Check AND OR NOT.

Exceptions
ilLuceneQueryParserException

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

261  : 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  }

◆ checkAsterisk()

static ilLuceneQueryParser::checkAsterisk ( string  $query)
staticprotected

Check asterisk.

Exceptions
ilLuceneQueryParserException

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

133  : bool
134  {
135  if (preg_match('/^[\*]*$|[\s]\*|^\*[^\s]/', $query) != 0) {
136  throw new ilLuceneQueryParserException('lucene_err_asterisk');
137  }
138  return true;
139  }
$query

◆ checkCaret()

static ilLuceneQueryParser::checkCaret ( string  $query)
staticprotected

Check carets.

Exceptions
ilLuceneQueryParserException

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

Referenced by validateQuery().

159  : bool
160  {
161  if (preg_match('/[^\\\]\^([^\s]*[^0-9.]+)|[^\\\]\^$/', $query) != 0) {
162  throw new ilLuceneQueryParserException('lucene_err_caret');
163  }
164  return true;
165  }
$query
+ Here is the caller graph for this function:

◆ checkColon()

static ilLuceneQueryParser::checkColon ( string  $a_query)
staticprotected

Check colon.

Exceptions
ilLuceneQueryParserException

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

297  : 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  }

◆ checkExclamationMark()

static ilLuceneQueryParser::checkExclamationMark ( string  $query)
staticprotected

Check exclamation marks (replacement for NOT)

Exceptions
ilLuceneQueryParserException

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

183  : 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  }
$query

◆ checkParenthesis()

static ilLuceneQueryParser::checkParenthesis ( string  $a_query)
staticprotected

Check parenthesis.

Exceptions
ilLuceneQueryParserException

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

Referenced by validateQuery().

207  : 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  }
+ Here is the caller graph for this function:

◆ checkPlusMinus()

static ilLuceneQueryParser::checkPlusMinus ( string  $a_query)
staticprotected

Check plus minus.

Exceptions
ilLuceneQueryParserException

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

248  : 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  }

◆ checkQuestionMark()

static ilLuceneQueryParser::checkQuestionMark ( string  $query)
staticprotected

Check question mark (wild card single character)

Exceptions
ilLuceneQueryParserException

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

195  : 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  }
$query

◆ checkQuotes()

static ilLuceneQueryParser::checkQuotes ( string  $a_query)
staticprotected

Check quotes.

Exceptions
ilLuceneQueryParserException

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

Referenced by validateQuery().

274  : 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  }
+ Here is the caller graph for this function:

◆ checkSquiggle()

static ilLuceneQueryParser::checkSquiggle ( string  $query)
staticprotected

Check squiggles.

Exceptions
ilLuceneQueryParserException

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

Referenced by validateQuery().

171  : 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  }
$query
+ Here is the caller graph for this function:

◆ getQuery()

ilLuceneQueryParser::getQuery ( )

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

References $parsed_query.

77  : string
78  {
79  return $this->parsed_query;
80  }

◆ parse()

ilLuceneQueryParser::parse ( )

parse query string

Returns
void

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

References $query_string.

Referenced by ilLuceneUserSearchGUI\performSearch(), and ilLuceneSearchGUI\showSavedResults().

54  : void
55  {
56  $this->parsed_query = (string) preg_replace_callback(
57  '/(owner:)\s?([A-Za-z0-9_\.\+\*\@!\$\%\~\-]+)/',
58  array($this,'replaceOwnerCallback'),
60  );
61  }
+ Here is the caller graph for this function:

◆ parseAutoWildcard()

ilLuceneQueryParser::parseAutoWildcard ( )

Append asterisk for remote search from global search form field.

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

66  : 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  }

◆ replaceOwnerCallback()

ilLuceneQueryParser::replaceOwnerCallback ( array  $matches)
protected

Replace owner callback (preg_replace_callback)

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

References ilObjUser\_loginExists().

85  : 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  }
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...
+ 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 100 of file class.ilLuceneQueryParser.php.

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

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

100  : 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  }
static checkQuotes(string $a_query)
Check quotes.
static checkSquiggle(string $query)
Check squiggles.
static checkCaret(string $query)
Check carets.
static checkParenthesis(string $a_query)
Check parenthesis.
+ 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 38 of file class.ilLuceneQueryParser.php.

Referenced by getQuery().

◆ $query_string

string ilLuceneQueryParser::$query_string
protected

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

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


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