diff options
author | Jochen Topf <jochen@topf.org> | 2013-01-02 20:21:06 +0100 |
---|---|---|
committer | Jochen Topf <jochen@topf.org> | 2013-01-02 20:21:06 +0100 |
commit | 3b46e3856fbba3112612d387137e6f79237789ac (patch) | |
tree | f97304b843f399b684163f4ab42aafc40c06948c | |
parent | 387d3cfb7c150591f167bae16ca7f7128062aed8 (diff) | |
download | taginfo-3b46e3856fbba3112612d387137e6f79237789ac.tar taginfo-3b46e3856fbba3112612d387137e6f79237789ac.tar.gz |
Cleanup sortname/sortorder code
-rw-r--r-- | web/lib/api.rb | 15 | ||||
-rw-r--r-- | web/lib/api/db.rb | 38 | ||||
-rw-r--r-- | web/lib/api/josm.rb | 6 | ||||
-rw-r--r-- | web/lib/api/langtag.rb | 8 | ||||
-rw-r--r-- | web/lib/api/reports.rb | 4 | ||||
-rw-r--r-- | web/lib/api/search.rb | 6 | ||||
-rw-r--r-- | web/lib/api/wiki.rb | 2 | ||||
-rw-r--r-- | web/lib/sql.rb | 13 | ||||
-rw-r--r-- | web/lib/ui/keys_tags.rb | 6 |
9 files changed, 53 insertions, 45 deletions
diff --git a/web/lib/api.rb b/web/lib/api.rb index 28fe786..c020f56 100644 --- a/web/lib/api.rb +++ b/web/lib/api.rb @@ -81,7 +81,8 @@ end class APIParameters - attr_reader :page, :results_per_page + attr_reader :page, :results_per_page, :sortorder + attr_accessor :sortname def initialize(p) if p[:rp].nil? || p[:rp] == '0' || p[:rp] == '' || p[:page].nil? || p[:page] == '0' || p[:page] == '' @@ -97,6 +98,18 @@ class APIParameters @page = p[:page].to_i @results_per_page = p[:rp].to_i end + + if p[:sortname].nil? || p[:sortname] == '' + @sortname = nil + else + @sortname = p[:sortname].gsub(/[^a-z_]/, '_') + end + + if p[:sortorder] == 'desc' || p[:sortorder] == 'DESC' + @sortorder = 'DESC' + else + @sortorder = 'ASC' + end end def do_paging? diff --git a/web/lib/api/db.rb b/web/lib/api/db.rb index f4f14fb..9c2f5e5 100644 --- a/web/lib/api/db.rb +++ b/web/lib/api/db.rb @@ -53,7 +53,7 @@ class Taginfo < Sinatra::Base res = @db.select('SELECT * FROM db.keys'). condition_if("key LIKE '%' || ? || '%'", params[:query]). conditions(filters). - order_by(params[:sortname], params[:sortorder]) { |o| + order_by(@ap.sortname, @ap.sortorder) { |o| o.key o.count_all o.count_nodes @@ -80,7 +80,7 @@ class Taginfo < Sinatra::Base wikipages = @db.select('SELECT key, lang, title, type FROM wiki.wikipages'). condition("value IS NULL"). condition("key IN (#{ res.map{ |row| "'" + SQLite3::Database.quote(row['key']) + "'" }.join(',') })"). - order_by([:key, :lang]). + order_by([:key, :lang], 'ASC'). execute() wikipages.each do |wp| @@ -164,7 +164,7 @@ class Taginfo < Sinatra::Base res = @db.select('SELECT * FROM db.selected_tags'). condition_if("(skey LIKE '%' || ? || '%') OR (svalue LIKE '%' || ? || '%')", params[:query], params[:query]). - order_by(params[:sortname], params[:sortorder]) { |o| + order_by(@ap.sortname, @ap.sortorder) { |o| o.tag :skey o.tag :svalue o.count_all @@ -351,8 +351,8 @@ class Taginfo < Sinatra::Base lang = params[:lang] || 'en' filter_type = get_filter() - if params[:sortname] == 'count' - params[:sortname] = 'count_' + filter_type + if @ap.sortname == 'count' + @ap.sortname = ['count_' + filter_type] end (this_key_count, total) = @db.select("SELECT count_#{filter_type} AS count, values_#{filter_type} AS count_values FROM db.keys"). @@ -371,7 +371,7 @@ class Taginfo < Sinatra::Base condition("count_#{filter_type} > 0"). condition('key = ?', key). condition_if("value LIKE '%' || ? || '%'", params[:query]). - order_by(params[:sortname], params[:sortorder]){ |o| + order_by(@ap.sortname, @ap.sortorder) { |o| o.value o.count_all o.count_nodes @@ -434,10 +434,10 @@ class Taginfo < Sinatra::Base key = params[:key] filter_type = get_filter() - if params[:sortname] == 'to_count' - params[:sortname] = 'together_count' - elsif params[:sortname] == 'from_count' - params[:sortname] = ['from_fraction', 'together_count', 'other_key'] + if @ap.sortname == 'to_count' + @ap.sortname = ['together_count'] + elsif @ap.sortname == 'from_count' + @ap.sortname = ['from_fraction', 'together_count', 'other_key'] end cq = @db.count('db.keypairs') @@ -454,7 +454,7 @@ class Taginfo < Sinatra::Base 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.key2 LIKE '%' || ? || '%') AND p.count_#{filter_type} > 0", key, params[:query], key, params[:query]) : @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", key, key)). - order_by(params[:sortname], params[:sortorder]){ |o| + order_by(@ap.sortname, @ap.sortorder) { |o| o.together_count o.other_key o.from_fraction @@ -482,7 +482,7 @@ class Taginfo < Sinatra::Base res = @db.select('SELECT * FROM popular_keys'). condition_if("key LIKE '%' || ? || '%'", params[:query]). - order_by(params[:sortname], params[:sortorder]){ |o| + order_by(@ap.sortname, @ap.sortorder) { |o| o.key o.scale_count o.scale_user @@ -618,12 +618,12 @@ class Taginfo < Sinatra::Base value = params[:value] filter_type = get_filter() - if params[:sortname] == 'to_count' - params[:sortname] = 'together_count' - elsif params[:sortname] == 'from_count' - params[:sortname] = ['from_fraction', 'together_count', 'other_key', 'other_value'] - elsif params[:sortname] == 'other_tag' - params[:sortname] = ['other_key', 'other_value'] + if @ap.sortname == 'to_count' + @ap.sortname = ['together_count'] + elsif @ap.sortname == 'from_count' + @ap.sortname = ['from_fraction', 'together_count', 'other_key', 'other_value'] + elsif @ap.sortname == 'other_tag' + @ap.sortname = ['other_key', 'other_value'] end cq = @db.count('db.tagpairs') @@ -648,7 +648,7 @@ class Taginfo < Sinatra::Base UNION SELECT p.key1 AS other_key, '' AS other_value, 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.tagpairs p, db.keys k WHERE p.key1=k.key AND p.value1 = '' AND p.key2=? AND p.value2=? AND p.count_#{filter_type} > 0 UNION SELECT p.key2 AS other_key, p.value2 AS other_value, 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.tagpairs p, db.selected_tags k WHERE p.key2=k.skey AND p.value2=k.svalue AND k.svalue != '' AND p.key1=? AND p.value1=? AND p.count_#{filter_type} > 0 UNION SELECT p.key2 AS other_key, '' AS other_value, 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.tagpairs p, db.keys k WHERE p.key2=k.key AND p.value2 = '' AND p.key1=? AND p.value1=? AND p.count_#{filter_type} > 0", key, value, key, value, key, value, key, value)). - order_by(params[:sortname], params[:sortorder]){ |o| + order_by(@ap.sortname, @ap.sortorder) { |o| o.together_count o.other_key o.other_value diff --git a/web/lib/api/josm.rb b/web/lib/api/josm.rb index 06a3a6c..7c98b14 100644 --- a/web/lib/api/josm.rb +++ b/web/lib/api/josm.rb @@ -49,7 +49,7 @@ class Taginfo < Sinatra::Base res = @db.select('SELECT * FROM josm_style_rules'). condition_if("k LIKE '%' || ? || '%' OR v LIKE '%' || ? || '%'", params[:query], params[:query]). - order_by(params[:sortname], params[:sortorder]){ |o| + order_by(@ap.sortname, @ap.sortorder) { |o| o.k :k o.k :v o.k :b @@ -76,7 +76,7 @@ class Taginfo < Sinatra::Base res = @db.select('SELECT * FROM josm_style_rules'). condition('k = ?', key). condition_if("v LIKE '%' || ? || '%'", params[:query]). - order_by(params[:sortname], params[:sortorder]){ |o| + order_by(@ap.sortname, @ap.sortorder) { |o| o.v :v o.v :b o.b @@ -99,7 +99,7 @@ class Taginfo < Sinatra::Base res = @db.select('SELECT * FROM josm_style_rules'). condition('k = ?', key). condition('v = ?', value). - order_by([:k, :v]). + order_by([:k, :v], 'ASC'). paging(@ap). execute() diff --git a/web/lib/api/langtag.rb b/web/lib/api/langtag.rb index 2adf326..3f28ab8 100644 --- a/web/lib/api/langtag.rb +++ b/web/lib/api/langtag.rb @@ -22,7 +22,7 @@ class Taginfo < Sinatra::Base res = @db.select('SELECT * FROM db.keys'). condition("key LIKE '%name%'"). condition_if("key LIKE '%' || ? || '%'", params[:query]). - order_by(params[:sortname], params[:sortorder]) { |o| + order_by(@ap.sortname, @ap.sortorder) { |o| o.key o.count_all }. @@ -75,9 +75,9 @@ class Taginfo < Sinatra::Base total = entries.size - if params[:sortname] =~ /^(subtag|description|added)$/ - s = params[:sortname].to_sym - if params[:sortorder] == 'asc' + if @ap.sortname =~ /^(subtag|description|added)$/ + s = @ap.sortname.to_sym + if @ap.sortorder == 'ASC' entries.sort!{ |a, b| a.send(s) <=> b.send(s) } else entries.sort!{ |a, b| b.send(s) <=> a.send(s) } diff --git a/web/lib/api/reports.rb b/web/lib/api/reports.rb index 84e0306..3172fc4 100644 --- a/web/lib/api/reports.rb +++ b/web/lib/api/reports.rb @@ -44,7 +44,7 @@ class Taginfo < Sinatra::Base condition('count_all > ?', min_count). condition("in_wiki#{english} = 0"). condition_if("key LIKE '%' || ? || '%'", params[:query]). - order_by(params[:sortname], params[:sortorder]){ |o| + order_by(@ap.sortname, @ap.sortorder) { |o| o.key o.count_all o.values_all @@ -104,7 +104,7 @@ class Taginfo < Sinatra::Base :ui => '/reports/languages' }) do res = @db.select('SELECT * FROM languages'). - order_by(params[:sortname], params[:sortorder]){ |o| + order_by(@ap.sortname, @ap.sortorder) { |o| o.code o.native_name o.english_name diff --git a/web/lib/api/search.rb b/web/lib/api/search.rb index 87d3433..f1c390f 100644 --- a/web/lib/api/search.rb +++ b/web/lib/api/search.rb @@ -22,7 +22,7 @@ class Taginfo < Sinatra::Base res = @db.select('SELECT * FROM search.ftsearch'). condition_if("value MATCH ?", query). - order_by(params[:sortname], params[:sortorder]) { |o| + order_by(@ap.sortname, @ap.sortorder) { |o| o.count_all o.key o.value @@ -58,7 +58,7 @@ class Taginfo < Sinatra::Base end res = sel. - order_by(params[:sortname], params[:sortorder]) { |o| + order_by(@ap.sortname, @ap.sortorder) { |o| o.count_all o.key o.value @@ -85,7 +85,7 @@ class Taginfo < Sinatra::Base sel = @db.select("SELECT key, value FROM wiki.words WHERE words LIKE ('%' || ? || '%')", query) res = sel. - order_by(params[:sortname], params[:sortorder]) { |o| + order_by(@ap.sortname, @ap.sortorder) { |o| o.key o.value }. diff --git a/web/lib/api/wiki.rb b/web/lib/api/wiki.rb index 6e5d9de..350147a 100644 --- a/web/lib/api/wiki.rb +++ b/web/lib/api/wiki.rb @@ -35,7 +35,7 @@ class Taginfo < Sinatra::Base res = @db.select('SELECT key, langs FROM wiki.wikipages_keys'). condition_if("key LIKE '%' || ? || '%'", params[:query]). - order_by(params[:sortname], params[:sortorder]){ |o| + order_by(@ap.sortname, @ap.sortorder){ |o| o.key }. paging(@ap). diff --git a/web/lib/sql.rb b/web/lib/sql.rb index 55e4985..4bfb020 100644 --- a/web/lib/sql.rb +++ b/web/lib/sql.rb @@ -85,7 +85,7 @@ module SQL self end - def order_by(values, direction='ASC', &block) + def order_by(values, direction, &block) if values.is_a?(Array) values = values.compact else @@ -94,13 +94,8 @@ module SQL o = Order.new(values, &block) - if direction.nil? - direction = 'ASC' - else - direction = direction.to_s - if direction !~ /^(asc|desc)$/i - raise ArgumentError, 'direction must be ASC or DESC' - end + if direction != 'ASC' && direction != 'DESC' + raise ArgumentError, 'direction must be ASC or DESC' end values.each do |value| @@ -113,7 +108,7 @@ module SQL unless values.empty? @order_by = "ORDER BY " + values.map{ |value| value = o.default if value.nil? - o[value.to_s].map{ |oel| oel.to_s(direction.upcase) }.join(',') + o[value.to_s].map{ |oel| oel.to_s(direction) }.join(',') }.join(',') end diff --git a/web/lib/ui/keys_tags.rb b/web/lib/ui/keys_tags.rb index 94585fd..a9aa572 100644 --- a/web/lib/ui/keys_tags.rb +++ b/web/lib/ui/keys_tags.rb @@ -34,7 +34,7 @@ class Taginfo < Sinatra::Base @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'). + order_by([:count], 'DESC'). execute().map{ |row| [{ 'value' => row['value'], 'count' => row['count'].to_i }] } # add "(other)" label for the rest of the values @@ -52,9 +52,9 @@ class Taginfo < Sinatra::Base '<img src="/img/types/' + (@merkaartor_selector =~ /Type is #{name}/ ? type.to_s : 'none') + '.16.png" width="16" height="16" alt="' + name + '" title="' + name + '"/>' }.join(' ') - @merkaartor_values = @db.select('SELECT value FROM merkaartor.tags').condition('key=?', @key).order_by(:value).execute().map{ |row| row['value'] } + @merkaartor_values = @db.select('SELECT value FROM merkaartor.tags').condition('key=?', @key).order_by([:value], 'ASC').execute().map{ |row| row['value'] } - @merkaartor_desc = @db.select('SELECT lang, description FROM key_descriptions').condition('key=?', @key).order_by(:lang).execute() + @merkaartor_desc = @db.select('SELECT lang, description FROM key_descriptions').condition('key=?', @key).order_by([:lang], 'DESC').execute() @img_width = TaginfoConfig.get('geodistribution.width') * TaginfoConfig.get('geodistribution.scale_image') @img_height = TaginfoConfig.get('geodistribution.height') * TaginfoConfig.get('geodistribution.scale_image') |