3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 require_once($CFG->libdir
.'/lexer.php');
30 /** Constants for the various types of tokens */
32 define("TOKEN_USER","0");
33 define("TOKEN_META","1");
34 define("TOKEN_EXACT","2");
35 define("TOKEN_NEGATE","3");
36 define("TOKEN_STRING","4");
37 define("TOKEN_USERID","5");
38 define("TOKEN_DATEFROM","6");
39 define("TOKEN_DATETO","7");
40 define("TOKEN_INSTANCE","8");
41 define("TOKEN_TAGS","9");
44 * Class to hold token/value pairs after they're parsed.
47 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
48 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
54 public function __construct($type,$value){
56 $this->value
= $this->sanitize($value);
61 * Old syntax of class constructor. Deprecated in PHP7.
63 * @deprecated since Moodle 3.1
65 public function search_token($type, $value) {
66 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
67 self
::__construct($type, $value);
70 // Try to clean up user input to avoid potential security issues.
71 // Need to think about this some more.
73 function sanitize($userstring){
74 return htmlspecialchars($userstring);
86 * This class does the heavy lifting of lexing the search string into tokens.
87 * Using a full-blown lexer is probably overkill for this application, but
88 * might be useful for other tasks.
91 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
92 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
94 class search_lexer
extends Lexer
{
96 public function __construct(&$parser){
98 // Call parent constructor.
99 parent
::__construct($parser);
101 //Set up the state machine and pattern matches for transitions.
103 // Patterns to handle strings of the form datefrom:foo
105 // If we see the string datefrom: while in the base accept state, start
106 // parsing a username and go to the indatefrom state.
107 $this->addEntryPattern("datefrom:\S+","accept","indatefrom");
109 // Snarf everything into the username until we see whitespace, then exit
110 // back to the base accept state.
111 $this->addExitPattern("\s","indatefrom");
114 // If we see the string tags: while in the base accept state, start
115 // parsing tags and go to the intags state.
116 $this->addEntryPattern("tags:\S+","accept","intags");
118 // Snarf everything into the tags until we see whitespace, then exit
119 // back to the base accept state.
120 $this->addExitPattern("\s","intags");
122 // Patterns to handle strings of the form dateto:foo
124 // If we see the string dateto: while in the base accept state, start
125 // parsing a username and go to the indateto state.
126 $this->addEntryPattern("dateto:\S+","accept","indateto");
128 // Snarf everything into the username until we see whitespace, then exit
129 // back to the base accept state.
130 $this->addExitPattern("\s","indateto");
133 // Patterns to handle strings of the form instance:foo
135 // If we see the string instance: while in the base accept state, start
136 // parsing for instance number and go to the ininstance state.
137 $this->addEntryPattern("instance:\S+","accept","ininstance");
139 // Snarf everything into the username until we see whitespace, then exit
140 // back to the base accept state.
141 $this->addExitPattern("\s","ininstance");
144 // Patterns to handle strings of the form userid:foo
146 // If we see the string userid: while in the base accept state, start
147 // parsing a username and go to the inuserid state.
148 $this->addEntryPattern("userid:\S+","accept","inuserid");
150 // Snarf everything into the username until we see whitespace, then exit
151 // back to the base accept state.
152 $this->addExitPattern("\s","inuserid");
155 // Patterns to handle strings of the form user:foo
157 // If we see the string user: while in the base accept state, start
158 // parsing a username and go to the inusername state.
159 $this->addEntryPattern("user:\S+","accept","inusername");
161 // Snarf everything into the username until we see whitespace, then exit
162 // back to the base accept state.
163 $this->addExitPattern("\s","inusername");
166 // Patterns to handle strings of the form meta:foo
168 // If we see the string meta: while in the base accept state, start
169 // parsing a username and go to the inmeta state.
170 $this->addEntryPattern("subject:\S+","accept","inmeta");
172 // Snarf everything into the meta token until we see whitespace, then exit
173 // back to the base accept state.
174 $this->addExitPattern("\s","inmeta");
177 // Patterns to handle required exact match strings (+foo) .
179 // If we see a + sign while in the base accept state, start
180 // parsing an exact match string and enter the inrequired state
181 $this->addEntryPattern("\+\S+","accept","inrequired");
182 // When we see white space, exit back to accept state.
183 $this->addExitPattern("\s","inrequired");
185 // Handle excluded strings (-foo)
187 // If we see a - sign while in the base accept state, start
188 // parsing an excluded string and enter the inexcluded state
189 $this->addEntryPattern("\-\S+","accept","inexcluded");
190 // When we see white space, exit back to accept state.
191 $this->addExitPattern("\s","inexcluded");
194 // Patterns to handle quoted strings.
196 // If we see a quote while in the base accept state, start
197 // parsing a quoted string and enter the inquotedstring state.
198 // Grab everything until we see the closing quote.
200 $this->addEntryPattern("\"[^\"]+","accept","inquotedstring");
202 // When we see a closing quote, reenter the base accept state.
203 $this->addExitPattern("\"","inquotedstring");
205 // Patterns to handle ordinary, nonquoted words.
207 // When we see non-whitespace, snarf everything into the nonquoted word
208 // until we see whitespace again.
209 $this->addEntryPattern("\S+","accept","plainstring");
211 // Once we see whitespace, reenter the base accept state.
212 $this->addExitPattern("\s","plainstring");
217 * Old syntax of class constructor. Deprecated in PHP7.
219 * @deprecated since Moodle 3.1
221 public function search_lexer(&$parser) {
222 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
223 self
::__construct($parser);
231 * This class takes care of sticking the proper token type/value pairs into
232 * the parsed token array.
233 * Most functions in this class should only be called by the lexer, the
234 * one exception being getParseArray() which returns the result.
236 * @package moodlecore
237 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
238 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
240 class search_parser
{
243 // This function is called by the code that's interested in the result of the parse operation.
244 function get_parsed_array(){
245 return $this->tokens
;
249 * Functions below this are part of the state machine for the parse
250 * operation and should not be called directly.
253 // Base state. No output emitted.
258 // State for handling datefrom:foo constructs. Potentially emits a token.
259 function indatefrom($content){
260 if (strlen($content) < 10) { // State exit or missing parameter.
263 // Strip off the datefrom: part and add the reminder to the parsed token array
264 $param = trim(substr($content,9));
265 $this->tokens
[] = new search_token(TOKEN_DATEFROM
,$param);
269 // State for handling dateto:foo constructs. Potentially emits a token.
270 function indateto($content){
271 if (strlen($content) < 8) { // State exit or missing parameter.
274 // Strip off the dateto: part and add the reminder to the parsed token array
275 $param = trim(substr($content,7));
276 $this->tokens
[] = new search_token(TOKEN_DATETO
,$param);
280 // State for handling tags:tagname,tagname constructs. Potentially emits a token.
281 function intags($content){
282 if (strlen($content) < 5) { // State exit or missing parameter.
285 // Strip off the tags: part and add the reminder to the parsed token array
286 $param = trim(substr($content,5));
287 $this->tokens
[] = new search_token(TOKEN_TAGS
,$param);
291 // State for handling instance:foo constructs. Potentially emits a token.
292 function ininstance($content){
293 if (strlen($content) < 10) { // State exit or missing parameter.
296 // Strip off the instance: part and add the reminder to the parsed token array
297 $param = trim(substr($content,9));
298 $this->tokens
[] = new search_token(TOKEN_INSTANCE
,$param);
303 // State for handling userid:foo constructs. Potentially emits a token.
304 function inuserid($content){
305 if (strlen($content) < 8) { // State exit or missing parameter.
308 // Strip off the userid: part and add the reminder to the parsed token array
309 $param = trim(substr($content,7));
310 $this->tokens
[] = new search_token(TOKEN_USERID
,$param);
315 // State for handling user:foo constructs. Potentially emits a token.
316 function inusername($content){
317 if (strlen($content) < 6) { // State exit or missing parameter.
320 // Strip off the user: part and add the reminder to the parsed token array
321 $param = trim(substr($content,5));
322 $this->tokens
[] = new search_token(TOKEN_USER
,$param);
327 // State for handling meta:foo constructs. Potentially emits a token.
328 function inmeta($content){
329 if (strlen($content) < 9) { // Missing parameter.
332 // Strip off the meta: part and add the reminder to the parsed token array.
333 $param = trim(substr($content,8));
334 $this->tokens
[] = new search_token(TOKEN_META
,$param);
339 // State entered when we've seen a required string (+foo). Potentially
341 function inrequired($content){
342 if (strlen($content) < 2) { // State exit or missing parameter, don't emit.
345 // Strip off the + sign and add the reminder to the parsed token array.
346 $this->tokens
[] = new search_token(TOKEN_EXACT
,substr($content,1));
350 // State entered when we've seen an excluded string (-foo). Potentially
352 function inexcluded($content){
353 if (strlen($content) < 2) { // State exit or missing parameter.
356 // Strip off the -sign and add the reminder to the parsed token array.
357 $this->tokens
[] = new search_token(TOKEN_NEGATE
,substr($content,1));
362 // State entered when we've seen a quoted string. Potentially emits a token.
363 function inquotedstring($content){
364 if (strlen($content) < 2) { // State exit or missing parameter.
367 // Strip off the opening quote and add the reminder to the parsed token array.
368 $this->tokens
[] = new search_token(TOKEN_STRING
,substr($content,1));
372 // State entered when we've seen an ordinary, non-quoted word. Potentially
374 function plainstring($content){
375 if (trim($content) === '') { // State exit
378 // Add the string to the parsed token array.
379 $this->tokens
[] = new search_token(TOKEN_STRING
,$content);
385 * Primitive function to generate a SQL string from a parse tree
386 * using TEXT indexes. If searches aren't suitable to use TEXT
387 * this function calls the default search_generate_SQL() one.
389 * @deprecated since Moodle 2.9 MDL-48939
390 * @todo MDL-48940 This will be deleted in Moodle 3.2
391 * @see search_generate_SQL()
393 function search_generate_text_SQL($parsetree, $datafield, $metafield, $mainidfield, $useridfield,
394 $userfirstnamefield, $userlastnamefield, $timefield, $instancefield) {
395 debugging('search_generate_text_SQL() is deprecated, please use search_generate_SQL() instead.', DEBUG_DEVELOPER
);
397 return search_generate_SQL($parsetree, $datafield, $metafield, $mainidfield, $useridfield,
398 $userfirstnamefield, $userlastnamefield, $timefield, $instancefield);
402 * Primitive function to generate a SQL string from a parse tree.
405 * $parsetree should be a parse tree generated by a
406 * search_lexer/search_parser combination.
407 * Other fields are database table names to search.
412 function search_generate_SQL($parsetree, $datafield, $metafield, $mainidfield, $useridfield,
413 $userfirstnamefield, $userlastnamefield, $timefield, $instancefield,
418 if ($DB->sql_regex_supported()) {
419 $REGEXP = $DB->sql_regex(true);
420 $NOTREGEXP = $DB->sql_regex(false);
421 $regexwordbegin = $DB->sql_regex_get_word_beginning_boundary_marker();
422 $regexwordend = $DB->sql_regex_get_word_end_boundary_marker();
427 $ntokens = count($parsetree);
434 for ($i=0; $i<$ntokens; $i++
){
435 if ($i > 0) {// We have more than one clause, need to tack on AND
436 $SQLString .= ' AND ';
439 $type = $parsetree[$i]->getType();
440 $value = $parsetree[$i]->getValue();
442 /// Under Oracle and MSSQL, transform TOKEN searches into STRING searches and trim +- chars
443 if (!$DB->sql_regex_supported()) {
444 $value = trim($value, '+-');
445 if ($type == TOKEN_EXACT
) {
446 $type = TOKEN_STRING
;
455 $SQLString .= "((".$DB->sql_like($datafield, ":$name1", false).") OR (".$DB->sql_like($metafield, ":$name2", false)."))";
456 $params[$name1] = "%$value%";
457 $params[$name2] = "%$value%";
460 $SQLString .= "(($datafield $REGEXP :$name1) OR ($metafield $REGEXP :$name2))";
461 $params[$name1] = $regexwordbegin.$value.$regexwordend;
462 $params[$name2] = $regexwordbegin.$value.$regexwordend;
465 if ($metafield != '') {
466 $SQLString .= "(".$DB->sql_like($metafield, ":$name1", false).")";
467 $params[$name1] = "%$value%";
471 $SQLString .= "(($mainidfield = $useridfield) AND ((".$DB->sql_like($userfirstnamefield, ":$name1", false).") OR (".$DB->sql_like($userlastnamefield, ":$name2", false).")))";
472 $params[$name1] = "%$value%";
473 $params[$name2] = "%$value%";
476 $SQLString .= "($useridfield = :$name1)";
477 $params[$name1] = $value;
480 $SQLString .= "($instancefield = :$name1)";
481 $params[$name1] = $value;
484 $SQLString .= "($timefield <= :$name1)";
485 $params[$name1] = $value;
488 $SQLString .= "($timefield >= :$name1)";
489 $params[$name1] = $value;
493 foreach (explode(',', $value) as $tag) {
494 $paramname = $name1 . '_' . $nexttagfield;
495 if (isset($tagfields[$nexttagfield])) {
496 $sqlstrings[] = "($tagfields[$nexttagfield] = :$paramname)";
497 $params[$paramname] = $tag;
498 } else if (!isset($tagfields[$nexttagfield]) && !isset($stoppedprocessingtags)) {
499 // Show a debugging message the first time we hit this.
500 $stoppedprocessingtags = true;
501 \core\notification
::add(get_string('toomanytags'), \core\notification
::WARNING
);
505 $SQLString .= implode(' AND ', $sqlstrings);
508 $SQLString .= "(NOT ((".$DB->sql_like($datafield, ":$name1", false).") OR (".$DB->sql_like($metafield, ":$name2", false).")))";
509 $params[$name1] = "%$value%";
510 $params[$name2] = "%$value%";
517 return array($SQLString, $params);