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.
15 * Tokens are sorted by their normalized form, the token word. There are different
16 * kinds of tokens, represented by different Token* classes. Note that
17 * tokens do not have a common base class. All tokens need to have a field
18 * with the word id that points to an entry in the `word` database table
19 * but otherwise the information saved about a token can be very different.
21 * There are two different kinds of token words: full words and partial terms.
23 * Full words start with a space. They represent a complete name of a place.
24 * All special tokens are normally full words.
26 * Partial terms have no space at the beginning. They may represent a part of
27 * a name of a place (e.g. in the name 'World Trade Center' a partial term
28 * would be 'Trade' or 'Trade Center'). They are only used in TokenWord.
32 // List of list of tokens indexed by their word_token.
33 private $aTokens = array();
37 * Return total number of tokens.
41 public function count()
43 return count($this->aTokens);
47 * Check if there are tokens for the given token word.
49 * @param string $sWord Token word to look for.
51 * @return bool True if there is one or more token for the token word.
53 public function contains($sWord)
55 return isset($this->aTokens[$sWord]);
59 * Get the list of tokens for the given token word.
61 * @param string $sWord Token word to look for.
63 * @return object[] Array of tokens for the given token word or an
64 * empty array if no tokens could be found.
66 public function get($sWord)
68 return isset($this->aTokens[$sWord]) ? $this->aTokens[$sWord] : array();
72 * Add token information from the word table in the database.
74 * @param object $oDB Database connection.
75 * @param string[] $aTokens List of tokens to look up in the database.
76 * @param string[] $aCountryCodes List of country restrictions.
77 * @param string $sNormQuery Normalized query string.
78 * @param object $oNormalizer Normalizer function to use on tokens.
82 public function addTokensFromDB(&$oDB, &$aTokens, &$aCountryCodes, $sNormQuery, $oNormalizer)
84 // Check which tokens we have, get the ID numbers
85 $sSQL = 'SELECT word_id, word_token, word, class, type, country_code,';
86 $sSQL .= ' operator, coalesce(search_name_count, 0) as count';
87 $sSQL .= ' FROM word WHERE word_token in (';
88 $sSQL .= join(',', $oDB->getDBQuotedList($aTokens)).')';
90 Debug::printSQL($sSQL);
92 $aDBWords = chksql($oDB->getAll($sSQL), 'Could not get word tokens.');
94 foreach ($aDBWords as $aWord) {
96 $iId = (int) $aWord['word_id'];
98 if ($aWord['class']) {
99 // Special terms need to appear in their normalized form.
100 if ($aWord['word']) {
101 $sNormWord = $aWord['word'];
102 if ($oNormalizer != null) {
103 $sNormWord = $oNormalizer->transliterate($aWord['word']);
105 if (strpos($sNormQuery, $sNormWord) === false) {
110 if ($aWord['class'] == 'place' && $aWord['type'] == 'house') {
111 $oToken = new Token\HouseNumber($iId, trim($aWord['word_token']));
112 } elseif ($aWord['class'] == 'place' && $aWord['type'] == 'postcode') {
114 && pg_escape_string($aWord['word']) == $aWord['word']
116 $oToken = new Token\Postcode(
119 $aWord['country_code']
123 // near and in operator the same at the moment
124 $oToken = new Token\SpecialTerm(
128 $aWord['operator'] ? Operator::NEAR : Operator::NONE
131 } elseif ($aWord['country_code']) {
132 // Filter country tokens that do not match restricted countries.
134 || in_array($aWord['country_code'], $aCountryCodes)
136 $oToken = new Token\Country($iId, $aWord['country_code']);
139 $oToken = new Token\Word(
141 $aWord['word_token'][0] != ' ',
142 (int) $aWord['count']
147 $this->addToken($aWord['word_token'], $oToken);
153 * Add a new token for the given word.
155 * @param string $sWord Word the token describes.
156 * @param object $oToken Token object to add.
160 public function addToken($sWord, $oToken)
162 if (isset($this->aTokens[$sWord])) {
163 $this->aTokens[$sWord][] = $oToken;
165 $this->aTokens[$sWord] = array($oToken);
169 public function debugTokenByWordIdList()
171 $aWordsIDs = array();
172 foreach ($this->aTokens as $sToken => $aWords) {
173 foreach ($aWords as $aToken) {
174 if ($aToken->iId !== null) {
175 $aWordsIDs[$aToken->iId] =
176 '#'.$sToken.'('.$aToken->iId.')#';
184 public function debugInfo()
186 return $this->aTokens;