Module:Urltowiki
Appearance
-- This module takes a URL from a Wikimedia project and returns the equivalent wikitext.
-- Any actions such as edit, history, etc., are stripped, and percent-encoded characters
-- are converted to normal text.
p = {}
local interwiki_table = {
-- Projects.
{ domain = "wikipedia.org" , iw_prefix = "w" , title_prefix = "/wiki/" , takes_lang_prefix = true },
{ domain = "wiktionary.org" , iw_prefix = "wikt" , title_prefix = "/wiki/" , takes_lang_prefix = true },
{ domain = "wikinews.org" , iw_prefix = "n" , title_prefix = "/wiki/" , takes_lang_prefix = true },
{ domain = "wikibooks.org" , iw_prefix = "b" , title_prefix = "/wiki/" , takes_lang_prefix = true },
{ domain = "wikiquote.org" , iw_prefix = "q" , title_prefix = "/wiki/" , takes_lang_prefix = true },
{ domain = "wikisource.org" , iw_prefix = "s" , title_prefix = "/wiki/" , takes_lang_prefix = true },
{ domain = "species.wikimedia.org" , iw_prefix = "species" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "wikiversity.org" , iw_prefix = "v" , title_prefix = "/wiki/" , takes_lang_prefix = true },
{ domain = "wikivoyage.org" , iw_prefix = "voy" , title_prefix = "/wiki/" , takes_lang_prefix = true },
{ domain = "wikimediafoundation.org" , iw_prefix = "wmf" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "commons.wikimedia.org" , iw_prefix = "commons" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "wikidata.org" , iw_prefix = "d" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "meta.wikimedia.org" , iw_prefix = "m" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "incubator.wikimedia.org" , iw_prefix = "incubator" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "strategy.wikimedia.org" , iw_prefix = "strategy" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "mediawiki.org" , iw_prefix = "mw" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "bugzilla.wikimedia.org" , iw_prefix = "bugzilla" , title_prefix = "/show_bug.cgi?id=" , takes_lang_prefix = false },
{ domain = "test.wikipedia.org" , iw_prefix = "testwiki" , title_prefix = "/wiki/" , takes_lang_prefix = false },
-- Chapters.
{ domain = "wikimedia.org.ar" , iw_prefix = "wmar" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "wikimedia.org.au" , iw_prefix = "wmau" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "be.wikimedia.org" , iw_prefix = "wmbe" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "wikimedia.ca" , iw_prefix = "wmca" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "wikimedia.de" , iw_prefix = "wmde" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "fi.wikimedia.org" , iw_prefix = "wmfi" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "wikimedia.hk" , iw_prefix = "wmhk" , title_prefix = "/index.php/" , takes_lang_prefix = false },
{ domain = "wikimedia.hu" , iw_prefix = "wmhu" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "wiki.wikimedia.in" , iw_prefix = "wmin" , title_prefix = "/" , takes_lang_prefix = false },
{ domain = "wikimedia.org.id" , iw_prefix = "wmid" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "wikimedia.org.il" , iw_prefix = "wmil" , title_prefix = "/" , takes_lang_prefix = false },
{ domain = "wikimedia.it" , iw_prefix = "wmit" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "nl.wikimedia.org" , iw_prefix = "wmnl" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "no.wikimedia.org" , iw_prefix = "wmno" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "pl.wikimedia.org" , iw_prefix = "wmpl" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "no.wikimedia.org" , iw_prefix = "wmno" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "ru.wikimedia.org" , iw_prefix = "wmru" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "rs.wikimedia.org" , iw_prefix = "wmrs" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "se.wikimedia.org" , iw_prefix = "wmse" , title_prefix = "/wiki/" , takes_lang_prefix = false },
{ domain = "wikimedia.ch" , iw_prefix = "wmch" , title_prefix = "/" , takes_lang_prefix = false },
{ domain = "tw.wikimedia.org" , iw_prefix = "wmtw" , title_prefix = "/wiki/index.php5/" , takes_lang_prefix = false },
{ domain = "uk.wikimedia.org" , iw_prefix = "wmuk" , title_prefix = "/wiki/" , takes_lang_prefix = false },
}
local function getHostId(host)
if type(host) ~= "string" then
error("Non-string value for host name detected.")
end
-- Find the entry for the host in the interwiki table.
local id
for i,v in ipairs(interwiki_table) do
if mw.ustring.match(host, interwiki_table[i].domain) then
id = i
break
end
end
if not id then return end
return id
end
local function getInterwiki(host)
-- This function returns the value of the interwiki prefix.
if type(host) ~= "string" then
error("Non-string value for host name detected.")
end
local host_id = getHostId(host)
if not host_id then return end -- If the host isn't valid then the language and the interwiki are also invalid.
-- Find the language in the interwiki prefix, if applicable.
local lang = mw.ustring.match(host, "^(.-)%.") -- Find the text before the first period.
if not lang or not mw.language.isSupportedLanguage(lang) then -- Check if lang is a valid language code.
lang = false
end
-- A language prefix is not necessary if there is already a language prefix for the host in the interwiki table.
local domain_lang = mw.ustring.match(interwiki_table[host_id].domain, "^(.-)%.") -- Find the text before the first period.
if mw.language.isSupportedLanguage(domain_lang) then
lang = false
end
-- No need for an interwiki link if we are on the same site as the URL.
local current_host = mw.uri.new(mw.title.getCurrentTitle():fullUrl()).host -- Get the host portion of the current page URL.
if host == current_host then
return nil, lang, host_id
end
-- Check if the URL language is the same as the current language.
local same_lang
if lang and lang == mw.ustring.match(current_host, "^(.-)%.") then
same_lang = true
end
-- Check if the project is the same as the current project (but a different language).
local current_host_id = getHostId(current_host)
local same_project
if current_host_id == host_id then
same_project = true
end
-- Return the interwiki prefix, omitting the language or the project code if
-- it is not necessary.
local project = interwiki_table[host_id].iw_prefix
if same_lang or ( not lang and interwiki_table[host_id].takes_lang_prefix == false ) then
return project, lang, host_id
elseif same_project then
return lang, lang, host_id
elseif not lang then -- If the language code is bad but the rest of the host name is ok.
return nil, nil, host_id
else
return project .. ":" .. lang, lang, host_id
end
end
local function _urlToWiki(args)
local url = args[1] or error("No URL specified")
url = mw.ustring.match(url, "^%s*(.*%S)") or "" -- Trim whitespace.
url = mw.uri.new(url)
local host = url.host -- Get the host name.
-- Get the interwiki prefix. Lang and host_id are passed through so we don't have to work them out again.
local interwiki, lang, host_id = getInterwiki(host)
-- Get the page title.
local pagetitle, title_prefix
if host_id and not ( interwiki_table[host_id].takes_lang_prefix == true and not lang ) then
title_prefix = interwiki_table[host_id].title_prefix
end
-- If the URL path starts with the title prefix in the interwiki table, use that to get the title.
if title_prefix and mw.ustring.sub(url.path, 1, mw.ustring.len(title_prefix)) == title_prefix then
pagetitle = mw.ustring.sub(url.path, mw.ustring.len(title_prefix) + 1, -1)
-- Else, if the URL is a history "index.php", use url.query.title. Check for title_prefix
-- in case the URL isn't of a Wikimedia site.
elseif title_prefix and mw.ustring.match(url.path, "index%.php") and url.query.title then
pagetitle = url.query.title
-- Otherwise, use the whole URL as the title.
else
pagetitle = tostring(url)
end
-- Get the fragment and pre-process percent-encoded characters.
local fragment = url.fragment
if fragment then
fragment = mw.ustring.gsub(fragment, "%.([0-9A-F][0-9A-F])", "%%%1")
end
-- Assemble the wikilink.
local wikitext = pagetitle
if interwiki then
wikitext = interwiki .. ":" .. wikitext
end
if fragment and not (args.section == "no") then
wikitext = wikitext .. "#" .. fragment
end
-- Decode percent-encoded characters and convert underscores to spaces.
wikitext = mw.uri.decode(wikitext, "WIKI")
-- Use the [[Help:Colon trick]] with categories, interwikis, and files.
local colon_prefix = mw.ustring.match(wikitext, "^(.-):.*$") or "" -- Get the text before the first colon.
local current_lang = mw.language.getContentLanguage()
local ns = mw.site.namespaces
local need_colon_trick
if mw.language.isSupportedLanguage(colon_prefix) -- Check for interwiki links.
or current_lang:lc(ns[6].name) == current_lang:lc(colon_prefix) -- Check for files.
or current_lang:lc(ns[14].name) == current_lang:lc(colon_prefix) then -- Check for categories.
need_colon_trick = true
end
for i,v in ipairs(ns[6].aliases) do -- Check for file namespace aliases.
if current_lang:lc(v) == current_lang:lc(colon_prefix) then
need_colon_trick = true
break
end
end
for i,v in ipairs(ns[14].aliases) do -- Check for category namespace aliases.
if current_lang:lc(v) == current_lang:lc(colon_prefix) then
need_colon_trick = true
break
end
end
if need_colon_trick then
wikitext = ":" .. wikitext
end
return wikitext
end
function p.urlToWiki(frame)
local args
if frame == mw.getCurrentFrame() then
-- We're being called via #invoke. If the invoking template passed any args, use
-- them. Otherwise, use the args that were passed into the template.
args = frame:getParent().args
for k, v in pairs(frame.args) do
args = frame.args
break
end
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
end
return _urlToWiki(args)
end
return p