]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib/Geocode.php
replace PHP sizeof() with either count() or empty()
[nominatim.git] / lib / Geocode.php
index ae518d5137623c9e240dd6a10b116aeb03aef6b1..9daaff84df73a04026c77cc8d95d67c30a5663fa 100644 (file)
@@ -225,14 +225,14 @@ class Geocode
         $aViewbox = $oParams->getStringList('viewboxlbrt');
         if ($aViewbox) {
             if (count($aViewbox) != 4) {
-                userError("Bad parmater 'viewboxlbrt'. Expected 4 coordinates.");
+                userError("Bad parameter 'viewboxlbrt'. Expected 4 coordinates.");
             }
             $this->setViewbox($aViewbox);
         } else {
             $aViewbox = $oParams->getStringList('viewbox');
             if ($aViewbox) {
                 if (count($aViewbox) != 4) {
-                    userError("Bad parmater 'viewbox'. Expected 4 coordinates.");
+                    userError("Bad parameter 'viewbox'. Expected 4 coordinates.");
                 }
                 $this->setViewBox($aViewbox);
             } else {
@@ -303,7 +303,7 @@ class Geocode
         $this->loadStructuredAddressElement($sPostalCode, 'postalcode', 5, 11, array(5, 11));
         $this->loadStructuredAddressElement($sCountry, 'country', 4, 4, false);
 
-        if (sizeof($this->aStructuredQuery) > 0) {
+        if (!empty($this->aStructuredQuery)) {
             $this->sQuery = join(', ', $this->aStructuredQuery);
             if ($this->iMaxAddressRank < 30) {
                 $this->sAllowedTypesSQLList = '(\'place\',\'boundary\')';
@@ -317,7 +317,7 @@ class Geocode
 
         $aParams = $this->aStructuredQuery;
 
-        if (sizeof($aParams) == 1) return false;
+        if (count($aParams) == 1) return false;
 
         $aOrderToFallback = array('postalcode', 'street', 'city', 'county', 'state');
 
@@ -345,8 +345,6 @@ class Geocode
 
              Score how good the search is so they can be ordered
          */
-        $iGlobalRank = 0;
-
         foreach ($aPhrases as $iPhrase => $oPhrase) {
             $aNewPhraseSearches = array();
             $sPhraseType = $bIsStructured ? $oPhrase->getPhraseType() : '';
@@ -377,9 +375,8 @@ class Geocode
                                     $sPhraseType,
                                     $iToken == 0 && $iPhrase == 0,
                                     $iPhrase == 0,
-                                    $iToken + 1 == sizeof($aWordset)
-                                      && $iPhrase + 1 == sizeof($aPhrases),
-                                    $iGlobalRank
+                                    $iToken + 1 == count($aWordset)
+                                      && $iPhrase + 1 == count($aPhrases)
                                 );
 
                                 foreach ($aNewSearches as $oSearch) {
@@ -414,7 +411,7 @@ class Geocode
                     usort($aNewWordsetSearches, array('Nominatim\SearchDescription', 'bySearchRank'));
                     $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
                 }
-                //var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
+                //var_Dump('<hr>',count($aWordsetSearches)); exit;
 
                 $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
                 usort($aNewPhraseSearches, array('Nominatim\SearchDescription', 'bySearchRank'));
@@ -445,7 +442,7 @@ class Geocode
             $iSearchCount = 0;
             $aSearches = array();
             foreach ($aGroupedSearches as $iScore => $aNewSearches) {
-                $iSearchCount += sizeof($aNewSearches);
+                $iSearchCount += count($aNewSearches);
                 $aSearches = array_merge($aSearches, $aNewSearches);
                 if ($iSearchCount > 50) break;
             }
@@ -460,7 +457,7 @@ class Geocode
                 continue;
             }
 
-            $iRank = $oSearch->addToRank($iGlobalRank);
+            $iRank = $oSearch->getRank();
             if (!isset($aGroupedSearches[$iRank])) {
                 $aGroupedSearches[$iRank] = array();
             }
@@ -482,9 +479,9 @@ class Geocode
             osm_id: id of corresponding OSM object
             class: general object class (corresponds to tag key of primary OSM tag)
             type: subclass of object (corresponds to tag value of primary OSM tag)
-            admin_level: see http://wiki.openstreetmap.org/wiki/Admin_level
+            admin_level: see https://wiki.openstreetmap.org/wiki/Admin_level
             rank_search: rank in search hierarchy
-                        (see also http://wiki.openstreetmap.org/wiki/Nominatim/Development_overview#Country_to_street_level)
+                        (see also https://wiki.openstreetmap.org/wiki/Nominatim/Development_overview#Country_to_street_level)
             rank_address: rank in address hierarchy (determines orer in address)
             place_id: internal key (may differ between different instances)
             country_code: ISO country code
@@ -528,19 +525,19 @@ class Geocode
 
         $sNormQuery = $this->normTerm($this->sQuery);
         $sLanguagePrefArraySQL = getArraySQL(
-            array_map("getDBQuoted", $this->aLangPrefOrder)
+            array_map('getDBQuoted', $this->aLangPrefOrder)
         );
 
         $sQuery = $this->sQuery;
         if (!preg_match('//u', $sQuery)) {
-            userError("Query string is not UTF-8 encoded.");
+            userError('Query string is not UTF-8 encoded.');
         }
 
         // Conflicts between US state abreviations and various words for 'the' in different languages
         if (isset($this->aLangPrefOrder['name:en'])) {
-            $sQuery = preg_replace('/(^|,)\s*il\s*(,|$)/', '\1illinois\2', $sQuery);
-            $sQuery = preg_replace('/(^|,)\s*al\s*(,|$)/', '\1alabama\2', $sQuery);
-            $sQuery = preg_replace('/(^|,)\s*la\s*(,|$)/', '\1louisiana\2', $sQuery);
+            $sQuery = preg_replace('/(^|,)\s*il\s*(,|$)/i', '\1illinois\2', $sQuery);
+            $sQuery = preg_replace('/(^|,)\s*al\s*(,|$)/i', '\1alabama\2', $sQuery);
+            $sQuery = preg_replace('/(^|,)\s*la\s*(,|$)/i', '\1louisiana\2', $sQuery);
         }
 
         // Do we have anything that looks like a lat/lon pair?
@@ -580,7 +577,7 @@ class Geocode
                 $sSpecialTerm = pg_escape_string($sSpecialTerm);
                 $sToken = chksql(
                     $this->oDB->getOne("SELECT make_standard_name('$sSpecialTerm')"),
-                    "Cannot decode query. Wrong encoding?"
+                    'Cannot decode query. Wrong encoding?'
                 );
                 $sSQL = 'SELECT class, type FROM word ';
                 $sSQL .= '   WHERE word_token in (\' '.$sToken.'\')';
@@ -621,7 +618,7 @@ class Geocode
             foreach ($aInPhrases as $iPhrase => $sPhrase) {
                 $sPhrase = chksql(
                     $this->oDB->getOne('SELECT make_standard_name('.getDBQuoted($sPhrase).')'),
-                    "Cannot normalize query string (is it a UTF-8 string?)"
+                    'Cannot normalize query string (is it a UTF-8 string?)'
                 );
                 if (trim($sPhrase)) {
                     $oPhrase = new Phrase($sPhrase, is_string($iPhrase) ? $iPhrase : '');
@@ -630,18 +627,18 @@ class Geocode
                 }
             }
 
-            if (sizeof($aTokens)) {
+            if (!empty($aTokens)) {
                 // Check which tokens we have, get the ID numbers
                 $sSQL = 'SELECT word_id, word_token, word, class, type, country_code, operator, search_name_count';
                 $sSQL .= ' FROM word ';
-                $sSQL .= ' WHERE word_token in ('.join(',', array_map("getDBQuoted", $aTokens)).')';
+                $sSQL .= ' WHERE word_token in ('.join(',', array_map('getDBQuoted', $aTokens)).')';
 
                 if (CONST_Debug) var_Dump($sSQL);
 
                 $aValidTokens = array();
                 $aDatabaseWords = chksql(
                     $this->oDB->getAll($sSQL),
-                    "Could not get word tokens."
+                    'Could not get word tokens.'
                 );
                 $aWordFrequencyScores = array();
                 foreach ($aDatabaseWords as $aToken) {
@@ -705,8 +702,8 @@ class Geocode
                     // because order in the address doesn't matter.
                     $aPhrases = array_reverse($aPhrases);
                     $aPhrases[0]->invertWordSets();
-                    if (sizeof($aPhrases) > 1) {
-                        $aPhrases[sizeof($aPhrases)-1]->invertWordSets();
+                    if (count($aPhrases) > 1) {
+                        $aPhrases[count($aPhrases)-1]->invertWordSets();
                     }
                     $aReverseGroupedSearches = $this->getGroupedSearches($aSearches, $aPhrases, $aValidTokens, false);
 
@@ -741,7 +738,7 @@ class Geocode
                     $sHash = serialize($aSearch);
                     if (isset($aSearchHash[$sHash])) {
                         unset($aGroupedSearches[$iGroup][$iSearch]);
-                        if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
+                        if (empty($aGroupedSearches[$iGroup])) unset($aGroupedSearches[$iGroup]);
                     } else {
                         $aSearchHash[$sHash] = 1;
                     }
@@ -774,7 +771,7 @@ class Geocode
                     if ($iQueryLoop > 20) break;
                 }
 
-                if (sizeof($aResults) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30)) {
+                if (!empty($aResults) && ($this->iMinAddressRank != 0 || $this->iMaxAddressRank != 30)) {
                     // Need to verify passes rank limits before dropping out of the loop (yuk!)
                     // reduces the number of place ids, like a filter
                     // rank_address is 30 for interpolated housenumbers
@@ -783,15 +780,15 @@ class Geocode
                     if ($sPlaceIds) {
                         $sSQL = 'SELECT place_id FROM placex ';
                         $sSQL .= 'WHERE place_id in ('.$sPlaceIds.') ';
-                        $sSQL .= "  AND (";
+                        $sSQL .= '  AND (';
                         $sSQL .= "         placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
                         if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) {
                             $sSQL .= "     OR (extratags->'place') = 'city'";
                         }
                         if ($this->aAddressRankList) {
-                            $sSQL .= "     OR placex.rank_address in (".join(',', $this->aAddressRankList).")";
+                            $sSQL .= '     OR placex.rank_address in ('.join(',', $this->aAddressRankList).')';
                         }
-                        $sSQL .= ")";
+                        $sSQL .= ')';
                         $aFilterSql[] = $sSQL;
                     }
                     $sPlaceIds = Result::joinIdsByTable($aResults, Result::TABLE_POSTCODE);
@@ -800,9 +797,9 @@ class Geocode
                         $sSQL .= 'WHERE place_id in ('.$sPlaceIds.') ';
                         $sSQL .= "  AND (lp.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
                         if ($this->aAddressRankList) {
-                            $sSQL .= "     OR lp.rank_address in (".join(',', $this->aAddressRankList).")";
+                            $sSQL .= '     OR lp.rank_address in ('.join(',', $this->aAddressRankList).')';
                         }
-                        $sSQL .= ") ";
+                        $sSQL .= ') ';
                         $aFilterSql[] = $sSQL;
                     }
 
@@ -827,7 +824,7 @@ class Geocode
                     $aResults = $tempIDs;
                 }
 
-                if (sizeof($aResults)) break;
+                if (!empty($aResults)) break;
                 if ($iGroupLoop > 4) break;
                 if ($iQueryLoop > 30) break;
             }
@@ -838,7 +835,7 @@ class Geocode
 
             $oLookup = $oReverse->lookupPoint($oCtx->sqlNear, false);
 
-            if (CONST_Debug) var_dump("Reverse search", $aLookup);
+            if (CONST_Debug) var_dump('Reverse search', $aLookup);
 
             if ($oLookup) {
                 $aResults = array($oLookup->iId => $oLookup);
@@ -846,7 +843,7 @@ class Geocode
         }
 
         // No results? Done
-        if (!sizeof($aResults)) {
+        if (empty($aResults)) {
             if ($this->bFallback) {
                 if ($this->fallbackStructuredQuery()) {
                     return $this->lookup();
@@ -924,18 +921,18 @@ class Geocode
                 $aResult['importance'] = 0.001;
                 $aResult['foundorder'] = $aResult['addressimportance'];
             } else {
-                // Adjust importance for the number of exact string matches in the result
+                $aResult['importance'] = max(0.001, $aResult['importance']);
                 $aResult['importance'] *= $this->viewboxImportanceFactor(
                     $aResult['lon'],
                     $aResult['lat']
                 );
-                $aResult['importance'] = max(0.001, $aResult['importance']);
+                // Adjust importance for the number of exact string matches in the result
                 $iCountWords = 0;
                 $sAddress = $aResult['langaddress'];
                 foreach ($aRecheckWords as $i => $sWord) {
                     if (stripos($sAddress, $sWord)!==false) {
                         $iCountWords++;
-                        if (preg_match("/(^|,)\s*".preg_quote($sWord, '/')."\s*(,|$)/", $sAddress)) $iCountWords += 0.1;
+                        if (preg_match('/(^|,)\s*'.preg_quote($sWord, '/').'\s*(,|$)/', $sAddress)) $iCountWords += 0.1;
                     }
                 }
 
@@ -985,7 +982,7 @@ class Geocode
             }
 
             // Absolute limit on number of results
-            if (sizeof($aSearchResults) >= $this->iFinalLimit) break;
+            if (count($aSearchResults) >= $this->iFinalLimit) break;
         }
 
         if (CONST_Debug) var_dump($aSearchResults);