Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:Sandbox/Ahecht/interwiki

From Wikipedia, the free encyclopedia

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)
}