summaryrefslogtreecommitdiff
path: root/web/taginfo.rb
blob: bbef7a90742ad757217d41f4162d4b4dc4374374 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/ruby
#------------------------------------------------------------------------------
#
#  Taginfo
#
#------------------------------------------------------------------------------
#
#  taginfo.rb
#
#------------------------------------------------------------------------------
#
#  Copyright (C) 2010  Jochen Topf <jochen@remote.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#------------------------------------------------------------------------------

require 'rubygems'
require 'json'
require 'sqlite3'

require 'sinatra/base'
require 'sinatra/r18n'
require 'rack/contrib'

require 'lib/utils.rb'
require 'lib/config.rb'
require 'lib/javascript.rb'
require 'lib/language.rb'
require 'lib/sql.rb'
require 'lib/sources.rb'
require 'lib/reports.rb'
require 'lib/apidoc.rb'

#------------------------------------------------------------------------------

TaginfoConfig.read

#------------------------------------------------------------------------------

db = SQL::Database.new('../../data')

db.select('SELECT * FROM sources ORDER BY no').execute().each do |source|
    Source.new source['id'], source['name'], source['data_until'], source['update_start'], source['update_end'], source['visible'].to_i == 1
end

DATA_UNTIL = db.select("SELECT min(data_until) FROM sources").get_first_value().sub(/:..$/, '')

db.close

class Taginfo < Sinatra::Base

    register Sinatra::R18n

    use Rack::JSONP

    mime_type :opensearch, 'application/opensearchdescription+xml'

    configure do
        set :app_file, __FILE__

        if ARGV[0]
            # production
            set :host, 'localhost'
            set :port, ARGV[0]
            set :environment, :production
        else
            # test
            enable :logging
        end
    end

    # make h() method for escaping HTML available
    helpers do
        include Rack::Utils
        alias_method :h, :escape_html
    end

    # make trimming \n after %> the default in erb templates
    alias_method :erb_orig, :erb
    def erb(template, options={}, locals={})
        options[:trim] = '>' unless options[:trim]
        erb_orig template, options, locals
    end

    # when do we expect the next data update
    def next_update
        # three hours after midnight UTC
        ((Time.utc(Time.now.year(), Time.now.month(), Time.now.day(), 3, 0, 0) + (Time.now.hour < 3 ? 0 : 24)*60*60)-Time.now).to_i.to_i
    end

    before do
        if request.cookies['taginfo_locale'] && request.path != '/switch_locale'
            params[:locale] = request.cookies['taginfo_locale']
        end

        javascript 'jquery-1.5.1.min'
        javascript 'jquery-ui-1.8.10.custom.min'
