]> git.openstreetmap.org Git - nominatim.git/blobdiff - website/search.php
silently remove leading/trailing spaces
[nominatim.git] / website / search.php
index 6b49241452ff3b275b984029dc5da76c577aeae2..f0bcf077513b9017e884de74a7da3c03f948dd13 100755 (executable)
                $aPhrases = array_reverse($aPhrases); 
                $sQuery = join(', ',$aPhrases);
        }
                $aPhrases = array_reverse($aPhrases); 
                $sQuery = join(', ',$aPhrases);
        }
-
        if ($sQuery)
        {
                $hLog = logStart($oDB, 'search', $sQuery, $aLangPrefOrder);
        if ($sQuery)
        {
                $hLog = logStart($oDB, 'search', $sQuery, $aLangPrefOrder);
                        $sQuery = preg_replace('/\\bil\\b/','illinois', $sQuery);
                        $sQuery = preg_replace('/\\bal\\b/','alabama', $sQuery);
                        $sQuery = preg_replace('/\\bla\\b/','louisiana', $sQuery);
                        $sQuery = preg_replace('/\\bil\\b/','illinois', $sQuery);
                        $sQuery = preg_replace('/\\bal\\b/','alabama', $sQuery);
                        $sQuery = preg_replace('/\\bla\\b/','louisiana', $sQuery);
+                       $sQuery = preg_replace('/\\bde louisiana\\b/','de la', $sQuery);
                }
 
                // If we have a view box create the SQL
                }
 
                // If we have a view box create the SQL
                        $sViewboxSmallSQL = $oDB->getOne($sSQL);
                        if (PEAR::isError($sViewboxSmallSQL))
                        {
                        $sViewboxSmallSQL = $oDB->getOne($sSQL);
                        if (PEAR::isError($sViewboxSmallSQL))
                        {
-                               var_dump($sViewboxSmallSQL);
-                               exit;
+                               failInternalError("Could not get small viewbox.", $sSQL, $sViewboxSmallSQL);
                        }
                        $sViewboxSmallSQL = "'".$sViewboxSmallSQL."'::geometry";
 
                        }
                        $sViewboxSmallSQL = "'".$sViewboxSmallSQL."'::geometry";
 
                        $sViewboxLargeSQL = $oDB->getOne($sSQL);
                        if (PEAR::isError($sViewboxLargeSQL))
                        {
                        $sViewboxLargeSQL = $oDB->getOne($sSQL);
                        if (PEAR::isError($sViewboxLargeSQL))
                        {
-                               var_dump($sViewboxLargeSQL);
-                               exit;
+                               failInternalError("Could not get large viewbox.", $sSQL, $sViewboxLargeSQL);
                        }
                        $sViewboxLargeSQL = "'".$sViewboxLargeSQL."'::geometry";
                }
                        }
                        $sViewboxLargeSQL = "'".$sViewboxLargeSQL."'::geometry";
                }
                // Do we have anything that looks like a lat/lon pair?
                if (preg_match('/\\b([NS])[ ]+([0-9]+[0-9.]*)[ ]+([0-9.]+)?[, ]+([EW])[ ]+([0-9]+)[ ]+([0-9]+[0-9.]*)?\\b/', $sQuery, $aData))
                {
                // Do we have anything that looks like a lat/lon pair?
                if (preg_match('/\\b([NS])[ ]+([0-9]+[0-9.]*)[ ]+([0-9.]+)?[, ]+([EW])[ ]+([0-9]+)[ ]+([0-9]+[0-9.]*)?\\b/', $sQuery, $aData))
                {
-                       $_GET['nearlat'] = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
-                       $_GET['nearlon'] = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
-                       $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
+                       $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
+                       $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
+                       if ($fQueryLat <= 90.1 && $fQueryLat >= -90.1 && $fQueryLon <= 180.1 && $fQueryLon >= -180.1)
+                       {
+                               $_GET['nearlat'] = $fQueryLat;
+                               $_GET['nearlon'] = $fQueryLon;
+                               $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
+                       }
                }
                elseif (preg_match('/\\b([0-9]+)[ ]+([0-9]+[0-9.]*)?[ ]+([NS])[, ]+([0-9]+)[ ]+([0-9]+[0-9.]*)?[ ]+([EW])\\b/', $sQuery, $aData))
                {
                }
                elseif (preg_match('/\\b([0-9]+)[ ]+([0-9]+[0-9.]*)?[ ]+([NS])[, ]+([0-9]+)[ ]+([0-9]+[0-9.]*)?[ ]+([EW])\\b/', $sQuery, $aData))
                {
-                       $_GET['nearlat'] = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
-                       $_GET['nearlon'] = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
-                       $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
+                       $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
+                       $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
+                       if ($fQueryLat <= 90.1 && $fQueryLat >= -90.1 && $fQueryLon <= 180.1 && $fQueryLon >= -180.1)
+                       {
+                               $_GET['nearlat'] = $fQueryLat;
+                               $_GET['nearlon'] = $fQueryLon;
+                               $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
+                       }
                }
                }
