diff options
author | Jochen Topf <jochen@topf.org> | 2013-01-09 19:18:55 +0100 |
---|---|---|
committer | Jochen Topf <jochen@topf.org> | 2013-01-09 19:18:55 +0100 |
commit | 64a047f622a5ed15dea94b5e52dd8c948fce9e95 (patch) | |
tree | 1dfeecec0fa0477c1a246ef6567dffc56592d325 /web/lib | |
parent | 9cadfd89c12c9223e7c572646680d0bcce57310c (diff) | |
download | taginfo-64a047f622a5ed15dea94b5e52dd8c948fce9e95.tar taginfo-64a047f622a5ed15dea94b5e52dd8c948fce9e95.tar.gz |
Better support for wiki images.
Key and tag wiki pages can contain images. Until now we only got the titles of
those images. Now we also get the URL to the image, URL to thumbnails, width,
height, and mime type. This information is now exposed in the API and it is
used to show the images in the Overview tab of the key and tag pages.
While we are changing the update process anyway, I changed the program that
gets the list of all pages to also output the time those pages changed last.
This information is currently not used, but it could be used to cache those
pages locally making the update much faster and adding less strain to the
wiki server.
Diffstat (limited to 'web/lib')
-rw-r--r-- | web/lib/api/v4/key.rb | 12 | ||||
-rw-r--r-- | web/lib/api/v4/tag.rb | 12 | ||||
-rw-r--r-- | web/lib/ui/keys_tags.rb | 22 | ||||
-rw-r--r-- | web/lib/utils.rb | 10 |
4 files changed, 51 insertions, 5 deletions
diff --git a/web/lib/api/v4/key.rb b/web/lib/api/v4/key.rb index 7cc9532..420ef37 100644 --- a/web/lib/api/v4/key.rb +++ b/web/lib/api/v4/key.rb @@ -283,7 +283,15 @@ class Taginfo < Sinatra::Base [:language_en, :STRING, 'Language name in English.'], [:title, :STRING, 'Wiki page title.'], [:description, :STRING, 'Short description of key from wiki page.'], - [:image, :STRING, 'Wiki page title of associated image.'], + [:image, :HASH, 'Associated image.', [ + [:title, :STRING, 'Wiki page title of associated image.' ], + [:width, :INT, 'Width of image.' ], + [:height, :INT, 'Height of image.' ], + [:mime, :STRING, 'MIME type of image.' ], + [:image_url, :STRING, 'Image URL' ], + [:thumb_url_prefix, :STRING, 'Prefix of thumbnail URL.' ], + [:thumb_url_suffix, :STRING, 'Suffix of thumbnail URL.' ] + ]], [:on_node, :BOOL, 'Is this a key for nodes?'], [:on_way, :BOOL, 'Is this a key for ways?'], [:on_area, :BOOL, 'Is this a key for areas?'], @@ -297,7 +305,7 @@ class Taginfo < Sinatra::Base }) do key = params[:key] - res = @db.execute('SELECT * FROM wikipages WHERE value IS NULL AND key = ? ORDER BY lang', key) + res = @db.execute('SELECT * FROM wikipages LEFT OUTER JOIN wiki_images USING (image) WHERE value IS NULL AND key = ? ORDER BY lang', key) return get_wiki_result(res) end diff --git a/web/lib/api/v4/tag.rb b/web/lib/api/v4/tag.rb index 50e26c7..20a0559 100644 --- a/web/lib/api/v4/tag.rb +++ b/web/lib/api/v4/tag.rb @@ -177,7 +177,15 @@ class Taginfo < Sinatra::Base [:language_en, :STRING, 'Language name in English.'], [:title, :STRING, 'Wiki page title.'], [:description, :STRING, 'Short description of tag from wiki page.'], - [:image, :STRING, 'Wiki page title of associated image.'], + [:image, :HASH, 'Associated image.', [ + [:title, :STRING, 'Wiki page title of associated image.' ], + [:width, :INT, 'Width of image.' ], + [:height, :INT, 'Height of image.' ], + [:mime, :STRING, 'MIME type of image.' ], + [:image_url, :STRING, 'Image URL' ], + [:thumb_url_prefix, :STRING, 'Prefix of thumbnail URL.' ], + [:thumb_url_suffix, :STRING, 'Suffix of thumbnail URL.' ] + ]], [:on_node, :BOOL, 'Is this a tag for nodes?'], [:on_way, :BOOL, 'Is this a tag for ways?'], [:on_area, :BOOL, 'Is this a tag for areas?'], @@ -192,7 +200,7 @@ class Taginfo < Sinatra::Base key = params[:key] value = params[:value] - res = @db.execute('SELECT * FROM wikipages WHERE key = ? AND value = ? ORDER BY lang', key, value) + res = @db.execute('SELECT * FROM wikipages LEFT OUTER JOIN wiki_images USING (image) WHERE key = ? AND value = ? ORDER BY lang', key, value) return get_wiki_result(res) end diff --git a/web/lib/ui/keys_tags.rb b/web/lib/ui/keys_tags.rb index feaf90d..5e21c6e 100644 --- a/web/lib/ui/keys_tags.rb +++ b/web/lib/ui/keys_tags.rb @@ -1,6 +1,17 @@ # web/lib/ui/keys_tags.rb class Taginfo < Sinatra::Base + MAX_IMAGE_WIDTH = 300 + + def build_image_url(row) + w = row['width'].to_i + h = row['height'].to_i + if w > 0 && h > 0 + return "#{row['thumb_url_prefix']}#{ h <= w ? MAX_IMAGE_WIDTH : (MAX_IMAGE_WIDTH * w / h).to_i }#{ row['thumb_url_suffix'] }" + end + return nil + end + get %r{^/keys/(.*)} do |key| if params[:key].nil? @key = key @@ -28,6 +39,11 @@ class Taginfo < Sinatra::Base @desc = "<span title='#{ t.pages.key.description_from_wiki }' tipsy='w'>#{ @desc }</span" end + @db.select("SELECT width, height, thumb_url_prefix, thumb_url_suffix FROM wiki.wikipages LEFT OUTER JOIN wiki.wiki_images USING(image) WHERE lang=? AND key=? AND value IS NULL UNION SELECT width, height, thumb_url_prefix, thumb_url_suffix FROM wiki.wikipages LEFT OUTER JOIN wiki.wiki_images USING(image) WHERE lang='en' AND key=? AND value IS NULL LIMIT 1", r18n.locale.code, @key, @key). + execute() do |row| + @image_url = build_image_url(row) + end + @prevalent_values = @db.select("SELECT value, count_#{@filter_type} AS count FROM tags"). condition('key=?', @key). condition('count > ?', @count_all_values * 0.02). @@ -107,8 +123,14 @@ class Taginfo < Sinatra::Base @desc = "<span title='#{ t.pages.tag.description_from_wiki }' tipsy='w'>#{ @desc }</span" end + @db.select("SELECT width, height, thumb_url_prefix, thumb_url_suffix FROM wiki.wikipages LEFT OUTER JOIN wiki.wiki_images USING(image) WHERE lang=? AND key=? AND value=? UNION SELECT width, height, thumb_url_prefix, thumb_url_suffix FROM wiki.wikipages LEFT OUTER JOIN wiki.wiki_images USING(image) WHERE lang='en' AND key=? AND value=? LIMIT 1", r18n.locale.code, @key, @value, @key, @value). + execute() do |row| + @image_url = build_image_url(row) + end + javascript "#{ r18n.locale.code }/tag" erb :tag end end + diff --git a/web/lib/utils.rb b/web/lib/utils.rb index 20a4dab..2549b57 100644 --- a/web/lib/utils.rb +++ b/web/lib/utils.rb @@ -184,7 +184,15 @@ def get_wiki_result(res) :language_en => h(::Language[row['lang']].english_name), :title => h(row['title']), :description => h(row['description']), - :image => h(row['image']), + :image => { + :title => h(row['image']), + :width => row['width'].to_i, + :height => row['height'].to_i, + :mime => h(row['mime']), + :image_url => h(row['image_url']), + :thumb_url_prefix => h(row['thumb_url_prefix']), + :thumb_url_suffix => h(row['thumb_url_suffix']) + }, :on_node => row['on_node'].to_i == 1, :on_way => row['on_way'].to_i == 1, :on_area => row['on_area'].to_i == 1, |