#        javascript 'flexigrid-minified'
        javascript 'flexigrid'
        javascript 'protovis-r3.2'
        javascript 'lang/' + r18n.locale.code
        javascript 'taginfo'

        # set to immediate expire on normal pages
        # (otherwise switching languages doesn't work)
        expires 0, :no_cache

        @db = SQL::Database.new('../../data')

        @data_until = DATA_UNTIL
    end

    after do
        @db.close
    end

    #-------------------------------------

    before '/api/*' do
        content_type :json
        expires next_update
    end

    #-------------------------------------

    # This is called when the language is changed with the pull-down menu in the top-right corner.
    # It sets a cookie and redirects back to the page the user was coming from.
    get '/switch_locale' do
        response.set_cookie('taginfo_locale', params[:locale])
        redirect(TaginfoConfig.get('instance.url') + params[:url])
    end

    #-------------------------------------

    get '/' do
        tagcloud_number_of_tags = TaginfoConfig.get('tagcloud.number_of_tags', 200)
        @tags = @db.select("SELECT key, scale1 FROM popular_keys ORDER BY scale1 DESC LIMIT #{ tagcloud_number_of_tags }").
            execute().
            each_with_index{ |tag, idx| tag['pos'] = (tagcloud_number_of_tags - idx) / tagcloud_number_of_tags.to_f }.
            sort_by{ |row| row['key'] }
        erb :index
    end

    %w(about download keys tags).each do |page|
        get '/' + page do
            @title = t.taginfo[page]
            erb page.to_sym
        end
    end

    get! '/sources' do
        @title = t.taginfo.sources
        erb :'sources/index'
    end

    #-------------------------------------

    get %r{^/keys/(.*)} do |key|
        if params[:key].nil?
            @key = key
        else
            @key = params[:key]
        end

        @key_html = escape_html(@key)
        @key_uri  = escape(@key)
        @key_json = @key.to_json
        @key_pp   = pp_key(@key)

        @title = [@key_html, t.osm.keys]
        @section = 'keys'
        @section_title = t.taginfo[@section]

        @filter_type = get_filter()
        @sel = Hash.new('')
        @sel[@filter_type] = ' selected="selected"'

        @count_all_values = @db.select("SELECT count_#{@filter_type} FROM db.keys").condition('key = ?', @key).get_first_value().to_i

        @desc = h(@db.select("SELECT description FROM wiki.wikipages WHERE lang=? AND key=? AND value IS NULL", r18n.locale.code, @key).get_first_value())
        @desc = h(@db.select("SELECT description FROM wiki.wikipages WHERE lang='en' AND key=? AND value IS NULL", @key).get_first_value()) if @desc == ''
        @desc = "[#{ t.pages.key.no_description_in_wiki }]" if @desc == ''

        @prevalent_values = @db.select("SELECT value, count_#{@filter_type} AS count FROM tags").
            condition('key=?', @key).
            condition('count > ?', @count_all_values * 0.02).
            order_by(:count, 'DESC').
            execute().map{ |row| [{ 'value' => row['value'], 'count' => row['count'].to_i }] }

        # add "(other)" label for the rest of the values
        sum = @prevalent_values.inject(0){ |sum, x| sum += x[0]['count'] }
        if sum < @count_all_values
            @prevalent_values << [{ 'value' => '(other)', 'count' => @count_all_values - sum }]
        end

        @wiki_count = @db.count('wiki.wikipages').condition('value IS NULL').condition('key=?', @key).get_first_value().to_i
        
        (@merkaartor_type, @merkaartor_link, @merkaartor_selector) = @db.select('SELECT tag_type, link, selector FROM merkaartor.keys').condition('key=?', @key).get_columns(:tag_type, :link, :selector)
        @merkaartor_images = [:node, :way, :area, :relation].map{ |type|
            name = type.to_s.capitalize
            '<img src="/img/types/' + (@merkaartor_selector =~ /Type is #{name}/ ? type.to_s : 'none') + '.16.png" alt="' + name + '" title="' + name + '"/>'
        }.join('&nbsp;')

        @merkaartor_values = @db.select('SELECT value FROM merkaartor.tags').condition('key=?', @key).order_by(:value).execute().map{ |row| row['value'] }

        @merkaartor_desc = @db.select('SELECT lang, description FROM key_descriptions').condition('key=?', @key).order_by(:lang).execute()

        @img_width  = TaginfoConfig.get('geodistribution.width')  * TaginfoConfig.get('geodistribution.scale_image')
        @img_height = TaginfoConfig.get('geodistribution.height') * TaginfoConfig.get('geodistribution.scale_image')

        erb :key
    end

    #-------------------------------------

    get %r{^/tags/(.*)} do |tag|
        if tag.match(/=/)
            kv = tag.split('=', 2)
        else
            kv = [ tag, '' ]
        end
        if params[:key].nil?
            @key = kv[0]
        else
            @key = params[:key]
        end
        if params[:value].nil?
            @value = kv[1]
        else
            @value = params[:value]
        end
        @tag = @key + '=' + @value

        @key_html = escape_html(@key)
        @key_uri  = escape(@key)
        @key_json = @key.to_json
        @key_pp   = pp_key(@key)

        @value_html = escape_html(@value)
        @value_uri  = escape(@value)
        @value_json = @value.to_json
        @value_pp   = pp_value(@value)

        @title = [@key_html + '=' + @value_html, t.taginfo.tags]
        @section = 'tags'
        @section_title = t.taginfo[@section]

        @filter_type = get_filter()
        @sel = Hash.new('')
        @sel[@filter_type] = ' selected="selected"'

        @count_all = @db.select('SELECT count_all FROM db.tags').condition('key = ? AND value = ?', @key, @value).get_first_value().to_i

        @desc = h(@db.select("SELECT description FROM wiki.wikipages WHERE lang=? AND key=? AND value=?", r18n.locale.code, @key, @value).get_first_value())
        @desc = h(@db.select("SELECT description FROM wiki.wikipages WHERE lang='en' AND key=? AND value=?", @key, @value).get_first_value()) if @desc == ''
        @desc = "[#{ t.pages.tag.no_description_in_wiki }]" if @desc == ''

        erb :tag
    end

    #-------------------------------------

    get '/js/lang/:lang.js' do
        expires next_update
        trans = R18n::I18n.new(params[:lang], 'i18n')
        return 'var texts = ' + {
            :flexigrid => {
                :pagetext => trans.t.flexigrid.pagetext,
                :pagestat => trans.t.flexigrid.pagestat,
                :outof    => trans.t.flexigrid.outof,
                :findtext => trans.t.flexigrid.findtext,
                :procmsg  => trans.t.flexigrid.procmsg,
                :nomsg    => trans.t.flexigrid.nomsg,
                :errormsg => trans.t.flexigrid.errormsg,
            },
            :instance_description => {
                :title => trans.t.taginfo.instance.title,
            },
            :misc => {
                :values_less_than_one_percent => trans.t.misc.values_less_than_one_percent,
                :empty_string => trans.t.misc.empty_string,
                :count => trans.t.misc.count,
                :all => trans.t.misc.all,
            },
            :osm => {
                :key => trans.t.osm.key,
                :keys => trans.t.osm.keys,
                :value => trans.t.osm.value,
                :values => trans.t.osm.values,
                :tag => trans.t.osm.tag,
                :tags => trans.t.osm.tags,
                :node => trans.t.osm.node,
                :nodes => trans.t.osm.nodes,
                :way => trans.t.osm.way,
                :ways => trans.t.osm.ways,
                :relation => trans.t.osm.relation,
                :relations => trans.t.osm.relations,
                :all => trans.t.osm.all
            },
            :pages => {
                :key => {
                    :other_keys_used => {
                        :other => trans.t.pages.key.other_keys_used.other,
                    },
                    :number_objects => trans.t.pages.key.number_objects,
                },
                :tag => {
                    :other_tags_used => {
                        :other => trans.t.pages.tag.other_tags_used.other,
                    },
                },
            },
        }.to_json + ";\n"
    end

    #--------------------------------------------------------------------------

    get '/apidoc' do
        @title = 'API Documentation'
        erb :apidoc
    end

    #--------------------------------------------------------------------------

    load 'lib/api/db.rb'
    load 'lib/api/wiki.rb'
    load 'lib/api/josm.rb'
    load 'lib/api/reports.rb'
    load 'lib/api/search.rb'

    load 'lib/ui/search.rb'
    load 'lib/ui/reports.rb'
    load 'lib/ui/sources/db.rb'
    load 'lib/ui/sources/wiki.rb'
    load 'lib/ui/sources/josm.rb'
    load 'lib/ui/sources/potlatch.rb'
    load 'lib/ui/sources/merkaartor.rb'
    load 'lib/ui/embed.rb'
    load 'lib/ui/test.rb'

    # run application
    run! if app_file == $0

end