]> git.openstreetmap.org Git - nominatim.git/blob - lib/Geocode.php
202917a25dedfe87d5dd011422fa50226ba1fd49
[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]) || strlen($sToken) < 4 || 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                                                                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
559                                                                                         /*
560                                                                                         // Fall back to not searching for this item (better than nothing)
561                                                                                         $aSearch = $aCurrentSearch;
562                                                                                         $aSearch['iSearchRank'] += 1;
563                                                                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
564                                                                                          */
565                                                                                 }
566                                                                         }
567                                                                         elseif ($sPhraseType == '' && $aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null)
568                                                                         {
569                                                                                 if ($aSearch['sClass'] === '')
570                                                                                 {
571                                                                                         $aSearch['sOperator'] = $aSearchTerm['operator'];
572                                                                                         $aSearch['sClass'] = $aSearchTerm['class'];
573                                                                                         $aSearch['sType'] = $aSearchTerm['type'];
574                                                                                         if (sizeof($aSearch['aName'])) $aSearch['sOperator'] = 'name';
575                                                                                         else $aSearch['sOperator'] = 'near'; // near = in for the moment
576                                                                                         if (strlen($aSearchTerm['operator']) == 0) $aSearch['iSearchRank'] += 1;
577
578                                                                                         // Do we have a shortcut id?
579                                                                                         if ($aSearch['sOperator'] == 'name')
580                                                                                         {
581                                                                                                 $sSQL = "select get_tagpair('".$aSearch['sClass']."', '".$aSearch['sType']."')";
582                                                                                                 if ($iAmenityID = $this->oDB->getOne($sSQL))
583                                                                                                 {
584                                                                                                         $aValidTokens[$aSearch['sClass'].':'.$aSearch['sType']] = array('word_id' => $iAmenityID);
585                                                                                                         $aSearch['aName'][$iAmenityID] = $iAmenityID;
586                                                                                                         $aSearch['sClass'] = '';
587                                                                                                         $aSearch['sType'] = '';
588                                                                                                 }
589                                                                                         }
590                                                                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
591                                                                                 }
592                                                                         }
593                                                                         elseif (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
594                                                                         {
595                                                                                 if (sizeof($aSearch['aName']))
596                                                                                 {
597                                                                                         if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strlen($sToken) < 4 || strpos($sToken, ' ') !== false))
598                                                                                         {
599                                                                                                 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
600                                                                                         }
601                                                                                         else
602                                                                                         {
603                                                                                                 $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
604                                                                                                 $aSearch['iSearchRank'] += 1000; // skip;
605                                                                                         }
606                                                                                 }
607                                                                                 else
608                                                                                 {
609                                                                                         $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
610                                                                                         //$aSearch['iNamePhrase'] = $iPhrase;
611                                                                                 }
612                                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
613                                                                         }
614                                                                 }
615                                                         }
616                                                         // Look for partial matches.
617                                                         // Note that there is no point in adding country terms here
618                                                         // because country are omitted in the address.
619                                                         if (isset($aValidTokens[$sToken]) && $sPhraseType != 'country')
620                                                         {
621                                                                 // Allow searching for a word - but at extra cost
622                                                                 foreach($aValidTokens[$sToken] as $aSearchTerm)
623                                                                 {
624                                                                         if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
625                                                                         {
626                                                                                 if ((!$bStructuredPhrases || $iPhrase > 0) && sizeof($aCurrentSearch['aName']) && strpos($sToken, ' ') === false)
627                                                                                 {
628                                                                                         $aSearch = $aCurrentSearch;
629                                                                                         $aSearch['iSearchRank'] += 1;
630                                                                                         if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
631                                                                                         {
632                                                                                                 $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
633                                                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
634                                                                                         }
635                                                                                         elseif (isset($aValidTokens[' '.$sToken]) && strlen($sToken) >= 4) // revert to the token version?
636                                                                                         {
637                                                                                                 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
638                                                                                                 $aSearch['iSearchRank'] += 1;
639                                                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
640                                                                                                 foreach($aValidTokens[' '.$sToken] as $aSearchTermToken)
641                                                                                                 {
642                                                                                                         if (empty($aSearchTermToken['country_code'])
643                                                                                                                         && empty($aSearchTermToken['lat'])
644                                                                                                                         && empty($aSearchTermToken['class']))
645                                                                                                         {
646                                                                                                                 $aSearch = $aCurrentSearch;
647                                                                                                                 $aSearch['iSearchRank'] += 1;
648                                                                                                                 $aSearch['aAddress'][$aSearchTermToken['word_id']] = $aSearchTermToken['word_id'];
649                                                                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
650                                                                                                         }
651                                                                                                 }
652                                                                                         }
653                                                                                         else
654                                                                                         {
655                                                                                                 $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
656                                                                                                 if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
657                                                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
658                                                                                         }
659                                                                                 }
660
661                                                                                 if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase)
662                                                                                 {
663                                                                                         $aSearch = $aCurrentSearch;
664                                                                                         $aSearch['iSearchRank'] += 1;
665                                                                                         if (!sizeof($aCurrentSearch['aName'])) $aSearch['iSearchRank'] += 1;
666                                                                                         if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
667                                                                                         if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency)
668                                                                                                 $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
669                                                                                         else
670                                                                                                 $aSearch['aNameNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
671                                                                                         $aSearch['iNamePhrase'] = $iPhrase;
672                                                                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
673                                                                                 }
674                                                                         }
675                                                                 }
676                                                         }
677                                                         else
678                                                         {
679                                                                 // Allow skipping a word - but at EXTREAM cost
680                                                                 //$aSearch = $aCurrentSearch;
681                                                                 //$aSearch['iSearchRank']+=100;
682                                                                 //$aNewWordsetSearches[] = $aSearch;
683                                                         }
684                                                 }
685                                                 // Sort and cut
686                                                 usort($aNewWordsetSearches, 'bySearchRank');
687                                                 $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
688                                         }
689                                         //var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
690
691                                         $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
692                                         usort($aNewPhraseSearches, 'bySearchRank');
693
694                                         $aSearchHash = array();
695                                         foreach($aNewPhraseSearches as $iSearch => $aSearch)
696                                         {
697                                                 $sHash = serialize($aSearch);
698                                                 if (isset($aSearchHash[$sHash])) unset($aNewPhraseSearches[$iSearch]);
699                                                 else $aSearchHash[$sHash] = 1;
700                                         }
701
702                                         $aNewPhraseSearches = array_slice($aNewPhraseSearches, 0, 50);
703                                 }
704
705                                 // Re-group the searches by their score, junk anything over 20 as just not worth trying
706                                 $aGroupedSearches = array();
707                                 foreach($aNewPhraseSearches as $aSearch)
708                                 {
709                                         if ($aSearch['iSearchRank'] < $this->iMaxRank)
710                                         {
711                                                 if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
712                                                 $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
713                                         }
714                                 }
715                                 ksort($aGroupedSearches);
716
717                                 $iSearchCount = 0;
718                                 $aSearches = array();
719                                 foreach($aGroupedSearches as $iScore => $aNewSearches)
720                                 {
721                                         $iSearchCount += sizeof($aNewSearches);
722                                         $aSearches = array_merge($aSearches, $aNewSearches);
723                                         if ($iSearchCount > 50) break;
724                                 }
725
726                                 //if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
727
728                         }
729                         return $aGroupedSearches;
730
731                 }
732
733                 /* Perform the actual query lookup.
734
735                         Returns an ordered list of results, each with the following fields:
736                           osm_type: type of corresponding OSM object
737                                                         N - node
738                                                         W - way
739                                                         R - relation
740                                                         P - postcode (internally computed)
741                           osm_id: id of corresponding OSM object
742                           class: general object class (corresponds to tag key of primary OSM tag)
743                           type: subclass of object (corresponds to tag value of primary OSM tag)
744                           admin_level: see http://wiki.openstreetmap.org/wiki/Admin_level
745                           rank_search: rank in search hierarchy
746                                                         (see also http://wiki.openstreetmap.org/wiki/Nominatim/Development_overview#Country_to_street_level)
747                           rank_address: rank in address hierarchy (determines orer in address)
748                           place_id: internal key (may differ between different instances)
749                           country_code: ISO country code
750                           langaddress: localized full address
751                           placename: localized name of object
752                           ref: content of ref tag (if available)
753                           lon: longitude
754                           lat: latitude
755                           importance: importance of place based on Wikipedia link count
756                           addressimportance: cumulated importance of address elements
757                           extra_place: type of place (for admin boundaries, if there is a place tag)
758                           aBoundingBox: bounding Box
759                           label: short description of the object class/type (English only) 
760                           name: full name (currently the same as langaddress)
761                           foundorder: secondary ordering for places with same importance
762                 */
763                 function lookup()
764                 {
765                         if (!$this->sQuery && !$this->aStructuredQuery) return false;
766
767                         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$this->aLangPrefOrder))."]";
768
769                         $sCountryCodesSQL = false;
770                         if ($this->aCountryCodes && sizeof($this->aCountryCodes))
771                         {
772                                 $sCountryCodesSQL = join(',', array_map('addQuotes', $this->aCountryCodes));
773                         }
774
775                         $sQuery = $this->sQuery;
776
777                         // Conflicts between US state abreviations and various words for 'the' in different languages
778                         if (isset($this->aLangPrefOrder['name:en']))
779                         {
780                                 $sQuery = preg_replace('/(^|,)\s*il\s*(,|$)/','\1illinois\2', $sQuery);
781                                 $sQuery = preg_replace('/(^|,)\s*al\s*(,|$)/','\1alabama\2', $sQuery);
782                                 $sQuery = preg_replace('/(^|,)\s*la\s*(,|$)/','\1louisiana\2', $sQuery);
783                         }
784
785                         // View Box SQL
786                         $sViewboxCentreSQL = false;
787                         $bBoundingBoxSearch = false;
788                         if ($this->aViewBox)
789                         {
790                                 $fHeight = $this->aViewBox[0]-$this->aViewBox[2];
791                                 $fWidth = $this->aViewBox[1]-$this->aViewBox[3];
792                                 $aBigViewBox[0] = $this->aViewBox[0] + $fHeight;
793                                 $aBigViewBox[2] = $this->aViewBox[2] - $fHeight;
794                                 $aBigViewBox[1] = $this->aViewBox[1] + $fWidth;
795                                 $aBigViewBox[3] = $this->aViewBox[3] - $fWidth;
796
797                                 $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)";
798                                 $this->sViewboxLargeSQL = "ST_SetSRID(ST_MakeBox2D(ST_Point(".(float)$aBigViewBox[0].",".(float)$aBigViewBox[1]."),ST_Point(".(float)$aBigViewBox[2].",".(float)$aBigViewBox[3].")),4326)";
799                                 $bBoundingBoxSearch = $this->bBoundedSearch;
800                         }
801
802                         // Route SQL
803                         if ($this->aRoutePoints)
804                         {
805                                 $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
806                                 $bFirst = true;
807                                 foreach($this->aRoutePoints as $aPoint)
808                                 {
809                                         if (!$bFirst) $sViewboxCentreSQL .= ",";
810                                         $sViewboxCentreSQL .= $aPoint[0].' '.$aPoint[1];
811                                         $bFirst = false;
812                                 }
813                                 $sViewboxCentreSQL .= ")'::geometry,4326)";
814
815                                 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/69).")";
816                                 $this->sViewboxSmallSQL = $this->oDB->getOne($sSQL);
817                                 if (PEAR::isError($this->sViewboxSmallSQL))
818                                 {
819                                         failInternalError("Could not get small viewbox.", $sSQL, $this->sViewboxSmallSQL);
820                                 }
821                                 $this->sViewboxSmallSQL = "'".$this->sViewboxSmallSQL."'::geometry";
822
823                                 $sSQL = "select st_buffer(".$sViewboxCentreSQL.",".(float)($_GET['routewidth']/30).")";
824                                 $this->sViewboxLargeSQL = $this->oDB->getOne($sSQL);
825                                 if (PEAR::isError($this->sViewboxLargeSQL))
826                                 {
827                                         failInternalError("Could not get large viewbox.", $sSQL, $this->sViewboxLargeSQL);
828                                 }
829                                 $this->sViewboxLargeSQL = "'".$this->sViewboxLargeSQL."'::geometry";
830                                 $bBoundingBoxSearch = $this->bBoundedSearch;
831                         }
832
833                         // Do we have anything that looks like a lat/lon pair?
834                         if ( $aLooksLike = looksLikeLatLonPair($sQuery) ){
835                                 $this->setNearPoint(array($aLooksLike['lat'], $aLooksLike['lon']));
836                                 $sQuery = $aLooksLike['query'];                 
837                         }
838
839                         $aSearchResults = array();
840                         if ($sQuery || $this->aStructuredQuery)
841                         {
842                                 // Start with a blank search
843                                 $aSearches = array(
844                                         array('iSearchRank' => 0, 'iNamePhrase' => -1, 'sCountryCode' => false, 'aName'=>array(), 'aAddress'=>array(), 'aFullNameAddress'=>array(),
845                                               'aNameNonSearch'=>array(), 'aAddressNonSearch'=>array(),
846                                               'sOperator'=>'', 'aFeatureName' => array(), 'sClass'=>'', 'sType'=>'', 'sHouseNumber'=>'', 'fLat'=>'', 'fLon'=>'', 'fRadius'=>'')
847                                 );
848
849                                 // Do we have a radius search?
850                                 $sNearPointSQL = false;
851                                 if ($this->aNearPoint)
852                                 {
853                                         $sNearPointSQL = "ST_SetSRID(ST_Point(".(float)$this->aNearPoint[1].",".(float)$this->aNearPoint[0]."),4326)";
854                                         $aSearches[0]['fLat'] = (float)$this->aNearPoint[0];
855                                         $aSearches[0]['fLon'] = (float)$this->aNearPoint[1];
856                                         $aSearches[0]['fRadius'] = (float)$this->aNearPoint[2];
857                                 }
858
859                                 // Any 'special' terms in the search?
860                                 $bSpecialTerms = false;
861                                 preg_match_all('/\\[(.*)=(.*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
862                                 $aSpecialTerms = array();
863                                 foreach($aSpecialTermsRaw as $aSpecialTerm)
864                                 {
865                                         $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
866                                         $aSpecialTerms[strtolower($aSpecialTerm[1])] = $aSpecialTerm[2];
867                                 }
868
869                                 preg_match_all('/\\[([\\w ]*)\\]/u', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
870                                 $aSpecialTerms = array();
871                                 if (isset($aStructuredQuery['amenity']) && $aStructuredQuery['amenity'])
872                                 {
873                                         $aSpecialTermsRaw[] = array('['.$aStructuredQuery['amenity'].']', $aStructuredQuery['amenity']);
874                                         unset($aStructuredQuery['amenity']);
875                                 }
876                                 foreach($aSpecialTermsRaw as $aSpecialTerm)
877                                 {
878                                         $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
879                                         $sToken = $this->oDB->getOne("select make_standard_name('".$aSpecialTerm[1]."') as string");
880                                         $sSQL = 'select * from (select word_id,word_token, word, class, type, country_code, operator';
881                                         $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';
882                                         if (CONST_Debug) var_Dump($sSQL);
883                                         $aSearchWords = $this->oDB->getAll($sSQL);
884                                         $aNewSearches = array();
885                                         foreach($aSearches as $aSearch)
886                                         {
887                                                 foreach($aSearchWords as $aSearchTerm)
888                                                 {
889                                                         $aNewSearch = $aSearch;
890                                                         if ($aSearchTerm['country_code'])
891                                                         {
892                                                                 $aNewSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
893                                                                 $aNewSearches[] = $aNewSearch;
894                                                                 $bSpecialTerms = true;
895                                                         }
896                                                         if ($aSearchTerm['class'])
897                                                         {
898                                                                 $aNewSearch['sClass'] = $aSearchTerm['class'];
899                                                                 $aNewSearch['sType'] = $aSearchTerm['type'];
900                                                                 $aNewSearches[] = $aNewSearch;
901                                                                 $bSpecialTerms = true;
902                                                         }
903                                                 }
904                                         }
905                                         $aSearches = $aNewSearches;
906                                 }
907
908                                 // Split query into phrases
909                                 // Commas are used to reduce the search space by indicating where phrases split
910                                 if ($this->aStructuredQuery)
911                                 {
912                                         $aPhrases = $this->aStructuredQuery;
913                                         $bStructuredPhrases = true;
914                                 }
915                                 else
916                                 {
917                                         $aPhrases = explode(',',$sQuery);
918                                         $bStructuredPhrases = false;
919                                 }
920
921                                 // Convert each phrase to standard form
922                                 // Create a list of standard words
923                                 // Get all 'sets' of words
924                                 // Generate a complete list of all
925                                 $aTokens = array();
926                                 foreach($aPhrases as $iPhrase => $sPhrase)
927                                 {
928                                         $aPhrase = $this->oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string");
929                                         if (PEAR::isError($aPhrase))
930                                         {
931                                                 userError("Illegal query string (not an UTF-8 string): ".$sPhrase);
932                                                 if (CONST_Debug) var_dump($aPhrase);
933                                                 exit;
934                                         }
935                                         if (trim($aPhrase['string']))
936                                         {
937                                                 $aPhrases[$iPhrase] = $aPhrase;
938                                                 $aPhrases[$iPhrase]['words'] = explode(' ',$aPhrases[$iPhrase]['string']);
939                                                 $aPhrases[$iPhrase]['wordsets'] = getWordSets($aPhrases[$iPhrase]['words'], 0);
940                                                 $aTokens = array_merge($aTokens, getTokensFromSets($aPhrases[$iPhrase]['wordsets']));
941                                         }
942                                         else
943                                         {
944                                                 unset($aPhrases[$iPhrase]);
945                                         }
946                                 }
947
948                                 // Reindex phrases - we make assumptions later on that they are numerically keyed in order
949                                 $aPhraseTypes = array_keys($aPhrases);
950                                 $aPhrases = array_values($aPhrases);
951
952                                 if (sizeof($aTokens))
953                                 {
954                                         // Check which tokens we have, get the ID numbers
955                                         $sSQL = 'select word_id,word_token, word, class, type, country_code, operator, search_name_count';
956                                         $sSQL .= ' from word where word_token in ('.join(',',array_map("getDBQuoted",$aTokens)).')';
957
958                                         if (CONST_Debug) var_Dump($sSQL);
959
960                                         $aValidTokens = array();
961                                         if (sizeof($aTokens)) $aDatabaseWords = $this->oDB->getAll($sSQL);
962                                         else $aDatabaseWords = array();
963                                         if (PEAR::IsError($aDatabaseWords))
964                                         {
965                                                 failInternalError("Could not get word tokens.", $sSQL, $aDatabaseWords);
966                                         }
967                                         $aPossibleMainWordIDs = array();
968                                         $aWordFrequencyScores = array();
969                                         foreach($aDatabaseWords as $aToken)
970                                         {
971                                                 // Very special case - require 2 letter country param to match the country code found
972                                                 if ($bStructuredPhrases && $aToken['country_code'] && !empty($aStructuredQuery['country'])
973                                                                 && strlen($aStructuredQuery['country']) == 2 && strtolower($aStructuredQuery['country']) != $aToken['country_code'])
974                                                 {
975                                                         continue;
976                                                 }
977
978                                                 if (isset($aValidTokens[$aToken['word_token']]))
979                                                 {
980                                                         $aValidTokens[$aToken['word_token']][] = $aToken;
981                                                 }
982                                                 else
983                                                 {
984                                                         $aValidTokens[$aToken['word_token']] = array($aToken);
985                                                 }
986                                                 if (!$aToken['class'] && !$aToken['country_code']) $aPossibleMainWordIDs[$aToken['word_id']] = 1;
987                                                 $aWordFrequencyScores[$aToken['word_id']] = $aToken['search_name_count'] + 1;
988                                         }
989                                         if (CONST_Debug) var_Dump($aPhrases, $aValidTokens);
990
991                                         // Try and calculate GB postcodes we might be missing
992                                         foreach($aTokens as $sToken)
993                                         {
994                                                 // Source of gb postcodes is now definitive - always use
995                                                 if (preg_match('/^([A-Z][A-Z]?[0-9][0-9A-Z]? ?[0-9])([A-Z][A-Z])$/', strtoupper(trim($sToken)), $aData))
996                                                 {
997                                                         if (substr($aData[1],-2,1) != ' ')
998                                                         {
999                                                                 $aData[0] = substr($aData[0],0,strlen($aData[1])-1).' '.substr($aData[0],strlen($aData[1])-1);
1000                                                                 $aData[1] = substr($aData[1],0,-1).' '.substr($aData[1],-1,1);
1001                                                         }
1002                                                         $aGBPostcodeLocation = gbPostcodeCalculate($aData[0], $aData[1], $aData[2], $this->oDB);
1003                                                         if ($aGBPostcodeLocation)
1004                                                         {
1005                                                                 $aValidTokens[$sToken] = $aGBPostcodeLocation;
1006                                                         }
1007                                                 }
1008                                                 // US ZIP+4 codes - if there is no token,
1009                                                 //      merge in the 5-digit ZIP code
1010                                                 else if (!isset($aValidTokens[$sToken]) && preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData))
1011                                                 {
1012                                                         if (isset($aValidTokens[$aData[1]]))
1013                                                         {
1014                                                                 foreach($aValidTokens[$aData[1]] as $aToken)
1015                                                                 {
1016                                                                         if (!$aToken['class'])
1017                                                                         {
1018                                                                                 if (isset($aValidTokens[$sToken]))
1019                                                                                 {
1020                                                                                         $aValidTokens[$sToken][] = $aToken;
1021                                                                                 }
1022                                                                                 else
1023                                                                                 {
1024                                                                                         $aValidTokens[$sToken] = array($aToken);
1025                                                                                 }
1026                                                                         }
1027                                                                 }
1028                                                         }
1029                                                 }
1030                                         }
1031
1032                                         foreach($aTokens as $sToken)
1033                                         {
1034                                                 // Unknown single word token with a number - assume it is a house number
1035                                                 if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken,' ') === false && preg_match('/[0-9]/', $sToken))
1036                                                 {
1037                                                         $aValidTokens[' '.$sToken] = array(array('class'=>'place','type'=>'house'));
1038                                                 }
1039                                         }
1040
1041                                         // Any words that have failed completely?
1042                                         // TODO: suggestions
1043
1044                                         // Start the search process
1045                                         $aResultPlaceIDs = array();
1046
1047                                         $aGroupedSearches = $this->getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases);
1048
1049                                         if ($this->bReverseInPlan)
1050                                         {
1051                                                 // Reverse phrase array and also reverse the order of the wordsets in
1052                                                 // the first and final phrase. Don't bother about phrases in the middle
1053                                                 // because order in the address doesn't matter.
1054                                                 $aPhrases = array_reverse($aPhrases);
1055                                                 $aPhrases[0]['wordsets'] = getInverseWordSets($aPhrases[0]['words'], 0);
1056                                                 if (sizeof($aPhrases) > 1)
1057                                                 {
1058                                                         $aFinalPhrase = end($aPhrases);
1059                                                         $aFinalPhrase['wordsets'] = getInverseWordSets($aFinalPhrase['words'], 0);
1060                                                 }
1061                                                 $aReverseGroupedSearches = $this->getGroupedSearches($aSearches, null, $aPhrases, $aValidTokens, $aWordFrequencyScores, false);
1062
1063                                                 foreach($aGroupedSearches as $aSearches)
1064                                                 {
1065                                                         foreach($aSearches as $aSearch)
1066                                                         {
1067                                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank)
1068                                                                 {
1069                                                                         if (!isset($aReverseGroupedSearches[$aSearch['iSearchRank']])) $aReverseGroupedSearches[$aSearch['iSearchRank']] = array();
1070                                                                         $aReverseGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1071                                                                 }
1072
1073                                                         }
1074                                                 }
1075
1076                                                 $aGroupedSearches = $aReverseGroupedSearches;
1077                                                 ksort($aGroupedSearches);
1078                                         }
1079                                 }
1080                                 else
1081                                 {
1082                                         // Re-group the searches by their score, junk anything over 20 as just not worth trying
1083                                         $aGroupedSearches = array();
1084                                         foreach($aSearches as $aSearch)
1085                                         {
1086                                                 if ($aSearch['iSearchRank'] < $this->iMaxRank)
1087                                                 {
1088                                                         if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
1089                                                         $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
1090                                                 }
1091                                         }
1092                                         ksort($aGroupedSearches);
1093                                 }
1094
1095                                 if (CONST_Debug) var_Dump($aGroupedSearches);
1096
1097                                 if (CONST_Search_TryDroppedAddressTerms && sizeof($aStructuredQuery) > 0)
1098                                 {
1099                                         $aCopyGroupedSearches = $aGroupedSearches;
1100                                         foreach($aCopyGroupedSearches as $iGroup => $aSearches)
1101                                         {
1102                                                 foreach($aSearches as $iSearch => $aSearch)
1103                                                 {
1104                                                         $aReductionsList = array($aSearch['aAddress']);
1105                                                         $iSearchRank = $aSearch['iSearchRank'];
1106                                                         while(sizeof($aReductionsList) > 0)
1107                                                         {
1108                                                                 $iSearchRank += 5;
1109                                                                 if ($iSearchRank > iMaxRank) break 3;
1110                                                                 $aNewReductionsList = array();
1111                                                                 foreach($aReductionsList as $aReductionsWordList)
1112                                                                 {
1113                                                                         for ($iReductionWord = 0; $iReductionWord < sizeof($aReductionsWordList); $iReductionWord++)
1114                                                                         {
1115                                                                                 $aReductionsWordListResult = array_merge(array_slice($aReductionsWordList, 0, $iReductionWord), array_slice($aReductionsWordList, $iReductionWord+1));
1116                                                                                 $aReverseSearch = $aSearch;
1117                                                                                 $aSearch['aAddress'] = $aReductionsWordListResult;
1118                                                                                 $aSearch['iSearchRank'] = $iSearchRank;
1119                                                                                 $aGroupedSearches[$iSearchRank][] = $aReverseSearch;
1120                                                                                 if (sizeof($aReductionsWordListResult) > 0)
1121                                                                                 {
1122                                                                                         $aNewReductionsList[] = $aReductionsWordListResult;
1123                                                                                 }
1124                                                                         }
1125                                                                 }
1126                                                                 $aReductionsList = $aNewReductionsList;
1127                                                         }
1128                                                 }
1129                                         }
1130                                         ksort($aGroupedSearches);
1131                                 }
1132
1133                                 // Filter out duplicate searches
1134                                 $aSearchHash = array();
1135                                 foreach($aGroupedSearches as $iGroup => $aSearches)
1136                                 {
1137                                         foreach($aSearches as $iSearch => $aSearch)
1138                                         {
1139                                                 $sHash = serialize($aSearch);
1140                                                 if (isset($aSearchHash[$sHash]))
1141                                                 {
1142                                                         unset($aGroupedSearches[$iGroup][$iSearch]);
1143                                                         if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
1144                                                 }
1145                                                 else
1146                                                 {
1147                                                         $aSearchHash[$sHash] = 1;
1148                                                 }
1149                                         }
1150                                 }
1151
1152                                 if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
1153
1154                                 $iGroupLoop = 0;
1155                                 $iQueryLoop = 0;
1156                                 foreach($aGroupedSearches as $iGroupedRank => $aSearches)
1157                                 {
1158                                         $iGroupLoop++;
1159                                         foreach($aSearches as $aSearch)
1160                                         {
1161                                                 $iQueryLoop++;
1162
1163                                                 if (CONST_Debug) { echo "<hr><b>Search Loop, group $iGroupLoop, loop $iQueryLoop</b>"; }
1164                                                 if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
1165
1166                                                 // No location term?
1167                                                 if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon'])
1168                                                 {
1169                                                         if ($aSearch['sCountryCode'] && !$aSearch['sClass'] && !$aSearch['sHouseNumber'])
1170                                                         {
1171                                                                 // Just looking for a country by code - look it up
1172                                                                 if (4 >= $this->iMinAddressRank && 4 <= $this->iMaxAddressRank)
1173                                                                 {
1174                                                                         $sSQL = "select place_id from placex where calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
1175                                                                         if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1176                                     if ($bBoundingBoxSearch)
1177                                         $sSQL .= " and _st_intersects($this->sViewboxSmallSQL, geometry)";
1178                                                                         $sSQL .= " order by st_area(geometry) desc limit 1";
1179                                                                         if (CONST_Debug) var_dump($sSQL);
1180                                                                         $aPlaceIDs = $this->oDB->getCol($sSQL);
1181                                                                 }
1182                                                                 else
1183                                                                 {
1184                                                                         $aPlaceIDs = array();
1185                                                                 }
1186                                                         }
1187                                                         else
1188                                                         {
1189                                                                 if (!$bBoundingBoxSearch && !$aSearch['fLon']) continue;
1190                                                                 if (!$aSearch['sClass']) continue;
1191                                                                 $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1192                                                                 if ($this->oDB->getOne($sSQL))
1193                                                                 {
1194                                                                         $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1195                                                                         if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1196                                                                         $sSQL .= " where st_contains($this->sViewboxSmallSQL, ct.centroid)";
1197                                                                         if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1198                                                                         if (sizeof($this->aExcludePlaceIDs))
1199                                                                         {
1200                                                                                 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1201                                                                         }
1202                                                                         if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1203                                                                         $sSQL .= " limit $this->iLimit";
1204                                                                         if (CONST_Debug) var_dump($sSQL);
1205                                                                         $aPlaceIDs = $this->oDB->getCol($sSQL);
1206
1207                                                                         // If excluded place IDs are given, it is fair to assume that
1208                                                                         // there have been results in the small box, so no further
1209                                                                         // expansion in that case.
1210                                                                         // Also don't expand if bounded results were requested.
1211                                                                         if (!sizeof($aPlaceIDs) && !sizeof($this->aExcludePlaceIDs) && !$this->bBoundedSearch)
1212                                                                         {
1213                                                                                 $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
1214                                                                                 if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
1215                                                                                 $sSQL .= " where st_contains($this->sViewboxLargeSQL, ct.centroid)";
1216                                                                                 if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1217                                                                                 if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, ct.centroid) asc";
1218                                                                                 $sSQL .= " limit $this->iLimit";
1219                                                                                 if (CONST_Debug) var_dump($sSQL);
1220                                                                                 $aPlaceIDs = $this->oDB->getCol($sSQL);
1221                                                                         }
1222                                                                 }
1223                                                                 else
1224                                                                 {
1225                                                                         $sSQL = "select place_id from placex where class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1226                                                                         $sSQL .= " and st_contains($this->sViewboxSmallSQL, geometry) and linked_place_id is null";
1227                                                                         if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1228                                                                         if ($sViewboxCentreSQL) $sSQL .= " order by st_distance($sViewboxCentreSQL, centroid) asc";
1229                                                                         $sSQL .= " limit $this->iLimit";
1230                                                                         if (CONST_Debug) var_dump($sSQL);
1231                                                                         $aPlaceIDs = $this->oDB->getCol($sSQL);
1232                                                                 }
1233                                                         }
1234                                                 }
1235                                                 else
1236                                                 {
1237                                                         $aPlaceIDs = array();
1238
1239                                                         // First we need a position, either aName or fLat or both
1240                                                         $aTerms = array();
1241                                                         $aOrder = array();
1242
1243                                                         // TODO: filter out the pointless search terms (2 letter name tokens and less)
1244                                                         // they might be right - but they are just too darned expensive to run
1245                                                         if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'],",")."]";
1246                                                         //if (sizeof($aSearch['aNameNonSearch'])) $aTerms[] = "array_cat(name_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aNameNonSearch'],",")."]";
1247                                                         if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress'])
1248                                                         {
1249                                                                 // For infrequent name terms disable index usage for address
1250                                                                 if (CONST_Search_NameOnlySearchFrequencyThreshold &&
1251                                                                                 sizeof($aSearch['aName']) == 1 &&
1252                                                                                 $aWordFrequencyScores[$aSearch['aName'][reset($aSearch['aName'])]] < CONST_Search_NameOnlySearchFrequencyThreshold)
1253                                                                 {
1254                                                                         //$aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join(array_merge($aSearch['aAddress'],$aSearch['aAddressNonSearch']),",")."]";
1255                                                                         $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddress'],",")."]";
1256                                                                 }
1257                                                                 else
1258                                                                 {
1259                                                                         $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'],",")."]";
1260                                                                         //if (sizeof($aSearch['aAddressNonSearch'])) $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddressNonSearch'],",")."]";
1261                                                                 }
1262                                                         }
1263                                                         if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
1264                                                         if ($aSearch['sHouseNumber'])
1265                                                         {
1266                                                                 $aTerms[] = "address_rank between 16 and 27";
1267                                                         }
1268                                                         else
1269                                                         {
1270                                                                 if ($this->iMinAddressRank > 0)
1271                                                                 {
1272                                                                         $aTerms[] = "address_rank >= ".$this->iMinAddressRank;
1273                                                                 }
1274                                                                 if ($this->iMaxAddressRank < 30)
1275                                                                 {
1276                                                                         $aTerms[] = "address_rank <= ".$this->iMaxAddressRank;
1277                                                                 }
1278                                                         }
1279                                                         if ($aSearch['fLon'] && $aSearch['fLat'])
1280                                                         {
1281                                                                 $aTerms[] = "ST_DWithin(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326), ".$aSearch['fRadius'].")";
1282                                                                 $aOrder[] = "ST_Distance(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326)) ASC";
1283                                                         }
1284                                                         if (sizeof($this->aExcludePlaceIDs))
1285                                                         {
1286                                                                 $aTerms[] = "place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1287                                                         }
1288                                                         if ($sCountryCodesSQL)
1289                                                         {
1290                                                                 $aTerms[] = "country_code in ($sCountryCodesSQL)";
1291                                                         }
1292
1293                                                         if ($bBoundingBoxSearch) $aTerms[] = "centroid && $this->sViewboxSmallSQL";
1294                                                         if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
1295
1296                                                         if ($aSearch['sHouseNumber'])
1297                                                         {
1298                                                                 $sImportanceSQL = '- abs(26 - address_rank) + 3';
1299                                                         }
1300                                                         else
1301                                                         {
1302                                                                 $sImportanceSQL = '(case when importance = 0 OR importance IS NULL then 0.75-(search_rank::float/40) else importance end)';
1303                                                         }
1304                                                         if ($this->sViewboxSmallSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
1305                                                         if ($this->sViewboxLargeSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
1306
1307                                                         $aOrder[] = "$sImportanceSQL DESC";
1308                                                         if (sizeof($aSearch['aFullNameAddress']))
1309                                                         {
1310                                                                 $sExactMatchSQL = '(select count(*) from (select unnest(ARRAY['.join($aSearch['aFullNameAddress'],",").']) INTERSECT select unnest(nameaddress_vector))s) as exactmatch';
1311                                                                 $aOrder[] = 'exactmatch DESC';
1312                                                         } else {
1313                                                                 $sExactMatchSQL = '0::int as exactmatch';
1314                                                         }
1315
1316                                                         if (sizeof($aTerms))
1317                                                         {
1318                                                                 $sSQL = "select place_id, ";
1319                                                                 $sSQL .= $sExactMatchSQL;
1320                                                                 $sSQL .= " from search_name";
1321                                                                 $sSQL .= " where ".join(' and ',$aTerms);
1322                                                                 $sSQL .= " order by ".join(', ',$aOrder);
1323                                                                 if ($aSearch['sHouseNumber'] || $aSearch['sClass'])
1324                                                                         $sSQL .= " limit 50";
1325                                                                 elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass'])
1326                                                                         $sSQL .= " limit 1";
1327                                                                 else
1328                                                                         $sSQL .= " limit ".$this->iLimit;
1329
1330                                                                 if (CONST_Debug) { var_dump($sSQL); }
1331                                                                 $aViewBoxPlaceIDs = $this->oDB->getAll($sSQL);
1332                                                                 if (PEAR::IsError($aViewBoxPlaceIDs))
1333                                                                 {
1334                                                                         failInternalError("Could not get places for search terms.", $sSQL, $aViewBoxPlaceIDs);
1335                                                                 }
1336                                                                 //var_dump($aViewBoxPlaceIDs);
1337                                                                 // Did we have an viewbox matches?
1338                                                                 $aPlaceIDs = array();
1339                                                                 $bViewBoxMatch = false;
1340                                                                 foreach($aViewBoxPlaceIDs as $aViewBoxRow)
1341                                                                 {
1342                                                                         //if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
1343                                                                         //if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
1344                                                                         //if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
1345                                                                         //else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
1346                                                                         $aPlaceIDs[] = $aViewBoxRow['place_id'];
1347                                                                         $this->exactMatchCache[$aViewBoxRow['place_id']] = $aViewBoxRow['exactmatch'];
1348                                                                 }
1349                                                         }
1350                                                         //var_Dump($aPlaceIDs);
1351                                                         //exit;
1352
1353                                                         if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs))
1354                                                         {
1355                                                                 $aRoadPlaceIDs = $aPlaceIDs;
1356                                                                 $sPlaceIDs = join(',',$aPlaceIDs);
1357
1358                                                                 // Now they are indexed look for a house attached to a street we found
1359                                                                 $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
1360                                                                 $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
1361                                                                 if (sizeof($this->aExcludePlaceIDs))
1362                                                                 {
1363                                                                         $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1364                                                                 }
1365                                                                 $sSQL .= " limit $this->iLimit";
1366                                                                 if (CONST_Debug) var_dump($sSQL);
1367                                                                 $aPlaceIDs = $this->oDB->getCol($sSQL);
1368
1369                                                                 // If not try the aux fallback table
1370                                                                 /*
1371                                                                 if (!sizeof($aPlaceIDs))
1372                                                                 {
1373                                                                         $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1374                                                                         if (sizeof($this->aExcludePlaceIDs))
1375                                                                         {
1376                                                                                 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1377                                                                         }
1378                                                                         //$sSQL .= " limit $this->iLimit";
1379                                                                         if (CONST_Debug) var_dump($sSQL);
1380                                                                         $aPlaceIDs = $this->oDB->getCol($sSQL);
1381                                                                 }
1382                                                                 */
1383
1384                                                                 if (!sizeof($aPlaceIDs))
1385                                                                 {
1386                                                                         $sSQL = "select place_id from location_property_tiger where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
1387                                                                         if (sizeof($this->aExcludePlaceIDs))
1388                                                                         {
1389                                                                                 $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1390                                                                         }
1391                                                                         //$sSQL .= " limit $this->iLimit";
1392                                                                         if (CONST_Debug) var_dump($sSQL);
1393                                                                         $aPlaceIDs = $this->oDB->getCol($sSQL);
1394                                                                 }
1395
1396                                                                 // Fallback to the road
1397                                                                 if (!sizeof($aPlaceIDs) && preg_match('/[0-9]+/', $aSearch['sHouseNumber']))
1398                                                                 {
1399                                                                         $aPlaceIDs = $aRoadPlaceIDs;
1400                                                                 }
1401
1402                                                         }
1403
1404                                                         if ($aSearch['sClass'] && sizeof($aPlaceIDs))
1405                                                         {
1406                                                                 $sPlaceIDs = join(',',$aPlaceIDs);
1407                                                                 $aClassPlaceIDs = array();
1408
1409                                                                 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name')
1410                                                                 {
1411                                                                         // If they were searching for a named class (i.e. 'Kings Head pub') then we might have an extra match
1412                                                                         $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
1413                                                                         $sSQL .= " and linked_place_id is null";
1414                                                                         if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
1415                                                                         $sSQL .= " order by rank_search asc limit $this->iLimit";
1416                                                                         if (CONST_Debug) var_dump($sSQL);
1417                                                                         $aClassPlaceIDs = $this->oDB->getCol($sSQL);
1418                                                                 }
1419
1420                                                                 if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') // & in
1421                                                                 {
1422                                                                         $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
1423                                                                         $bCacheTable = $this->oDB->getOne($sSQL);
1424
1425                                                                         $sSQL = "select min(rank_search) from placex where place_id in ($sPlaceIDs)";
1426
1427                                                                         if (CONST_Debug) var_dump($sSQL);
1428                                                                         $this->iMaxRank = ((int)$this->oDB->getOne($sSQL));
1429
1430                                                                         // For state / country level searches the normal radius search doesn't work very well
1431                                                                         $sPlaceGeom = false;
1432                                                                         if ($this->iMaxRank < 9 && $bCacheTable)
1433                                                                         {
1434                                                                                 // Try and get a polygon to search in instead
1435                                                                                 $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";
1436                                                                                 if (CONST_Debug) var_dump($sSQL);
1437                                                                                 $sPlaceGeom = $this->oDB->getOne($sSQL);
1438                                                                         }
1439
1440                                                                         if ($sPlaceGeom)
1441                                                                         {
1442                                                                                 $sPlaceIDs = false;
1443                                                                         }
1444                                                                         else
1445                                                                         {
1446                                                                                 $this->iMaxRank += 5;
1447                                                                                 $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank";
1448                                                                                 if (CONST_Debug) var_dump($sSQL);
1449                                                                                 $aPlaceIDs = $this->oDB->getCol($sSQL);
1450                                                                                 $sPlaceIDs = join(',',$aPlaceIDs);
1451                                                                         }
1452
1453                                                                         if ($sPlaceIDs || $sPlaceGeom)
1454                                                                         {
1455
1456                                                                                 $fRange = 0.01;
1457                                                                                 if ($bCacheTable)
1458                                                                                 {
1459                                                                                         // More efficient - can make the range bigger
1460                                                                                         $fRange = 0.05;
1461
1462                                                                                         $sOrderBySQL = '';
1463                                                                                         if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
1464                                                                                         else if ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
1465                                                                                         else if ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
1466
1467                                                                                         $sSQL = "select distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
1468                                                                                         if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
1469                                                                                         if ($sPlaceIDs)
1470                                                                                         {
1471                                                                                                 $sSQL .= ",placex as f where ";
1472                                                                                                 $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, f.centroid, $fRange) ";
1473                                                                                         }
1474                                                                                         if ($sPlaceGeom)
1475                                                                                         {
1476                                                                                                 $sSQL .= " where ";
1477                                                                                                 $sSQL .= "ST_Contains('".$sPlaceGeom."', l.centroid) ";
1478                                                                                         }
1479                                                                                         if (sizeof($this->aExcludePlaceIDs))
1480                                                                                         {
1481                                                                                                 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1482                                                                                         }
1483                                                                                         if ($sCountryCodesSQL) $sSQL .= " and lp.calculated_country_code in ($sCountryCodesSQL)";
1484                                                                                         if ($sOrderBySQL) $sSQL .= "order by ".$sOrderBySQL." asc";
1485                                                                                         if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1486                                                                                         $sSQL .= " limit $this->iLimit";
1487                                                                                         if (CONST_Debug) var_dump($sSQL);
1488                                                                                         $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1489                                                                                 }
1490                                                                                 else
1491                                                                                 {
1492                                                                                         if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
1493
1494                                                                                         $sOrderBySQL = '';
1495                                                                                         if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
1496                                                                                         else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
1497
1498                                                                                         $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from placex as l,placex as f where ";
1499                                                                                         $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, f.centroid, $fRange) ";
1500                                                                                         $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
1501                                                                                         if (sizeof($this->aExcludePlaceIDs))
1502                                                                                         {
1503                                                                                                 $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
1504                                                                                         }
1505                                                                                         if ($sCountryCodesSQL) $sSQL .= " and l.calculated_country_code in ($sCountryCodesSQL)";
1506                                                                                         if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
1507                                                                                         if ($this->iOffset) $sSQL .= " offset $this->iOffset";
1508                                                                                         $sSQL .= " limit $this->iLimit";
1509                                                                                         if (CONST_Debug) var_dump($sSQL);
1510                                                                                         $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
1511                                                                                 }
1512                                                                         }
1513                                                                 }
1514
1515                                                                 $aPlaceIDs = $aClassPlaceIDs;
1516
1517                                                         }
1518
1519                                                 }
1520
1521                                                 if (PEAR::IsError($aPlaceIDs))
1522                                                 {
1523                                                         failInternalError("Could not get place IDs from tokens." ,$sSQL, $aPlaceIDs);
1524                                                 }
1525
1526                                                 if (CONST_Debug) { echo "<br><b>Place IDs:</b> "; var_Dump($aPlaceIDs); }
1527
1528                                                 foreach($aPlaceIDs as $iPlaceID)
1529                                                 {
1530                                                         $aResultPlaceIDs[$iPlaceID] = $iPlaceID;
1531                                                 }
1532                                                 if ($iQueryLoop > 20) break;
1533                                         }
1534
1535                                         if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30))
1536                                         {
1537                                                 // Need to verify passes rank limits before dropping out of the loop (yuk!)
1538                                                 $sSQL = "select place_id from placex where place_id in (".join(',',$aResultPlaceIDs).") ";
1539                                                 $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
1540                                                 if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
1541                                                 if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")";
1542                                                 $sSQL .= ") UNION select place_id from location_property_tiger where place_id in (".join(',',$aResultPlaceIDs).") ";
1543                                                 $sSQL .= "and (30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
1544                                                 if ($this->aAddressRankList) $sSQL .= " OR 30 in (".join(',',$this->aAddressRankList).")";
1545                                                 $sSQL .= ")";
1546                                                 if (CONST_Debug) var_dump($sSQL);
1547                                                 $aResultPlaceIDs = $this->oDB->getCol($sSQL);
1548                                         }
1549
1550                                         //exit;
1551                                         if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs)) break;
1552                                         if ($iGroupLoop > 4) break;
1553                                         if ($iQueryLoop > 30) break;
1554                                 }
1555
1556                                 // Did we find anything?
1557                                 if (isset($aResultPlaceIDs) && sizeof($aResultPlaceIDs))
1558                                 {
1559                                         $aSearchResults = $this->getDetails($aResultPlaceIDs);
1560                                 }
1561
1562                         }
1563                         else
1564                         {
1565                                 // Just interpret as a reverse geocode
1566                                 $iPlaceID = geocodeReverse((float)$this->aNearPoint[0], (float)$this->aNearPoint[1]);
1567                                 if ($iPlaceID)
1568                                         $aSearchResults = $this->getDetails(array($iPlaceID));
1569                                 else
1570                                         $aSearchResults = array();
1571                         }
1572
1573                         // No results? Done
1574                         if (!sizeof($aSearchResults))
1575                         {
1576                                 if ($this->bFallback)
1577                                 {
1578                                         if ($this->fallbackStructuredQuery())
1579                                         {
1580                                                 return $this->lookup();
1581                                         }
1582                                 }
1583
1584                                 return array();
1585                         }
1586
1587                         $aClassType = getClassTypesWithImportance();
1588                         $aRecheckWords = preg_split('/\b[\s,\\-]*/u',$sQuery);
1589                         foreach($aRecheckWords as $i => $sWord)
1590                         {
1591                                 if (!$sWord) unset($aRecheckWords[$i]);
1592                         }
1593
1594                         foreach($aSearchResults as $iResNum => $aResult)
1595                         {
1596                                 if (CONST_Search_AreaPolygons)
1597                                 {
1598                                         // Get the bounding box and outline polygon
1599                                         $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
1600                                         $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
1601                                         $sSQL .= "ST_Y(ST_PointN(ST_ExteriorRing(Box2D(geometry)),4)) as minlat,ST_Y(ST_PointN(ST_ExteriorRing(Box2D(geometry)),2)) as maxlat,";
1602                                         $sSQL .= "ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),1)) as minlon,ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),3)) as maxlon";
1603                                         if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
1604                                         if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
1605                                         if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
1606                                         if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
1607                                         $sSQL .= " from placex where place_id = ".$aResult['place_id'].' and st_geometrytype(Box2D(geometry)) = \'ST_Polygon\'';
1608                                         $aPointPolygon = $this->oDB->getRow($sSQL);
1609                                         if (PEAR::IsError($aPointPolygon))
1610                                         {
1611                                                 failInternalError("Could not get outline.", $sSQL, $aPointPolygon);
1612                                         }
1613
1614                                         if ($aPointPolygon['place_id'])
1615                                         {
1616                                                 if ($this->bIncludePolygonAsGeoJSON) $aResult['asgeojson'] = $aPointPolygon['asgeojson'];
1617                                                 if ($this->bIncludePolygonAsKML) $aResult['askml'] = $aPointPolygon['askml'];
1618                                                 if ($this->bIncludePolygonAsSVG) $aResult['assvg'] = $aPointPolygon['assvg'];
1619                                                 if ($this->bIncludePolygonAsText) $aResult['astext'] = $aPointPolygon['astext'];
1620
1621                                                 if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null )
1622                                                 {
1623                                                         $aResult['lat'] = $aPointPolygon['centrelat'];
1624                                                         $aResult['lon'] = $aPointPolygon['centrelon'];
1625                                                 }
1626
1627                                                 if ($this->bIncludePolygonAsPoints)
1628                                                 {
1629                                                         // Translate geometary string to point array
1630                                                         if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1631                                                         {
1632                                                                 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1633                                                         }
1634                             /*
1635                                                         elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
1636                                                         {
1637                                                                 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
1638                                                         }
1639                             */
1640                                                         elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#',$aPointPolygon['astext'],$aMatch))
1641                                                         {
1642                                                                 $fRadius = 0.01;
1643                                                                 $iSteps = ($fRadius * 40000)^2;
1644                                                                 $fStepSize = (2*pi())/$iSteps;
1645                                                                 $aPolyPoints = array();
1646                                                                 for($f = 0; $f < 2*pi(); $f += $fStepSize)
1647                                                                 {
1648                                                                         $aPolyPoints[] = array('',$aMatch[1]+($fRadius*sin($f)),$aMatch[2]+($fRadius*cos($f)));
1649                                                                 }
1650                                                                 $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
1651                                                                 $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
1652                                                                 $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
1653                                                                 $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
1654                                                         }
1655                                                 }
1656
1657                                                 // Output data suitable for display (points and a bounding box)
1658                                                 if ($this->bIncludePolygonAsPoints && isset($aPolyPoints))
1659                                                 {
1660                                                         $aResult['aPolyPoints'] = array();
1661                                                         foreach($aPolyPoints as $aPoint)
1662                                                         {
1663                                                                 $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1664                                                         }
1665                                                 }
1666                                                 $aResult['aBoundingBox'] = array($aPointPolygon['minlat'],$aPointPolygon['maxlat'],$aPointPolygon['minlon'],$aPointPolygon['maxlon']);
1667                                         }
1668                                 }
1669
1670                                 if ($aResult['extra_place'] == 'city')
1671                                 {
1672                                         $aResult['class'] = 'place';
1673                                         $aResult['type'] = 'city';
1674                                         $aResult['rank_search'] = 16;
1675                                 }
1676
1677                                 if (!isset($aResult['aBoundingBox']))
1678                                 {
1679                                         // Default
1680                                         $fDiameter = 0.0001;
1681
1682                                         if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1683                                                         && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
1684                                         {
1685                                                 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defzoom'];
1686                                         }
1687                                         elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1688                                                         && $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
1689                                         {
1690                                                 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'];
1691                                         }
1692                                         $fRadius = $fDiameter / 2;
1693
1694                                         $iSteps = max(8,min(100,$fRadius * 3.14 * 100000));
1695                                         $fStepSize = (2*pi())/$iSteps;
1696                                         $aPolyPoints = array();
1697                                         for($f = 0; $f < 2*pi(); $f += $fStepSize)
1698                                         {
1699                                                 $aPolyPoints[] = array('',$aResult['lon']+($fRadius*sin($f)),$aResult['lat']+($fRadius*cos($f)));
1700                                         }
1701                                         $aPointPolygon['minlat'] = $aResult['lat'] - $fRadius;
1702                                         $aPointPolygon['maxlat'] = $aResult['lat'] + $fRadius;
1703                                         $aPointPolygon['minlon'] = $aResult['lon'] - $fRadius;
1704                                         $aPointPolygon['maxlon'] = $aResult['lon'] + $fRadius;
1705
1706                                         // Output data suitable for display (points and a bounding box)
1707                                         if ($this->bIncludePolygonAsPoints)
1708                                         {
1709                                                 $aResult['aPolyPoints'] = array();
1710                                                 foreach($aPolyPoints as $aPoint)
1711                                                 {
1712                                                         $aResult['aPolyPoints'][] = array($aPoint[1], $aPoint[2]);
1713                                                 }
1714                                         }
1715                                         $aResult['aBoundingBox'] = array((string)$aPointPolygon['minlat'],(string)$aPointPolygon['maxlat'],(string)$aPointPolygon['minlon'],(string)$aPointPolygon['maxlon']);
1716                                 }
1717
1718                                 // Is there an icon set for this type of result?
1719                                 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1720                                                 && $aClassType[$aResult['class'].':'.$aResult['type']]['icon'])
1721                                 {
1722                                         $aResult['icon'] = CONST_Website_BaseURL.'images/mapicons/'.$aClassType[$aResult['class'].':'.$aResult['type']]['icon'].'.p.20.png';
1723                                 }
1724
1725                                 if (isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1726                                                 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'])
1727                                 {
1728                                         $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['label'];
1729                                 }
1730                                 elseif (isset($aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1731                                                 && $aClassType[$aResult['class'].':'.$aResult['type']]['label'])
1732                                 {
1733                                         $aResult['label'] = $aClassType[$aResult['class'].':'.$aResult['type']]['label'];
1734                                 }
1735
1736                                 if ($this->bIncludeAddressDetails)
1737                                 {
1738                                         $aResult['address'] = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code']);
1739                                         if ($aResult['extra_place'] == 'city' && !isset($aResult['address']['city']))
1740                                         {
1741                                                 $aResult['address'] = array_merge(array('city' => array_shift(array_values($aResult['address']))), $aResult['address']);
1742                                         }
1743                                 }
1744
1745                                 // Adjust importance for the number of exact string matches in the result
1746                                 $aResult['importance'] = max(0.001,$aResult['importance']);
1747                                 $iCountWords = 0;
1748                                 $sAddress = $aResult['langaddress'];
1749                                 foreach($aRecheckWords as $i => $sWord)
1750                                 {
1751                                         if (stripos($sAddress, $sWord)!==false)
1752                                         {
1753                                                 $iCountWords++;
1754                                                 if (preg_match("/(^|,)\s*$sWord\s*(,|$)/", $sAddress)) $iCountWords += 0.1;
1755                                         }
1756                                 }
1757
1758                                 $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
1759
1760                                 $aResult['name'] = $aResult['langaddress'];
1761                                 // secondary ordering (for results with same importance (the smaller the better):
1762                                 //   - approximate importance of address parts
1763                                 $aResult['foundorder'] = -$aResult['addressimportance']/10;
1764                                 //   - number of exact matches from the query
1765                                 if (isset($this->exactMatchCache[$aResult['place_id']]))
1766                                         $aResult['foundorder'] -= $this->exactMatchCache[$aResult['place_id']];
1767                                 else if (isset($this->exactMatchCache[$aResult['parent_place_id']]))
1768                                         $aResult['foundorder'] -= $this->exactMatchCache[$aResult['parent_place_id']];
1769                                 //  - importance of the class/type
1770                                 if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1771                                         && $aClassType[$aResult['class'].':'.$aResult['type']]['importance'])
1772                                 {
1773                                         $aResult['foundorder'] = $aResult['foundorder'] + 0.000001 * $aClassType[$aResult['class'].':'.$aResult['type']]['importance'];
1774                                 }
1775                                 else
1776                                 {
1777                                         $aResult['foundorder'] = $aResult['foundorder'] + 0.001;
1778                                 }
1779                                 $aSearchResults[$iResNum] = $aResult;
1780                         }
1781                         uasort($aSearchResults, 'byImportance');
1782
1783                         $aOSMIDDone = array();
1784                         $aClassTypeNameDone = array();
1785                         $aToFilter = $aSearchResults;
1786                         $aSearchResults = array();
1787
1788                         $bFirst = true;
1789                         foreach($aToFilter as $iResNum => $aResult)
1790                         {
1791                                 $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
1792                                 if ($bFirst)
1793                                 {
1794                                         $fLat = $aResult['lat'];
1795                                         $fLon = $aResult['lon'];
1796                                         if (isset($aResult['zoom'])) $iZoom = $aResult['zoom'];
1797                                         $bFirst = false;
1798                                 }
1799                                 if (!$this->bDeDupe || (!isset($aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']])
1800                                                         && !isset($aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']])))
1801                                 {
1802                                         $aOSMIDDone[$aResult['osm_type'].$aResult['osm_id']] = true;
1803                                         $aClassTypeNameDone[$aResult['osm_type'].$aResult['class'].$aResult['type'].$aResult['name'].$aResult['admin_level']] = true;
1804                                         $aSearchResults[] = $aResult;
1805                                 }
1806
1807                                 // Absolute limit on number of results
1808                                 if (sizeof($aSearchResults) >= $this->iFinalLimit) break;
1809                         }
1810
1811                         return $aSearchResults;
1812
1813                 } // end lookup()
1814
1815
1816         } // end class
1817