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      * Check if there are partial or full tokens for the given word.
 
  61      * @param string $sWord Token word to look for.
 
  63      * @return bool True if there is one or more token for the token word.
 
  65     public function containsAny($sWord)
 
  67         return isset($this->aTokens[$sWord]) || isset($this->aTokens[' '.$sWord]);
 
  71      * Get the list of tokens for the given token word.
 
  73      * @param string $sWord Token word to look for.
 
  75      * @return object[] Array of tokens for the given token word or an
 
  76      *                  empty array if no tokens could be found.
 
  78     public function get($sWord)
 
  80         return isset($this->aTokens[$sWord]) ? $this->aTokens[$sWord] : array();
 
  84      * Add token information from the word table in the database.
 
  86      * @param object   $oDB           Nominatim::DB instance.
 
  87      * @param string[] $aTokens       List of tokens to look up in the database.
 
  88      * @param string[] $aCountryCodes List of country restrictions.
 
  89      * @param string   $sNormQuery    Normalized query string.
 
  90      * @param object   $oNormalizer   Normalizer function to use on tokens.
 
  94     public function addTokensFromDB(&$oDB, &$aTokens, &$aCountryCodes, $sNormQuery, $oNormalizer)
 
  96         // Check which tokens we have, get the ID numbers
 
  97         $sSQL = 'SELECT word_id, word_token, word, class, type, country_code,';
 
  98         $sSQL .= ' operator, coalesce(search_name_count, 0) as count';
 
  99         $sSQL .= ' FROM word WHERE word_token in (';
 
 100         $sSQL .= join(',', $oDB->getDBQuotedList($aTokens)).')';
 
 102         Debug::printSQL($sSQL);
 
 104         $aDBWords = $oDB->getAll($sSQL, null, 'Could not get word tokens.');
 
 106         foreach ($aDBWords as $aWord) {
 
 108             $iId = (int) $aWord['word_id'];
 
 110             if ($aWord['class']) {
 
 111                 // Special terms need to appear in their normalized form.
 
 112                 if ($aWord['word']) {
 
 113                     $sNormWord = $aWord['word'];
 
 114                     if ($oNormalizer != null) {
 
 115                         $sNormWord = $oNormalizer->transliterate($aWord['word']);
 
 117                     if (strpos($sNormQuery, $sNormWord) === false) {
 
 122                 if ($aWord['class'] == 'place' && $aWord['type'] == 'house') {
 
 123                     $oToken = new Token\HouseNumber($iId, trim($aWord['word_token']));
 
 124                 } elseif ($aWord['class'] == 'place' && $aWord['type'] == 'postcode') {
 
 126                         && pg_escape_string($aWord['word']) == $aWord['word']
 
 128                         $oToken = new Token\Postcode(
 
 131                             $aWord['country_code']
 
 135                     // near and in operator the same at the moment
 
 136                     $oToken = new Token\SpecialTerm(
 
 140                         $aWord['operator'] ? Operator::NEAR : Operator::NONE
 
 143             } elseif ($aWord['country_code']) {
 
 144                 // Filter country tokens that do not match restricted countries.
 
 146                     || in_array($aWord['country_code'], $aCountryCodes)
 
 148                     $oToken = new Token\Country($iId, $aWord['country_code']);
 
 151                 $oToken = new Token\Word(
 
 153                     $aWord['word_token'][0] != ' ',
 
 154                     (int) $aWord['count']
 
 159                 $this->addToken($aWord['word_token'], $oToken);
 
 165      * Add a new token for the given word.
 
 167      * @param string $sWord  Word the token describes.
 
 168      * @param object $oToken Token object to add.
 
 172     public function addToken($sWord, $oToken)
 
 174         if (isset($this->aTokens[$sWord])) {
 
 175             $this->aTokens[$sWord][] = $oToken;
 
 177             $this->aTokens[$sWord] = array($oToken);
 
 181     public function debugTokenByWordIdList()
 
 183         $aWordsIDs = array();
 
 184         foreach ($this->aTokens as $sToken => $aWords) {
 
 185             foreach ($aWords as $aToken) {
 
 186                 if ($aToken->iId !== null) {
 
 187                     $aWordsIDs[$aToken->iId] =
 
 188                         '#'.$sToken.'('.$aToken->iId.')#';
 
 196     public function debugInfo()
 
 198         return $this->aTokens;