MDL-46929 mod_forum: Fix for edge cases when searching

This commit is contained in:
Andrew Hancox 2017-04-04 17:48:20 +01:00
parent 151113f93a
commit e9c46768b5
3 changed files with 21 additions and 12 deletions

View File

@ -1890,6 +1890,7 @@ $string['today'] = 'Today';
$string['todaylogs'] = 'Today\'s logs';
$string['toeveryone'] = 'to everyone';
$string['toomanybounces'] = 'That email address has had too many bounces. You <b>must</b> change it to continue.';
$string['toomanytags'] = 'This search included too many tags, some will have been ignored.';
$string['toomanytoshow'] = 'There are too many users to show.';
$string['toomanyusersmatchsearch'] = 'Too many users ({$a->count}) match \'{$a->search}\'';
$string['toomanyuserstoshow'] = 'Too many users ({$a}) to show';

View File

@ -490,11 +490,14 @@ function search_generate_SQL($parsetree, $datafield, $metafield, $mainidfield, $
$sqlstrings = [];
foreach (explode(',', $value) as $tag) {
$paramname = $name1 . '_' . $nexttagfield;
if (!isset($tagfields[$nexttagfield])) {
throw new coding_exception('Not enough tag fields supplied for search.');
if (isset($tagfields[$nexttagfield])) {
$sqlstrings[] = "($tagfields[$nexttagfield] = :$paramname)";
$params[$paramname] = $tag;
} else if (!isset($tagfields[$nexttagfield]) && !isset($stoppedprocessingtags)) {
// Show a debugging message the first time we hit this.
$stoppedprocessingtags = true;
\core\notification::add(get_string('toomanytags'), \core\notification::WARNING);
}
$sqlstrings[] = "($tagfields[$nexttagfield] = :$paramname)";
$params[$paramname] = $tag;
$nexttagfield++;
}
$SQLString .= implode(' AND ', $sqlstrings);

View File

@ -2098,16 +2098,21 @@ function forum_search_posts($searchterms, $courseid=0, $limitfrom=0, $limitnum=5
$tagjoins = '';
$tagfields = [];
$tagfieldcount = 0;
foreach ($parsearray as $token) {
if ($token->getType() == TOKEN_TAGS) {
for ($i = substr_count($token->getValue(), ','); $i >= 0; $i--) {
$tagjoins .= "\n LEFT JOIN {tag_instance} ti_$i
ON p.id = ti_$i.itemid
AND ti_$i.component='mod_forum'
AND ti_$i.itemtype = 'forum_posts'";
$tagjoins .= "\n LEFT JOIN {tag} t_$i ON t_$i.id = ti_$i.tagid";
$tagfields[] = "t_$i.rawname";
for ($i = 0; $i <= substr_count($token->getValue(), ','); $i++) {
// Queries can only have a limited number of joins so set a limit sensible users won't exceed.
if ($tagfieldcount > 10) {
continue;
}
$tagjoins .= " LEFT JOIN {tag_instance} ti_$tagfieldcount
ON p.id = ti_$tagfieldcount.itemid
AND ti_$tagfieldcount.component = 'mod_forum'
AND ti_$tagfieldcount.itemtype = 'forum_posts'";
$tagjoins .= " LEFT JOIN {tag} t_$tagfieldcount ON t_$tagfieldcount.id = ti_$tagfieldcount.tagid";
$tagfields[] = "t_$tagfieldcount.rawname";
$tagfieldcount++;
}
}
}