]> git.openstreetmap.org Git - rails.git/blob - app/helpers/browse_tags_helper.rb
Merge remote-tracking branch 'upstream/pull/2182'
[rails.git] / app / helpers / browse_tags_helper.rb
1 module BrowseTagsHelper
2   def format_key(key)
3     if url = wiki_link("key", key)
4       link_to h(key), url, :title => t("browse.tag_details.wiki_link.key", :key => key)
5     else
6       h(key)
7     end
8   end
9
10   def format_value(key, value)
11     if wp = wikipedia_link(key, value)
12       link_to h(wp[:title]), wp[:url], :title => t("browse.tag_details.wikipedia_link", :page => wp[:title])
13     elsif wdt = wikidata_links(key, value)
14       # IMPORTANT: Note that wikidata_links() returns an array of hashes, unlike for example wikipedia_link(),
15       # which just returns one such hash.
16       wdt = wdt.map do |w|
17         link_to(w[:title], w[:url], :title => t("browse.tag_details.wikidata_link", :page => w[:title].strip))
18       end
19       safe_join(wdt, ";")
20     elsif url = wiki_link("tag", "#{key}=#{value}")
21       link_to h(value), url, :title => t("browse.tag_details.wiki_link.tag", :key => key, :value => value)
22     elsif phones = telephone_links(key, value)
23       # similarly, telephone_links() returns an array of phone numbers
24       phones = phones.map do |p|
25         link_to(h(p[:phone_number]), p[:url], :title => t("browse.tag_details.telephone_link", :phone_number => p[:phone_number]))
26       end
27       safe_join(phones, "; ")
28     else
29       linkify h(value)
30     end
31   end
32
33   private
34
35   def wiki_link(type, lookup)
36     locale = I18n.locale.to_s
37
38     # update-wiki-pages does s/ /_/g on keys before saving them, we
39     # have to replace spaces with underscore so we'll link
40     # e.g. `source=Isle of Man Government aerial imagery (2001)' to
41     # the correct page.
42     lookup_us = lookup.tr(" ", "_")
43
44     if page = WIKI_PAGES.dig(locale, type, lookup_us)
45       url = "https://wiki.openstreetmap.org/wiki/#{page}?uselang=#{locale}"
46     elsif page = WIKI_PAGES.dig("en", type, lookup_us)
47       url = "https://wiki.openstreetmap.org/wiki/#{page}?uselang=#{locale}"
48     end
49
50     url
51   end
52
53   def wikipedia_link(key, value)
54     # Some k/v's are wikipedia=http://en.wikipedia.org/wiki/Full%20URL
55     return nil if value =~ %r{^https?://}
56
57     if key == "wikipedia"
58       # This regex should match Wikipedia language codes, everything
59       # from de to zh-classical
60       lang = if value =~ /^([a-z-]{2,12}):(.+)$/i
61                # Value is <lang>:<title> so split it up
62                # Note that value is always left as-is, see: https://trac.openstreetmap.org/ticket/4315
63                Regexp.last_match(1)
64              else
65                # Value is <title> so default to English Wikipedia
66                "en"
67              end
68     elsif key =~ /^wikipedia:(\S+)$/
69       # Language is in the key, so assume value is the title
70       lang = Regexp.last_match(1)
71     else
72       # Not a wikipedia key!
73       return nil
74     end
75
76     if value =~ /^([^#]*)#(.*)/
77       # Contains a reference to a section of the wikipedia article
78       # Must break it up to correctly build the url
79       value = Regexp.last_match(1)
80       section = "#" + Regexp.last_match(2)
81       encoded_section = "#" + CGI.escape(Regexp.last_match(2).gsub(/ +/, "_")).tr("%", ".")
82     else
83       section = ""
84       encoded_section = ""
85     end
86
87     {
88       :url => "https://#{lang}.wikipedia.org/wiki/#{value}?uselang=#{I18n.locale}#{encoded_section}",
89       :title => value + section
90     }
91   end
92
93   def wikidata_links(key, value)
94     # The simple wikidata-tag (this is limited to only one value)
95     if key == "wikidata" && value =~ /^[Qq][1-9][0-9]*$/
96       return [{
97         :url => "//www.wikidata.org/entity/#{value}?uselang=#{I18n.locale}",
98         :title => value
99       }]
100     # Key has to be one of the accepted wikidata-tags
101     elsif key =~ /(architect|artist|brand|name:etymology|network|operator|subject):wikidata/ &&
102           # Value has to be a semicolon-separated list of wikidata-IDs (whitespaces allowed before and after semicolons)
103           value =~ /^[Qq][1-9][0-9]*(\s*;\s*[Qq][1-9][0-9]*)*$/
104       # Splitting at every semicolon to get a separate hash for each wikidata-ID
105       return value.split(";").map do |id|
106         { :title => id, :url => "//www.wikidata.org/entity/#{id.strip}?uselang=#{I18n.locale}" }
107       end
108     end
109     nil
110   end
111
112   def telephone_links(_key, value)
113     # Does it look like a global phone number? eg "+1 (234) 567-8901 "
114     # or a list of alternate numbers separated by ;
115     #
116     # Per RFC 3966, this accepts the visual separators -.() within the number,
117     # which are displayed and included in the tel: URL, and accepts whitespace,
118     # which is displayed but not included in the tel: URL.
119     #  (see: http://tools.ietf.org/html/rfc3966#section-5.1.1)
120     #
121     # Also accepting / as a visual separator although not given in RFC 3966,
122     # because it is used as a visual separator in OSM data in some countries.
123     if value =~ %r{^\s*\+[\d\s\(\)/\.-]{6,25}\s*(;\s*\+[\d\s\(\)/\.-]{6,25}\s*)*$}
124       return value.split(";").map do |phone_number|
125         # for display, remove leading and trailing whitespace
126         phone_number = phone_number.strip
127
128         # for tel: URL, remove all whitespace
129         # "+1 (234) 567-8901 " -> "tel:+1(234)567-8901"
130         phone_no_whitespace = phone_number.gsub(/\s+/, "")
131         { :phone_number => phone_number, :url => "tel:#{phone_no_whitespace}" }
132       end
133     end
134     nil
135   end
136 end