MDL-74912 mod_forum: Fix regexp word boundaries markers in MySQL 8

This commit is contained in:
Matteo Scaramuccia
2022-11-05 10:09:04 +01:00
parent 71841556aa
commit 314c9671fc
4 changed files with 64 additions and 2 deletions
+24
View File
@@ -2483,6 +2483,30 @@ abstract class moodle_database {
return '';
}
/**
* Returns the word-beginning boundary marker if this database driver supports regex syntax when searching.
* @return string The word-beginning boundary marker. Otherwise, an empty string.
*/
public function sql_regex_get_word_beginning_boundary_marker() {
if ($this->sql_regex_supported()) {
return '[[:<:]]';
}
return '';
}
/**
* Returns the word-end boundary marker if this database driver supports regex syntax when searching.
* @return string The word-end boundary marker. Otherwise, an empty string.
*/
public function sql_regex_get_word_end_boundary_marker() {
if ($this->sql_regex_supported()) {
return '[[:>:]]';
}
return '';
}
/**
* Returns the SQL that allows to find intersection of two or more queries
*
+32
View File
@@ -1876,6 +1876,38 @@ class mysqli_native_moodle_database extends moodle_database {
return $collation . ($positivematch ? 'REGEXP' : 'NOT REGEXP');
}
/**
* Returns the word-beginning boundary marker based on MySQL version.
* @return string The word-beginning boundary marker.
*/
public function sql_regex_get_word_beginning_boundary_marker() {
$ismysql = ($this->get_dbtype() == 'mysqli' || $this->get_dbtype() == 'auroramysql');
$ismysqlge8d0d4 = ($ismysql && version_compare($this->get_server_info()['version'], '8.0.4', '>='));
if ($ismysqlge8d0d4) {
return '\\b';
}
// Prior to MySQL 8.0.4, MySQL used the Henry Spencer regular expression library to support regular expression operations,
// rather than International Components for Unicode (ICU).
// MariaDB still supports the "old marker" (MDEV-5357).
return '[[:<:]]';
}
/**
* Returns the word-end boundary marker based on MySQL version.
* @return string The word-end boundary marker.
*/
public function sql_regex_get_word_end_boundary_marker() {
$ismysql = ($this->get_dbtype() == 'mysqli' || $this->get_dbtype() == 'auroramysql');
$ismysqlge8d0d4 = ($ismysql && version_compare($this->get_server_info()['version'], '8.0.4', '>='));
if ($ismysqlge8d0d4) {
return '\\b';
}
// Prior to MySQL 8.0.4, MySQL used the Henry Spencer regular expression library to support regular expression operations,
// rather than International Components for Unicode (ICU).
// MariaDB still supports the "old marker" (MDEV-5357).
return '[[:>:]]';
}
/**
* Returns the SQL to be used in order to an UNSIGNED INTEGER column to SIGNED.
*
+4 -2
View File
@@ -418,6 +418,8 @@ function search_generate_SQL($parsetree, $datafield, $metafield, $mainidfield, $
if ($DB->sql_regex_supported()) {
$REGEXP = $DB->sql_regex(true);
$NOTREGEXP = $DB->sql_regex(false);
$regexwordbegin = $DB->sql_regex_get_word_beginning_boundary_marker();
$regexwordend = $DB->sql_regex_get_word_end_boundary_marker();
}
$params = array();
@@ -456,8 +458,8 @@ function search_generate_SQL($parsetree, $datafield, $metafield, $mainidfield, $
break;
case TOKEN_EXACT:
$SQLString .= "(($datafield $REGEXP :$name1) OR ($metafield $REGEXP :$name2))";
$params[$name1] = "[[:<:]]".$value."[[:>:]]";
$params[$name2] = "[[:<:]]".$value."[[:>:]]";
$params[$name1] = $regexwordbegin.$value.$regexwordend;
$params[$name2] = $regexwordbegin.$value.$regexwordend;
break;
case TOKEN_META:
if ($metafield != '') {
+4
View File
@@ -1,6 +1,10 @@
This files describes API changes in core libraries and APIs,
information provided here is intended especially for developers.
=== 3.11.11 ===
* New DML driver methods `$DB->sql_regex_get_word_beginning_boundary_marker` and `$DB->sql_regex_get_word_end_boundary_marker`
for managing word boundary markers in a database driver supporting regex syntax when searching.
=== 3.11.8 ===
* The check for $plugin->incompatible was found to be incorrect. The $plugin->incompatible attribute is meant to define the minimum
Moodle version the plugin is incompatible with but the implemented logic for the check was the opposite. Plugins declaring this