diff options
author | Jochen Topf <jochen@topf.org> | 2010-10-19 17:43:39 +0200 |
---|---|---|
committer | Jochen Topf <jochen@topf.org> | 2010-10-19 17:43:39 +0200 |
commit | 4b36dcb5089d5ce8069e0dcfccd314c72fd4878e (patch) | |
tree | 833bb880f970d0c1ae10e6d8e0c971c5c43f0754 /web/lib | |
parent | 2fdf3e394b619b81fcc78b8cdda5ea881089ea78 (diff) | |
download | taginfo-4b36dcb5089d5ce8069e0dcfccd314c72fd4878e.tar taginfo-4b36dcb5089d5ce8069e0dcfccd314c72fd4878e.tar.gz |
Improved handling of strange characters in tags
Diffstat (limited to 'web/lib')
-rw-r--r-- | web/lib/api/db.rb | 44 | ||||
-rw-r--r-- | web/lib/api/josm.rb | 21 | ||||
-rw-r--r-- | web/lib/api/wiki.rb | 14 | ||||
-rw-r--r-- | web/lib/utils.rb | 26 |
4 files changed, 65 insertions, 40 deletions
diff --git a/web/lib/api/db.rb b/web/lib/api/db.rb index 87e9c33..90a4a4c 100644 --- a/web/lib/api/db.rb +++ b/web/lib/api/db.rb @@ -17,7 +17,7 @@ class Taginfo < Sinatra::Base :rp => params[:rp].to_i, :total => total, :data => res.map{ |row| { - :key => h(row['key']), + :key => row['key'], :count_all => row['count_all'].to_i, :count_all_fraction => row['count_all'].to_f / @stats['objects'], :count_nodes => row['count_nodes'].to_i, @@ -28,16 +28,17 @@ class Taginfo < Sinatra::Base :count_relations_fraction => row['count_relations'].to_f / @stats['relations'], :values_all => row['values_all'].to_i, :users_all => row['users_all'].to_i, - :prevalent_values => (row['prevalent_values'] || '').split('|').map{ |pv| h(pv) } + :prevalent_values => (row['prevalent_values'] || '').split('|').map{ |pv| pv } } } }.to_json end - get '/api/1/db/keys/overview/:key' do + get %r{^/api/1/db/keys/overview/(.*)} do + key = params[:captures].first out = Hash.new @db.select('SELECT * FROM db.keys'). - condition('key = ?', params[:key]). + condition('key = ?', key). execute() do |row| ['all', 'nodes', 'ways', 'relations'].each do |type| out[type] = { @@ -52,14 +53,16 @@ class Taginfo < Sinatra::Base out.to_json end - get '/api/1/db/keys/distribution/:key' do + get %r{^/api/1/db/keys/distribution/(.*)} do + key = params[:captures].first content_type :png @db.select('SELECT png FROM db.key_distributions'). - condition('key = ?', params[:key]). + condition('key = ?', key). get_first_value() end - get '/api/1/db/keys/values/:key' do + get %r{^/api/1/db/keys/values/(.*)} do + key = params[:captures].first filter_type = get_filter() if params[:sortname] == 'count' @@ -67,20 +70,20 @@ class Taginfo < Sinatra::Base end (this_key_count, total) = @db.select("SELECT count_#{filter_type} AS count, values_#{filter_type} AS count_values FROM db.keys"). - condition('key = ?', params[:key]). + condition('key = ?', key). get_columns(:count, :count_values) if params[:query].to_s != '' total = @db.count('db.tags'). condition("count_#{filter_type} > 0"). - condition('key = ?', params[:key]). + condition('key = ?', key). condition_if("value LIKE '%' || ? || '%'", params[:query]). get_first_value() end res = @db.select('SELECT * FROM db.tags'). condition("count_#{filter_type} > 0"). - condition('key = ?', params[:key]). + condition('key = ?', key). condition_if("value LIKE '%' || ? || '%'", params[:query]). order_by([:value, :count_all, :count_nodes, :count_ways, :count_relations], params[:sortname], params[:sortorder]). paging(params[:rp], params[:page]). @@ -91,14 +94,15 @@ class Taginfo < Sinatra::Base :rp => params[:rp].to_i, :total => total.to_i, :data => res.map{ |row| { - :value => h(row['value']), + :value => row['value'], :count => row['count_' + filter_type].to_i, :fraction => row['count_' + filter_type].to_f / this_key_count.to_f } } }.to_json end - get '/api/1/db/keys/keys/:key' do + get %r{^/api/1/db/keys/keys/(.*)} do + key = params[:captures].first filter_type = get_filter() if params[:sortname] == 'to_count' @@ -108,16 +112,16 @@ class Taginfo < Sinatra::Base end total = @db.count('db.keypairs'). - condition('key1 = ? OR key2 = ?', params[:key], params[:key]). + condition('key1 = ? OR key2 = ?', key, key). condition("count_#{filter_type} > 0"). get_first_value().to_i has_this_key = @db.select("SELECT count_#{filter_type} FROM db.keys"). - condition('key = ?', params[:key]). + condition('key = ?', key). get_first_value() res = @db.select("SELECT p.key1 AS other_key, p.count_#{filter_type} AS together_count, k.count_#{filter_type} AS other_count, CAST(p.count_#{filter_type} AS REAL) / k.count_#{filter_type} AS from_fraction FROM db.keypairs p, db.keys k WHERE p.key1=k.key AND p.key2=? AND p.count_#{filter_type} > 0 - UNION SELECT p.key2 AS other_key, p.count_#{filter_type} AS together_count, k.count_#{filter_type} AS other_count, CAST(p.count_#{filter_type} AS REAL) / k.count_#{filter_type} AS from_fraction FROM db.keypairs p, db.keys k WHERE p.key2=k.key AND p.key1=? AND p.count_#{filter_type} > 0", params[:key], params[:key]). + UNION SELECT p.key2 AS other_key, p.count_#{filter_type} AS together_count, k.count_#{filter_type} AS other_count, CAST(p.count_#{filter_type} AS REAL) / k.count_#{filter_type} AS from_fraction FROM db.keypairs p, db.keys k WHERE p.key2=k.key AND p.key1=? AND p.count_#{filter_type} > 0", key, key). order_by([:together_count, :other_key, :from_fraction], params[:sortname], params[:sortorder]). paging(params[:rp], params[:page]). execute() @@ -127,7 +131,7 @@ class Taginfo < Sinatra::Base :rp => params[:rp].to_i, :total => total, :data => res.map{ |row| { - :other_key => h(row['other_key']), + :other_key => row['other_key'], :together_count => row['together_count'].to_i, :to_fraction => row['together_count'].to_f / has_this_key.to_f, :from_fraction => row['from_fraction'].to_f @@ -151,7 +155,7 @@ class Taginfo < Sinatra::Base :rp => params[:rp].to_i, :total => total, :data => res.map{ |row| { - :key => h(row['key']), + :key => row['key'], :scale_count => row['scale_count'].to_f, :scale_users => row['scale_users'].to_f, :scale_wiki => row['scale_wiki'].to_f, @@ -162,9 +166,9 @@ class Taginfo < Sinatra::Base }.to_json end - get '/api/1/db/tags/overview/:tag' do - tag = params[:tag] - (key, value) = tag.split('=', 2) + get '/api/1/db/tags/overview/' do + key = params[:key] + value = params[:value] out = {} diff --git a/web/lib/api/josm.rb b/web/lib/api/josm.rb index ddc529d..d4de265 100644 --- a/web/lib/api/josm.rb +++ b/web/lib/api/josm.rb @@ -13,9 +13,9 @@ class Taginfo < Sinatra::Base :rp => params[:rp].to_i, :total => total, :data => res.map{ |row| { - :k => h(row['k']), - :v => h(row['v']), - :b => h(row['b']), + :k => row['k'], + :v => row['v'], + :b => row['b'], :scale_min => row['scale_min'].nil? ? nil : row['scale_min'].to_i, :scale_max => row['scale_max'].nil? ? nil : row['scale_max'].to_i, :rule => h(row['rule']) @@ -61,14 +61,17 @@ class Taginfo < Sinatra::Base end end - get '/api/1/josm/styles/:style/keys/:key' do + get %r{^/api/1/josm/styles/([^/]+)/keys/(.*)} do + style = params[:captures].first # XXX do something with this + key = params[:captures][1] + total = @db.count('josm_style_rules'). - condition('k = ?', params[:key]). + condition('k = ?', key). condition_if("v LIKE '%' || ? || '%'", params[:query]). get_first_value().to_i res = @db.select('SELECT * FROM josm_style_rules'). - condition('k = ?', params[:key]). + condition('k = ?', key). condition_if("v LIKE '%' || ? || '%'", params[:query]). order_by([:v, :b, :scale_min, :scale_max], sort_by_for_values, params[:sortorder]). paging(params[:rp], params[:page]). @@ -77,9 +80,9 @@ class Taginfo < Sinatra::Base return get_josm_result(total, res); end - get '/api/1/josm/styles/:style/tags/:tag' do - tag = params[:tag] - (key, value) = tag.split('=', 2) + get '/api/1/josm/styles/:style/tags/' do + key = params[:key] + value = params[:value] total = @db.count('josm_style_rules'). condition('k = ?', key). diff --git a/web/lib/api/wiki.rb b/web/lib/api/wiki.rb index f844b3b..2ffd5a3 100644 --- a/web/lib/api/wiki.rb +++ b/web/lib/api/wiki.rb @@ -26,7 +26,7 @@ class Taginfo < Sinatra::Base (lang, status) = l.split(' ', 2) lang_hash[lang] = status } - { :key => h(row['key']), :lang => lang_hash } + { :key => row['key'], :lang => lang_hash } } }.to_json end @@ -42,9 +42,9 @@ class Taginfo < Sinatra::Base :on_way => row['on_way'] == '1' ? true : false, :on_area => row['on_area'] == '1' ? true : false, :on_relation => row['on_relation'] == '1' ? true : false, - :tags_implies => row['tags_implies' ].split(',').map{ |tag| h(tag) }, - :tags_combination => row['tags_combination'].split(',').map{ |tag| h(tag) }, - :tags_linked => row['tags_linked' ].split(',').map{ |tag| h(tag) } + :tags_implies => row['tags_implies' ].split(','), + :tags_combination => row['tags_combination'].split(','), + :tags_linked => row['tags_linked' ].split(',') } }.to_json end @@ -57,9 +57,9 @@ class Taginfo < Sinatra::Base return get_wiki_result(res) end - get %r{/api/1/wiki/tags/(.*)} do - tag = params[:captures].first - (key, value) = tag.split('=', 2) + get '/api/1/wiki/tags/' do + key = params[:key] + value = params[:value] res = @db.execute('SELECT * FROM wikipages WHERE key = ? AND value = ? ORDER BY lang', key, value) diff --git a/web/lib/utils.rb b/web/lib/utils.rb index 66eb2fb..b9bfdac 100644 --- a/web/lib/utils.rb +++ b/web/lib/utils.rb @@ -100,17 +100,35 @@ def get_total(type) return @stats[key] end +# see also web/public/js/taginfo.js def pp_key(key) if key == '' - return '<b><i>empty string<i></b>' + return '<span class="badchar empty">empty string</span>' end - return key.gsub(/([&<>#;\/]+)/, '<b>\1</b>').gsub(/ /, '<b>␣</b>').gsub(/\s+/, '<b>?</b>').gsub(/([-!"\$%'()*+,.=@\[\\\]^`{|}~]+)/, '<b>\1</b>') + + pp_chars = '!"#$%&()*+,-/;<=>?@[\\]^`{|}~' + "'"; + + result = '' + key.each_char do |c| + if (!pp_chars.index(c).nil?) + result += '<span class="badchar">' + c + '</span>' + elsif (c == ' ') + result += '<span class="badchar">␣</span>' + elsif (c.match(/\s/)) + result += '<span class="whitespace"> </span>' + else + result += c; + end + end + + return result; end +# see also web/public/js/taginfo.js def pp_value(value) if value == '' - return '<b><i>empty string<i></b>' + return '<span class="badchar empty">empty string</span>' end - return escape_html(value).gsub(/ /, '␣').gsub(/\s/, '<b>?</b>') + return escape_html(value).gsub(/ /, '␣').gsub(/\s/, '<span class="whitespace"> </span>') end |