+
+ /**
+ * Query database for places that match this search.
+ *
+ * @param object $oDB Nominatim::DB instance to use.
+ * @param integer $iMinRank Minimum address rank to restrict search to.
+ * @param integer $iMaxRank Maximum address rank to restrict search to.
+ * @param integer $iLimit Maximum number of results.
+ *
+ * @return mixed[] An array with two fields: IDs contains the list of
+ * matching place IDs and houseNumber the houseNumber
+ * if appicable or -1 if not.
+ */
+ public function query(&$oDB, $iMinRank, $iMaxRank, $iLimit)
+ {
+ $aResults = array();
+ $iHousenumber = -1;
+
+ if ($this->sCountryCode
+ && empty($this->aName)
+ && !$this->iOperator
+ && !$this->sClass
+ && !$this->oContext->hasNearPoint()
+ ) {
+ // Just looking for a country - look it up
+ if (4 >= $iMinRank && 4 <= $iMaxRank) {
+ $aResults = $this->queryCountry($oDB);
+ }
+ } elseif (empty($this->aName) && empty($this->aAddress)) {
+ // Neither name nor address? Then we must be
+ // looking for a POI in a geographic area.
+ if ($this->oContext->isBoundedSearch()) {
+ $aResults = $this->queryNearbyPoi($oDB, $iLimit);
+ }
+ } elseif ($this->iOperator == Operator::POSTCODE) {
+ // looking for postcode
+ $aResults = $this->queryPostcode($oDB, $iLimit);
+ } else {
+ // Ordinary search:
+ // First search for places according to name and address.
+ $aResults = $this->queryNamedPlace(
+ $oDB,
+ $iMinRank,
+ $iMaxRank,
+ $iLimit
+ );
+
+ // Now search for housenumber, if housenumber provided. Can be zero.
+ if (($this->sHouseNumber || $this->sHouseNumber === '0') && !empty($aResults)) {
+ // Downgrade the rank of the street results, they are missing
+ // the housenumber.
+ foreach ($aResults as $oRes) {
+ $oRes->iResultRank++;
+ }
+
+ $aHnResults = $this->queryHouseNumber($oDB, $aResults);
+
+ if (!empty($aHnResults)) {
+ foreach ($aHnResults as $oRes) {
+ $aResults[$oRes->iId] = $oRes;
+ }
+ }
+ }
+
+ // finally get POIs if requested
+ if ($this->sClass && !empty($aResults)) {
+ $aResults = $this->queryPoiByOperator($oDB, $aResults, $iLimit);
+ }
+ }
+
+ Debug::printDebugTable('Place IDs', $aResults);
+
+ if (!empty($aResults) && $this->sPostcode) {
+ $sPlaceIds = Result::joinIdsByTable($aResults, Result::TABLE_PLACEX);
+ if ($sPlaceIds) {
+ $sSQL = 'SELECT place_id FROM placex';
+ $sSQL .= ' WHERE place_id in ('.$sPlaceIds.')';
+ $sSQL .= " AND postcode != '".$this->sPostcode."'";
+ Debug::printSQL($sSQL);
+ $aFilteredPlaceIDs = $oDB->getCol($sSQL);
+ if ($aFilteredPlaceIDs) {
+ foreach ($aFilteredPlaceIDs as $iPlaceId) {
+ $aResults[$iPlaceId]->iResultRank++;
+ }
+ }
+ }
+ }
+
+ return $aResults;
+ }
+
+
+ private function queryCountry(&$oDB)