From c3300d8c93d82af7d5f685215c77bdd84dca5fc4 Mon Sep 17 00:00:00 2001 From: Manfred Stock Date: Wed, 12 Nov 2014 21:36:19 +0100 Subject: [PATCH] Turn wikimedia_commons values into links This turns the value of the 'wikimedia_commons' tag into a link to the respective page on commons.wikimedia.org (if it is a 'File:' or 'Category:' item) by enhancing the browse tags helper. See [1] for the tag description. [1] http://wiki.openstreetmap.org/wiki/Key:wikimedia_commons --- app/helpers/browse_tags_helper.rb | 12 ++++++++++ config/locales/en.yml | 1 + test/helpers/browse_tags_helper_test.rb | 31 +++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/app/helpers/browse_tags_helper.rb b/app/helpers/browse_tags_helper.rb index 375300e14..bbf6f3cf7 100644 --- a/app/helpers/browse_tags_helper.rb +++ b/app/helpers/browse_tags_helper.rb @@ -17,6 +17,8 @@ module BrowseTagsHelper link_to(w[:title], w[:url], :title => t("browse.tag_details.wikidata_link", :page => w[:title].strip)) end safe_join(wdt, ";") + elsif wmc = wikimedia_commons_link(key, value) + link_to h(wmc[:title]), wmc[:url], :title => t("browse.tag_details.wikimedia_commons_link", :page => wmc[:title]) elsif url = wiki_link("tag", "#{key}=#{value}") link_to h(value), url, :title => t("browse.tag_details.wiki_link.tag", :key => key, :value => value) elsif phones = telephone_links(key, value) @@ -111,6 +113,16 @@ module BrowseTagsHelper nil end + def wikimedia_commons_link(key, value) + if key == "wikimedia_commons" && value =~ /^(?:file|category):/i + return { + :url => "//commons.wikimedia.org/wiki/#{value}?uselang=#{I18n.locale}", + :title => value + } + end + nil + end + def telephone_links(_key, value) # Does it look like a global phone number? eg "+1 (234) 567-8901 " # or a list of alternate numbers separated by ; diff --git a/config/locales/en.yml b/config/locales/en.yml index 35da13709..917c06a95 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -278,6 +278,7 @@ en: tag: "The wiki description page for the %{key}=%{value} tag" wikidata_link: "The %{page} item on Wikidata" wikipedia_link: "The %{page} article on Wikipedia" + wikimedia_commons_link: "The %{page} item on Wikimedia Commons" telephone_link: "Call %{phone_number}" colour_preview: "Colour %{colour_value} preview" note: diff --git a/test/helpers/browse_tags_helper_test.rb b/test/helpers/browse_tags_helper_test.rb index 222c7c669..a6390df38 100644 --- a/test/helpers/browse_tags_helper_test.rb +++ b/test/helpers/browse_tags_helper_test.rb @@ -48,6 +48,9 @@ class BrowseTagsHelperTest < ActionView::TestCase html = format_value("name:etymology:wikidata", "Q123") assert_dom_equal "Q123", html + html = format_value("wikimedia_commons", "File:Test.jpg") + assert_dom_equal "File:Test.jpg", html + html = format_value("colour", "#f00") assert_dom_equal %(#f00), html end @@ -190,6 +193,34 @@ class BrowseTagsHelperTest < ActionView::TestCase assert_nil link end + def test_wikimedia_commons_link + link = wikimedia_commons_link("wikimedia_commons", "http://commons.wikimedia.org/wiki/File:Full%20URL.jpg") + assert_nil link + + link = wikimedia_commons_link("wikimedia_commons", "https://commons.wikimedia.org/wiki/File:Full%20URL.jpg") + assert_nil link + + link = wikimedia_commons_link("wikimedia_commons", "Test.jpg") + assert_nil link + + link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg") + assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=en", link[:url] + assert_equal "File:Test.jpg", link[:title] + + link = wikimedia_commons_link("wikimedia_commons", "Category:Test_Category") + assert_equal "//commons.wikimedia.org/wiki/Category:Test_Category?uselang=en", link[:url] + assert_equal "Category:Test_Category", link[:title] + + I18n.locale = "pt-BR" + + link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg") + assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=pt-BR", link[:url] + assert_equal "File:Test.jpg", link[:title] + + link = wikimedia_commons_link("foo", "Test") + assert_nil link + end + def test_telephone_links links = telephone_links("foo", "Test") assert_nil links -- 2.39.5