5 require_once(CONST_LibDir.'/SimpleWordList.php');
 
  12     private $oTransliterator;
 
  14     public function __construct(&$oDB)
 
  17         $this->oNormalizer = \Transliterator::createFromRules(CONST_Term_Normalization_Rules);
 
  18         $this->oTransliterator = \Transliterator::createFromRules(CONST_Transliteration);
 
  21     public function checkStatus()
 
  23         $sSQL = 'SELECT word_id FROM word WHERE word_id is not null limit 1';
 
  24         $iWordID = $this->oDB->getOne($sSQL);
 
  25         if ($iWordID === false) {
 
  26             throw new \Exception('Query failed', 703);
 
  29             throw new \Exception('No value', 704);
 
  34     public function normalizeString($sTerm)
 
  36         if ($this->oNormalizer === null) {
 
  40         return $this->oNormalizer->transliterate($sTerm);
 
  43     private function makeStandardWord($sTerm)
 
  45         return trim($this->oTransliterator->transliterate(' '.$sTerm.' '));
 
  49     public function tokensForSpecialTerm($sTerm)
 
  53         $sSQL = "SELECT word_id, info->>'class' as class, info->>'type' as type ";
 
  54         $sSQL .= '   FROM word WHERE word_token = :term and type = \'S\'';
 
  56         Debug::printVar('Term', $sTerm);
 
  57         Debug::printSQL($sSQL);
 
  58         $aSearchWords = $this->oDB->getAll($sSQL, array(':term' => $this->makeStandardWord($sTerm)));
 
  60         Debug::printVar('Results', $aSearchWords);
 
  62         foreach ($aSearchWords as $aSearchTerm) {
 
  63             $aResults[] = new \Nominatim\Token\SpecialTerm(
 
  64                 $aSearchTerm['word_id'],
 
  65                 $aSearchTerm['class'],
 
  67                 \Nominatim\Operator::TYPE
 
  71         Debug::printVar('Special term tokens', $aResults);
 
  77     public function extractTokensFromPhrases(&$aPhrases)
 
  80         $aWordLists = array();
 
  82         foreach ($aPhrases as $iPhrase => $oPhrase) {
 
  83             $sNormQuery .= ','.$this->normalizeString($oPhrase->getPhrase());
 
  84             $sPhrase = $this->makeStandardWord($oPhrase->getPhrase());
 
  85             Debug::printVar('Phrase', $sPhrase);
 
  87             $oWordList = new SimpleWordList($sPhrase);
 
  88             $aTokens = array_merge($aTokens, $oWordList->getTokens());
 
  89             $aWordLists[] = $oWordList;
 
  92         Debug::printVar('Tokens', $aTokens);
 
  93         Debug::printVar('WordLists', $aWordLists);
 
  95         $oValidTokens = $this->computeValidTokens($aTokens, $sNormQuery);
 
  97         foreach ($aPhrases as $iPhrase => $oPhrase) {
 
  98             $oPhrase->setWordSets($aWordLists[$iPhrase]->getWordSets($oValidTokens));
 
 101         return $oValidTokens;
 
 105     private function computeValidTokens($aTokens, $sNormQuery)
 
 107         $oValidTokens = new TokenList();
 
 109         if (!empty($aTokens)) {
 
 110             $this->addTokensFromDB($oValidTokens, $aTokens, $sNormQuery);
 
 112             // Try more interpretations for Tokens that could not be matched.
 
 113             foreach ($aTokens as $sToken) {
 
 114                 if ($sToken[0] != ' ' && !$oValidTokens->contains($sToken)) {
 
 115                     if (preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData)) {
 
 116                         // US ZIP+4 codes - merge in the 5-digit ZIP code
 
 117                         $oValidTokens->addToken(
 
 119                             new Token\Postcode(null, $aData[1], 'us')
 
 121                     } elseif (preg_match('/^[0-9]+$/', $sToken)) {
 
 122                         // Unknown single word token with a number.
 
 123                         // Assume it is a house number.
 
 124                         $oValidTokens->addToken(
 
 126                             new Token\HouseNumber(null, trim($sToken))
 
 133         return $oValidTokens;
 
 137     private function addTokensFromDB(&$oValidTokens, $aTokens, $sNormQuery)
 
 139         // Check which tokens we have, get the ID numbers
 
 140         $sSQL = 'SELECT word_id, word_token, type, word,';
 
 141         $sSQL .= "      info->>'op' as operator,";
 
 142         $sSQL .= "      info->>'class' as class, info->>'type' as ctype,";
 
 143         $sSQL .= "      info->>'count' as count";
 
 144         $sSQL .= ' FROM word WHERE word_token in (';
 
 145         $sSQL .= join(',', $this->oDB->getDBQuotedList($aTokens)).')';
 
 147         Debug::printSQL($sSQL);
 
 149         $aDBWords = $this->oDB->getAll($sSQL, null, 'Could not get word tokens.');
 
 151         foreach ($aDBWords as $aWord) {
 
 152             $iId = (int) $aWord['word_id'];
 
 153             $sTok = $aWord['word_token'];
 
 155             switch ($aWord['type']) {
 
 156                 case 'C':  // country name tokens
 
 157                     if ($aWord['word'] !== null) {
 
 158                         $oValidTokens->addToken(
 
 160                             new Token\Country($iId, $aWord['word'])
 
 164                 case 'H':  // house number tokens
 
 165                     $oValidTokens->addToken($sTok, new Token\HouseNumber($iId, $aWord['word_token']));
 
 167                 case 'P':  // postcode tokens
 
 168                     // Postcodes are not normalized, so they may have content
 
 169                     // that makes SQL injection possible. Reject postcodes
 
 170                     // that would need special escaping.
 
 171                     if ($aWord['word'] !== null
 
 172                         && pg_escape_string($aWord['word']) == $aWord['word']
 
 174                         $sNormPostcode = $this->normalizeString($aWord['word']);
 
 175                         if (strpos($sNormQuery, $sNormPostcode) !== false) {
 
 176                             $oValidTokens->addToken(
 
 178                                 new Token\Postcode($iId, $aWord['word'], null)
 
 183                 case 'S':  // tokens for classification terms (special phrases)
 
 184                     if ($aWord['class'] !== null && $aWord['ctype'] !== null) {
 
 185                         $oValidTokens->addToken($sTok, new Token\SpecialTerm(
 
 189                             (isset($aWord['operator'])) ? Operator::NEAR : Operator::NONE
 
 193                 case 'W': // full-word tokens
 
 194                     $oValidTokens->addToken($sTok, new Token\Word(
 
 196                         (int) $aWord['count'],
 
 197                         substr_count($aWord['word_token'], ' ')
 
 200                 case 'w':  // partial word terms
 
 201                     $oValidTokens->addToken($sTok, new Token\Partial(
 
 203                         $aWord['word_token'],
 
 204                         (int) $aWord['count']