Skip to content

Commit

Permalink
Add lang parameter to db/keys/values API call, it now shows descripti…
Browse files Browse the repository at this point in the history
…on from wiki; add this to values tab on key page.
  • Loading branch information
joto committed Jan 23, 2012
1 parent 46d84e2 commit 44e35a3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
21 changes: 19 additions & 2 deletions web/lib/api/db.rb
Expand Up @@ -332,6 +332,7 @@ class Taginfo < Sinatra::Base
:description => 'Get values used with a given key.',
:parameters => {
:key => 'Tag key (required).',
:lang => "Language (optional, default: 'en').",
:query => 'Only show results where the value matches this query (substring match, optional).'
},
:paging => :optional,
Expand All @@ -342,11 +343,12 @@ class Taginfo < Sinatra::Base
:relations => { :doc => 'Only values on tags used on relations.' }
},
:sort => %w( value count_all count_nodes count_ways count_relations ),
:result => { :value => :STRING, :count => :INT, :fraction => :FLOAT },
:result => { :value => :STRING, :count => :INT, :fraction => :FLOAT, :description => :STRING },
:example => { :key => 'highway', :page => 1, :rp => 10, :sortname => 'count_ways', :sortorder => 'desc' },
:ui => '/keys/highway#values'
}) do
key = params[:key]
lang = params[:lang] || 'en'
filter_type = get_filter()

if params[:sortname] == 'count'
Expand Down Expand Up @@ -379,14 +381,29 @@ class Taginfo < Sinatra::Base
paging(params[:rp], params[:page]).
execute()

# Read description for tag from wikipages, first in English then in the chosen
# language. This way the chosen language description will overwrite the default
# English one.
wikidesc = {}
['en', lang].uniq.each do |lang|
@db.select('SELECT value, description FROM wiki.wikipages').
condition('lang = ?', lang).
condition('key = ?', key).
condition("value IN (#{ res.map{ |row| "'" + SQLite3::Database.quote(row['value']) + "'" }.join(',') })").
execute().each do |row|
wikidesc[row['value']] = row['description']
end
end

return {
:page => params[:page].to_i,
:rp => params[:rp].to_i,
:total => total.to_i,
:data => res.map{ |row| {
:value => row['value'],
:count => row['count_' + filter_type].to_i,
:fraction => (row['count_' + filter_type].to_f / this_key_count.to_f).round_to(4)
:fraction => (row['count_' + filter_type].to_f / this_key_count.to_f).round_to(4),
:description => wikidesc[row['value']]
} }
}.to_json
end
Expand Down
14 changes: 8 additions & 6 deletions web/public/js/taginfo.js
Expand Up @@ -608,12 +608,13 @@ var create_flexigrid_for = {
}
});
},
values: function(key, filter_type) {
values: function(key, filter_type, lang) {
create_flexigrid('grid-values', {
url: '/api/2/db/keys/values?key=' + encodeURIComponent(key) + '&filter=' + encodeURIComponent(filter_type),
url: '/api/2/db/keys/values?key=' + encodeURIComponent(key) + '&filter=' + encodeURIComponent(filter_type) + '&lang=' + encodeURIComponent(lang),
colModel: [
{ display: texts.osm.value, name: 'value', width: 500, sortable: true },
{ display: texts.misc.count, name: 'count', width: 300, sortable: true, align: 'center' }
{ display: texts.osm.value, name: 'value', width: 200, sortable: true },
{ display: texts.misc.count, name: 'count', width: 240, sortable: true, align: 'center' },
{ display: 'Description', name: 'description', width: 600, sortable: false, align: 'left' }
],
searchitems: [
{ display: texts.osm.value, name: 'value' }
Expand All @@ -623,8 +624,9 @@ var create_flexigrid_for = {
preProcess: function(data) {
data.rows = jQuery.map(data.data, function(row, i) {
return { 'cell': [
link_to_value(key, row.value),
print_value_with_percent(row.count, row.fraction)
hover_expand(link_to_value(key, row.value)),
print_value_with_percent(row.count, row.fraction),
row.description
] };
});
delete data.data;
Expand Down
2 changes: 1 addition & 1 deletion web/views/key.erb
Expand Up @@ -126,7 +126,7 @@ function page_init() {
window.location.search = jQuery.param(qs);
});
init_tabs('key', [#{ @key.to_json }, #{ @filter_type.to_json }]);
init_tabs('key', [#{ @key.to_json }, #{ @filter_type.to_json }, #{ r18n.locale.code.to_json }]);
var data = #{ @prevalent_values.to_json() };
Expand Down

0 comments on commit 44e35a3

Please sign in to comment.