Skip to content

Commit

Permalink
MDL-48939 core: Depreacte search_generate_text_SQL
Browse files Browse the repository at this point in the history
This has never been used outside of forum, where it was introduced in
Moodle 1.8 and required manual creation of an index.
  • Loading branch information
andrewnicols committed Jan 22, 2015
1 parent da0ef2e commit 2f19417
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 147 deletions.
138 changes: 6 additions & 132 deletions lib/searchlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,142 +345,16 @@ function plainstring($content){
* using TEXT indexes. If searches aren't suitable to use TEXT
* this function calls the default search_generate_SQL() one.
*
* $parsetree should be a parse tree generated by a
* search_lexer/search_parser combination.
* Other fields are database table names to search.
*
* @global object
* @global object
* @deprecated since Moodle 2.9 MDL-48939
* @todo MDL-48940 This will be deleted in Moodle 3.2
* @see search_generate_SQL()
*/
function search_generate_text_SQL($parsetree, $datafield, $metafield, $mainidfield, $useridfield,
$userfirstnamefield, $userlastnamefield, $timefield, $instancefield) {
global $CFG, $DB;
static $p = 0;

/// First of all, search for reasons to switch to standard SQL generation
/// Only mysql are supported for now
if ($DB->get_dbfamily() != 'mysql') {
return search_generate_SQL($parsetree, $datafield, $metafield, $mainidfield, $useridfield,
$userfirstnamefield, $userlastnamefield, $timefield, $instancefield);
}

/// Some languages don't have "word separators" and MySQL FULLTEXT doesn't perform well with them, so
/// switch to standard SQL search generation
if ($DB->get_dbfamily() == 'mysql') {
$nonseparatedlangs = array('ja', 'th', 'zh_cn', 'zh_tw');
if (in_array(current_language(), $nonseparatedlangs)) {
return search_generate_SQL($parsetree, $datafield, $metafield, $mainidfield, $useridfield,
$userfirstnamefield, $userlastnamefield, $timefield, $instancefield);
}
}

/// Here we'll acumulate non-textual tokens
$non_text_tokens = array();
$params = array();

$ntokens = count($parsetree);
if ($ntokens == 0) {
return "";
}

$SQLString = '';
$text_sql_string = '';

$datasearch_clause = '';
$metasearch_clause = '';

foreach ($parsetree as $token) {

$type = $token->getType();
$value = $token->getValue();

switch($type){
case TOKEN_STRING:
/// If it's a multiword token, quote it
if (strstr($value, ' ')) {
$datasearch_clause .= '"' . $value . '" ';
/// Simple word token, search for it as prefix
} else {
$datasearch_clause .= '+' . $value . '* ';
}
break;
case TOKEN_EXACT:
/// token must be exactly as requested
$datasearch_clause .= '+' . $value . ' ';
break;
case TOKEN_NEGATE:
/// token must not exist as prefix
$datasearch_clause .= '-' . $value . '* ';
break;
case TOKEN_META:
/// token in metafield, search for it as prefix
$metasearch_clause .= '+' . $value . '* ';
break;
case TOKEN_USER:
case TOKEN_USERID:
case TOKEN_INSTANCE:
case TOKEN_DATETO:
case TOKEN_DATEFROM:
/// delegate to standard search
$non_text_tokens[] = $token;
break;
default:
return '';
}
}

/// Call to standard search for pending tokens
if (!empty($non_text_tokens)) {
list($SQLString, $sparams) = search_generate_SQL($non_text_tokens, $datafield, $metafield, $mainidfield, $useridfield,
$userfirstnamefield, $userlastnamefield, $timefield, $instancefield);
$params = array_merge($params, $sparams);
}
/// Build the final SQL clause
if (!empty($datasearch_clause)) {
/// Must have $datafield to search within
if (!empty($datafield)) {
$text_sql_string .= 'MATCH (' . $datafield;
/// And optionally $metafield
if (!empty($metafield)) {
$text_sql_string .= ', ' . $metafield;
}
/// Begin with the AGAINST clause
$text_sql_string .= ') AGAINST (';
/// Add the search terms
$text_sql_string .= ':sgt'.$p;
$params['sgt'.$p++] = trim($datasearch_clause);
/// Close AGAINST clause
$text_sql_string .= " IN BOOLEAN MODE)";
}
}
/// Now add the metasearch_clause
if (!empty($metasearch_clause)) {
/// Must have $metafield to search within
if (!empty($metafield)) {
/// AND operator if needed
if (!empty($text_sql_string)) {
$text_sql_string .= ' AND ';
}
$text_sql_string .= 'MATCH (' . $metafield;
/// Begin with the AGAINST clause
$text_sql_string .= ') AGAINST (';
/// Add the search terms
$text_sql_string .= ':sgt'.$p;
$params['sgt'.$p++] = trim($metasearch_clause);
/// Close AGAINST clause
$text_sql_string .= " IN BOOLEAN MODE)";
}
}
/// Finally add the non-text conditions
if (!empty($SQLString)) {
/// AND operator if needed
if (!empty($text_sql_string)) {
$text_sql_string .= ' AND ';
}
$text_sql_string .= $SQLString;
}
debugging('search_generate_text_SQL() is deprecated, please use search_generate_SQL() instead.', DEBUG_DEVELOPER);

return array($text_sql_string, $params);
return search_generate_SQL($parsetree, $datafield, $metafield, $mainidfield, $useridfield,
$userfirstnamefield, $userlastnamefield, $timefield, $instancefield);
}

/**
Expand Down
18 changes: 3 additions & 15 deletions mod/forum/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2152,21 +2152,9 @@ function forum_search_posts($searchterms, $courseid=0, $limitfrom=0, $limitnum=5

if ($lexer->parse($searchstring)) {
$parsearray = $parser->get_parsed_array();
// Experimental feature under 1.8! MDL-8830
// Use alternative text searches if defined
// This feature only works under mysql until properly implemented for other DBs
// Requires manual creation of text index for forum_posts before enabling it:
// CREATE FULLTEXT INDEX foru_post_tix ON [prefix]forum_posts (subject, message)
// Experimental feature under 1.8! MDL-8830
if (!empty($CFG->forum_usetextsearches)) {
list($messagesearch, $msparams) = search_generate_text_SQL($parsearray, 'p.message', 'p.subject',
'p.userid', 'u.id', 'u.firstname',
'u.lastname', 'p.modified', 'd.forum');
} else {
list($messagesearch, $msparams) = search_generate_SQL($parsearray, 'p.message', 'p.subject',
'p.userid', 'u.id', 'u.firstname',
'u.lastname', 'p.modified', 'd.forum');
}
list($messagesearch, $msparams) = search_generate_SQL($parsearray, 'p.message', 'p.subject',
'p.userid', 'u.id', 'u.firstname',
'u.lastname', 'p.modified', 'd.forum');
$params = array_merge($params, $msparams);
}

Expand Down

0 comments on commit 2f19417

Please sign in to comment.