1 -- SPDX-License-Identifier: GPL-2.0-only
3 -- This file is part of Nominatim. (https://nominatim.org)
5 -- Copyright (C) 2022 by the Nominatim developer community.
6 -- For a full list of authors see the git log.
8 -- Functions for interpreting wkipedia/wikidata tags and computing importance.
10 DROP TYPE IF EXISTS wikipedia_article_match CASCADE;
11 CREATE TYPE wikipedia_article_match as (
17 DROP TYPE IF EXISTS place_importance CASCADE;
18 CREATE TYPE place_importance as (
23 {% if 'wikimedia_importance' in db.tables %}
25 CREATE OR REPLACE FUNCTION get_wikipedia_match(extratags HSTORE, country_code varchar(2))
26 RETURNS wikipedia_article_match
30 wiki_article_title TEXT;
31 wiki_article_language TEXT;
32 result wikipedia_article_match;
35 IF extratags ? 'wikipedia' and strpos(extratags->'wikipedia', ':') IN (3,4) THEN
36 wiki_article_language := lower(trim(split_part(extratags->'wikipedia', ':', 1)));
37 wiki_article_title := trim(substr(extratags->'wikipedia',
38 strpos(extratags->'wikipedia', ':') + 1));
41 SELECT language, title, importance FROM wikimedia_importance
42 WHERE language = wiki_article_language
43 and title = replace(wiki_article_title, ' ', '_')
49 FOREACH wiki_article_language IN ARRAY ARRAY['ar','bg','ca','cs','da','de','en','es','eo','eu','fa','fr','ko','hi','hr','id','it','he','lt','hu','ms','nl','ja','no','pl','pt','kk','ro','ru','sk','sl','sr','fi','sv','tr','uk','vi','vo','war','zh']
51 IF extratags ? ('wikipedia:' || wiki_article_language) THEN
52 wiki_article_title := extratags->('wikipedia:' || wiki_article_language);
55 SELECT language, title, importance FROM wikimedia_importance
56 WHERE language = wiki_article_language
57 and title = replace(wiki_article_title, ' ', '_')
68 LANGUAGE plpgsql IMMUTABLE;
72 -- See: http://stackoverflow.com/questions/6410088/how-can-i-mimic-the-php-urldecode-function-in-postgresql
73 CREATE OR REPLACE FUNCTION decode_url_part(p varchar)
76 SELECT convert_from(CAST(E'\\x' || array_to_string(ARRAY(
77 SELECT CASE WHEN length(r.m[1]) = 1 THEN encode(convert_to(r.m[1], 'SQL_ASCII'), 'hex') ELSE substring(r.m[1] from 2 for 2) END
78 FROM regexp_matches($1, '%[0-9a-f][0-9a-f]|.', 'gi') AS r(m)
79 ), '') AS bytea), 'UTF8');
81 LANGUAGE SQL IMMUTABLE STRICT;
84 CREATE OR REPLACE FUNCTION catch_decode_url_part(p varchar)
89 RETURN decode_url_part(p);
91 WHEN others THEN return null;
94 LANGUAGE plpgsql IMMUTABLE STRICT;
97 CREATE OR REPLACE FUNCTION get_wikipedia_match(extratags HSTORE, country_code varchar(2))
98 RETURNS wikipedia_article_match
104 wiki_article_title TEXT;
105 wiki_article_language TEXT;
106 result wikipedia_article_match;
108 langs := ARRAY['english','country','ar','bg','ca','cs','da','de','en','es','eo','eu','fa','fr','ko','hi','hr','id','it','he','lt','hu','ms','nl','ja','no','pl','pt','kk','ro','ru','sk','sl','sr','fi','sv','tr','uk','vi','vo','war','zh'];
110 WHILE langs[i] IS NOT NULL LOOP
111 wiki_article := extratags->(case when langs[i] in ('english','country') THEN 'wikipedia' ELSE 'wikipedia:'||langs[i] END);
112 IF wiki_article is not null THEN
113 wiki_article := replace(wiki_article,' ','_');
114 IF strpos(wiki_article, ':') IN (3,4) THEN
115 wiki_article_language := lower(trim(split_part(wiki_article, ':', 1)));
116 wiki_article_title := trim(substr(wiki_article, strpos(wiki_article, ':')+1));
118 wiki_article_title := trim(wiki_article);
119 wiki_article_language := CASE WHEN langs[i] = 'english' THEN 'en' WHEN langs[i] = 'country' THEN get_country_language_code(country_code) ELSE langs[i] END;
122 select wikipedia_article.language,wikipedia_article.title,wikipedia_article.importance
123 from wikipedia_article
124 where language = wiki_article_language and
125 (title = wiki_article_title OR title = catch_decode_url_part(wiki_article_title) OR title = replace(catch_decode_url_part(wiki_article_title),E'\\',''))
127 select wikipedia_article.language,wikipedia_article.title,wikipedia_article.importance
128 from wikipedia_redirect join wikipedia_article on (wikipedia_redirect.language = wikipedia_article.language and wikipedia_redirect.to_title = wikipedia_article.title)
129 where wikipedia_redirect.language = wiki_article_language and
130 (from_title = wiki_article_title OR from_title = catch_decode_url_part(wiki_article_title) OR from_title = replace(catch_decode_url_part(wiki_article_title),E'\\',''))
131 order by importance desc limit 1 INTO result;
133 IF result.language is not null THEN
142 LANGUAGE plpgsql STABLE;
146 CREATE OR REPLACE FUNCTION compute_importance(extratags HSTORE,
147 country_code varchar(2),
148 rank_search SMALLINT,
150 RETURNS place_importance
154 result place_importance;
155 osm_views_exists BIGINT;
158 -- add importance by wikipedia article if the place has one
160 SELECT * FROM get_wikipedia_match(extratags, country_code)
161 WHERE language is not NULL
163 result.importance := match.importance;
164 result.wikipedia := match.language || ':' || match.title;
168 -- Nothing? Then try with the wikidata tag.
169 IF result.importance is null AND extratags ? 'wikidata' THEN
171 {% if 'wikimedia_importance' in db.tables %}
172 SELECT * FROM wikimedia_importance
173 WHERE wikidata = extratags->'wikidata'
176 SELECT * FROM wikipedia_article
177 WHERE wd_page_title = extratags->'wikidata'
178 ORDER BY language = 'en' DESC, langcount DESC LIMIT 1
181 result.importance := match.importance;
182 result.wikipedia := match.language || ':' || match.title;
187 -- Still nothing? Fall back to a default.
188 IF result.importance is null THEN
189 result.importance := 0.40001 - (rank_search::float / 75);
192 {% if 'secondary_importance' in db.tables %}
194 SELECT ST_Value(rast, centroid) as importance
195 FROM secondary_importance
196 WHERE ST_Intersects(ST_ConvexHull(rast), centroid) LIMIT 1
198 -- Secondary importance as tie breaker with 0.0001 weight.
199 result.importance := result.importance + match.importance::float / 655350000;