2018-05-15 00:04:15 +03:00
|
|
|
<?php
|
2022-01-03 18:23:58 +03:00
|
|
|
/**
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
*
|
|
|
|
* This file is part of Nominatim. (https://nominatim.org)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2022 by the Nominatim developer community.
|
|
|
|
* For a full list of authors see the git log.
|
|
|
|
*/
|
2018-05-15 00:04:15 +03:00
|
|
|
|
|
|
|
namespace Nominatim\Token;
|
|
|
|
|
2020-12-15 12:09:55 +03:00
|
|
|
require_once(CONST_LibDir.'/SpecialSearchOperator.php');
|
2018-05-15 00:04:15 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A word token describing a place type.
|
|
|
|
*/
|
|
|
|
class SpecialTerm
|
|
|
|
{
|
2018-05-15 00:23:38 +03:00
|
|
|
/// Database word id, if applicable.
|
2021-07-18 21:20:22 +03:00
|
|
|
private $iId;
|
2018-05-15 00:23:38 +03:00
|
|
|
/// Class (or OSM tag key) of the place to look for.
|
2021-07-18 21:20:22 +03:00
|
|
|
private $sClass;
|
2018-05-15 00:23:38 +03:00
|
|
|
/// Type (or OSM tag value) of the place to look for.
|
2021-07-18 21:20:22 +03:00
|
|
|
private $sType;
|
2018-05-15 00:23:38 +03:00
|
|
|
/// Relationship of the operator to the object (see Operator class).
|
2021-07-18 21:20:22 +03:00
|
|
|
private $iOperator;
|
2018-05-15 00:04:15 +03:00
|
|
|
|
|
|
|
public function __construct($iID, $sClass, $sType, $iOperator)
|
|
|
|
{
|
|
|
|
$this->iId = $iID;
|
|
|
|
$this->sClass = $sClass;
|
|
|
|
$this->sType = $sType;
|
|
|
|
$this->iOperator = $iOperator;
|
|
|
|
}
|
|
|
|
|
2021-07-17 21:24:33 +03:00
|
|
|
public function getId()
|
|
|
|
{
|
|
|
|
return $this->iId;
|
|
|
|
}
|
|
|
|
|
2021-07-17 23:01:35 +03:00
|
|
|
/**
|
|
|
|
* Check if the token can be added to the given search.
|
|
|
|
* Derive new searches by adding this token to an existing search.
|
|
|
|
*
|
|
|
|
* @param object $oSearch Partial search description derived so far.
|
|
|
|
* @param object $oPosition Description of the token position within
|
|
|
|
the query.
|
|
|
|
*
|
|
|
|
* @return True if the token is compatible with the search configuration
|
|
|
|
* given the position.
|
|
|
|
*/
|
|
|
|
public function isExtendable($oSearch, $oPosition)
|
|
|
|
{
|
2021-09-02 19:13:45 +03:00
|
|
|
return !$oSearch->hasOperator()
|
|
|
|
&& $oPosition->isPhrase('')
|
|
|
|
&& ($this->iOperator != \Nominatim\Operator::NONE
|
|
|
|
|| (!$oSearch->hasAddress() && !$oSearch->hasHousenumber() && !$oSearch->hasCountry()));
|
2021-07-17 23:01:35 +03:00
|
|
|
}
|
|
|
|
|
2021-07-17 21:24:33 +03:00
|
|
|
/**
|
|
|
|
* Derive new searches by adding this token to an existing search.
|
|
|
|
*
|
|
|
|
* @param object $oSearch Partial search description derived so far.
|
|
|
|
* @param object $oPosition Description of the token position within
|
|
|
|
the query.
|
|
|
|
*
|
|
|
|
* @return SearchDescription[] List of derived search descriptions.
|
|
|
|
*/
|
|
|
|
public function extendSearch($oSearch, $oPosition)
|
|
|
|
{
|
2022-11-15 13:55:40 +03:00
|
|
|
$iSearchCost = 0;
|
2021-07-17 21:24:33 +03:00
|
|
|
|
|
|
|
$iOp = $this->iOperator;
|
|
|
|
if ($iOp == \Nominatim\Operator::NONE) {
|
2022-11-15 13:55:40 +03:00
|
|
|
if ($oPosition->isFirstToken()
|
|
|
|
|| $oSearch->hasName()
|
|
|
|
|| $oSearch->getContext()->isBoundedSearch()
|
|
|
|
) {
|
2021-07-17 21:24:33 +03:00
|
|
|
$iOp = \Nominatim\Operator::NAME;
|
2022-11-15 13:55:40 +03:00
|
|
|
$iSearchCost += 3;
|
2021-07-17 21:24:33 +03:00
|
|
|
} else {
|
|
|
|
$iOp = \Nominatim\Operator::NEAR;
|
2022-11-15 13:55:40 +03:00
|
|
|
$iSearchCost += 4;
|
|
|
|
if (!$oPosition->isFirstToken()) {
|
|
|
|
$iSearchCost += 3;
|
|
|
|
}
|
2021-07-17 21:24:33 +03:00
|
|
|
}
|
2022-11-15 13:55:40 +03:00
|
|
|
} elseif ($oPosition->isFirstToken()) {
|
2021-07-17 21:24:33 +03:00
|
|
|
$iSearchCost += 2;
|
2022-11-15 13:55:40 +03:00
|
|
|
} elseif ($oPosition->isLastToken()) {
|
|
|
|
$iSearchCost += 4;
|
|
|
|
} else {
|
|
|
|
$iSearchCost += 6;
|
2021-07-17 21:24:33 +03:00
|
|
|
}
|
2022-11-15 13:55:40 +03:00
|
|
|
|
2021-07-17 21:24:33 +03:00
|
|
|
if ($oSearch->hasHousenumber()) {
|
|
|
|
$iSearchCost ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$oNewSearch = $oSearch->clone($iSearchCost);
|
|
|
|
$oNewSearch->setPoiSearch($iOp, $this->sClass, $this->sType);
|
|
|
|
|
|
|
|
return array($oNewSearch);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-15 00:04:15 +03:00
|
|
|
public function debugInfo()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'ID' => $this->iId,
|
|
|
|
'Type' => 'special term',
|
|
|
|
'Info' => array(
|
|
|
|
'class' => $this->sClass,
|
|
|
|
'type' => $this->sType,
|
2018-07-17 23:23:27 +03:00
|
|
|
'operator' => \Nominatim\Operator::toString($this->iOperator)
|
2018-05-15 00:04:15 +03:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2021-07-17 21:24:33 +03:00
|
|
|
|
|
|
|
public function debugCode()
|
|
|
|
{
|
|
|
|
return 'S';
|
|
|
|
}
|
2018-05-15 00:04:15 +03:00
|
|
|
}
|