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