-               elseif (preg_match('/(\\[|\\b)(-?[0-9]+[0-9.]*)[, ]+(-?[0-9]+[0-9.]*)(\\]|\\b)/', $sQuery, $aData))
+               elseif (preg_match('/(\\[|^|\\b)(-?[0-9]+[0-9.]*)[, ]+(-?[0-9]+[0-9.]*)(\\]|$|\\b)/', $sQuery, $aData))
                {
                {
-                       $_GET['nearlat'] = $aData[2];
-                       $_GET['nearlon'] = $aData[3];
-                       $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
+                       $fQueryLat = $aData[2];
+                       $fQueryLon = $aData[3];
+                       if ($fQueryLat <= 90.1 && $fQueryLat >= -90.1 && $fQueryLon <= 180.1 && $fQueryLon >= -180.1)
+                       {
+                               $_GET['nearlat'] = $fQueryLat;
+                               $_GET['nearlon'] = $fQueryLon;
+                               $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
+                       }
                }
 
                if ($sQuery)
                }
 
                if ($sQuery)
                                $aPhrase = $oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string");
                                if (PEAR::isError($aPhrase))
                                {
                                $aPhrase = $oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string");
                                if (PEAR::isError($aPhrase))
                                {
-                                       var_dump($aPhrase);
+                                       echo "Illegal query string (not an UTF-8 string): ".$sPhrase;
+                                       if (CONST_Debug) var_dump($aPhrase);
                                        exit;
                                }
                                if (trim($aPhrase['string']))
                                        exit;
                                }
                                if (trim($aPhrase['string']))
                        // Check which tokens we have, get the ID numbers                       
                        $sSQL = 'select word_id,word_token, word, class, type, location, country_code, operator';
                        $sSQL .= ' from word where word_token in ('.join(',',array_map("getDBQuoted",$aTokens)).')';
                        // Check which tokens we have, get the ID numbers                       
                        $sSQL = 'select word_id,word_token, word, class, type, location, country_code, operator';
                        $sSQL .= ' from word where word_token in ('.join(',',array_map("getDBQuoted",$aTokens)).')';
-                       $sSQL .= ' and (class is null or class not in (\'highway\'))';
+                       // HACK WARNING
+                       // (mis)using search_name_count to exclude words that return too many
+                       // search results. saerch_name_count is currently set to 1 by hand
+                       // because there is no fast way to extract this count from a live database. 
+                       $sSQL .= ' and search_name_count = 0';
+//                     $sSQL .= ' and (class is null or class not in (\'highway\'))';
 //                     $sSQL .= ' group by word_token, word, class, type, location, country_code';
 
                        if (CONST_Debug) var_Dump($sSQL);
 //                     $sSQL .= ' group by word_token, word, class, type, location, country_code';
 
                        if (CONST_Debug) var_Dump($sSQL);
                                $aDatabaseWords = array();
                        if (PEAR::IsError($aDatabaseWords))
                        {
                                $aDatabaseWords = array();
                        if (PEAR::IsError($aDatabaseWords))
                        {
-                               var_dump($sSQL, $aDatabaseWords);
-                               exit;
+                               failInternalError("Could not get word tokens.", $sSQL, $aDatabaseWords);
                        }
                        $aPossibleMainWordIDs = array();
                        foreach($aDatabaseWords as $aToken)
                        }
                        $aPossibleMainWordIDs = array();
                        foreach($aDatabaseWords as $aToken)
                                }
                        }
                        if ($bSuggestion) $sSuggestion = join(', ',$aSuggestion);
                                }
                        }
                        if ($bSuggestion) $sSuggestion = join(', ',$aSuggestion);
