public static function rebuildSearchTokens($pids, $transaction = true) { require_once 'name.func.inc.php'; if (!is_array($pids)) { $pids = array($pids); } $keys = XDB::iterator("(SELECT pid, name, type, IF(type = 'nickname', 2, 1) AS score, '' AS public\n FROM profile_private_names\n WHERE pid IN {?})\n UNION\n (SELECT pid, lastname_main, 'lastname' AS type, 10 AS score, 'public' AS public\n FROM profile_public_names\n WHERE lastname_main != '' AND pid IN {?})\n UNION\n (SELECT pid, lastname_marital, 'lastname' AS type, 10 AS score, 'public' AS public\n FROM profile_public_names\n WHERE lastname_marital != '' AND pid IN {?})\n UNION\n (SELECT pid, lastname_ordinary, 'lastname' AS type, 10 AS score, 'public' AS public\n FROM profile_public_names\n WHERE lastname_ordinary != '' AND pid IN {?})\n UNION\n (SELECT pid, firstname_main, 'firstname' AS type, 10 AS score, 'public' AS public\n FROM profile_public_names\n WHERE firstname_main != '' AND pid IN {?})\n UNION\n (SELECT pid, firstname_ordinary, 'firstname' AS type, 10 AS score, 'public' AS public\n FROM profile_public_names\n WHERE firstname_ordinary != '' AND pid IN {?})\n UNION\n (SELECT pid, pseudonym, 'nickname' AS type, 10 AS score, 'public' AS public\n FROM profile_public_names\n WHERE pseudonym != '' AND pid IN {?})", $pids, $pids, $pids, $pids, $pids, $pids, $pids); $names = array(); while ($key = $keys->next()) { if ($key['name'] == '') { continue; } $pid = $key['pid']; $toks = split_name_for_search($key['name']); $toks = array_reverse($toks); /* Split the score between the tokens to avoid the user to be over-rated. * Let says my user name is "Machin-Truc Bidule" and I also have a user named * 'Machin Truc'. Distributing the score force "Machin Truc" to be displayed * before "Machin-Truc" for both "Machin Truc" and "Machin" searches. */ $eltScore = ceil((double) $key['score'] / (double) count($toks)); $token = ''; foreach ($toks as $tok) { $token = $tok . $token; $names["{$pid}-{$token}"] = XDB::format('({?}, {?}, {?}, {?}, {?}, {?})', $token, $pid, soundex_fr($token), $eltScore, $key['public'], $key['type']); } } if ($transaction) { XDB::startTransaction(); } XDB::execute('DELETE FROM search_name WHERE pid IN {?}', $pids); if (count($names) > 0) { XDB::rawExecute('INSERT INTO search_name (token, pid, soundex, score, flags, general_type) VALUES ' . implode(', ', $names)); } if ($transaction) { XDB::commit(); } }
public function buildCondition(PlFilter $uf) { $conds = array(); foreach ($this->tokens as $i => $token) { $sub = $uf->addNameTokensFilter($token); if ($this->soundex) { $c = XDB::format($sub . '.soundex = {?}', soundex_fr($token)); } else { if ($this->exact) { $c = XDB::format($sub . '.token = {?}', $token); } else { $c = $sub . '.token ' . XDB::formatWildcards(XDB::WILDCARD_PREFIX, $token); } } if ($this->flags != null) { $c .= XDB::format(' AND ' . $sub . '.flags IN {?}', $this->flags); } if ($this->general_type) { $c .= XDB::format(' AND ' . $sub . '.general_type = {?}', $this->general_type); } $c .= ' AND (' . $uf->getVisibilityConditionAbsolute(Visibility::EXPORT_PRIVATE) . ' OR ' . $sub . '.general_type != \'nickname\')'; $conds[] = $c; } return implode(' AND ', $conds); }