Usage edit source

isCurrentIW edit source

{{#invoke:Sandbox/Ahecht/interwiki|isCurrentIW|page name}}

{{#invoke:Sandbox/Ahecht/interwiki|isCurrentIW|Main Page}}: false

{{#invoke:Sandbox/Ahecht/interwiki|isCurrentIW|en:Main Page}}: false

{{#invoke:Sandbox/Ahecht/interwiki|isCurrentIW|c:Main Page}}: false

ifCurrentIW edit source

{{#invoke:Sandbox/Ahecht/interwiki|ifCurrentIW|page name|text if true|text if false}}

{{#invoke:Sandbox/Ahecht/interwiki|ifCurrentIW|c:Main Page|Interwiki link to the current wiki|Not an interwiki link to the current wiki}}: Not an interwiki link to the current wiki

{{#invoke:Sandbox/Ahecht/interwiki|ifCurrentIW|w:Main Page|Interwiki link to the current wiki|Not an interwiki link to the current wiki}}: Not an interwiki link to the current wiki

{{#invoke:Sandbox/Ahecht/interwiki|ifCurrentIW|Main Page|Why are you using an interwiki here?}}:


isValidIW edit source

{{#invoke:Sandbox/Ahecht/interwiki|isValidIW|page name|local=true/false}}

{{#invoke:Sandbox/Ahecht/interwiki|isValidIW|Main Page}}: false

{{#invoke:Sandbox/Ahecht/interwiki|isValidIW|de:Main Page}}: false

{{#invoke:Sandbox/Ahecht/interwiki|isValidIW|fakewiki:Main Page}}: false

{{#invoke:Sandbox/Ahecht/interwiki|isValidIW|openstreetmap:Main Page}}: false

{{#invoke:Sandbox/Ahecht/interwiki|isValidIW|openstreetmap:Main Page|local=true}}: false

{{#invoke:Sandbox/Ahecht/interwiki|isValidIW|c:Main Page|local=true}}: false

ifValidIW edit source

{{#invoke:Sandbox/Ahecht/interwiki|ifValidIW|page name|text if true|text if false|local=true/false}}

{{#invoke:Sandbox/Ahecht/interwiki|ifValidIW|c:Main Page|Interwiki link to a valid wiki|Not an interwiki link to a valid wiki}}: Not an interwiki link to a valid wiki

{{#invoke:Sandbox/Ahecht/interwiki|ifValidIW|openstreetmap:Main Page|Interwiki link to a valid local wiki|Not an interwiki link to a valid local wiki|local=true}}: Not an interwiki link to a valid local wiki

{{#invoke:Sandbox/Ahecht/interwiki|ifValidIW|Main Page|Interwiki link to a valid wiki}}:


current edit source

{{#invoke:Sandbox/Ahecht/interwiki|current|sep=separator between entries}}

{{#invoke:Sandbox/Ahecht/interwiki|current}}:


{{#invoke:Sandbox/Ahecht/interwiki|current|sep={{break}}}}:


map edit source

{{#invoke:Sandbox/Ahecht/interwiki|map|sep=separator between entries|local=true/false}}

{{#invoke:Sandbox/Ahecht/interwiki|map}}: meatball, uncyclopedia, senseislibrary, acronym, dictionary, s23wiki, wikidata, mw, metawiki, twiki, squeak, wikt, wikiversity, hrwiki, imdb, cache, doi, kmwiki, mozillawiki, metawikimedia, c2find, pmid, wiki, wikif1, freebsdman, theopedia, wikispecies, arxiv, lqwiki, linuxwiki, unreal, commons, elibre, oeis, wikibooks, wikipedia, drumcorpswiki, tmnet, wikinfo, dwjwiki, pythoninfo, wikia, wikivoyage, gentoo-wiki, wikisource, wikihow, google, wikiquote, wikinews, tmbw, memoryalpha, wiktionary, lojban, wikimedia, shoutwiki, seattlewireless, advogato, rfc, openwiki, mediawikiwiki, usemod, hammondwiki, googlegroups, foxwiki, foldoc, emacswiki

{{#invoke:Sandbox/Ahecht/interwiki|map|local=true|sep=; }}:



local p = {}

function p._current()
	out = {}
	for k, v in pairs(mw.site.interwikiMap("local")) do
		if v.isCurrentWiki then
			table.insert(out, k)
		end
	end
	return out
end

function p._map(isLocal)
	isLocal = isLocal and "local" or nil
	out = {}
	for k, v in pairs(mw.site.interwikiMap(isLocal)) do
		table.insert(out, k)
	end
	return out
end

function p._isCurrentIW(title)
	for _, v in ipairs(p._current()) do
		pattern = "^:?" .. v .. ":"
		if mw.ustring.match(title, pattern) then
			return true
		end
	end
	return false
end

function p._isValidIW(title, isLocal)
	for _, v in ipairs(p._map(isLocal)) do
		pattern = "^:?" .. v .. ":"
		if mw.ustring.match(title, pattern) then
			return true
		end
	end
	return false
end


local function current(args)
	return table.concat(p._current(), (args.sep or ", "))
end

local function map(args)
	return table.concat(p._map(args["local"]), (args.sep or ", "))
end

local function isCurrentIW(args)
	return p._isCurrentIW((args[1] or ""))
end

local function ifCurrentIW(args)
	if p._isCurrentIW((args[1] or "")) then
		return args[2] or ""
	else
		return args[3] or ""
	end
end

local function isValidIW(args)
	return p._isValidIW((args[1] or ""), args["local"])
end

local function ifValidIW(args)
	if p._isValidIW((args[1] or ""), args["local"]) then
		return args[2] or ""
	else
		return args[3] or ""
	end
end

local function makeWrapper(func)
    return function (frame)
        -- If called via #invoke, use the args passed into the invoking template.
        -- Otherwise, for testing purposes, assume args are being passed directly in.
        local origArgs
        if frame == mw.getCurrentFrame() then
            origArgs = frame:getParent().args
            for k, v in pairs(frame.args) do
                origArgs = frame.args
                break
            end
        else
            origArgs = frame
        end
 
        -- Strip whitespace, and treat blank arguments as nil.
        -- 'sep' has different behaviour depending on whether
        -- it is blank or nil, so keep it as it is.
        -- isLocal needs to be converted from a string to a bool.
        local args = {}
        for k, v in pairs(origArgs) do
            v = mw.text.trim(v)
            if v ~= '' or k == 'sep' then
                if k == 'local' then
                	t = {["true"]=true,["false"]=false}
                	args[k] = (t[v] or v==true)
                else
                	args[k] = v
                end
            end
        end
    
        return func(args)
    end
end

return {
    current = makeWrapper(current),
    map = makeWrapper(map),
    isCurrentIW = makeWrapper(isCurrentIW),
    ifCurrentIW = makeWrapper(ifCurrentIW),
    isValidIW = makeWrapper(isValidIW),
    ifValidIW = makeWrapper(ifValidIW)
}