]> git.openstreetmap.org Git - rails.git/blob - app/helpers/browse_helper.rb
Fixed changeset time inconsistencies
[rails.git] / app / helpers / browse_helper.rb
1 module BrowseHelper
2   def printable_name(object, version=false)
3     if object.id.is_a?(Array)
4       id = object.id[0]
5     else
6       id = object.id
7     end
8     name = t 'printable_name.with_id', :id => id.to_s
9     if version
10       name = t 'printable_name.with_version', :id => name, :version => object.version.to_s
11     end
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       if object.tags.include? "name:#{I18n.locale}"
17         name = t 'printable_name.with_name',  :name => object.tags["name:#{I18n.locale}"].to_s, :id => name
18       elsif object.tags.include? 'name'
19         name = t 'printable_name.with_name',  :name => object.tags['name'].to_s, :id => name
20       end
21     end
22
23     return name
24   end
25
26   def link_class(type, object)
27     if object.redacted?
28       type + " deleted"
29     else
30       type + " " + h(icon_tags(object).join(' ')) + (object.visible == false ? ' deleted' : '')
31     end
32   end
33
34   def link_title(object)
35     if object.redacted?
36       ""
37     else
38       h(icon_tags(object).map { |k,v| k + '=' + v }.to_sentence)
39     end
40   end
41
42   def format_key(key)
43     if url = wiki_link("key", key)
44       link_to h(key), url, :title => t('browse.tag_details.wiki_link.key', :key => key)
45     else
46       h(key)
47     end
48   end
49
50   def format_value(key, value)
51     if wp = wikipedia_link(key, value)
52       link_to h(wp[:title]), wp[:url], :title => t('browse.tag_details.wikipedia_link', :page => wp[:title])
53     elsif url = wiki_link("tag", "#{key}=#{value}")
54       link_to h(value), url, :title => t('browse.tag_details.wiki_link.tag', :key => key, :value => value)
55     else
56       linkify h(value)
57     end
58   end
59
60   def type_and_paginated_count(type, pages)
61     if pages.page_count == 1
62       t "browse.changeset_details.#{type}",
63         :count => pages.item_count
64     else
65       t "browse.changeset_details.#{type}_paginated",
66         :x => pages.current_page.first_item,
67         :y => pages.current_page.last_item,
68         :count => pages.item_count
69     end
70   end
71
72 private
73
74   ICON_TAGS = [
75     "aeroway", "amenity", "barrier", "building", "highway", "historic", "landuse",
76     "leisure", "man_made", "natural", "railway", "shop", "tourism", "waterway"
77   ]
78
79   def icon_tags(object)
80     object.tags.find_all { |k,v| ICON_TAGS.include? k }
81   end
82
83   def wiki_link(type, lookup)
84     locale = I18n.locale.to_s
85
86     # update-wiki-pages does s/ /_/g on keys before saving them, we
87     # have to replace spaces with underscore so we'll link
88     # e.g. `source=Isle of Man Government aerial imagery (2001)' to
89     # the correct page.
90     lookup_us = lookup.tr(" ", "_")
91
92     if page = WIKI_PAGES[locale][type][lookup_us] rescue nil
93       url = "http://wiki.openstreetmap.org/wiki/#{page}?uselang=#{locale}"
94     elsif page = WIKI_PAGES["en"][type][lookup_us] rescue nil
95       url = "http://wiki.openstreetmap.org/wiki/#{page}?uselang=#{locale}"
96     end
97
98     return url
99   end
100
101   def wikipedia_link(key, value)
102     # Some k/v's are wikipedia=http://en.wikipedia.org/wiki/Full%20URL
103     return nil if value =~ /^https?:\/\//
104
105     if key == "wikipedia"
106       # This regex should match Wikipedia language codes, everything
107       # from de to zh-classical
108       if value =~ /^([a-z-]{2,12}):(.+)$/i
109         # Value is <lang>:<title> so split it up
110         # Note that value is always left as-is, see: https://trac.openstreetmap.org/ticket/4315
111         lang  = $1
112       else
113         # Value is <title> so default to English Wikipedia
114         lang = 'en'
115       end
116     elsif key =~ /^wikipedia:(\S+)$/
117       # Language is in the key, so assume value is a simple title
118       lang = $1
119     else
120       # Not a wikipedia key!
121       return nil
122     end
123
124     return {
125       :url => "http://#{lang}.wikipedia.org/wiki/#{value}?uselang=#{I18n.locale}",
126       :title => value
127     }
128   end
129 end