Skip to content

Commit

Permalink
fandom: generate /wiki/File:name.jpg page URLs instead of `/wiki/Ga…
Browse files Browse the repository at this point in the history
…llery?file=name.jpg`.

Change it so that image URLs are normalized into page URLs like this:

    https://typemoon.fandom.com/wiki/File:Memories_of_Trifas.png

instead of like this:

    https://typemoon.fandom.com/wiki/Gallery?file=File:Memories_of_Trifas.png

Previous we stopped using `/wiki/File:name.jpg` URLs because Fandom started redirecting them to the
first wiki page containing the file, rather than the file itself. We used `/wiki/Gallery?file=name.jpg`
to prevent this. Now Fandom opens the image in an overlay above the wiki page, so we can use File:
URLs again. This way we can see the image itself, and the wiki page the image is from.
  • Loading branch information
evazion committed May 23, 2024
1 parent 5699060 commit 6823232
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
15 changes: 10 additions & 5 deletions app/logical/source/url/fandom.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

class Source::URL::Fandom < Source::URL
# Matches language codes like 'ja' or 'pt-br'
LANG_CODE_REGEX = /^[a-z]{2,3}(-[a-z]{2,3})?$/

RESERVED_WIKI_NAMES = [nil, "auth", "www"]

WIKI_NAMES = {
Expand Down Expand Up @@ -35,7 +38,7 @@ class Source::URL::Fandom < Source::URL
[nil, "masterofeternity_gamepedia_en"] => "masterofeternity",
[nil, "ninehourspersonsdoors"] => "zeroescape",
[nil, "onigiri-en"] => "onigiri",
["protagonist", "p__"] => "hero",
[nil, "p__"] => "hero",
[nil, "ritualofthenight"] => "bloodstained",
[nil, "rockman_x_dive"] => "rockman-x-dive",
[nil, "romancingsaga"] => "saga",
Expand Down Expand Up @@ -79,7 +82,7 @@ def parse

# https://genshin-impact.fandom.com/pt-br/f
# https://genshin-impact.fandom.com/ja/wiki/凝光/ギャラリー
@path_prefix = segments.shift if segments.first&.match?(/^[a-z]{2,3}(-[a-z]{2,3})?$/)
@path_prefix = segments.shift if segments.first&.match?(LANG_CODE_REGEX)

# https://typemoon.fandom.com/f/p/4400000000000077950
return if segments.first == "f"
Expand Down Expand Up @@ -158,8 +161,10 @@ def full_image_url
end

def page_url
if profile_url.present? && file.present?
"#{profile_url}/wiki/Gallery?file=#{Danbooru::URL.escape(file)}"
if profile_url.present? && page.present? && file.present? && !page.starts_with?("File:")
"#{profile_url}/wiki/#{page}?file=#{Danbooru::URL.escape(file)}"
elsif profile_url.present? && file.present?
"#{profile_url}/wiki/File:#{Danbooru::URL.escape(file)}"
elsif profile_url.present? && page.present?
"#{profile_url}/wiki/#{page}"
end
Expand All @@ -174,7 +179,7 @@ def profile_url
end

def lang
@path_prefix unless @path_prefix == "en"
path_prefix unless path_prefix == "en" || !path_prefix&.match?(LANG_CODE_REGEX)
end

def wiki
Expand Down
38 changes: 19 additions & 19 deletions test/unit/sources/fandom_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class FandomTest < ActiveSupport::TestCase
"https://static.wikia.nocookie.net/typemoon/images/9/96/Caster_Extra_Takeuchi_design_1.png/revision/latest/scale-to-width-down/1000?cb=20130523100711",
image_urls: %w[https://static.wikia.nocookie.net/typemoon/images/9/96/Caster_Extra_Takeuchi_design_1.png?format=original],
media_files: [{ file_size: 6_085_628 }],
page_url: "https://typemoon.fandom.com/wiki/Gallery?file=Caster_Extra_Takeuchi_design_1.png",
page_url: "https://typemoon.fandom.com/wiki/File:Caster_Extra_Takeuchi_design_1.png",
profile_urls: %w[https://typemoon.fandom.com/wiki/User:Nikonu],
display_name: nil,
username: "Nikonu",
Expand Down Expand Up @@ -38,7 +38,7 @@ class FandomTest < ActiveSupport::TestCase
"https://kancolle.fandom.com/Mutsuki?file=Mutsuki_Full.png",
image_urls: %w[https://static.wikia.nocookie.net/kancolle/images/3/3b/Mutsuki_Full.png?format=original],
media_files: [{ file_size: 135_822 }],
page_url: "https://kancolle.fandom.com/wiki/Gallery?file=Mutsuki_Full.png",
page_url: "https://kancolle.fandom.com/wiki/Mutsuki?file=Mutsuki_Full.png",
profile_urls: %w[https://kancolle.fandom.com/wiki/User:Botkaze],
display_name: nil,
username: "Botkaze",
Expand All @@ -60,7 +60,7 @@ class FandomTest < ActiveSupport::TestCase
strategy_should_work(
"https://typemoon.fandom.com/wiki/Kara_no_Kyoukai_-_The_Garden_of_sinners_Movie_4:_The_Hollow_Shrine?file=The_Garden_of_sinners_Chapter_4_Preview",
image_urls: [],
page_url: "https://typemoon.fandom.com/wiki/Gallery?file=The_Garden_of_sinners_Chapter_4_Preview",
page_url: "https://typemoon.fandom.com/wiki/Kara_no_Kyoukai_-_The_Garden_of_sinners_Movie_4:_The_Hollow_Shrine?file=The_Garden_of_sinners_Chapter_4_Preview",
profile_urls: %w[https://typemoon.fandom.com/wiki/User:Nikonu],
display_name: nil,
username: "Nikonu",
Expand All @@ -79,7 +79,7 @@ class FandomTest < ActiveSupport::TestCase
"https://typemoon.fandom.com/wiki/File:Memories_of_Trifas.png",
image_urls: %w[https://static.wikia.nocookie.net/typemoon/images/2/2f/Memories_of_Trifas.png?format=original],
media_files: [{ file_size: 450_447 }],
page_url: "https://typemoon.fandom.com/wiki/Gallery?file=Memories_of_Trifas.png",
page_url: "https://typemoon.fandom.com/wiki/File:Memories_of_Trifas.png",
profile_urls: %w[https://typemoon.fandom.com/wiki/User:Lemostr00],
display_name: nil,
username: "Lemostr00",
Expand All @@ -106,9 +106,9 @@ class FandomTest < ActiveSupport::TestCase

context "A deleted or nonexistent wiki file" do
strategy_should_work(
"https://kancolle.fandom.com/wiki/Gallery?file=bad.png",
"https://kancolle.fandom.com/wiki/File:bad.png",
image_urls: [],
page_url: "https://kancolle.fandom.com/wiki/Gallery?file=bad.png",
page_url: "https://kancolle.fandom.com/wiki/File:bad.png",
profile_urls: %w[https://kancolle.fandom.com],
display_name: nil,
username: nil,
Expand All @@ -119,19 +119,19 @@ class FandomTest < ActiveSupport::TestCase
end

should "convert fandom image urls to page urls" do
assert_equal("https://valkyriecrusade.fandom.com/wiki/Gallery?file=Crimson_Hatsune_H.png", Source::URL.page_url("https://vignette.wikia.nocookie.net/valkyriecrusade/images/c/c5/Crimson_Hatsune_H.png/revision/latest?cb=20180702031954"))
assert_equal("https://ishtaria.fandom.com/wiki/Gallery?file=Union-List.png", Source::URL.page_url("https://static.wikia.nocookie.net/age-of-ishtaria/images/f/f9/Union-List.png/revision/latest/scale-to-width-down/670?cb=20141219153314"))
assert_equal("https://atelier.fandom.com/wiki/Gallery?file=Marie_(Front_Page_Art).jpg", Source::URL.page_url("https://static.wikia.nocookie.net/atelierseries/images/2/22/Marie_%28Front_Page_Art%29.jpg/revision/latest/scale-to-width-down/670?cb=20130129113100"))
assert_equal("https://senrankagura.fandom.com/wiki/Gallery?file=Kagurapedia_Header.png", Source::URL.page_url("https://static.wikia.nocookie.net/kagura/images/9/9f/Kagurapedia_Header.png/revision/latest/scale-to-width-down/650?cb=20171016220119"))
assert_equal("https://genshin-impact.fandom.com/wiki/Gallery?file=Character_Kamisato_Ayato_Card.png", Source::URL.page_url("https://static.wikia.nocookie.net/gensin-impact/images/2/22/Character_Kamisato_Ayato_Card.png/revision/latest/scale-to-width-down/281?cb=20220204094446"))
assert_equal("https://neptunia.fandom.com/wiki/Gallery?file=Plutia_B%2BNvZ.png", Source::URL.page_url("http://vignette3.wikia.nocookie.net/neptunia/images/2/28/Plutia_B%2BNvZ.png/revision/latest?cb=20160420084622"))
assert_equal("https://typemoon.fandom.com/wiki/Gallery?file=Caster_Extra_Takeuchi_design_1.png", Source::URL.page_url("http://img3.wikia.nocookie.net/__cb20130523100711/typemoon/images/9/96/Caster_Extra_Takeuchi_design_1.png"))
assert_equal("https://virtualyoutuber.fandom.com/vi/wiki/Gallery?file=Alicia_Cotton_2nd_Outfit_2.png", Source::URL.page_url("https://static.wikia.nocookie.net/virtualyoutuber/images/c/c4/Alicia_Cotton_2nd_Outfit_2.png/revision/latest?cb=20211107035307&path-prefix=vi"))
assert_equal("https://typemoon.fandom.com/fr/wiki/Gallery?file=Aozaki_Aoko_Blue.png", Source::URL.page_url("http://img3.wikia.nocookie.net/__cb20140404214519/typemoon/fr/images/f/fd/Aozaki_Aoko_Blue.png"))
assert_equal("https://typemoon.fandom.com/fr/wiki/Gallery?file=Aozaki_Aoko_Blue.png", Source::URL.page_url("http://img3.wikia.nocookie.net/__cb20140404214519/typemoon/images/f/fd/Aozaki_Aoko_Blue.png/revision/latest?path-prefix=fr"))
assert_equal("https://typemoon.fandom.com/fr/wiki/Gallery?file=Aozaki_Aoko_Blue.png", Source::URL.page_url("http://img3.wikia.nocookie.net/__cb20140404214519/typemoon/fr/images/thumb/f/fd/Aozaki_Aoko_Blue.png/500px-Aozaki_Aoko_Blue.png"))
assert_equal("https://allanimefanon.fandom.com/wiki/Gallery?file=2560-1600-104761.jpg", Source::URL.page_url("http://img3.wikia.nocookie.net/__cb20130520180921/allanimefanon/images/thumb/8/82/2560-1600-104761.jpg/2000px-2560-1600-104761.jpg"))
assert_equal("https://hero.fandom.com/protagonist/wiki/Gallery?file=Yukiko_Amagi_(BlazBlue_Cross_Tag_Battle,_Character_Select_Artwork).png", Source::URL.page_url("https://static.wikia.nocookie.net/p__/protagonist/images/3/3f/Yukiko_Amagi_(BlazBlue_Cross_Tag_Battle%2C_Character_Select_Artwork).png"))
assert_equal("https://valkyriecrusade.fandom.com/wiki/File:Crimson_Hatsune_H.png", Source::URL.page_url("https://vignette.wikia.nocookie.net/valkyriecrusade/images/c/c5/Crimson_Hatsune_H.png/revision/latest?cb=20180702031954"))
assert_equal("https://ishtaria.fandom.com/wiki/File:Union-List.png", Source::URL.page_url("https://static.wikia.nocookie.net/age-of-ishtaria/images/f/f9/Union-List.png/revision/latest/scale-to-width-down/670?cb=20141219153314"))
assert_equal("https://atelier.fandom.com/wiki/File:Marie_(Front_Page_Art).jpg", Source::URL.page_url("https://static.wikia.nocookie.net/atelierseries/images/2/22/Marie_%28Front_Page_Art%29.jpg/revision/latest/scale-to-width-down/670?cb=20130129113100"))
assert_equal("https://senrankagura.fandom.com/wiki/File:Kagurapedia_Header.png", Source::URL.page_url("https://static.wikia.nocookie.net/kagura/images/9/9f/Kagurapedia_Header.png/revision/latest/scale-to-width-down/650?cb=20171016220119"))
assert_equal("https://genshin-impact.fandom.com/wiki/File:Character_Kamisato_Ayato_Card.png", Source::URL.page_url("https://static.wikia.nocookie.net/gensin-impact/images/2/22/Character_Kamisato_Ayato_Card.png/revision/latest/scale-to-width-down/281?cb=20220204094446"))
assert_equal("https://neptunia.fandom.com/wiki/File:Plutia_B%2BNvZ.png", Source::URL.page_url("http://vignette3.wikia.nocookie.net/neptunia/images/2/28/Plutia_B%2BNvZ.png/revision/latest?cb=20160420084622"))
assert_equal("https://typemoon.fandom.com/wiki/File:Caster_Extra_Takeuchi_design_1.png", Source::URL.page_url("http://img3.wikia.nocookie.net/__cb20130523100711/typemoon/images/9/96/Caster_Extra_Takeuchi_design_1.png"))
assert_equal("https://virtualyoutuber.fandom.com/vi/wiki/File:Alicia_Cotton_2nd_Outfit_2.png", Source::URL.page_url("https://static.wikia.nocookie.net/virtualyoutuber/images/c/c4/Alicia_Cotton_2nd_Outfit_2.png/revision/latest?cb=20211107035307&path-prefix=vi"))
assert_equal("https://typemoon.fandom.com/fr/wiki/File:Aozaki_Aoko_Blue.png", Source::URL.page_url("http://img3.wikia.nocookie.net/__cb20140404214519/typemoon/fr/images/f/fd/Aozaki_Aoko_Blue.png"))
assert_equal("https://typemoon.fandom.com/fr/wiki/File:Aozaki_Aoko_Blue.png", Source::URL.page_url("http://img3.wikia.nocookie.net/__cb20140404214519/typemoon/images/f/fd/Aozaki_Aoko_Blue.png/revision/latest?path-prefix=fr"))
assert_equal("https://typemoon.fandom.com/fr/wiki/File:Aozaki_Aoko_Blue.png", Source::URL.page_url("http://img3.wikia.nocookie.net/__cb20140404214519/typemoon/fr/images/thumb/f/fd/Aozaki_Aoko_Blue.png/500px-Aozaki_Aoko_Blue.png"))
assert_equal("https://allanimefanon.fandom.com/wiki/File:2560-1600-104761.jpg", Source::URL.page_url("http://img3.wikia.nocookie.net/__cb20130520180921/allanimefanon/images/thumb/8/82/2560-1600-104761.jpg/2000px-2560-1600-104761.jpg"))
assert_equal("https://hero.fandom.com/wiki/File:Yukiko_Amagi_(BlazBlue_Cross_Tag_Battle,_Character_Select_Artwork).png", Source::URL.page_url("https://static.wikia.nocookie.net/p__/protagonist/images/3/3f/Yukiko_Amagi_(BlazBlue_Cross_Tag_Battle%2C_Character_Select_Artwork).png"))
end

should "parse URLs correctly" do
Expand Down

0 comments on commit 6823232

Please sign in to comment.