Module:InterwikiLink

-- -- @authors Noreplyz

-- --[[                             Module:InterwikiLink

Handles the various ways people enter URLs and wikinames when they want to link two wikis together.

--]] --

local InterwikiLink = {} local getArgs = require('Dev:Arguments').getArgs local wikialang = require('Module:InterwikiLink/wikialang')

local DEFAULT_DOMAIN = 'fandom'

-- remove excess or wrongly entered content local function trimURL(s) s = s:match( "^%s*(.-)%s*$" ) s = s:gsub("http:%/%/","") s = s:gsub("https:%/%/","") s = s:gsub("%/+$", "/") return s end

-- Splits wiki, domain and language from a full URL -- example: http://community.fandom.com/es/ function InterwikiLink._splitWikiFromURL(url) url = trimURL(url) .. '/'

-- try wikia.com parse local domain = 'wikia' local res, _, lang, wikiname = string.find(url, '([^%.]-)%.*([^%.]*)%.wikia%.com.*') -- try fandom.com parse if not res then domain = 'fandom' res, _, wikiname, lang = string.find(url, '([^%.]-)%.fandom%.com%/*([^%/]*)%/*.*') end -- try wikia.org parse if not res then domain = 'org' res, _, wikiname, lang = string.find(url, '([^%.]-)%.wikia%.org%/*([^%/]*)%/*.*') end -- revert to the default: wikia if not res then domain = DEFAULT_DOMAIN wikiname = url end -- Force CC to be wikia.com if wikiname == 'community' or wikiname == 'c' then domain = 'wikia' end if lang ~= '' and not wikialang[lang] then -- deal with situations like naruto.answers.wikia.com if lang ~= 'wiki' and domain == 'wikia' then wikiname = lang .. '.' .. wikiname end lang = '' end return {wikiname, domain, lang} end

-- Splits wiki, domain and language from a standard entry -- example: es.community, fandom function InterwikiLink._splitWikiFromPartial(wiki) local res, _, lang, wikiname = string.find(wiki, '([^%.]-)%.*([^%.]-)$') if not res then lang = '' wikiname = wiki end if lang ~= '' and not wikialang[lang] then -- deal with situations like naruto.answers.wikia.com if lang ~= 'wiki' then wikiname = lang .. '.' .. wikiname end lang = '' end return {wikiname, lang} end

-- Returns a wiki URL based on domain and language function InterwikiLink._getWikiURL(wiki, domain, lang) if domain == 'fandom' then if lang == '' then return wiki .. '.fandom.com' else return wiki .. '.fandom.com/' .. lang end elseif domain == 'org' then if lang == '' then return wiki .. '.wikia.org' else return wiki .. '.wikia.org/' .. lang end else if lang == '' then if string.find(wiki, '%.') then return wiki .. '.wikia.com' end return wiki .. '.wikia.com' else return lang .. '.' .. wiki .. '.wikia.com' end end end

-- Domain is one of [wikia, fandom, org] function InterwikiLink._getWikiComponents(wiki, domain) local lang wiki = wiki or 'WIKI' -- es.community.wikia.com or community.fandom.com/es if string.match(wiki, 'wikia%.com') or string.match(wiki, 'fandom%.com') or string.match(wiki, 'wikia%.org') then if domain then wiki, _, lang = unpack(InterwikiLink._splitWikiFromURL(wiki)) else wiki, domain, lang = unpack(InterwikiLink._splitWikiFromURL(wiki)) end -- es.community elseif domain then if not domain == 'wikia' and not domain == 'fandom' and not domain == 'org' then domain = DEFAULT_DOMAIN end wiki, lang = unpack(InterwikiLink._splitWikiFromPartial(wiki)) -- deal with situations like naruto.answers.wikia.com local res, _, originalwiki = string.find(wiki, '%.([^%.]-)$') if domain ~= 'wikia' and res then wiki = originalwiki end else wiki, lang = unpack(InterwikiLink._splitWikiFromPartial(wiki)) domain = DEFAULT_DOMAIN end

local wikiurl = InterwikiLink._getWikiURL(wiki, domain, lang) local partial = wiki if lang ~= '' then partial = lang .. '.' .. wiki end return {wikiurl, partial, wiki, domain, lang} end

function InterwikiLink.link(frame) local args = getArgs(frame) local wiki1 = args[1] local wiki2 = args[2] local domain1 = args[3] local domain2 = args[4]

if not wiki1 or not wiki2 then return ' Error: Please enter at least two wikis. Check again! '   end local wikiurl1, partial1 = unpack(InterwikiLink._getWikiComponents(args[1], args[3])) local wikiurl2, partial2 = unpack(InterwikiLink._getWikiComponents(args[2], args[4]))

local ret = {}

ret[#ret + 1] = ' to ' ret[#ret + 1] = ' ' ret[#ret + 1] = '  ' ret[#ret + 1] = '(.. wikiurl1 .. '&ext_wikia=' .. wikiurl2 .. ' link wikis)' return table.concat(ret) end

return InterwikiLink --