-/*
+
                        // Try and calculate GB postcodes we might be missing
                        foreach($aTokens as $sToken)
                        {
                        // Try and calculate GB postcodes we might be missing
                        foreach($aTokens as $sToken)
                        {
                                        }
                                }
                        }
                                        }
                                }
                        }
-*/
 
                        foreach($aTokens as $sToken)
                        {
                                // Unknown single word token with a number - assume it is a house number
                                if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken,' ') === false && preg_match('/[0-9]/', $sToken))
                                {
 
                        foreach($aTokens as $sToken)
                        {
                                // Unknown single word token with a number - assume it is a house number
                                if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken,' ') === false && preg_match('/[0-9]/', $sToken))
                                {
-                                       $aValidTokens[' '.$sToken] = array('class'=>'place','type'=>'house');
+                                       $aValidTokens[' '.$sToken] = array(array('class'=>'place','type'=>'house'));
                                }
                        }
 
                                }
                        }
 
                                                                        $sSQL .= " limit ".$iLimit;
 
                                                                if (CONST_Debug) var_dump($sSQL);
                                                                        $sSQL .= " limit ".$iLimit;
 
                                                                if (CONST_Debug) var_dump($sSQL);
+                                                               $iStartTime = time();
                                                                $aViewBoxPlaceIDs = $oDB->getAll($sSQL);
                                                                if (PEAR::IsError($aViewBoxPlaceIDs))
                                                                {
                                                                $aViewBoxPlaceIDs = $oDB->getAll($sSQL);
                                                                if (PEAR::IsError($aViewBoxPlaceIDs))
                                                                {
-                                                                       var_dump($sSQL, $aViewBoxPlaceIDs);                                     
-                                                                       exit;
+                                                                       failInternalError("Could not get places for search terms.", $sSQL, $aViewBoxPlaceIDs);
                                                                }
                                                                }
+                                                               if (time() - $iStartTime > 60) {
+                                                                       file_put_contents(CONST_BasePath.'/log/long_queries.log', date('Y-m-d H:i:s', $iStartTime).' '.$sSQL."\n", FILE_APPEND);
+                                                               }
+
 //var_dump($aViewBoxPlaceIDs);
                                                                // Did we have an viewbox matches?
                                                                $aPlaceIDs = array();
 //var_dump($aViewBoxPlaceIDs);
                                                                // Did we have an viewbox matches?
                                                                $aPlaceIDs = array();
                                                                $sPlaceIDs = join(',',$aPlaceIDs);
        
                                                                // Now they are indexed look for a house attached to a street we found
                                                                $sPlaceIDs = join(',',$aPlaceIDs);
        
                                                                // Now they are indexed look for a house attached to a street we found
