4 def printable_name(object, version = false)
5 id = if object.id.is_a?(Array)
10 name = t "printable_name.with_id", :id => id.to_s
11 name = t "printable_name.with_version", :id => name, :version => object.version.to_s if version
13 # don't look at object tags if redacted, so as to avoid giving
14 # away redacted version tag information.
15 unless object.redacted?
16 locale = I18n.locale.to_s
18 locale = locale.sub(/-[^-]+/, "") while locale =~ /-[^-]+/ && !object.tags.include?("name:#{I18n.locale}")
20 if object.tags.include? "name:#{locale}"
21 name = t "printable_name.with_name_html", :name => content_tag(:bdi, object.tags["name:#{locale}"].to_s), :id => content_tag(:bdi, name)
22 elsif object.tags.include? "name"
23 name = t "printable_name.with_name_html", :name => content_tag(:bdi, object.tags["name"].to_s), :id => content_tag(:bdi, name)
24 elsif object.tags.include? "ref"
25 name = t "printable_name.with_name_html", :name => content_tag(:bdi, object.tags["ref"].to_s), :id => content_tag(:bdi, name)
32 def link_class(type, object)
38 classes += icon_tags(object).flatten.map { |t| h(t) }
39 classes << "deleted" unless object.visible?
45 def link_title(object)
49 h(icon_tags(object).map { |k, v| k + "=" + v }.to_sentence)
53 def link_follow(object)
54 "nofollow" if object.tags.empty?
58 if url = wiki_link("key", key)
59 link_to h(key), url, :title => t("browse.tag_details.wiki_link.key", :key => key)
65 def format_value(key, value)
66 if wp = wikipedia_link(key, value)
67 link_to h(wp[:title]), wp[:url], :title => t("browse.tag_details.wikipedia_link", :page => wp[:title])
68 elsif wdt = wikidata_links(key, value)
69 # IMPORTANT: Note that wikidata_links() returns an array of hashes, unlike for example wikipedia_link(),
70 # which just returns one such hash.
72 link_to(w[:title], w[:url], :title => t("browse.tag_details.wikidata_link", :page => w[:title].strip))
75 elsif url = wiki_link("tag", "#{key}=#{value}")
76 link_to h(value), url, :title => t("browse.tag_details.wiki_link.tag", :key => key, :value => value)
77 elsif phones = telephone_links(key, value)
78 # similarly, telephone_links() returns an array of phone numbers
79 phones = phones.map do |p|
80 link_to(h(p[:phone_number]), p[:url], :title => t("browse.tag_details.telephone_link", :phone_number => p[:phone_number]))
82 safe_join(phones, "; ")
88 def type_and_paginated_count(type, pages)
89 if pages.page_count == 1
90 t "browse.changeset.#{type}",
91 :count => pages.item_count
93 t "browse.changeset.#{type}_paginated",
94 :x => pages.current_page.first_item,
95 :y => pages.current_page.last_item,
96 :count => pages.item_count
102 ICON_TAGS = %w[aeroway amenity barrier building highway historic landuse leisure man_made natural railway shop tourism waterway].freeze
104 def icon_tags(object)
105 object.tags.find_all { |k, _v| ICON_TAGS.include? k }.sort
108 def wiki_link(type, lookup)
109 locale = I18n.locale.to_s
111 # update-wiki-pages does s/ /_/g on keys before saving them, we
112 # have to replace spaces with underscore so we'll link
113 # e.g. `source=Isle of Man Government aerial imagery (2001)' to
115 lookup_us = lookup.tr(" ", "_")
117 if page = WIKI_PAGES.dig(locale, type, lookup_us)
118 url = "https://wiki.openstreetmap.org/wiki/#{page}?uselang=#{locale}"
119 elsif page = WIKI_PAGES.dig("en", type, lookup_us)
120 url = "https://wiki.openstreetmap.org/wiki/#{page}?uselang=#{locale}"
126 def wikipedia_link(key, value)
127 # Some k/v's are wikipedia=http://en.wikipedia.org/wiki/Full%20URL
128 return nil if value =~ %r{^https?://}
130 if key == "wikipedia"
131 # This regex should match Wikipedia language codes, everything
132 # from de to zh-classical
133 lang = if value =~ /^([a-z-]{2,12}):(.+)$/i
134 # Value is <lang>:<title> so split it up
135 # Note that value is always left as-is, see: https://trac.openstreetmap.org/ticket/4315
138 # Value is <title> so default to English Wikipedia
141 elsif key =~ /^wikipedia:(\S+)$/
142 # Language is in the key, so assume value is the title
143 lang = Regexp.last_match(1)
145 # Not a wikipedia key!
149 if value =~ /^([^#]*)#(.*)/
150 # Contains a reference to a section of the wikipedia article
151 # Must break it up to correctly build the url
152 value = Regexp.last_match(1)
153 section = "#" + Regexp.last_match(2)
154 encoded_section = "#" + CGI.escape(Regexp.last_match(2).gsub(/ +/, "_")).tr("%", ".")
161 :url => "https://#{lang}.wikipedia.org/wiki/#{value}?uselang=#{I18n.locale}#{encoded_section}",
162 :title => value + section
166 def wikidata_links(key, value)
167 # The simple wikidata-tag (this is limited to only one value)
168 if key == "wikidata" && value =~ /^[Qq][1-9][0-9]*$/
170 :url => "//www.wikidata.org/entity/#{value}?uselang=#{I18n.locale}",
173 # Key has to be one of the accepted wikidata-tags
174 elsif key =~ /(architect|artist|brand|name:etymology|network|operator|subject):wikidata/ &&
175 # Value has to be a semicolon-separated list of wikidata-IDs (whitespaces allowed before and after semicolons)
176 value =~ /^[Qq][1-9][0-9]*(\s*;\s*[Qq][1-9][0-9]*)*$/
177 # Splitting at every semicolon to get a separate hash for each wikidata-ID
178 return value.split(";").map do |id|
179 { :title => id, :url => "//www.wikidata.org/entity/#{id.strip}?uselang=#{I18n.locale}" }
185 def telephone_links(_key, value)
186 # Does it look like a global phone number? eg "+1 (234) 567-8901 "
187 # or a list of alternate numbers separated by ;
189 # Per RFC 3966, this accepts the visual separators -.() within the number,
190 # which are displayed and included in the tel: URL, and accepts whitespace,
191 # which is displayed but not included in the tel: URL.
192 # (see: http://tools.ietf.org/html/rfc3966#section-5.1.1)
194 # Also accepting / as a visual separator although not given in RFC 3966,
195 # because it is used as a visual separator in OSM data in some countries.
196 if value =~ %r{^\s*\+[\d\s\(\)/\.-]{6,25}\s*(;\s*\+[\d\s\(\)/\.-]{6,25}\s*)*$}
197 return value.split(";").map do |phone_number|
198 # for display, remove leading and trailing whitespace
199 phone_number = phone_number.strip
201 # for tel: URL, remove all whitespace
202 # "+1 (234) 567-8901 " -> "tel:+1(234)567-8901"
203 phone_no_whitespace = phone_number.gsub(/\s+/, "")
204 { :phone_number => phone_number, :url => "tel:#{phone_no_whitespace}" }