]> git.openstreetmap.org Git - rails.git/blob - app/helpers/browse_helper.rb
Update query icon to display cursor with '?'
[rails.git] / app / helpers / browse_helper.rb
1 require "cgi"
2
3 module BrowseHelper
4   def printable_name(object, version = false)
5     id = if object.id.is_a?(Array)
6            object.id[0]
7          else
8            object.id
9          end
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
12
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
17
18       locale = locale.sub(/-[^-]+/, "") while locale =~ /-[^-]+/ && !object.tags.include?("name:#{I18n.locale}")
19
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)
26       end
27     end
28
29     name
30   end
31
32   def link_class(type, object)
33     classes = [type]
34
35     if object.redacted?
36       classes << "deleted"
37     else
38       classes += icon_tags(object).flatten.map { |t| h(t) }
39       classes << "deleted" unless object.visible?
40     end
41
42     classes.join(" ")
43   end
44
45   def link_title(object)
46     if object.redacted?
47       ""
48     else
49       h(icon_tags(object).map { |k, v| k + "=" + v }.to_sentence)
50     end
51   end
52
53   def link_follow(object)
54     "nofollow" if object.tags.empty?
55   end
56
57   def format_key(key)
58     if url = wiki_link("key", key)
59       link_to h(key), url, :title => t("browse.tag_details.wiki_link.key", :key => key)
60     else
61       h(key)
62     end
63   end
64
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.
71       wdt = wdt.map do |w|
72         link_to(w[:title], w[:url], :title => t("browse.tag_details.wikidata_link", :page => w[:title].strip))
73       end
74       safe_join(wdt, ";")
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 url = telephone_link(key, value)
78       link_to h(value), url, :title => t("browse.tag_details.telephone_link", :phone_number => value)
79     else
80       linkify h(value)
81     end
82   end
83
84   def type_and_paginated_count(type, pages)
85     if pages.page_count == 1
86       t "browse.changeset.#{type}",
87         :count => pages.item_count
88     else
89       t "browse.changeset.#{type}_paginated",
90         :x => pages.current_page.first_item,
91         :y => pages.current_page.last_item,
92         :count => pages.item_count
93     end
94   end
95
96   private
97
98   ICON_TAGS = %w[aeroway amenity barrier building highway historic landuse leisure man_made natural railway shop tourism waterway].freeze
99
100   def icon_tags(object)
101     object.tags.find_all { |k, _v| ICON_TAGS.include? k }.sort
102   end
103
104   def wiki_link(type, lookup)
105     locale = I18n.locale.to_s
106
107     # update-wiki-pages does s/ /_/g on keys before saving them, we
108     # have to replace spaces with underscore so we'll link
109     # e.g. `source=Isle of Man Government aerial imagery (2001)' to
110     # the correct page.
111     lookup_us = lookup.tr(" ", "_")
112
113     if page = WIKI_PAGES.dig(locale, type, lookup_us)
114       url = "https://wiki.openstreetmap.org/wiki/#{page}?uselang=#{locale}"
115     elsif page = WIKI_PAGES.dig("en", type, lookup_us)
116       url = "https://wiki.openstreetmap.org/wiki/#{page}?uselang=#{locale}"
117     end
118
119     url
120   end
121
122   def wikipedia_link(key, value)
123     # Some k/v's are wikipedia=http://en.wikipedia.org/wiki/Full%20URL
124     return nil if value =~ %r{^https?://}
125
126     if key == "wikipedia"
127       # This regex should match Wikipedia language codes, everything
128       # from de to zh-classical
129       lang = if value =~ /^([a-z-]{2,12}):(.+)$/i
130                # Value is <lang>:<title> so split it up
131                # Note that value is always left as-is, see: https://trac.openstreetmap.org/ticket/4315
132                Regexp.last_match(1)
133              else
134                # Value is <title> so default to English Wikipedia
135                "en"
136              end
137     elsif key =~ /^wikipedia:(\S+)$/
138       # Language is in the key, so assume value is the title
139       lang = Regexp.last_match(1)
140     else
141       # Not a wikipedia key!
142       return nil
143     end
144
145     if value =~ /^([^#]*)#(.*)/
146       # Contains a reference to a section of the wikipedia article
147       # Must break it up to correctly build the url
148       value = Regexp.last_match(1)
149       section = "#" + Regexp.last_match(2)
150       encoded_section = "#" + CGI.escape(Regexp.last_match(2).gsub(/ +/, "_")).tr("%", ".")
151     else
152       section = ""
153       encoded_section = ""
154     end
155
156     {
157       :url => "https://#{lang}.wikipedia.org/wiki/#{value}?uselang=#{I18n.locale}#{encoded_section}",
158       :title => value + section
159     }
160   end
161
162   def wikidata_links(key, value)
163     # The simple wikidata-tag (this is limited to only one value)
164     if key == "wikidata" && value =~ /^[Qq][1-9][0-9]*$/
165       return [{
166         :url => "//www.wikidata.org/wiki/#{value}?uselang=#{I18n.locale}",
167         :title => value
168       }]
169     # Key has to be one of the accepted wikidata-tags
170     elsif key =~ /(architect|artist|brand|operator|subject):wikidata/ &&
171           # Value has to be a semicolon-separated list of wikidata-IDs (whitespaces allowed before and after semicolons)
172           value =~ /^[Qq][1-9][0-9]*(\s*;\s*[Qq][1-9][0-9]*)*$/
173       # Splitting at every semicolon to get a separate hash for each wikidata-ID
174       return value.split(";").map do |id|
175         { :title => id, :url => "//www.wikidata.org/wiki/#{id.strip}?uselang=#{I18n.locale}" }
176       end
177     end
178     nil
179   end
180
181   def telephone_link(_key, value)
182     # does it look like a phone number? eg "+1 (234) 567-8901 " ?
183     return nil unless value =~ %r{^\s*\+[\d\s\(\)/\.-]{6,25}\s*$}
184
185     # remove all whitespace instead of encoding it http://tools.ietf.org/html/rfc3966#section-5.1.1
186     # "+1 (234) 567-8901 " -> "+1(234)567-8901"
187     value_no_whitespace = value.gsub(/\s+/, "")
188
189     "tel:#{value_no_whitespace}"
190   end
191 end