-                                                               $sHouseNumberRegex = '\\\\m'.str_replace(' ','[-, ]',$aSearch['sHouseNumber']).'\\\\M';                                         
+                                                               $sHouseNumberRegex = '\\\\m'.str_replace(' ','[-,/ ]',$aSearch['sHouseNumber']).'\\\\M';                                                
                                                                $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and housenumber ~* E'".$sHouseNumberRegex."'";
                                                                if (sizeof($aExcludePlaceIDs))
                                                                {
                                                                $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and housenumber ~* E'".$sHouseNumberRegex."'";
                                                                if (sizeof($aExcludePlaceIDs))
                                                                {
                                                                        if ($bCacheTable)
                                                                        {
                                                                                // More efficient - can make the range bigger
                                                                        if ($bCacheTable)
                                                                        {
                                                                                // More efficient - can make the range bigger
-                                                                               $fRange = 0.05;
+                                                                               $fRange = 0.05;
+
+                                                                               $sOrderBySQL = '';
+                                                                               if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
+                                                                               else if ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
+                                                                               else if ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
                                                                                
                                                                                
-                                                                               $sSQL = "select l.place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
+                                                                               $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
                                                                                if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
                                                                                if ($sPlaceIDs)
                                                                                {
                                                                                if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
                                                                                if ($sPlaceIDs)
                                                                                {
                                                                                        $sSQL .= " and l.place_id not in (".join(',',$aExcludePlaceIDs).")";
                                                                                }
                                                                                if ($sCountryCodesSQL) $sSQL .= " and lp.country_code in ($sCountryCodesSQL)";
                                                                                        $sSQL .= " and l.place_id not in (".join(',',$aExcludePlaceIDs).")";
                                                                                }
                                                                                if ($sCountryCodesSQL) $sSQL .= " and lp.country_code in ($sCountryCodesSQL)";
-                                                                               if ($sNearPointSQL) $sSQL .= " order by ST_Distance($sNearPointSQL, l.centroid) ASC";
-                                                                               else if ($sPlaceIDs) $sSQL .= " order by ST_Distance(l.centroid, f.geometry) asc";
+                                                                               if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
+                                                                               if ($iOffset) $sSQL .= " offset $iOffset";
                                                                                $sSQL .= " limit $iLimit";
                                                                                if (CONST_Debug) var_dump($sSQL);
                                                                                $aPlaceIDs = $oDB->getCol($sSQL);
                                                                                $sSQL .= " limit $iLimit";
                                                                                if (CONST_Debug) var_dump($sSQL);
                                                                                $aPlaceIDs = $oDB->getCol($sSQL);
                                                                        else
                                                                        {
                                                                                if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
                                                                        else
                                                                        {
                                                                                if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
-                                                                               $sSQL = "select l.place_id from placex as l,placex as f where ";
+
+                                                                               $sOrderBySQL = '';
+                                                                               if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
+                                                                               else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
+
+                                                                               $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from placex as l,placex as f where ";
                                                                                $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, st_centroid(f.geometry), $fRange) ";
                                                                                $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
                                                                                if (sizeof($aExcludePlaceIDs))
                                                                                $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, st_centroid(f.geometry), $fRange) ";
                                                                                $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
                                                                                if (sizeof($aExcludePlaceIDs))
                                                                                        $sSQL .= " and l.place_id not in (".join(',',$aExcludePlaceIDs).")";
                                                                                }
                                                                                if ($sCountryCodesSQL) $sSQL .= " and l.country_code in ($sCountryCodesSQL)";                                                           
                                                                                        $sSQL .= " and l.place_id not in (".join(',',$aExcludePlaceIDs).")";
                                                                                }
                                                                                if ($sCountryCodesSQL) $sSQL .= " and l.country_code in ($sCountryCodesSQL)";                                                           
-                                                                               if ($sNearPointSQL) $sSQL .= " order by ST_Distance($sNearPointSQL, l.geometry) ASC";
-                                                                               else $sSQL .= " order by ST_Distance(l.geometry, f.geometry) asc, l.rank_search ASC";
+                                                                               if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
+                                                                               if ($iOffset) $sSQL .= " offset $iOffset";
                                                                                $sSQL .= " limit $iLimit";
                                                                                if (CONST_Debug) var_dump($sSQL);
                                                                                $aPlaceIDs = $oDB->getCol($sSQL);
                                                                                $sSQL .= " limit $iLimit";
                                                                                if (CONST_Debug) var_dump($sSQL);
                                                                                $aPlaceIDs = $oDB->getCol($sSQL);
 
                                                if (PEAR::IsError($aPlaceIDs))
                                                {
 
                                                if (PEAR::IsError($aPlaceIDs))
                                                {
-                                                       var_dump($sSQL, $aPlaceIDs);                                    
-                                                       exit;
+                                                       failInternalError("Could not get place IDs from tokens." ,$sSQL, $aPlaceIDs);
                                                }
 
                                                if (CONST_Debug) var_Dump($aPlaceIDs);
                                                }
 
                                                if (CONST_Debug) var_Dump($aPlaceIDs);
 
                                        if (PEAR::IsError($aSearchResults))
                                        {
 
                                        if (PEAR::IsError($aSearchResults))
                                        {
-                                               var_dump($sSQL, $aSearchResults);                                       
-                                               exit;
+                                               failInternalError("Could not get details for place.", $sSQL, $aSearchResults);
                                        }
                                }
                        } // end if ($sQuery)
                                        }
                                }
                        } // end if ($sQuery)
 
                                        if (PEAR::IsError($aSearchResults))
                                        {
 
                                        if (PEAR::IsError($aSearchResults))
                                        {
-                                               var_dump($sSQL, $aSearchResults);                                       
-                                               exit;
+                        failInternalError("Could not get details for place (near).", $sSQL, $aSearchResults);
                                        }
                                }
                        }
                                        }
                                }
                        }
                        $aPointPolygon = $oDB->getRow($sSQL);
                        if (PEAR::IsError($aPointPolygon))
                        {
                        $aPointPolygon = $oDB->getRow($sSQL);
                        if (PEAR::IsError($aPointPolygon))
                        {
-                               var_dump($sSQL, $aPointPolygon);
-                               exit;
+                               failInternalError("Could not get outline.", $sSQL, $aPointPolygon);
                        }
                        if ($aPointPolygon['place_id'])
                        {
                        }
                        if ($aPointPolygon['place_id'])
                        {
                                }
 
                                // Output data suitable for display (points and a bounding box)
                                }
 
                                // Output data suitable for display (points and a bounding box)
-                               if ($bShowPolygons)
+                               if ($bShowPolygons && isset($aPolyPoints))
                                {
                                        $aResult['aPolyPoints'] = array();
                                        foreach($aPolyPoints as $aPoint)
                                {
                                        $aResult['aPolyPoints'] = array();
                                        foreach($aPolyPoints as $aPoint)