5 require_once(CONST_BasePath.'/lib/TokenCountry.php');
6 require_once(CONST_BasePath.'/lib/TokenHousenumber.php');
7 require_once(CONST_BasePath.'/lib/TokenPostcode.php');
8 require_once(CONST_BasePath.'/lib/TokenSpecialTerm.php');
9 require_once(CONST_BasePath.'/lib/TokenWord.php');
10 require_once(CONST_BasePath.'/lib/SpecialSearchOperator.php');
13 * Saves information about the tokens that appear in a search query.
18 // List of list of tokens indexed by their word_token.
19 private $aTokens = array();
21 public function contains($sWord)
23 return isset($this->aTokens[$sWord]);
26 public function get($sWord)
28 return isset($this->aTokens[$sWord]) ? $this->aTokens[$sWord] : array();
32 * Add token information from the word table in the database.
34 * @param object $oDB Database connection.
35 * @param string[] $aTokens List of tokens to look up in the database.
36 * @param string[] $aCountryCodes List of country restrictions.
37 * @param string $sNormQuery Normalized query string.
38 * @param object $oNormalizer Normalizer function to use on tokens.
42 public function addTokensFromDB(&$oDB, &$aTokens, &$aCountryCodes, $sNormQuery, $oNormalizer)
44 // Check which tokens we have, get the ID numbers
45 $sSQL = 'SELECT word_id, word_token, word, class, type, country_code,';
46 $sSQL .= ' operator, coalesce(search_name_count, 0) as count';
47 $sSQL .= ' FROM word WHERE word_token in (';
48 $sSQL .= join(',', array_map('getDBQuoted', $aTokens)).')';
50 Debug::printSQL($sSQL);
52 $aDBWords = chksql($oDB->getAll($sSQL), 'Could not get word tokens.');
54 foreach ($aDBWords as $aWord) {
56 $iId = (int) $aWord['word_id'];
58 if ($aWord['class']) {
59 // Special terms need to appear in their normalized form.
61 $sNormWord = $aWord['word'];
62 if ($oNormalizer != null) {
63 $sNormWord = $oNormalizer->transliterate($aWord['word']);
65 if (strpos($sNormQuery, $sNormWord) === false) {
70 if ($aWord['class'] == 'place' && $aWord['type'] == 'house') {
71 $oToken = new Token\HouseNumber($iId, trim($aWord['word_token']));
72 } elseif ($aWord['class'] == 'place' && $aWord['type'] == 'postcode') {
74 && pg_escape_string($aWord['word']) == $aWord['word']
76 $oToken = new Token\Postcode(
79 $aWord['country_code']
83 // near and in operator the same at the moment
84 $oToken = new Token\SpecialTerm(
88 $aWord['operator'] ? Operator::NONE : Operator::NEAR
91 } elseif ($aWord['country_code']) {
92 // Filter country tokens that do not match restricted countries.
94 || in_array($aWord['country_code'], $aCountryCodes)
96 $oToken = new Token\Country($iId, $aWord['country_code']);
99 $oToken = new Token\Word(
101 $aWord['word'][0] != ' ',
102 (int) $aWord['count']
107 $this->addToken($aWord['word_token'], $oToken);
113 * Add a new token for the given word.
115 * @param string $sWord Word the token describes.
116 * @param object $oToken Token object to add.
120 public function addToken($sWord, $oToken)
122 if (isset($this->aTokens[$sWord])) {
123 $this->aTokens[$sWord][] = $oToken;
125 $this->aTokens[$sWord] = array($oToken);
129 public function debugTokenByWordIdList()
131 $aWordsIDs = array();
132 foreach ($this->aTokens as $sToken => $aWords) {
133 foreach ($aWords as $aToken) {
134 if ($aToken->iId !== null) {
135 $aWordsIDs[$aToken->iId] =
136 '#'.$sToken.'('.$aToken->iId.')#';
144 public function debugInfo()
146 return $this->aTokens;