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