From: Tom Hughes Date: Thu, 14 May 2015 19:34:25 +0000 (+0100) Subject: Escape fragment identifiers in wikipedia URLs X-Git-Tag: live~4648 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/b40353b9b05350bccac09e136315f2fcb0093ca7 Escape fragment identifiers in wikipedia URLs Fixes #967 --- diff --git a/app/helpers/browse_helper.rb b/app/helpers/browse_helper.rb index 7ccfca334..a2d0e440d 100644 --- a/app/helpers/browse_helper.rb +++ b/app/helpers/browse_helper.rb @@ -1,3 +1,5 @@ +require "uri" + module BrowseHelper def printable_name(object, version = false) if object.id.is_a?(Array) @@ -133,17 +135,19 @@ module BrowseHelper return nil end - if value =~ /^([^#]*)(#.*)/ + if value =~ /^([^#]*)#(.*)/ # Contains a reference to a section of the wikipedia article # Must break it up to correctly build the url value = $1 - section = $2 + section = "#" + $2 + encoded_section = "#" + URI.encode($2.gsub(" ", "_")).gsub("%3A", ":").gsub("%", ".") else section = "" + encoded_section = "" end { - :url => "http://#{lang}.wikipedia.org/wiki/#{value}?uselang=#{I18n.locale}#{section}", + :url => "http://#{lang}.wikipedia.org/wiki/#{value}?uselang=#{I18n.locale}#{encoded_section}", :title => value + section } end diff --git a/test/helpers/browse_helper_test.rb b/test/helpers/browse_helper_test.rb index e72f370d0..c4f0e8c31 100644 --- a/test/helpers/browse_helper_test.rb +++ b/test/helpers/browse_helper_test.rb @@ -188,6 +188,14 @@ class BrowseHelperTest < ActionView::TestCase assert_equal "http://fr.wikipedia.org/wiki/de:Test?uselang=en", link[:url] assert_equal "de:Test", link[:title] + link = wikipedia_link("wikipedia", "de:Englischer Garten (München)#Japanisches Teehaus") + assert_equal "http://de.wikipedia.org/wiki/de:Englischer Garten (München)?uselang=en#Japanisches_Teehaus", link[:url] + assert_equal "de:Englischer Garten (München)#Japanisches Teehaus", link[:title] + + link = wikipedia_link("wikipedia", "de:Alte Brücke (Heidelberg)#Brückenaffe") + assert_equal "http://de.wikipedia.org/wiki/de:Alte Brücke (Heidelberg)?uselang=en#Br.C3.BCckenaffe", link[:url] + assert_equal "de:Alte Brücke (Heidelberg)#Brückenaffe", link[:title] + I18n.locale = "pt-BR" link = wikipedia_link("wikipedia", "zh-classical:Test#Section")