]> git.openstreetmap.org Git - nominatim.git/blob - lib/Geocode.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / lib / Geocode.php
1 <?php
2         class Geocode
3         {
4                 protected $oDB;
5
6                 protected $aLangPrefOrder = array();
7
8                 protected $bIncludeAddressDetails = false;
9
10                 protected $bIncludePolygonAsPoints = false;
11                 protected $bIncludePolygonAsText = false;
12                 protected $bIncludePolygonAsGeoJSON = false;
13                 protected $bIncludePolygonAsKML = false;
14                 protected $bIncludePolygonAsSVG = false;
15
16                 protected $aExcludePlaceIDs = array();
17                 protected $bDeDupe = true;
18                 protected $bReverseInPlan = true;
19
20                 protected $iLimit = 20;
21                 protected $iFinalLimit = 10;
22                 protected $iOffset = 0;
23                 protected $bFallback = false;
24
25                 protected $aCountryCodes = false;
26                 protected $aNearPoint = false;
27
28                 protected $bBoundedSearch = false;
29                 protected $aViewBox = false;
30                 protected $sViewboxSmallSQL = false;
31                 protected $sViewboxLargeSQL = false;
32                 protected $aRoutePoints = false;
33
34                 protected $iMaxRank = 20;
35                 protected $iMinAddressRank = 0;
36                 protected $iMaxAddressRank = 30;
37                 protected $aAddressRankList = array();
38                 protected $exactMatchCache = array();
39
40                 protected $sAllowedTypesSQLList = false;
41
42                 protected $sQuery = false;
43                 protected $aStructuredQuery = false;
44
45                 function Geocode(&$oDB)
46                 {
47                         $this->oDB =& $oDB;
48                 }
49
50                 function setReverseInPlan($bReverse)
51                 {
52                         $this->bReverseInPlan = $bReverse;
53                 }
54
55                 function setLanguagePreference($aLangPref)
56                 {
57                         $this->aLangPrefOrder = $aLangPref;
58                 }
59
60                 function setIncludeAddressDetails($bAddressDetails = true)
61                 {
62                         $this->bIncludeAddressDetails = (bool)$bAddressDetails;
63                 }
64
65                 function getIncludeAddressDetails()
66                 {
67                         return $this->bIncludeAddressDetails;
68                 }
69
70                 function setIncludePolygonAsPoints($b = true)
71                 {
72                         $this->bIncludePolygonAsPoints = $b;
73                 }
74
75                 function getIncludePolygonAsPoints()
76                 {
77                         return $this->bIncludePolygonAsPoints;
78                 }
79
80                 function setIncludePolygonAsText($b = true)
81                 {
82                         $this->bIncludePolygonAsText = $b;
83                 }
84
85                 function getIncludePolygonAsText()
86                 {
87                         return $this->bIncludePolygonAsText;
88                 }
89
90                 function setIncludePolygonAsGeoJSON($b = true)
91                 {
92                         $this->bIncludePolygonAsGeoJSON = $b;
93                 }
94
95                 function setIncludePolygonAsKML($b = true)
96                 {
97                         $this->bIncludePolygonAsKML = $b;
98                 }
99
100                 function setIncludePolygonAsSVG($b = true)
101                 {
102                         $this->bIncludePolygonAsSVG = $b;
103                 }
104
105                 function setDeDupe($bDeDupe = true)
106                 {
107                         $this->bDeDupe = (bool)$bDeDupe;
108                 }
109
110                 function setLimit($iLimit = 10)
111                 {
112                         if ($iLimit > 50) $iLimit = 50;
113                         if ($iLimit < 1) $iLimit = 1;
114
115                         $this->iFinalLimit = $iLimit;
116                         $this->iLimit = $this->iFinalLimit + min($this->iFinalLimit, 10);
117                 }
118
119                 function setOffset($iOffset = 0)
120                 {
121                         $this->iOffset = $iOffset;
122                 }
123
124                 function setFallback($bFallback = true)
125                 {
126                         $this->bFallback = (bool)$bFallback;
127                 }
128
129                 function setExcludedPlaceIDs($a)
130                 {
131                         // TODO: force to int
132                         $this->aExcludePlaceIDs = $a;
133                 }
134
135                 function getExcludedPlaceIDs()
136                 {
137                         return $this->aExcludePlaceIDs;
138                 }
139
140                 function setBounded($bBoundedSearch = true)
141                 {
142                         $this->bBoundedSearch = (bool)$bBoundedSearch;
143                 }
144
145                 function setViewBox($fLeft, $fBottom, $fRight, $fTop)
146                 {
147                         $this->aViewBox = array($fLeft, $fBottom, $fRight, $fTop);
148                 }
149
150                 function getViewBoxString()
151                 {
152                         if (!$this->aViewBox) return null;
153                         return $this->aViewBox[0].','.$this->aViewBox[3].','.$this->aViewBox[2].','.$this->aViewBox[1];
154                 }
155
156                 function setRoute($aRoutePoints)
157                 {
158                         $this->aRoutePoints = $aRoutePoints;
159                 }
160
161                 function setFeatureType($sFeatureType)
162                 {
163                         switch($sFeatureType)
164                         {
165                         case 'country':
166                                 $this->setRankRange(4, 4);
167                                 break;
168                         case 'state':
169                                 $this->setRankRange(8, 8);
170                                 break;
171                         case 'city':
172                                 $this->setRankRange(14, 16);
173                                 break;
174                         case 'settlement':
175                                 $this->setRankRange(8, 20);
176                                 break;
177                         }
178                 }
179
180                 function setRankRange($iMin, $iMax)
181                 {
182                         $this->iMinAddressRank = (int)$iMin;
183                         $this->iMaxAddressRank = (int)$iMax;
184                 }
185
186                 function setNearPoint($aNearPoint, $fRadiusDeg = 0.1)
187                 {
188                         $this->aNearPoint = array((float)$aNearPoint[0], (float)$aNearPoint[1], (float)$fRadiusDeg);
189                 }
190
191                 function setCountryCodesList($aCountryCodes)
192                 {
193                         $this->aCountryCodes = $aCountryCodes;
194                 }
195
196                 function setQuery($sQueryString)
197                 {
198                         $this->sQuery = $sQueryString;
199                         $this->aStructuredQuery = false;
200                 }
201
202                 function getQueryString()
203                 {
204                         return $this->sQuery;
205                 }
206
207
208                 function loadParamArray($aParams)
209                 {
210                         if (isset($aParams['addressdetails'])) $this->bIncludeAddressDetails = (bool)$aParams['addressdetails'];
211                         if (isset($aParams['bounded'])) $this->bBoundedSearch = (bool)$aParams['bounded'];
212                         if (isset($aParams['dedupe'])) $this->bDeDupe = (bool)$aParams['dedupe'];
213
214                         if (isset($aParams['limit'])) $this->setLimit((int)$aParams['limit']);
215                         if (isset($aParams['offset'])) $this->iOffset = (int)$aParams['offset'];
216
217                         if (isset($aParams['fallback'])) $this->bFallback = (bool)$aParams['fallback'];
218
219                         // List of excluded Place IDs - used for more acurate pageing
220                         if (isset($aParams['exclude_place_ids']) && $aParams['exclude_place_ids'])
221                         {
222                                 foreach(explode(',',$aParams['exclude_place_ids']) as $iExcludedPlaceID)
223                                 {
224                                         $iExcludedPlaceID = (int)$iExcludedPlaceID;
225                                         if ($iExcludedPlaceID) $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
226                                 }
227                                 $this->aExcludePlaceIDs = $aExcludePlaceIDs;
228                         }
229
230                         // Only certain ranks of feature
231                         if (isset($aParams['featureType'])) $this->setFeatureType($aParams['featureType']);
232                         if (isset($aParams['featuretype'])) $this->setFeatureType($aParams['featuretype']);
233
234                         // Country code list
235                         if (isset($aParams['countrycodes']))
236                         {
237                                 $aCountryCodes = array();
238                                 foreach(explode(',',$aParams['countrycodes']) as $sCountryCode)
239                                 {
240                                         if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode))
241                                         {
242                                                 $aCountryCodes[] = strtolower($sCountryCode);
243                                         }
244                                 }
245                                 $this->aCountryCodes = $aCountryCodes;
246                         }
247
248                         if (isset($aParams['viewboxlbrt']) && $aParams['viewboxlbrt'])
249                         {
250                                 $aCoOrdinatesLBRT = explode(',',$aParams['viewboxlbrt']);
251                                 $this->setViewBox($aCoOrdinatesLBRT[0], $aCoOrdinatesLBRT[1], $aCoOrdinatesLBRT[2], $aCoOrdinatesLBRT[3]);
252                         }
253                         else if (isset($aParams['viewbox']) && $aParams['viewbox'])
254                         {
255                                 $aCoOrdinatesLTRB = explode(',',$aParams['viewbox']);
256                                 $this->setViewBox($aCoOrdinatesLTRB[0], $aCoOrdinatesLTRB[3], $aCoOrdinatesLTRB[2], $aCoOrdinatesLTRB[1]);
257                         }
258
259                         if (isset($aParams['route']) && $aParams['route'] && isset($aParams['routewidth']) && $aParams['routewidth'])
260                         {
261                                 $aPoints = explode(',',$aParams['route']);
262                                 if (sizeof($aPoints) % 2 != 0)
263                                 {
264                                         userError("Uneven number of points");
265                                         exit;
266                                 }
267                                 $fPrevCoord = false;
268                                 $aRoute = array();
269                                 foreach($aPoints as $i => $fPoint)
270                                 {
271                                         if ($i%2)
272                                         {
273                                                 $aRoute[] = array((float)$fPoint, $fPrevCoord);
274                                         }
275                                         else
276                                         {
277                                                 $fPrevCoord = (float)$fPoint;
278                                         }
279                                 }
280                                 $this->aRoutePoints = $aRoute;
281                         }
282                 }
283
284                 function setQueryFromParams($aParams)
285                 {
286                         // Search query
287                         $sQuery = (isset($aParams['q'])?trim($aParams['q']):'');
288                         if (!$sQuery)
289                         {
290                                 $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
291                                 $this->setReverseInPlan(false);
292                         }
293                         else
294                         {
295                                 $this->setQuery($sQuery);
296                         }
297                 }
298
299                 function loadStructuredAddressElement($sValue, $sKey, $iNewMinAddressRank, $iNewMaxAddressRank, $aItemListValues)
300                 {
301                         $sValue = trim($sValue);
302                         if (!$sValue) return false;
303                         $this->aStructuredQuery[$sKey] = $sValue;
304                         if ($this->iMinAddressRank == 0 && $this->iMaxAddressRank == 30)
305                         {
306                                 $this->iMinAddressRank = $iNewMinAddressRank;
307                                 $this->iMaxAddressRank = $iNewMaxAddressRank;
308                         }
309                         if ($aItemListValues) $this->aAddressRankList = array_merge($this->aAddressRankList, $aItemListValues);
310                         return true;
311                 }
312
313                 function setStructuredQuery($sAmentiy = false, $sStreet = false, $sCity = false, $sCounty = false, $sState = false, $sCountry = false, $sPostalCode = false)
314                 {
315                         $this->sQuery = false;
316
317                         // Reset
318                         $this->iMinAddressRank = 0;
319                         $this->iMaxAddressRank = 30;
320                         $this->aAddressRankList = array();
321
322                         $this->aStructuredQuery = array();
323                         $this->sAllowedTypesSQLList = '';
324
325                         $this->loadStructuredAddressElement($sAmentiy, 'amenity', 26, 30, false);
326                         $this->loadStructuredAddressElement($sStreet, 'street', 26, 30, false);
327                         $this->loadStructuredAddressElement($sCity, 'city', 14, 24, false);
328                         $this->loadStructuredAddressElement($sCounty, 'county', 9, 13, false);
329                         $this->loadStructuredAddressElement($sState, 'state', 8, 8, false);
330                         $this->loadStructuredAddressElement($sPostalCode, 'postalcode' , 5, 11, array(5, 11));
331                         $this->loadStructuredAddressElement($sCountry, 'country', 4, 4, false);
332
333                         if (sizeof($this->aStructuredQuery) > 0) 
334                         {
335                                 $this->sQuery = join(', ', $this->aStructuredQuery);
336                                 if ($this->iMaxAddressRank < 30)
337                                 {
338                                         $sAllowedTypesSQLList = '(\'place\',\'boundary\')';
339                                 }
340                         }
341                 }
342
343                 function fallbackStructuredQuery()
344                 {
345                         if (!$this->aStructuredQuery) return false;
346
347                         $aParams = $this->aStructuredQuery;
348
349                         if (sizeof($aParams) == 1) return false;
350
351                         $aOrderToFallback = array('postalcode', 'street', 'city', 'county', 'state');
352
353                         foreach($aOrderToFallback as $sType)
354                         {
355                                 if (isset($aParams[$sType]))
356                                 {
357                                         unset($aParams[$sType]);
358                                         $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
359                                         return true;
360                                 }
361                         }
362
363                         return false;
364                 }
365
366                 function getDetails($aPlaceIDs)
367                 {
368                         if (sizeof($aPlaceIDs) == 0)  return array();
369
370                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
371
372                         // Get the details for display (is this a redundant extra step?)
373                         $sPlaceIDs = join(',',$aPlaceIDs);
374
375                         $sImportanceSQL = '';
376                         if ($this->sViewboxSmallSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxSmallSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
377                         if ($this->sViewboxLargeSQL) $sImportanceSQL .= " case when ST_Contains($this->sViewboxLargeSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
378
379                         $sSQL = "select osm_type,osm_id,class,type,admin_level,rank_search,rank_address,min(place_id) as place_id, min(parent_place_id) as parent_place_id, calculated_country_code as country_code,";
380                         $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
381                         $sSQL .= "get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
382                         $sSQL .= "get_name_by_language(name, ARRAY['ref']) as ref,";
383                         $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
384                         $sSQL .= $sImportanceSQL."coalesce(importance,0.75-(rank_search::float/40)) as importance, ";
385                         $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(CASE WHEN placex.rank_search < 28 THEN placex.place_id ELSE placex.parent_place_id END) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, ";
386                         $sSQL .= "(extratags->'place') as extra_place ";
387                         $sSQL .= "from placex where place_id in ($sPlaceIDs) ";
388                         $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
389                         if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
390                         if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
391                         $sSQL .= ") ";
392                         if ($this->sAllowedTypesSQLList) $sSQL .= "and placex.class in $this->sAllowedTypesSQLList ";
393                         $sSQL .= "and linked_place_id is null ";
394                         $sSQL .= "group by osm_type,osm_id,class,type,admin_level,rank_search,rank_address,calculated_country_code,importance";
395                         if (!$this->bDeDupe) $sSQL .= ",place_id";
396                         $sSQL .= ",langaddress ";
397                         $sSQL .= ",placename ";
398                         $sSQL .= ",ref ";
399                         $sSQL .= ",extratags->'place' ";
400
401                         if (30 >= $this->iMinAddressRank && 30 <= $this->iMaxAddressRank)
402                         {
403                                 $sSQL .= " union ";
404                                 $sSQL .= "select 'T' as osm_type,place_id as osm_id,'place' as class,'house' as type,null as admin_level,30 as rank_search,30 as rank_address,min(place_id) as place_id, min(parent_place_id) as parent_place_id,'us' as country_code,";
405                                 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
406                                 $sSQL .= "null as placename,";
407                                 $sSQL .= "null as ref,";
408                                 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
409                                 $sSQL .= $sImportanceSQL."-1.15 as importance, ";
410                                 $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(location_property_tiger.parent_place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, ";
411                                 $sSQL .= "null as extra_place ";
412                                 $sSQL .= "from location_property_tiger where place_id in ($sPlaceIDs) ";
413                                 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
414                                 $sSQL .= "group by place_id";
415                                 if (!$this->bDeDupe) $sSQL .= ",place_id ";
416                                 /*
417                                 $sSQL .= " union ";
418                                 $sSQL .= "select 'L' as osm_type,place_id as osm_id,'place' as class,'house' as type,null as admin_level,30 as rank_search,30 as rank_address,min(place_id) as place_id, min(parent_place_id) as parent_place_id,'us' as country_code,";
419                                 $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
420                                 $sSQL .= "null as placename,";
421                                 $sSQL .= "null as ref,";
422                                 $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
423                                 $sSQL .= $sImportanceSQL."-1.10 as importance, ";
424                                 $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(location_property_aux.parent_place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, ";
425                                 $sSQL .= "null as extra_place ";
426                                 $sSQL .= "from location_property_aux where place_id in ($sPlaceIDs) ";
427                                 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
428                                 $sSQL .= "group by place_id";
429                                 if (!$this->bDeDupe) $sSQL .= ",place_id";
430                                 $sSQL .= ",get_address_by_language(place_id, $sLanguagePrefArraySQL) ";
431                                 */
432                         }
433
434                         $sSQL .= " order by importance desc";
435                         if (CONST_Debug) { echo "<hr>"; var_dump($sSQL); }
436                         $aSearchResults = $this->oDB->getAll($sSQL);
437
438                         if (PEAR::IsError($aSearchResults))
439                         {
440                                 failInternalError("Could not get details for place.", $sSQL, $aSearchResults);
441                         }
442
443                         return $aSearchResults;
444                 }
445
446                 function getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases)
447                 {
448                         /*
449                            Calculate all searches using aValidTokens i.e.
450                            'Wodsworth Road, Sheffield' =>
451
452                            Phrase Wordset
453                            0      0       (wodsworth road)
454                            0      1       (wodsworth)(road)
455                            1      0       (sheffield)
456
457                            Score how good the search is so they can be ordered
458                          */
459                         foreach($aPhrases as $iPhrase => $sPhrase)
460                         {
461                                 $aNewPhraseSearches = array();
462                                 if ($bStructuredPhrases) $sPhraseType = $aPhraseTypes[$iPhrase];
463                                 else $sPhraseType = '';
464
465                                 foreach($aPhrases[$iPhrase]['wordsets'] as $iWordSet => $aWordset)
466                                 {
467                                         // Too many permutations - too expensive
468                                         if ($iWordSet > 120) break;
469
470                                         $aWordsetSearches = $aSearches;
471
472                                         // Add all words from this wordset
473                                         foreach($aWordset as $iToken => $sToken)
474                                         {
475                                                 //echo "<br><b>$sToken</b>";
476                                                 $aNewWordsetSearches = array();
477
478                                                 foreach($aWordsetSearches as $aCurrentSearch)
479                                                 {
480                                                         //echo "<i>";
481                                                         //var_dump($aCurrentSearch);
482                                                         //echo "</i>";
483
484                                                         // If the token is valid
485                                                         if (isset($aValidTokens[' '.$sToken]))
486                                                         {
487                                                                 foreach($aValidTokens[' '.$sToken] as $aSearchTerm)
488                                                                 {
489                                                                         $aSearch = $aCurrentSearch;
490                                                                         $aSearch['iSearchRank']++;
491                                                                         if (($sPhraseType == '' || $sPhraseType == 'country') && !empty($aSearchTerm['country_code']) && $aSearchTerm['country_code'] != '0')
492                                                                         {
493                                                                                 if ($aSearch['sCountryCode'] === false)
494                                                                                 {
495                                                                                         $aSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
496                                                                                         // Country is almost always at the end of the string - increase score for finding it anywhere else (optimisation)
497                                                                                         if (($iToken+1 != sizeof($aWordset) || $iPhrase+1 != sizeof($aPhrases)))
498                                                                                         {
499                                                                                                 $aSearch['iSearchRank'] += 5;
500                                                                                         }
501                                                                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
502                                                                                 }
503                                                                         }
504                                                                         elseif (isset($aSearchTerm['lat']) && $aSearchTerm['lat'] !== '' && $aSearchTerm['lat'] !== null)
505                                                                         {
506                                                                                 if ($aSearch['fLat'] === '')
507                                                                                 {
508                                                                                         $aSearch['fLat'] = $aSearchTerm['lat'];
509                                                                                         $aSearch['fLon'] = $aSearchTerm['lon'];
510                                                                                         $aSearch['fRadius'] = $aSearchTerm['radius'];
511                                                                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
512                                                                                 }
513                                                                         }
514                                                                         elseif ($sPhraseType == 'postalcode')
515                                                                         {
516                                                                                 // We need to try the case where the postal code is the primary element (i.e. no way to tell if it is (postalcode, city) OR (city, postalcode) so try both
517                                                                                 if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
518                                                                                 {
519                                                                                         // If we already have a name try putting the postcode first
520                                                                                         if (sizeof($aSearch['aName']))
521                                                                                         {
522                                                                                                 $aNewSearch = $aSearch;
523                                                                                                 $aNewSearch['aAddress'] = array_merge($aNewSearch['aAddress'], $aNewSearch['aName']);
524                                                                                                 $aNewSearch['aName'] = array();
525                                                                                                 $aNewSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
526                                                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aNewSearch;
527                                                                                         }
528
529                                                                                         if (sizeof($aSearch['aName']))
530                                                                                         {
531                                                                                                 if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false))
532                                                                                                 {
533                                                                                                         $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
534                                                                                                 }
535                                                                                                 else
536                                                                                                 {
537                                                                                                         $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
538                                                                                                         $aSearch['iSearchRank'] += 1000; // skip;
539                                                                                                 }
540                                                                                         }
541                                                                                         else
542                                                                                         {
543                                                                                                 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
544                                                                                                 //$aSearch['iNamePhrase'] = $iPhrase;
545                                                                                         }
546                                                                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
547                                                                                 }
548
549                                                                         }
550                                                                         elseif (($sPhraseType == '' || $sPhraseType == 'street') && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house')
551                                                                         {
552                                                                                 if ($aSearch['sHouseNumber'] === '')
553                                                                                 {
554                                                                                         $aSearch['sHouseNumber'] = $sToken;
555                                                                                         // sanity check: if the housenumber is not mainly made
556                                                                                         // up of numbers, add a penalty
557                                                                                         if (preg_match_all("/[^0-9]/", $sToken, $aMatches) > 2) $aSearch['iSearchRank']++;
558                                                                                         // also housenumbers should appear in the first or second phrase
559                                                                                         if ($iPhrase > 1) $aSearch['iSearchRank'] += 1;
560                                                                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
561                                                                                         /*
562                                                                                         // Fall back to not searching for this item (better than nothing)
563                                                                                         $aSearch = $aCurrentSearch;
564                                                                                         $aSearch['iSearchRank'] += 1;
565                                                                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
566                                                                                          */
567                                                                                 }
568                                                                         }
569                                                                         elseif ($sPhraseType == '' && $aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null)
570                                                                         {
571                                                                                 if ($aSearch['sClass'] === '')
572                                                                                 {
573                                                                                         $aSearch['sOperator'] = $aSearchTerm['operator'];
574                                                                                         $aSearch['sClass'] = $aSearchTerm['class'];
575                                                                                         $aSearch['sType'] = $aSearchTerm['type'];
576                                                                                         if (sizeof($aSearch['aName'])) $aSearch['sOperator'] = 'name';
577                                                                                         else $aSearch['sOperator'] = 'near'; // near = in for the moment
578                                                                                         if (strlen($aSearchTerm['operator']) == 0) $aSearch['iSearchRank'] += 1;
579
580                                                                                         // Do we have a shortcut id?
581                                                                                         if ($aSearch['sOperator'] == 'name')
582                                                                                         {
583                                                                                                 $sSQL = "select get_tagpair('".$aSearch['sClass']."', '".$aSearch['sType']."')";
584                                                                                                 if ($iAmenityID = $this->oDB->getOne($sSQL))
585                                                                                                 {
586                                                                                                         $aValidTokens[$aSearch['sClass'].':'.$aSearch['sType']] = array('word_id' => $iAmenityID);
587                                                                                                         $aSearch['aName'][$iAmenityID] = $iAmenityID;
588                                                                                                         $aSearch['sClass'] = '';
589                                                                                                         $aSearch['sType'] = '';
590                                                                                                 }
591                                                                                         }
592                                                                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
593                                                                                 }
594                                                                         }
595                                                                         elseif (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
596                                                                         {
597                                                                                 if (sizeof($aSearch['aName']))
598                                                                                 {
599                                                                                         if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false))
600                                                                                         {
601                                                                                                 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
602                                                                                         }
603                                                                                         else
604                                                                                         {
605                                                                                                 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
606                                                                                                 $aSearch['iSearchRank'] += 1000; // skip;
607                                                                                         }
608                                                                                 }
609                                                                                 else
610                                                                                 {
611                                                                                         $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
612                                                                                         //$aSearch['iNamePhrase'] = $iPhrase;
613                                                                                 }
614                                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
615                                                                         }
616                                                                 }
617                                                         }
618                                                         // Look for partial matches.
619                                                         // Note that there is no point in adding country terms here
620                                                         // because country are omitted in the address.
621                                                         if (isset($aValidTokens[$sToken]) && $sPhraseType != 'country')
622                                                         {
623                                                                 // Allow searching for a word - but at extra cost
624                                                                 foreach($aValidTokens[$sToken] as $aSearchTerm)
625                                                                 {
626                                                                         if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
627                                                                         {
628                                                                                 if ((!$bStructuredPhrases || $iPhrase > 0) && sizeof($aCurrentSearch['aName']) && strpos($sToken, ' ') === false)
629                                                                                 {
630                                                                                         $aSearch = $aCurrentSearch;
631                                                                                         $aSearch['iSearchRank'] += 1;
632                                                                                         if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
633                                                                                         {
634                                                                                                 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
635                                                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
636                                                                                         }
637                                                                                         elseif (isset($aValidTokens[' '.$sToken])) // revert to the token version?
638                                                                                         {
639                                                                                                 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
640                                                                                                 $aSearch['iSearchRank'] += 1;
641                                                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
642                                                                                                 foreach($aValidTokens[' '.$sToken] as $aSearchTermToken)
643                                                                                                 {
644                                                                                                         if (empty($aSearchTermToken['country_code'])
645                                                                                                                         && empty($aSearchTermToken['lat'])
646                                                                                                                         && empty($aSearchTermToken['class']))
647                                                                                                         {
648                                                                                                                 $aSearch = $aCurrentSearch;
649                                                                                                                 $aSearch['iSearchRank'] += 1;
650                                                                                                                 $aSearch['aAddress'][$aSearchTermToken['word_id']] = $aSearchTermToken['word_id'];
651                                                                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
652                                                                                                         }
653                                                                                                 }
654                                                                                         }
655                                                                                         else
656                                                                                         {
657                                                                                                 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
658                                                                                                 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
659                                                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
660                                                                                         }
661                                                                                 }
662
663                                                                                 if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase)
664                                                                                 {
665                                                                                         $aSearch = $aCurrentSearch;
666                                                                                         $aSearch['iSearchRank'] += 1;
667                                                                                         if (!sizeof($aCurrentSearch['aName'])) $aSearch['iSearchRank'] += 1;
668                                                                                         if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
669                                                                                         if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
670                                                                                                 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
671                                                                                         else
672                                                                                                 $aSearch['aNameNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
673                                                                                         $aSearch['iNamePhrase'] = $iPhrase;
674                                                                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
675                                                                                 }
676                                                                         }
677                                                                 }
678                                                         }
679                                                         else
680                                                         {
681                                                                 // Allow skipping a word - but at EXTREAM cost
682                                                                 //$aSearch = $aCurrentSearch;
683                                                                 //$aSearch['iSearchRank']+=100;
684                                                                 //$aNewWordsetSearches[] = $aSearch;
685                                                         }
686                                                 }
687                                                 // Sort and cut
688                                                 usort($aNewWordsetSearches, 'bySearchRank');
689                                                 $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
690                                         }
691                                         //var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
692
693                                         $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
694                                         usort($aNewPhraseSearches, 'bySearchRank');
695
696                                         $aSearchHash = array();
697                                         foreach($aNewPhraseSearches as $iSearch => $aSearch)
698                                         {
699                                                 $sHash = serialize($aSearch);
700                                                 if (isset($aSearchHash[$sHash])) unset($aNewPhraseSearches[$iSearch]);
701                                                 else $aSearchHash[$sHash] = 1;
702                                         }
703
704                                         $aNewPhraseSearches = array_slice($aNewPhraseSearches, 0, 50);
705                                 }
706
707                                 // Re-group the searches by their score, junk anything over 20 as just not worth trying
708                                 $aGroupedSearches = array();
709                                 foreach($aNewPhraseSearches as $aSearch)
710                                 {
711                                         if ($aSearch['iSearchRank'] < $this->iMaxRank)
712                                         {
713                                                 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
714                                                 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
715                                         }
716                                 }
717                                 ksort($aGroupedSearches);
718
719                                 $iSearchCount = 0;
720                                 $aSearches = array();
721                                 foreach($aGroupedSearches as $iScore => $aNewSearches)
722                                 {
723                                         $iSearchCount += sizeof($aNewSearches);
724                                         $aSearches = array_merge($aSearches, $aNewSearches);
725                                         if ($iSearchCount > 50) break;
726                                 }
727
728                                 //if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
729
730                         }
731                         return $aGroupedSearches;
732
733                 }
734
735                 /* Perform the actual query lookup.
736
737                         Returns an ordered list of results, each with the following fields:
738                           osm_type: type of corresponding OSM object
739                                                         N - node
740                                                         W - way
741                                                         R - relation
742                                                         P - postcode (internally computed)
743                           osm_id: id of corresponding OSM object
744                           class: general object class (corresponds to tag key of primary OSM tag)
745                           type: subclass of object (corresponds to tag value of primary OSM tag)
746                           admin_level: see http://wiki.openstreetmap.org/wiki/Admin_level
747                           rank_search: rank in search hierarchy
748                                                         (see also http://wiki.openstreetmap.org/wiki/Nominatim/Development_overview#Country_to_street_level)
749                           rank_address: rank in address hierarchy (determines orer in address)
750                           place_id: internal key (may differ between different instances)
751                           country_code: ISO country code
752                           langaddress: localized full address
753                           placename: localized name of object
754                           ref: content of ref tag (if available)
755                           lon: longitude
756                           lat: latitude
757                           importance: importance of place based on Wikipedia link count
758                           addressimportance: cumulated importance of address elements
759                           extra_place: type of place (for admin boundaries, if there is a place tag)
760                           aBoundingBox: bounding Box
761                           label: short description of the object class/type (English only) 
762                           name: full name (currently the same as langaddress)
763                           foundorder: secondary ordering for places with same importance
764                 */
765                 function lookup()
766                 {
767                         if (!$this->sQuery && !$this->aStructuredQuery) return false;
768
769                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
770
771                         $sCountryCodesSQL = false;
772                         if ($this->aCountryCodes && sizeof($this->aCountryCodes))
773                         {
774                                 $sCountryCodesSQL = join(',', array_map('addQuotes', $this->aCountryCodes));
775                         }
776
777                         $sQuery = $this->sQuery;
778
779                         // Conflicts between US state abreviations and various words for 'the' in different languages
780                         if (isset($this->aLangPrefOrder['name:en']))
781                         {
782                                 $sQuery = preg_replace('/(^|,)\s*il\s*(,|$)/','\1illinois\2', $sQuery);
783                                 $sQuery = preg_replace('/(^|,)\s*al\s*(,|$)/','\1alabama\2', $sQuery);
784                                 $sQuery = preg_replace('/(^|,)\s*la\s*(,|$)/','\1louisiana\2', $sQuery);
785                         }
786
787                         // View Box SQL
788                         $sViewboxCentreSQL = false;
789                         $bBoundingBoxSearch = false;
790                         if ($this->aViewBox)
791                         {
792                                 $fHeight = $this->aViewBox[0]-$this->aViewBox[2];
793                                 $fWidth = $this->aViewBox[1]-$this->aViewBox[3];
794                                 $aBigViewBox[0] = $this->aViewBox[0] + $fHeight;
795                                 $aBigViewBox[2] = $this->aViewBox[2] - $fHeight;
796                                 $aBigViewBox[1] = $this->aViewBox[1] + $fWidth;
797                                 $aBigViewBox[3] = $this->aViewBox[3] - $fWidth;
798
799                                 $this->sViewboxSmallSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".(float)$this->aViewBox[0].",".(float)$this->aViewBox[1]."),ST_Point(".(float)$this->aViewBox[2].",".(float)$this->aViewBox[3].")),4326)";
800                                 $this->sViewboxLargeSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".(float)$aBigViewBox[0].",".(float)$aBigViewBox[1]."),ST_Point(".(float)$aBigViewBox[2].",".(float)$aBigViewBox[3].")),4326)";
801                                 $bBoundingBoxSearch = $this->bBoundedSearch;
802                         }
803
804                         // Route SQL
805                         if ($this->aRoutePoints)
806                         {
807                                 $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
808                                 $bFirst = true;
809                                 foreach($this->aRoutePoints as $aPoint)
810                                 {
811                                         if (!$bFirst) $sViewboxCentreSQL .= ",";
812                                         $sViewboxCentreSQL .= $aPoint[0].' '.$aPoint[1];
813                                         $bFirst = false;
814                                 }
815                                 $sViewboxCentreSQL .= ")'::geometry,4326)";
816
817                                 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/69).")";
818                                 $this->sViewboxSmallSQL = $this->oDB->getOne($sSQL);
819                                 if (PEAR::isError($this->sViewboxSmallSQL))
820                                 {
821                                         failInternalError("Could not get small viewbox.", $sSQL, $this->sViewboxSmallSQL);
822                                 }
823                                 $this->sViewboxSmallSQL = "'".$this->sViewboxSmallSQL."'::geometry";
824
825                                 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/30).")";
826                                 $this->sViewboxLargeSQL = $this->oDB->getOne($sSQL);
827                                 if (PEAR::isError($this->sViewboxLargeSQL))
828                                 {
829                                         failInternalError("Could not get large viewbox.", $sSQL, $this->sViewboxLargeSQL);
830                                 }
831                                 $this->sViewboxLargeSQL = "'".$this->sViewboxLargeSQL."'::geometry";
832                                 $bBoundingBoxSearch = $this->bBoundedSearch;
833                         }
834
835                         // Do we have anything that looks like a lat/lon pair?
836                         if ( $aLooksLike = looksLikeLatLonPair($sQuery) ){
837                                 $this->setNearPoint(array($aLooksLike['lat'], $aLooksLike['lon']));
838                                 $sQuery = $aLooksLike['query'];                 
839                         }
840
841                         $aSearchResults = array();
842                         if ($sQuery || $this->aStructuredQuery)
843                         {
844                                 // Start with a blank search
845                                 $aSearches = array(
846                                         array('iSearchRank' => 0, 'iNamePhrase' => -1, 'sCountryCode' => false, 'aName'=>array(), 'aAddress'=>array(), 'aFullNameAddress'=>array(),
847                                               'aNameNonSearch'=>array(), 'aAddressNonSearch'=>array(),
848                                               'sOperator'=>'', 'aFeatureName' => array(), 'sClass'=>'', 'sType'=>'', 'sHouseNumber'=>'', 'fLat'=>'', 'fLon'=>'', 'fRadius'=>'')
849                                 );
850
851                                 // Do we have a radius search?
852                                 $sNearPointSQL = false;
853                                 if ($this->aNearPoint)
854                                 {
855                                         $sNearPointSQL = "ST_SetSRID(ST_Point(".(float)$this->aNearPoint[1].",".(float)$this->aNearPoint[0]."),4326)";
856                                         $aSearches[0]['fLat'] = (float)$this->aNearPoint[0];
857                                         $aSearches[0]['fLon'] = (float)$this->aNearPoint[1];
858                                         $aSearches[0]['fRadius'] = (float)$this->aNearPoint[2];
859                                 }
860
861                                 // Any 'special' terms in the search?
862                                 $bSpecialTerms = false;
863                                 preg_match_all('/\\[(.*)=(.*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
864                                 $aSpecialTerms = array();
865                                 foreach($aSpecialTermsRaw as $aSpecialTerm)
866                                 {
867                                         $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
868                                         $aSpecialTerms[strtolower($aSpecialTerm[1])] = $aSpecialTerm[2];
869                                 }
870
871                                 preg_match_all('/\\[([\\w ]*)\\]/u', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
872                                 $aSpecialTerms = array();
873                                 if (isset($aStructuredQuery['amenity']) && $aStructuredQuery['amenity'])
874                                 {
875                                         $aSpecialTermsRaw[] = array('['.$aStructuredQuery['amenity'].']', $aStructuredQuery['amenity']);
876                                         unset($aStructuredQuery['amenity']);
877                                 }
878                                 foreach($aSpecialTermsRaw as $aSpecialTerm)
879                                 {
880                                         $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
881                                         $sToken = $this->oDB->getOne("select make_standard_name('".$aSpecialTerm[1]."') as string");
882                                         $sSQL = 'select * from (select word_id,word_token, word, class, type, country_code, operator';
883                                         $sSQL .= ' from word where word_token in (\' '.$sToken.'\')) as x where (class is not null and class not in (\'place\')) or country_code is not null';
884                                         if (CONST_Debug) var_Dump($sSQL);
885                                         $aSearchWords = $this->oDB->getAll($sSQL);
886                                         $aNewSearches = array();
887                                         foreach($aSearches as $aSearch)
888                                         {
889                                                 foreach($aSearchWords as $aSearchTerm)
890                                                 {
891                                                         $aNewSearch = $aSearch;
892                                                         if ($aSearchTerm['country_code'])
893                                                         {
894                                                                 $aNewSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
895                                                                 $aNewSearches[] = $aNewSearch;
896                                                                 $bSpecialTerms = true;
897                                                         }
898                                                         if ($aSearchTerm['class'])
899                                                         {
900                                                                 $aNewSearch['sClass'] = $aSearchTerm['class'];
901                                                                 $aNewSearch['sType'] = $aSearchTerm['type'];
902                                                                 $aNewSearches[] = $aNewSearch;
903                                                                 $bSpecialTerms = true;
904                                                         }
905                                                 }
906                                         }
907                                         $aSearches = $aNewSearches;
908                                 }
909
910                                 // Split query into phrases
911                                 // Commas are used to reduce the search space by indicating where phrases split
912                                 if ($this->aStructuredQuery)
913                                 {
914                                         $aPhrases = $this->aStructuredQuery;
915                                         $bStructuredPhrases = true;
916                                 }
917                                 else
918                                 {
919                                         $aPhrases = explode(',',$sQuery);
920                                         $bStructuredPhrases = false;
921                                 }
922
923                                 // Convert each phrase to standard form
924                                 // Create a list of standard words
925                                 // Get all 'sets' of words
926                                 // Generate a complete list of all
927                                 $aTokens = array();
928                                 foreach($aPhrases as $iPhrase => $sPhrase)
929                                 {
930                                         $aPhrase = $this->oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string");
931                                         if (PEAR::isError($aPhrase))
932                                         {
933                                                 userError("Illegal query string (not an UTF-8 string): ".$sPhrase);
934                                                 if (CONST_Debug) var_dump($aPhrase);
935                                                 exit;
936                                         }
937                                         if (trim($aPhrase['string']))
938                                         {
939                                                 $aPhrases[$iPhrase] = $aPhrase;
940                                                 $aPhrases[$iPhrase]['words'] = explode(' ',$aPhrases[$iPhrase]['string']);
941                                                 $aPhrases[$iPhrase]['wordsets'] = getWordSets($aPhrases[$iPhrase]['words'], 0);
942                                                 $aTokens = array_merge($aTokens, getTokensFromSets($aPhrases[$iPhrase]['wordsets']));
943                                         }
944                                         else
945                                         {
946                                                 unset($aPhrases[$iPhrase]);
947                                         }
948                                 }
949
950                                 // Reindex phrases - we make assumptions later on that they are numerically keyed in order
951                                 $aPhraseTypes = array_keys($aPhrases);
952                                 $aPhrases = array_values($aPhrases);
953
954                                 if (sizeof($aTokens))
955                                 {
956                                         // Check which tokens we have, get the ID numbers
957                                         $sSQL = 'select word_id,word_token, word, class, type, country_code, operator, search_name_count';
958                                         $sSQL .= ' from word where word_token in ('.join(',',array_map("getDBQuoted",$aTokens)).')';
959
960                                         if (CONST_Debug) var_Dump($sSQL);
961
962                                         $aValidTokens = array();
963                                         if (sizeof($aTokens)) $aDatabaseWords = $this->oDB->getAll($sSQL);
964                                         else $aDatabaseWords = array();
965                                         if (PEAR::IsError($aDatabaseWords))
966                                         {
967                                                 failInternalError("Could not get word tokens.", $sSQL, $aDatabaseWords);
968                                         }
969                                         $aPossibleMainWordIDs = array();
970                                         $aWordFrequencyScores = array();
971                                         foreach($aDatabaseWords as $aToken)
972                                         {
973                                                 // Very special case - require 2 letter country param to match the country code found
974                                                 if ($bStructuredPhrases && $aToken['country_code'] && !empty($aStructuredQuery['country'])
975                                                                 && strlen($aStructuredQuery['country']) == 2 && strtolower($aStructuredQuery['country']) != $aToken['country_code'])
976                                                 {
977                                                         continue;
978                                                 }
979
980                                                 if (isset($aValidTokens[$aToken['word_token']]))
981                                                 {
982                                                         $aValidTokens[$aToken['word_token']][] = $aToken;
983                                                 }
984                                                 else
985                                                 {
986                                                         $aValidTokens[$aToken['word_token']] = array($aToken);
987                                                 }
988                                                 if (!$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1;
989                                                 $aWordFrequencyScores[$aToken['word_id']] = $aToken['search_name_count'] + 1;
990                                         }
991                                         if (CONST_Debug) var_Dump($aPhrases, $aValidTokens);
992
993                                         // Try and calculate GB postcodes we might be missing
994                                         foreach($aTokens as $sToken)
995                                         {
996                                                 // Source of gb postcodes is now definitive - always use
997                                                 if (preg_match('/^([A-Z][A-Z]?[0-9][0-9A-Z]? ?[0-9])([A-Z][A-Z])$/', strtoupper(trim($sToken)), $aData))
998                                                 {
999                                                         if (substr($aData[1],-2,1) != ' ')
1000                                                         {
1001                                                                 $aData[0] = substr($aData[0],0,strlen($aData[1])-1).' '.substr($aData[0],strlen($aData[1])-1);
1002                                                                 $aData[1] = substr($aData[1],0,-1).' '.substr($aData[1],-1,1);
1003                                                         }
1004                                                         $aGBPostcodeLocation = gbPostcodeCalculate($aData[0], $aData[1], $aData[2], $this->oDB);
1005                                                         if ($aGBPostcodeLocation)
1006                                                         {
1007                                                                 $aValidTokens[$sToken] = $aGBPostcodeLocation;
1008                                                         }
1009                                                 }
1010                                                 // US ZIP+4 codes - if there is no token,
1011                                                 //      merge in the 5-digit ZIP code
1012                                                 else if (!isset($aValidTokens[$sToken]) && preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData))
1013                                                 {
1014                                                         if (isset($aValidTokens[$aData[1]]))
1015                                                         {
1016                                                                 foreach($aValidTokens[$aData[1]] as $aToken)
1017                                                                 {
1018                                                                         if (!$aToken['class'])
1019                                                                         {
1020                                                                                 if (isset($aValidTokens[$sToken]))
1021                                                                                 {
1022                                                                                         $aValidTokens[$sToken][] = $aToken;
1023                                                                                 }
1024                                                                                 else
1025                                                                                 {
1026                                                                                         $aValidTokens[$sToken] = array($aToken);
1027                                                                                 }
1028                                                                         }
1029                                                                 }
1030                                                         }
1031                                                 }
1032                                         }
1033
1034                                         foreach($aTokens as $sToken)
1035                                         {
1036                                                 // Unknown single word token with a number - assume it is a house number
1037                                                 if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken,' ') === false && preg_match('/[0-9]/', $sToken))
1038                                                 {
1039                                                         $aValidTokens[' '.$sToken] = array(array('class'=>'place','type'=>'house'));
1040                                                 }
1041                                         }
1042
1043                                         // Any words that have failed completely?
1044                                         // TODO: suggestions
1045
1046                                         // Start the search process
1047                                         $aResultPlaceIDs = array();
1048
1049                                         $aGroupedSearches = $this->getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases);
1050
1051                                         if ($this->bReverseInPlan)
1052                                         {
1053                                                 // Reverse phrase array and also reverse the order of the wordsets in
1054                                                 // the first and final phrase. Don't bother about phrases in the middle
1055                                                 // because order in the address doesn't matter.
1056                                                 $aPhrases = array_reverse($aPhrases);
1057                                                 $aPhrases[0]['wordsets'] = getInverseWordSets($aPhrases[0]['words'], 0);
1058                                                 if (sizeof($aPhrases) > 1)
1059                                                 {
1060                                                         $aFinalPhrase = end($aPhrases);
1061                                                         $aPhrases[sizeof($aPhrases)-1]['wordsets'] = getInverseWordSets($aFinalPhrase['words'], 0);
1062                                                 }
1063                                                 $aReverseGroupedSearches = $this->getGroupedSearches($aSearches, null, $aPhrases, $aValidTokens, $aWordFrequencyScores, false);
1064
1065                                                 foreach($aGroupedSearches as $aSearches)
1066                                                 {
1067                                                         foreach($aSearches as $aSearch)
1068                                                         {
1069                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank)
1070                                                                 {
1071                                                                         if (!isset($aReverseGroupedSearches[$aSearch['iSearchRank']])) $aReverseGroupedSearches[$aSearch['iSearchRank']] = array();
1072                                                                         $aReverseGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1073                                                                 }
1074
1075                                                         }
1076                                                 }
1077
1078                                                 $aGroupedSearches = $aReverseGroupedSearches;
1079                                                 ksort($aGroupedSearches);
1080                                         }
1081                                 }
1082                                 else
1083                                 {
1084                                         // Re-group the searches by their score, junk anything over 20 as just not worth trying
1085                                         $aGroupedSearches = array();
1086                                         foreach($aSearches as $aSearch)
1087                                         {
1088                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank)
1089                                                 {
1090                                                         if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
1091                                                         $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1092                                                 }
1093                                         }
1094                                         ksort($aGroupedSearches);
1095                                 }
1096
1097                                 if (CONST_Debug) var_Dump($aGroupedSearches);
1098
1099                                 if (CONST_Search_TryDroppedAddressTerms && sizeof($aStructuredQuery) > 0)
1100                                 {
1101                                         $aCopyGroupedSearches = $aGroupedSearches;
1102                                         foreach($aCopyGroupedSearches as $iGroup => $aSearches)
1103                                         {
1104                                                 foreach($aSearches as $iSearch => $aSearch)
1105                                                 {
1106                                                         $aReductionsList = array($aSearch['aAddress']);
1107                                                         $iSearchRank = $aSearch['iSearchRank'];
1108                                                         while(sizeof($aReductionsList) > 0)
1109                                                         {
1110                                                                 $iSearchRank += 5;
1111                                                                 if ($iSearchRank > iMaxRank) break 3;
1112                                                                 $aNewReductionsList = array();
1113                                                                 foreach($aReductionsList as $aReductionsWordList)
1114                                                                 {
1115                                                                         for ($iReductionWord = 0; $iReductionWord < sizeof($aReductionsWordList); $iReductionWord++)
1116                                                                         {
1117                                                                                 $aReductionsWordListResult = array_merge(array_slice($aReductionsWordList, 0, $iReductionWord), array_slice($aReductionsWordList, $iReductionWord+1));
1118                                                                                 $aReverseSearch = $aSearch;
1119                                                                                 $aSearch['aAddress'] = $aReductionsWordListResult;
1120                                                                                 $aSearch['iSearchRank'] = $iSearchRank;
1121                                                                                 $aGroupedSearches[$iSearchRank][] = $aReverseSearch;
1122                                                                                 if (sizeof($aReductionsWordListResult) > 0)
1123                                                                                 {
1124                                                                                         $aNewReductionsList[] = $aReductionsWordListResult;
1125                                                                                 }
1126                                                                         }
1127                                                                 }
1128                                                                 $aReductionsList = $aNewReductionsList;
1129                                                         }
1130                                                 }
1131                                         }
1132                                         ksort($aGroupedSearches);
1133                                 }
1134
1135                                 // Filter out duplicate searches
1136                                 $aSearchHash = array();
1137                                 foreach($aGroupedSearches as $iGroup => $aSearches)
1138                                 {
1139                                         foreach($aSearches as $iSearch => $aSearch)
1140                                         {
1141                                                 $sHash = serialize($aSearch);
1142                                                 if (isset($aSearchHash[$sHash]))
1143                                                 {
1144                                                         unset($aGroupedSearches[$iGroup][$iSearch]);
1145                                                         if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
1146                                                 }
1147                                                 else
1148                                                 {
1149                                                         $aSearchHash[$sHash] = 1;
1150                                                 }
1151                                         }
1152                                 }
1153
1154                                 if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
1155
1156                                 $iGroupLoop = 0;
1157                                 $iQueryLoop = 0;
1158                                 foreach($aGroupedSearches as $iGroupedRank => $aSearches)
1159                                 {
1160                                         $iGroupLoop++;
1161                                         foreach($aSearches as $aSearch)
1162                                         {
1163                                                 $iQueryLoop++;
1164
1165                                                 if (CONST_Debug) { echo "<hr><b>Search Loop, group $iGroupLoop, loop $iQueryLoop</b>"; }
1166                                                 if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
1167
1168                                                 // No location term?
1169                                                 if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon'])
1170                                                 {
1171                                                         if ($aSearch['sCountryCode'] && !$aSearch['sClass'] && !$aSearch['sHouseNumber'])
1172                                                         {
1173                                                                 // Just looking for a country by code - look it up
1174                                                                 if (4 >= $this->iMinAddressRank && 4 <= $this->iMaxAddressRank)
1175                                                                 {
1176                                                                         $sSQL = "select place_id from placex where calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
1177                                                                         if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1178                                     if ($bBoundingBoxSearch)
1179                                         $sSQL .= " and _st_intersects($this->sViewboxSmallSQL, geometry)";
1180                                                                         $sSQL .= " order by st_area(geometry) desc limit 1";
1181                                                                         if (CONST_Debug) var_dump($sSQL);
1182                                                                         $aPlaceIDs = $this->oDB->getCol($sSQL);
1183                                                                 }
1184                                                                 else
1185                                                                 {
1186                                                                         $aPlaceIDs = array();
1187                                                                 }
1188                                                         }
1189                                                         else
1190                                                         {
1191                                                                 if (!$bBoundingBoxSearch && !$aSearch['fLon']) continue;
1192                                                                 if (!$aSearch['sClass']) continue;
1193                                                                 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1194                                                                 if ($this->oDB->getOne($sSQL))
1195                                                                 {
1196                                                                         $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1197                                                                         if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1198                                                                         $sSQL .= " where st_contains($this->sViewboxSmallSQL, ct.centroid)";
1199                                                                         if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1200                                                                         if (sizeof($this->aExcludePlaceIDs))
1201                                                                         {
1202                                                                                 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1203                                                                         }
1204                                                                         if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1205                                                                         $sSQL .= " limit $this->iLimit";
1206                                                                         if (CONST_Debug) var_dump($sSQL);
1207                                                                         $aPlaceIDs = $this->oDB->getCol($sSQL);
1208
1209                                                                         // If excluded place IDs are given, it is fair to assume that
1210                                                                         // there have been results in the small box, so no further
1211                                                                         // expansion in that case.
1212                                                                         // Also don't expand if bounded results were requested.
1213                                                                         if (!sizeof($aPlaceIDs) && !sizeof($this->aExcludePlaceIDs) && !$this->bBoundedSearch)
1214                                                                         {
1215                                                                                 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1216                                                                                 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1217                                                                                 $sSQL .= " where st_contains($this->sViewboxLargeSQL, ct.centroid)";
1218                                                                                 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1219                                                                                 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1220                                                                                 $sSQL .= " limit $this->iLimit";
1221                                                                                 if (CONST_Debug) var_dump($sSQL);
1222                                                                                 $aPlaceIDs = $this->oDB->getCol($sSQL);
1223                                                                         }
1224                                                                 }
1225                                                                 else
1226                                                                 {
1227                                                                         $sSQL = "select place_id from placex where class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1228                                                                         $sSQL .= " and st_contains($this->sViewboxSmallSQL, geometry) and linked_place_id is null";
1229                                                                         if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1230                                                                         if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, centroid) asc";
1231                                                                         $sSQL .= " limit $this->iLimit";
1232                                                                         if (CONST_Debug) var_dump($sSQL);
1233                                                                         $aPlaceIDs = $this->oDB->getCol($sSQL);
1234                                                                 }
1235                                                         }
1236                                                 }
1237                                                 else
1238                                                 {
1239                                                         $aPlaceIDs = array();
1240
1241                                                         // First we need a position, either aName or fLat or both
1242                                                         $aTerms = array();
1243                                                         $aOrder = array();
1244
1245                                                         if ($aSearch['sHouseNumber'] && sizeof($aSearch['aAddress']))
1246                                                         {
1247                                                                 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1248                                                                 $aOrder[] = "exists(select place_id from placex where parent_place_id = search_name.place_id and transliteration(housenumber) ~* E'".$sHouseNumberRegex."' limit 1) desc";
1249                                                         }
1250
1251                                                         // TODO: filter out the pointless search terms (2 letter name tokens and less)
1252                                                         // they might be right - but they are just too darned expensive to run
1253                                                         if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'],",")."]";
1254                                                         //if (sizeof($aSearch['aNameNonSearch'])) $aTerms[] = "array_cat(name_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aNameNonSearch'],",")."]";
1255                                                         if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress'])
1256                                                         {
1257                                                                 // For infrequent name terms disable index usage for address
1258                                                                 if (CONST_Search_NameOnlySearchFrequencyThreshold &&
1259                                                                                 sizeof($aSearch['aName']) == 1 &&
1260                                                                                 $aWordFrequencyScores[$aSearch['aName'][reset($aSearch['aName'])]] < CONST_Search_NameOnlySearchFrequencyThreshold)
1261                                                                 {
1262                                                                         //$aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join(array_merge($aSearch['aAddress'],$aSearch['aAddressNonSearch']),",")."]";
1263                                                                         $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddress'],",")."]";
1264                                                                 }
1265                                                                 else
1266                                                                 {
1267                                                                         $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'],",")."]";
1268                                                                         //if (sizeof($aSearch['aAddressNonSearch'])) $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddressNonSearch'],",")."]";
1269                                                                 }
1270                                                         }
1271                                                         if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
1272                                                         if ($aSearch['sHouseNumber'])
1273                                                         {
1274                                                                 $aTerms[] = "address_rank between 16 and 27";
1275                                                         }
1276                                                         else
1277                                                         {
1278                                                                 if ($this->iMinAddressRank > 0)
1279                                                                 {
1280                                                                         $aTerms[] = "address_rank >= ".$this->iMinAddressRank;
1281                                                                 }
1282                                                                 if ($this->iMaxAddressRank < 30)
1283                                                                 {
1284                                                                         $aTerms[] = "address_rank <= ".$this->iMaxAddressRank;
1285                                                                 }
1286                                                         }
1287                                                         if ($aSearch['fLon'] && $aSearch['fLat'])
1288                                                         {
1289                                                                 $aTerms[] = "ST_DWithin(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326), ".$aSearch['fRadius'].")";
1290                                                                 $aOrder[] = "ST_Distance(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326)) ASC";
1291                                                         }
1292                                                         if (sizeof($this->aExcludePlaceIDs))
1293                                                         {
1294                                                                 $aTerms[] = "place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1295                                                         }
1296                                                         if ($sCountryCodesSQL)
1297                                                         {
1298                                                                 $aTerms[] = "country_code in ($sCountryCodesSQL)";
1299                                                         }
1300
1301                                                         if ($bBoundingBoxSearch) $aTerms[] = "centroid && $this->sViewboxSmallSQL";
1302                                                         if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
1303
1304                                                         if ($aSearch['sHouseNumber'])
1305                                                         {
1306                                                                 $sImportanceSQL = '- abs(26 - address_rank) + 3';
1307                                                         }
1308                                                         else
1309                                                         {
1310                                                                 $sImportanceSQL = '(case when importance = 0 OR importance IS NULL then 0.75-(search_rank::float/40) else importance end)';
1311                                                         }
1312                                                         if ($this->sViewboxSmallSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
1313                                                         if ($this->sViewboxLargeSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
1314
1315                                                         $aOrder[] = "$sImportanceSQL DESC";
1316                                                         if (sizeof($aSearch['aFullNameAddress']))
1317                                                         {
1318                                                                 $sExactMatchSQL = '(select count(*) from (select unnest(ARRAY['.join($aSearch['aFullNameAddress'],",").']) INTERSECT select unnest(nameaddress_vector))s) as exactmatch';
1319                                                                 $aOrder[] = 'exactmatch DESC';
1320                                                         } else {
1321                                                                 $sExactMatchSQL = '0::int as exactmatch';
1322                                                         }
1323
1324                                                         if (sizeof($aTerms))
1325                                                         {
1326                                                                 $sSQL = "select place_id, ";
1327                                                                 $sSQL .= $sExactMatchSQL;
1328                                                                 $sSQL .= " from search_name";
1329                                                                 $sSQL .= " where ".join(' and ',$aTerms);
1330                                                                 $sSQL .= " order by ".join(', ',$aOrder);
1331                                                                 if ($aSearch['sHouseNumber'] || $aSearch['sClass'])
1332                                                                         $sSQL .= " limit 20";
1333                                                                 elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass'])
1334                                                                         $sSQL .= " limit 1";
1335                                                                 else
1336                                                                         $sSQL .= " limit ".$this->iLimit;
1337
1338                                                                 if (CONST_Debug) { var_dump($sSQL); }
1339                                                                 $aViewBoxPlaceIDs = $this->oDB->getAll($sSQL);
1340                                                                 if (PEAR::IsError($aViewBoxPlaceIDs))
1341                                                                 {
1342                                                                         failInternalError("Could not get places for search terms.", $sSQL, $aViewBoxPlaceIDs);
1343                                                                 }
1344                                                                 //var_dump($aViewBoxPlaceIDs);
1345                                                                 // Did we have an viewbox matches?
1346                                                                 $aPlaceIDs = array();
1347                                                                 $bViewBoxMatch = false;
1348                                                                 foreach($aViewBoxPlaceIDs as $aViewBoxRow)
1349                                                                 {
1350                                                                         //if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
1351                                                                         //if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
1352                                                                         //if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
1353                                                                         //else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
1354                                                                         $aPlaceIDs[] = $aViewBoxRow['place_id'];
1355                                                                         $this->exactMatchCache[$aViewBoxRow['place_id']] = $aViewBoxRow['exactmatch'];
1356                                                                 }
1357                                                         }
1358                                                         //var_Dump($aPlaceIDs);
1359                                                         //exit;
1360
1361                                                         if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs))
1362                                                         {
1363                                                                 $aRoadPlaceIDs = $aPlaceIDs;
1364                                                                 $sPlaceIDs = join(',',$aPlaceIDs);
1365
1366                                                                 // Now they are indexed look for a house attached to a street we found
1367                                                                 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1368                                                                 $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
1369                                                                 if (sizeof($this->aExcludePlaceIDs))
1370                                                                 {
1371                                                                         $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1372                                                                 }
1373                                                                 $sSQL .= " limit $this->iLimit";
1374                                                                 if (CONST_Debug) var_dump($sSQL);
1375                                                                 $aPlaceIDs = $this->oDB->getCol($sSQL);
1376
1377                                                                 // If not try the aux fallback table
1378                                                                 /*
1379                                                                 if (!sizeof($aPlaceIDs))
1380                                                                 {
1381                                                                         $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1382                                                                         if (sizeof($this->aExcludePlaceIDs))
1383                                                                         {
1384                                                                                 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1385                                                                         }
1386                                                                         //$sSQL .= " limit $this->iLimit";
1387                                                                         if (CONST_Debug) var_dump($sSQL);
1388                                                                         $aPlaceIDs = $this->oDB->getCol($sSQL);
1389                                                                 }
1390                                                                 */
1391
1392                                                                 if (!sizeof($aPlaceIDs))
1393                                                                 {
1394                                                                         $sSQL = "select place_id from location_property_tiger where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1395                                                                         if (sizeof($this->aExcludePlaceIDs))
1396                                                                         {
1397                                                                                 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1398                                                                         }
1399                                                                         //$sSQL .= " limit $this->iLimit";
1400                                                                         if (CONST_Debug) var_dump($sSQL);
1401                                                                         $aPlaceIDs = $this->oDB->getCol($sSQL);
1402                                                                 }
1403
1404                                                                 // Fallback to the road
1405                                                                 if (!sizeof($aPlaceIDs) && preg_match('/[0-9]+/', $aSearch['sHouseNumber']))
1406                                                                 {
1407                                                                         $aPlaceIDs = $aRoadPlaceIDs;
1408                                                                 }
1409
1410                                                         }
1411
1412                                                         if ($aSearch['sClass'] && sizeof($aPlaceIDs))
1413                                                         {
1414                                                                 $sPlaceIDs = join(',',$aPlaceIDs);
1415                                                                 $aClassPlaceIDs = array();
1416
1417                                                                 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name')
1418                                                                 {
1419                                                                         // If they were searching for a named class (i.e. 'Kings Head pub') then we might have an extra match
1420                                                                         $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1421                                                                         $sSQL .= " and linked_place_id is null";
1422                                                                         if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1423                                                                         $sSQL .= " order by rank_search asc limit $this->iLimit";
1424                                                                         if (CONST_Debug) var_dump($sSQL);
1425                                                                         $aClassPlaceIDs = $this->oDB->getCol($sSQL);
1426                                                                 }
1427
1428                                                                 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') // & in
1429                                                                 {
1430                                                                         $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1431                                                                         $bCacheTable = $this->oDB->getOne($sSQL);
1432
1433                                                                         $sSQL = "select min(rank_search) from placex where place_id in ($sPlaceIDs)";
1434
1435                                                                         if (CONST_Debug) var_dump($sSQL);
1436                                                                         $this->iMaxRank = ((int)$this->oDB->getOne($sSQL));
1437
1438                                                                         // For state / country level searches the normal radius search doesn't work very well
1439                                                                         $sPlaceGeom = false;
1440                                                                         if ($this->iMaxRank < 9 && $bCacheTable)
1441                                                                         {
1442                                                                                 // Try and get a polygon to search in instead
1443                                                                                 $sSQL = "select geometry from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank + 5 and st_geometrytype(geometry) in ('ST_Polygon','ST_MultiPolygon') order by rank_search asc limit 1";
1444                                                                                 if (CONST_Debug) var_dump($sSQL);
1445                                                                                 $sPlaceGeom = $this->oDB->getOne($sSQL);
1446                                                                         }
1447
1448                                                                         if ($sPlaceGeom)
1449                                                                         {
1450                                                                                 $sPlaceIDs = false;
1451                                                                         }
1452                                                                         else
1453                                                                         {
1454                                                                                 $this->iMaxRank += 5;
1455                                                                                 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank";
1456                                                                                 if (CONST_Debug) var_dump($sSQL);
1457                                                                                 $aPlaceIDs = $this->oDB->getCol($sSQL);
1458                                                                                 $sPlaceIDs = join(',',$aPlaceIDs);
1459                                                                         }
1460
1461                                                                         if ($sPlaceIDs || $sPlaceGeom)
1462                                                                         {
1463
1464                                                                                 $fRange = 0.01;
1465                                                                                 if ($bCacheTable)
1466                                                                                 {
1467                                                                                         // More efficient - can make the range bigger
1468                                                                                         $fRange = 0.05;
1469
1470                                                                                         $sOrderBySQL = '';
1471                                                                                         if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
1472                                                                                         else if ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
1473                                                                                         else if ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
1474
1475                                                                                         $sSQL = "select distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
1476                                                                                         if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
1477                                                                                         if ($sPlaceIDs)
1478                                                                                         {
1479                                                                                                 $sSQL .= ",placex as f where ";
1480                                                                                                 $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, f.centroid, $fRange) ";
1481                                                                                         }
1482                                                                                         if ($sPlaceGeom)
1483                                                                                         {
1484                                                                                                 $sSQL .= " where ";
1485                                                                                                 $sSQL .= "ST_Contains('".$sPlaceGeom."', l.centroid) ";
1486                                                                                         }
1487                                                                                         if (sizeof($this->aExcludePlaceIDs))
1488                                                                                         {
1489                                                                                                 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1490                                                                                         }
1491                                                                                         if ($sCountryCodesSQL) $sSQL .= " and lp.calculated_country_code in ($sCountryCodesSQL)";
1492                                                                                         if ($sOrderBySQL) $sSQL .= "order by ".$sOrderBySQL." asc";
1493                                                                                         if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1494                                                                                         $sSQL .= " limit $this->iLimit";
1495                                                                                         if (CONST_Debug) var_dump($sSQL);
1496                                                                                         $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1497                                                                                 }
1498                                                                                 else
1499                                                                                 {
1500                                                                                         if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
1501
1502                                                                                         $sOrderBySQL = '';
1503                                                                                         if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
1504                                                                                         else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
1505
1506                                                                                         $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from placex as l,placex as f where ";
1507                                                                                         $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, f.centroid, $fRange) ";
1508                                                                                         $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
1509                                                                                         if (sizeof($this->aExcludePlaceIDs))
1510                                                                                         {
1511                                                                                                 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1512                                                                                         }
1513                                                                                         if ($sCountryCodesSQL) $sSQL .= " and l.calculated_country_code in ($sCountryCodesSQL)";
1514                                                                                         if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
1515                                                                                         if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1516                                                                                         $sSQL .= " limit $this->iLimit";
1517                                                                                         if (CONST_Debug) var_dump($sSQL);
1518                                                                                         $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1519                                                                                 }
1520                                                                         }
1521                                                                 }
1522
1523                                                                 $aPlaceIDs = $aClassPlaceIDs;
1524
1525                                                         }
1526
1527                                                 }
1528
1529                                                 if (PEAR::IsError($aPlaceIDs))
1530                                                 {
1531                                                         failInternalError("Could not get place IDs from tokens." ,$sSQL, $aPlaceIDs);
1532                                                 }
1533
1534                                                 if (CONST_Debug) { echo "<br><b>Place IDs:</b> "; var_Dump($aPlaceIDs); }
1535
1536                                                 foreach($aPlaceIDs as $iPlaceID)
1537                                                 {
1538                                                         $aResultPlaceIDs[$iPlaceID] = $iPlaceID;
1539                                                 }
1540                                                 if ($iQueryLoop > 20) break;
1541                                         }
1542
1543                                         if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30))
1544                                         {
1545                                                 // Need to verify passes rank limits before dropping out of the loop (yuk!)
1546                                                 $sSQL = "select place_id from placex where place_id in (".join(',',$aResultPlaceIDs).") ";
1547                                                 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
1548                                                 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
1549                                                 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
1550                                                 $sSQL .= ") UNION select place_id from location_property_tiger where place_id in (".join(',',$aResultPlaceIDs).") ";
1551                                                 $sSQL .= "and (30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
1552                                                 if ($this->aAddressRankList) $sSQL .= " OR 30 in (".join(',',$this->aAddressRankList).")";
1553                                                 $sSQL .= ")";
1554                                                 if (CONST_Debug) var_dump($sSQL);
1555                                                 $aResultPlaceIDs = $this->oDB->getCol($sSQL);
1556                                         }
1557
1558                                         //exit;
1559                                         if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) break;
1560                                         if ($iGroupLoop > 4) break;
1561                                         if ($iQueryLoop > 30) break;
1562                                 }
1563
1564                                 // Did we find anything?
1565                                 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs))
1566                                 {
1567                                         $aSearchResults = $this->getDetails($aResultPlaceIDs);
1568                                 }
1569
1570                         }
1571                         else
1572                         {
1573                                 // Just interpret as a reverse geocode
1574                                 $iPlaceID = geocodeReverse((float)$this->aNearPoint[0], (float)$this->aNearPoint[1]);
1575                                 if ($iPlaceID)
1576                                         $aSearchResults = $this->getDetails(array($iPlaceID));
1577                                 else
1578                                         $aSearchResults = array();
1579                         }
1580
1581                         // No results? Done
1582                         if (!sizeof($aSearchResults))
1583                         {
1584                                 if ($this->bFallback)
1585                                 {
1586                                         if ($this->fallbackStructuredQuery())
1587                                         {
1588                                                 return $this->lookup();
1589                                         }
1590                                 }
1591
1592                                 return array();
1593                         }
1594
1595                         $aClassType = getClassTypesWithImportance();
1596                         $aRecheckWords = preg_split('/\b[\s,\\-]*/u',$sQuery);
1597                         foreach($aRecheckWords as $i => $sWord)
1598                         {
1599                                 if (!$sWord) unset($aRecheckWords[$i]);
1600                         }
1601
1602                         foreach($aSearchResults as $iResNum => $aResult)
1603                         {
1604                                 if (CONST_Search_AreaPolygons)
1605                                 {
1606                                         // Get the bounding box and outline polygon
1607                                         $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
1608                                         $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
1609                                         $sSQL .= "ST_YMin(geometry) as minlat,ST_YMax(geometry) as maxlat,";
1610                                         $sSQL .= "ST_XMin(geometry) as minlon,ST_XMax(geometry) as maxlon";
1611                                         if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
1612                                         if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
1613                                         if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
1614                                         if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
1615                                         $sSQL .= " from placex where place_id = ".$aResult['place_id'];
1616                                         $aPointPolygon = $this->oDB->getRow($sSQL);
1617                                         if (PEAR::IsError($aPointPolygon))
1618                                         {
1619                                                 failInternalError("Could not get outline.", $sSQL, $aPointPolygon);
1620                                         }
1621
1622                                         if ($aPointPolygon['place_id'])
1623                                         {
1624                                                 if ($this->bIncludePolygonAsGeoJSON) $aResult['asgeojson'] = $aPointPolygon['asgeojson'];
1625                                                 if ($this->bIncludePolygonAsKML) $aResult['askml'] = $aPointPolygon['askml'];
1626                                                 if ($this->bIncludePolygonAsSVG) $aResult['assvg'] = $aPointPolygon['assvg'];
1627                                                 if ($this->bIncludePolygonAsText) $aResult['astext'] = $aPointPolygon['astext'];
1628
1629                                                 if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null )
1630                                                 {
1631                                                         $aResult['lat'] = $aPointPolygon['centrelat'];
1632                                                         $aResult['lon'] = $aPointPolygon['centrelon'];
1633                                                 }
1634
1635                                                 if ($this->bIncludePolygonAsPoints)
1636                                                 {
1637                                                         // Translate geometry string to point array
1638                                                         if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1639                                                         {
1640                                                                 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1641                                                         }
1642                             /*
1643                                                         elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1644                                                         {
1645                                                                 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1646                                                         }
1647                             */
1648                                                         elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#',$aPointPolygon['astext'],$aMatch))
1649                                                         {
1650                                                                 $fRadius = 0.01;
1651                                                                 $iSteps = ($fRadius * 40000)^2;
1652                                                                 $fStepSize = (2*pi())/$iSteps;
1653                                                                 $aPolyPoints = array();
1654                                                                 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1655                                                                 {
1656                                                                         $aPolyPoints[] = array('',$aMatch[1]+($fRadius*sin($f)),$aMatch[2]+($fRadius*cos($f)));
1657                                                                 }
1658                                                                 $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
1659                                                                 $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
1660                                                                 $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
1661                                                                 $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
1662                                                         }
1663                                                 }
1664
1665                                                 // Output data suitable for display (points and a bounding box)
1666                                                 if ($this->bIncludePolygonAsPoints && isset($aPolyPoints))
1667                                                 {
1668                                                         $aResult['aPolyPoints'] = array();
1669                                                         foreach($aPolyPoints as $aPoint)
1670                                                         {
1671                                                                 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1672                                                         }
1673                                                 }
1674                                                 $aResult['aBoundingBox'] = array($aPointPolygon['minlat'],$aPointPolygon['maxlat'],$aPointPolygon['minlon'],$aPointPolygon['maxlon']);
1675                                         }
1676                                 }
1677
1678                                 if ($aResult['extra_place'] == 'city')
1679                                 {
1680                                         $aResult['class'] = 'place';
1681                                         $aResult['type'] = 'city';
1682                                         $aResult['rank_search'] = 16;
1683                                 }
1684
1685                                 if (!isset($aResult['aBoundingBox']))
1686                                 {
1687                                         // Default
1688                                         $fDiameter = 0.0001;
1689
1690                                         if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1691                                                         && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1692                                         {
1693                                                 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defzoom'];
1694                                         }
1695                                         elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1696                                                         && $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1697                                         {
1698                                                 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'];
1699                                         }
1700                                         $fRadius = $fDiameter / 2;
1701
1702                                         $iSteps = max(8,min(100,$fRadius * 3.14 * 100000));
1703                                         $fStepSize = (2*pi())/$iSteps;
1704                                         $aPolyPoints = array();
1705                                         for($f = 0; $f < 2*pi(); $f += $fStepSize)
1706                                         {
1707                                                 $aPolyPoints[] = array('',$aResult['lon']+($fRadius*sin($f)),$aResult['lat']+($fRadius*cos($f)));
1708                                         }
1709                                         $aPointPolygon['minlat'] = $aResult['lat'] - $fRadius;
1710                                         $aPointPolygon['maxlat'] = $aResult['lat'] + $fRadius;
1711                                         $aPointPolygon['minlon'] = $aResult['lon'] - $fRadius;
1712                                         $aPointPolygon['maxlon'] = $aResult['lon'] + $fRadius;
1713
1714                                         // Output data suitable for display (points and a bounding box)
1715                                         if ($this->bIncludePolygonAsPoints)
1716                                         {
1717                                                 $aResult['aPolyPoints'] = array();
1718                                                 foreach($aPolyPoints as $aPoint)
1719                                                 {
1720                                                         $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1721                                                 }
1722                                         }
1723                                         $aResult['aBoundingBox'] = array((string)$aPointPolygon['minlat'],(string)$aPointPolygon['maxlat'],(string)$aPointPolygon['minlon'],(string)$aPointPolygon['maxlon']);
1724                                 }
1725
1726                                 // Is there an icon set for this type of result?
1727                                 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1728                                                 && $aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1729                                 {
1730                                         $aResult['icon'] = CONST_Website_BaseURL.'images/mapicons/'.$aClassType[$aResult['class'].':'.$aResult['type']]['icon'].'.p.20.png';
1731                                 }
1732
1733                                 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1734                                                 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1735                                 {
1736                                         $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'];
1737                                 }
1738                                 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1739                                                 && $aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1740                                 {
1741                                         $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type']]['label'];
1742                                 }
1743
1744                                 if ($this->bIncludeAddressDetails)
1745                                 {
1746                                         $aResult['address'] = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code']);
1747                                         if ($aResult['extra_place'] == 'city' && !isset($aResult['address']['city']))
1748                                         {
1749                                                 $aResult['address'] = array_merge(array('city' => array_shift(array_values($aResult['address']))), $aResult['address']);
1750                                         }
1751                                 }
1752
1753                                 // Adjust importance for the number of exact string matches in the result
1754                                 $aResult['importance'] = max(0.001,$aResult['importance']);
1755                                 $iCountWords = 0;
1756                                 $sAddress = $aResult['langaddress'];
1757                                 foreach($aRecheckWords as $i => $sWord)
1758                                 {
1759                                         if (stripos($sAddress, $sWord)!==false)
1760                                         {
1761                                                 $iCountWords++;
1762                                                 if (preg_match("/(^|,)\s*$sWord\s*(,|$)/", $sAddress)) $iCountWords += 0.1;
1763                                         }
1764                                 }
1765
1766                                 $aResult['importance'] = $aResult['importance'] + ($iCountWords*0.1); // 0.1 is a completely arbitrary number but something in the range 0.1 to 0.5 would seem right
1767
1768                                 $aResult['name'] = $aResult['langaddress'];
1769                                 // secondary ordering (for results with same importance (the smaller the better):
1770                                 //   - approximate importance of address parts
1771                                 $aResult['foundorder'] = -$aResult['addressimportance']/10;
1772                                 //   - number of exact matches from the query
1773                                 if (isset($this->exactMatchCache[$aResult['place_id']]))
1774                                         $aResult['foundorder'] -= $this->exactMatchCache[$aResult['place_id']];
1775                                 else if (isset($this->exactMatchCache[$aResult['parent_place_id']]))
1776                                         $aResult['foundorder'] -= $this->exactMatchCache[$aResult['parent_place_id']];
1777                                 //  - importance of the class/type
1778                                 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1779                                         && $aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1780                                 {
1781                                         $aResult['foundorder'] = $aResult['foundorder'] + 0.000001 * $aClassType[$aResult['class'].':'.$aResult['type']]['importance'];
1782                                 }
1783                                 else
1784                                 {
1785                                         $aResult['foundorder'] = $aResult['foundorder'] + 0.001;
1786                                 }
1787                                 $aSearchResults[$iResNum] = $aResult;
1788                         }
1789                         uasort($aSearchResults, 'byImportance');
1790
1791                         $aOSMIDDone = array();
1792                         $aClassTypeNameDone = array();
1793                         $aToFilter = $aSearchResults;
1794                         $aSearchResults = array();
1795
1796                         $bFirst = true;
1797                         foreach($aToFilter as $iResNum => $aResult)
1798                         {
1799                                 $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
1800                                 if ($bFirst)
1801                                 {
1802                                         $fLat = $aResult['lat'];
1803                                         $fLon = $aResult['lon'];
1804                                         if (isset($aResult['zoom'])) $iZoom = $aResult['zoom'];
1805                                         $bFirst = false;
1806                                 }
1807                                 if (!$this->bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']])
1808                                                         && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']])))
1809                                 {
1810                                         $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true;
1811                                         $aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']] = true;
1812                                         $aSearchResults[] = $aResult;
1813                                 }
1814
1815                                 // Absolute limit on number of results
1816                                 if (sizeof($aSearchResults) >= $this->iFinalLimit) break;
1817                         }
1818
1819                         return $aSearchResults;
1820
1821                 } // end lookup()
1822
1823
1824         } // end class
1825