]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenSpecialTerm.php
don't even try heavily penalized searches
[nominatim.git] / lib-php / TokenSpecialTerm.php
1 <?php
2 /**
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This file is part of Nominatim. (https://nominatim.org)
6  *
7  * Copyright (C) 2022 by the Nominatim developer community.
8  * For a full list of authors see the git log.
9  */
10
11 namespace Nominatim\Token;
12
13 require_once(CONST_LibDir.'/SpecialSearchOperator.php');
14
15 /**
16  * A word token describing a place type.
17  */
18 class SpecialTerm
19 {
20     /// Database word id, if applicable.
21     private $iId;
22     /// Class (or OSM tag key) of the place to look for.
23     private $sClass;
24     /// Type (or OSM tag value) of the place to look for.
25     private $sType;
26     /// Relationship of the operator to the object (see Operator class).
27     private $iOperator;
28
29     public function __construct($iID, $sClass, $sType, $iOperator)
30     {
31         $this->iId = $iID;
32         $this->sClass = $sClass;
33         $this->sType = $sType;
34         $this->iOperator = $iOperator;
35     }
36
37     public function getId()
38     {
39         return $this->iId;
40     }
41
42     /**
43      * Check if the token can be added to the given search.
44      * Derive new searches by adding this token to an existing search.
45      *
46      * @param object  $oSearch      Partial search description derived so far.
47      * @param object  $oPosition    Description of the token position within
48                                     the query.
49      *
50      * @return True if the token is compatible with the search configuration
51      *         given the position.
52      */
53     public function isExtendable($oSearch, $oPosition)
54     {
55         return !$oSearch->hasOperator()
56                && $oPosition->isPhrase('')
57                && ($this->iOperator != \Nominatim\Operator::NONE
58                   || (!$oSearch->hasAddress() && !$oSearch->hasHousenumber() && !$oSearch->hasCountry()));
59     }
60
61     /**
62      * Derive new searches by adding this token to an existing search.
63      *
64      * @param object  $oSearch      Partial search description derived so far.
65      * @param object  $oPosition    Description of the token position within
66                                     the query.
67      *
68      * @return SearchDescription[] List of derived search descriptions.
69      */
70     public function extendSearch($oSearch, $oPosition)
71     {
72         $iSearchCost = 0;
73
74         $iOp = $this->iOperator;
75         if ($iOp == \Nominatim\Operator::NONE) {
76             if ($oPosition->isFirstToken()
77                 || $oSearch->hasName()
78                 || $oSearch->getContext()->isBoundedSearch()
79             ) {
80                 $iOp = \Nominatim\Operator::NAME;
81                 $iSearchCost += 3;
82             } else {
83                 $iOp = \Nominatim\Operator::NEAR;
84                 $iSearchCost += 4;
85                 if (!$oPosition->isFirstToken()) {
86                     $iSearchCost += 3;
87                 }
88             }
89         } elseif ($oPosition->isFirstToken()) {
90             $iSearchCost += 2;
91         } elseif ($oPosition->isLastToken()) {
92             $iSearchCost += 4;
93         } else {
94             $iSearchCost += 6;
95         }
96
97         if ($oSearch->hasHousenumber()) {
98             $iSearchCost ++;
99         }
100
101         $oNewSearch = $oSearch->clone($iSearchCost);
102         $oNewSearch->setPoiSearch($iOp, $this->sClass, $this->sType);
103
104         return array($oNewSearch);
105     }
106
107
108     public function debugInfo()
109     {
110         return array(
111                 'ID' => $this->iId,
112                 'Type' => 'special term',
113                 'Info' => array(
114                            'class' => $this->sClass,
115                            'type' => $this->sType,
116                            'operator' => \Nominatim\Operator::toString($this->iOperator)
117                           )
118                );
119     }
120
121     public function debugCode()
122     {
123         return 'S';
124     }
125 }