Modulu:Aux

Wikipedia, Entziklopedia askea

Documentation for this module may be created at Modulu:Aux/dok

local p = {}

function findInTable(vObject, tObject)
	if type(tObject) == type({}) then
		for i, v in pairs(tObject) do
			if mw.dumpObject(v) == mw.dumpObject(vObject) then
				return mw.dumpObject(i)
			end
		end
	end
	return nil
end

function getArgs(tFrame, bPreferParent, bAppend)
	local t, tI, tP, tPI = {}, {}, {}, {}
	if type(tFrame) == type({}) then
		if 
			(type(tFrame.getParent) == type(type)) and 
			(type(tFrame:getParent()) == type({})) and
			(type(tFrame:getParent().args) == type({}))
		then
			for i, v in pairs(tFrame:getParent().args) do
				if
					(type(i) == type(0)) and (i == math.floor(i)) and (i > 0)
				then
					tPI[i] = v
				else
					tP[i] = v
				end
			end
		end
		if type(tFrame.args) == type({}) then
			for i, v in pairs(tFrame.args) do
				if
					(type(i) == type(0)) and (i == math.floor(i)) and (i > 0)
				then
					tI[i] = v
				else
					t[i] = v
				end
			end
		end
	end
	if bPreferParent then
		tI, tPI = tPI, tI
		t, tP = tP, t
	end
	for i, v in pairs(t) do
		tP[i] = v
	end
	t = tP
	if bAppend then
		for i = 1, table.maxn(tPI) do
			tI[table.maxn(tI) + 1] = tPI[i]
		end
	else
		for i, v in pairs(tI) do
			tPI[i] = v
		end
		tI = tPI
	end
	for i, v in pairs(tI) do
		t[i] = v
	end
	return t
end

function getError(sDisplay, sCategory)
	sDisplay = sDisplay and tostring(sDisplay) or ""
	sCategory = sCategory and "[[Category:" .. tostring(sCategory) .. "]]" or ""
	return '<span class="error">' .. sDisplay .. sCategory .. '</span>'
end

function getInfo()
	return {
		modulename = "Module:Aux";
	}
end

function getLang()
	return mw.language.getContentLanguage():getCode()
end

function inflectCase(sBase, sEtc, vNum)
	local t = {root = sBase; infix = ""; suffix = sEtc;}
	local tProp = {
		"prop";
		"proper";
		"propio";
	}
	vNum = findInTable(vNum, {
		"sg";
		"singular";
		"singulars";
		"lang";
		"language";
		"languages";
		"singularra";
		"singularrak";
		"hizk";
		"hizkuntza";
		"hizkuntzak";
	}) and 1 or vNum
	vNum = findInTable(vNum, {
		"pl";
		"plural";
		"plurals";
		"plurala";
		"pluralak"
	}) and 2 or vNum
	vNum = tonumber(vNum) or vNum
	if (mw.ustring.sub(sBase, -1) == "r") and (
		not ((sEtc == "") and (not (type(vNum) == type(0))) or (sEtc == "tzat"))
	) then
		t.root = t.root .. "r"
	end
	if (mw.ustring.sub(sBase, -1) == "a") and (
		not findInTable(sEtc, {""; "ik"; "tzat";})
	) and (type(vNum) == type(0)) and (not (vNum == 1)) then
		t.root = mw.ustring.sub(t.root, 1, -2)
	end
	if sEtc == "" then
		if type(vNum) == type(0) then
			t.infix = (mw.ustring.sub(sBase, -1) == "a") and "" or "a"
			t.infix = t.infix .. ((vNum == 1) and "" or "k")
		end
	elseif sEtc == "i" then
		if type(vNum) == type(0) then
			if vNum == 1 then
				t.infix = (mw.ustring.sub(sBase, -1) == "a") and "r" or "ar"
			else
				t.infix = "e"
			end
		else
			t.infix = testVowel(mw.ustring.sub(sBase, -1)) and "r" or ""
		end
	elseif sEtc == "n" then
		t.infix = testVowel(mw.ustring.sub(sBase, -1)) and "" or "e"
		if type(vNum) == type(0) then
			if vNum == 1 then
				t.infix = t.infix .. (
					(mw.ustring.sub(sBase, -1) == "a") and "" or "a"
				)
			else
				t.infix = "eta"
			end
		else
			t.infix = t.infix .. (
				findInTable(vNum, tProp) and "" or "ta"
			)
		end
	elseif sEtc == "a" then
		t.infix = testVowel(mw.ustring.sub(sBase, -1)) and "" or "e"
		if type(vNum) == type(0) then
			if vNum == 1 then
				t.infix = t.infix .. "r"
			else
				t.infix = "etar"
			end
		else
			t.infix = findInTable(vNum, tProp) and (
				testVowel(mw.ustring.sub(sBase, -1)) and "r" or ""
			) or (t.infix .. "ta")
		end
	elseif sEtc == "ik" then
		t.infix = testVowel(mw.ustring.sub(sBase, -1)) and "r" or ""
	elseif sEtc == "tzat" then
		if (mw.ustring.sub(sBase, -1) == "t") then
			t.root = mw.ustring.sub(sBase, -2)
		elseif (mw.ustring.sub(sBase, -2) == "tz") then
			t.root = mw.ustring.sub(sBase, -3)
		end
	elseif findInTable(sEtc, {"k"; "z";}) then
		if type(vNum) == type(0) then
			if vNum == 1 then
				t.infix = t.infix .. (
					(mw.ustring.sub(sBase, -1) == "a") and "" or "a"
				)
			else
				t.infix = "e"
				if mw.ustring.sub(sBase, -1) == "a" then
					t.root = mw.ustring.sub(t.root, 1, -2)
				end
			end
		else
			t.infix = testVowel(mw.ustring.sub(sBase, -1)) and (
				(sEtc == "z") and findInTable(vNum, tProp) and "ta" or ""
			) or "e"
		end
	elseif findInTable(sEtc, {"ko"; "tik"}) then
		if findInTable(vNum, tProp) then
			if testSoftEnd(sBase) then
				if sEtc == "ko" then
					t.suffix = "go"
				elseif sEtc == "tik" then
					t.suffix = "dik"
				end
			end
		elseif (type(vNum) == type(0)) and (not findInTable(vNum, {1;})) then
			t.infix = "eta"
		else
			t.infix = testVowel(mw.ustring.sub(sBase, -1)) and "" or "e"
			if not findInTable(vNum, {1;}) then
				t.infix = t.infix .. "ta"
			end
		end
	elseif findInTable(sEtc, {"aino"; "antz"; "ako"}) then
		if (type(vNum) == type(0)) and (not findInTable(vNum, {1;})) then
			t.infix = "etar"
		else
			t.infix = testVowel(mw.ustring.sub(sBase, -1)) and "" or "e"
			t.infix = t.infix .. (
				findInTable(vNum, tProp) or findInTable(vNum, {1;})
			) "r" or "tar"
		end
	elseif findInTable(sEtc, {
		"ekin";
		"en";
		"engan";
		"engana";
		"enganaino";
		"enganako";
		"enganantz";
		"engandik";
		"engatik";
		"entzat";
	}) then
		if findInTable(vNum, {1;}) then
			t.infix = (mw.ustring.sub(sBase, -1) == "a") and "r" or "ar"
		elseif not (type(vNum) == type(0)) then
			t.infix = testVowel(mw.ustring.sub(sBase, -1)) and "r" or ""
		end
	else
		t.infix = nil
	end
	return t
end

function testSoftEnd(sBase)
	sBase = mw.ustring.lower(sBase)
	return (
		not (not findInTable(mw.ustring.sub(sBase, -2, -2), {"l"; "m"; "n";}))
	) and testVowel(mw.ustring.sub(sBase, -1))
end

function testVowel(sLetter)
	sLetter = mw.ustring.lower(sLetter)
	return not (not findInTable(sLetter, {"a"; "e"; "i"; "o"; "u";}))
end

function writeDate(sISO, sEtc, nLength, sLink)
	if not (type(sISO) == type("")) then
		return nil
	end
	sLink = (type(sLink) == type("")) and sLink or ""
	sISO = mw.ustring.match(sISO, "%S.*")
	local sY = mw.ustring.match(sISO, "^[-]?[0-9]+")
	sY = (
		mw.ustring.gsub(sY, "[0-9]*", "") ..
		mw.ustring.gsub(mw.ustring.sub(mw.ustring.gsub(
			sY, "[^0-9]*", ""
		), 1, -2), "^0*", "") ..
		mw.ustring.sub(mw.ustring.gsub(sY, "[^0-9]*", ""), -1)
	)
	sISO = mw.ustring.gsub(sISO, "^[-]?[0-9]+", "")
	if not tonumber(sY) then
		return nil
	end
	sISO = mw.ustring.gsub(sISO, "^%s*[-]%s*", "")
	local nM = tonumber(mw.ustring.match(sISO, "^[0-9]+"))
	sISO = mw.ustring.gsub(sISO, "^[0-9]+", "")
	if not nM then
		if mw.ustring.match(sISO, "^%s*$") then
			if mw.ustring.gsub(sLink, "[^UYuy]", "") == "" then
				return writeNum(sY, sEtc, ",", "", "K.a. 0")
			else
				return (
					"[[" .. writeNum(sY, nil, ",", "", "K.a. 0") .. "|" ..
					writeNum(sY, sEtc, ",", "", "K.a. 0") .. "]]"
				)
			end
		else
			return nil
		end
	elseif nM > 12 then
		return nil
	end
	sISO = mw.ustring.gsub(sISO, "^%s*[-]%s*", "")
	local nD = tonumber(mw.ustring.match(sISO, "^[0-9]+"))
	sISO = mw.ustring.gsub(sISO, "^[0-9]+", "")
	local s = ""
	if mw.ustring.gsub(sLink, "[^UYuy]", "") == "" then
		s = s .. writeNum(sY, "ko", ",", "", "K.a. 0") .. "ko "
	else
		s = s .. (
			"[[" .. writeNum(sY, nil, ",", "", "K.a. 0") .. "|" ..
			writeNum(sY, "ko", ",", "", "K.a. 0") .. "ko]] "
		)
	end
	if not nD then
		if mw.ustring.match(sISO, "^%s*$") then
			if mw.ustring.gsub(sLink, "[^HMhm]", "") == "" then
				return s .. writeMonth(nM, sEtc, nLength)
			else
				return s .. (
					"[[" .. writeMonth(nM) .. "|" ..
					writeMonth(nM, sEtc, nLength) .. "]]"
				)
			end
		else
			return nil
		end
	elseif nD > ({
		31;
		(tonumber(mw.ustring.sub(sY,-2)) % 4 == 0) and 29 or 28;
		31;
		30;
		31;
		30;
		31;
		31;
		30;
		31;
		30;
		31;
	})[nM] then
		return nil
	else
		if mw.ustring.match(sISO, "^%s*$") then
			if mw.ustring.gsub(sLink, "[^DEHMdehm]", "") == "" then
				return s .. (
					writeMonth(nM, "en", nLength, true) .. " " ..
					writeNum(nD, sEtc)
				)
			else
				return s .. (
					"[[" .. writeMonth(nM, "en", nLength, true) .. " " ..
					writeNum(nD) .. "|" ..
					writeMonth(nM, "en", nLength, true) .. " " ..
					writeNum(nD, sEtc) .. "]]"
				)
			end
		else
			return nil
		end
	end
end

function writeLang(sLang)
	if (not sLang) or sLang == getLang() then
		return nil
	else
		sLang = mw.language.fetchLanguageName(sLang, getLang()) or sLang
	end
	return sLang
end

function writeMonth(iMonth, sEtc, nLength, bEtc)
	if
		tonumber(iMonth) and math.floor(iMonth) >= 1 and math.floor(iMonth) <=12
	then
		iMonth = math.floor(tonumber(iMonth))
	else
		return nil
	end
	local s = mw.ustring.lower(mw.getCurrentFrame():callParserFunction(
		"#time", "F", "1111-" .. iMonth, getLang()
	))
	s = mw.ustring.sub(s, 1, -2)
	if tonumber(nLength) and (mw.ustring.len(s) > tonumber(nLength)) then
		return mw.ustring.lower(mw.getCurrentFrame():callParserFunction(
			"#time", "M", "1111-" .. iMonth, getLang()
		))
	end
	local t = inflectCase(s, sEtc, 1)
	if not sEtc then
		return s
	end
	if not t.infix then
		return getError(
			(sEtc or "") .. "?",
			"Parametroak gaizki dituzten artikuluak"
		)
	end
	return t.root .. t.infix .. (bEtc and t.suffix or "")
end

function writeNum(sNum, sEtc, sDecimal, sGroup, sMinus, sPlus, sUnsigned)
	local sSign, sInt, sFrac, nExp = "", "", "", 0
	sNum = tostring(sNum)
	sNum = mw.ustring.gsub(sNum, "%s", "")
	sSign = mw.ustring.match(sNum, "^[+-]?")
	if sSign == "-" then
		sSign = sMinus and tostring(sMinus) or "-0"
	elseif sSign == "+" then
		sSign = sPlus and tostring(sPlus) or "+0"
	else
		sSign = sUnsigned and tostring(sUnsigned) or "0"
	end
	sNum = mw.ustring.gsub(sNum, "^[+-]?", "")
	sInt = mw.ustring.match(sNum, "^[0-9]*")
	sNum = mw.ustring.gsub(sNum, "^[0-9]*", "")
	if mw.ustring.match(sNum, "^[^A-Za-z]") then
		sNum = mw.ustring.gsub(sNum, "^[^A-Za-z]", "")
		sFrac = mw.ustring.match(sNum, "^[0-9]*")
		sNum = mw.ustring.gsub(sNum, "^[0-9]*", "")
	end
	if mw.ustring.match(sNum, "^[A-Za-z][+-][0-9]+") then
		sNum = mw.ustring.gsub(sNum, "^[A-Za-z]", "")
		nExp = tonumber(mw.ustring.match(sNum, "^[+-][0-9]+"))
		while math.abs(nExp) >= 500 do
			nExp = (nExp < 0 and -1 or 1) * math.floor(math.abs(nExp) / 10)
		end
	end
	if nExp > 0 then
		if nExp > mw.ustring.len(sFrac) then
			sFrac = sFrac .. mw.ustring.rep("0", nExp - mw.ustring.len(sFrac))
		end
		sInt = sInt .. mw.ustring.sub(sFrac, 1, nExp)
		sFrac = mw.ustring.sub(sFrac, nExp + 1)
	end
	if nExp < 0 then
		if -nExp > mw.ustring.len(sInt) then
			sInt = mw.ustring.rep("0", -nExp - mw.ustring.len(sInt)) .. sInt
		end
		sFrac = mw.ustring.sub(sInt, nExp) .. sFrac
		sInt = mw.ustring.sub(sInt, 1, nExp - 1)
	end
	sInt = mw.ustring.gsub(sInt, "^0*", "")
	sInt = (sInt == "") and "0" or sInt
	sNum = ""
	if sEtc then
		if mw.ustring.len(sFrac) > 0 then
			sNum = ({
				[0] = "o"; "t"; "i"; "u"; "u"; "t"; "i"; "i"; "i"; "i";
			})[(tonumber(mw.ustring.sub(sFrac, -1)) or 0) % 10]
		else
			if mw.ustring.len(mw.ustring.match(sInt, "0*$")) < 2 then
				sNum = ({
					[0] = "i"; "t"; "i"; "u"; "u"; "t"; "i"; "i"; "i"; "i";
					"r"; "a"; "i"; "u"; "u"; "t"; "i"; "i"; "i"; "i";
				})[(tonumber(mw.ustring.sub(sInt, -2)) or 0) % 20]
			elseif mw.ustring.len(mw.ustring.match(sInt, "0*$")) == 2 then
				sNum = "n"
			elseif mw.ustring.len(mw.ustring.match(sInt, "0*$")) > 5 then
				sNum = ({[0] = "o"; "r";})[math.floor(
					mw.ustring.len(mw.ustring.match(sInt, "0*$")) / 3
				) % 2]
			else
				sNum = "a"
			end
		end
		sNum = inflectCase(sNum, sEtc, 1).infix or ""
	end
	sDecimal = (
		type(sDecimal) == type("")
	) and mw.ustring.gsub(sDecimal, "[-+0-9A-Za-z]", "") or ","
	sDecimal = (sDecimal == "") and "," or sDecimal
	sGroup = (
		type(sGroup) == type("")
	) and mw.ustring.gsub(sGroup, "[-+0-9A-Za-z]", "") or ""
	sGroup = (sGroup == sDecimal) and "" or sGroup
	if mw.ustring.len(sFrac) > 0 then
		sNum = sDecimal .. sFrac .. sNum
	end
	while mw.ustring.len(sInt) > 3 do
		sNum = sGroup .. mw.ustring.sub(sInt, -3) .. sNum
		sInt = mw.ustring.sub(sInt, 1, -4)
	end
	sNum = mw.ustring.gsub(sSign, "[0-9]+", sInt .. sNum)
	return sNum
end

function p.evalAnd(frame)
	local vR = ""
	if type(frame) == type({}) then
		if 
			(type(frame.getParent) == type(type)) and 
			(type(frame:getParent()) == type({})) and
			(type(frame:getParent().args) == type({}))
		then
			for i, v in ipairs(frame:getParent().args) do
				if v and not (v == "") then
					vR = v
				else
					return ""
				end
			end
		end
		if type(frame.args) == type({}) then
			for i, v in ipairs(frame.args) do
				if v and not (v == "") then
					vR = v
				else
					return ""
				end
			end
		end
	end
	return vR
end

function p.evalOr(frame)
	if type(frame) == type({}) then
		if 
			(type(frame.getParent) == type(type)) and 
			(type(frame:getParent()) == type({})) and
			(type(frame:getParent().args) == type({}))
		then
			for i, v in ipairs(frame:getParent().args) do
				if v and not (v == "") then
					return v
				end
			end
		end
		if type(frame.args) == type({}) then
			for i, v in ipairs(frame.args) do
				if v and not (v == "") then
					return v
				end
			end
		end
	end
	return ""
end

function p.findValue(frame)
	local tArg = getArgs(frame)
	if tArg["value"] then
		return findInTable(tArg["value"], tArg) or ""
	end
	return ""
end

function p.formatDate(frame)
	local tArg = {}
	if type(frame) == type({}) then
		if 
			(type(frame.getParent) == type(type)) and 
			(type(frame:getParent()) == type({})) and
			(type(frame:getParent().args) == type({}))
		then
			for i, v in pairs(frame:getParent().args) do
				tArg[i] = v
			end
		end
		if type(frame.args) == type({}) then
			for i, v in pairs(frame.args) do
				tArg[i] = v
			end
		end
	end
	if tArg[2] then
		if tArg[2] == "" then
			tArg[2] = nil
		else
			tArg[2] = mw.ustring.gsub(tArg[2], "^[-]", "")
		end
	end
	return writeDate(tArg[1], tArg[2], tArg[3], tArg[4])
end

function p.formatNum(frame)
	local tArg = {}
	if type(frame) == type({}) then
		if 
			(type(frame.getParent) == type(type)) and 
			(type(frame:getParent()) == type({})) and
			(type(frame:getParent().args) == type({}))
		then
			for i, v in pairs(frame:getParent().args) do
				tArg[i] = v
			end
		end
		if type(frame.args) == type({}) then
			for i, v in pairs(frame.args) do
				tArg[i] = v
			end
		end
	end
	if tArg[2] then
		if tArg[2] == "" then
			tArg[2] = nil
		else
			tArg[2] = mw.ustring.gsub(tArg[2], "^[-]", "")
		end
	end
	return writeNum(
		tArg[1], tArg[2], tArg[3], tArg[4], tArg[5], tArg[6], tArg[7]
	)
end

function p.getWebRef(frame)
	local tArg = getArgs(frame)
	if tArg["url"] and tArg["title"] and writeDate(tArg["accessdate"]) then
		local vR = ""
		if tArg["author"] then
			vR = vR .. tArg["author"] .. ": "
		end
		vR = vR .. "''[" .. tArg["url"] .. " " .. tArg["title"] .. "]''"
		if tArg["cotitle"] then
			vR = vR .. " " .. tArg["cotitle"]
		end
		vR = vR .. "."
		if tArg["work"] then
			vR = vR .. " Lanan: ''" .. tArg["work"] .. "''."
		end
		if tArg["publisher"] then
			vR = vR .. " " .. tArg["publisher"] .. "."
		end
		if tArg["pages"] then
			vR = vR .. " Or. " .. tArg["pages"] .. "."
		end
		if writeLang(tArg["language"]) or tArg["format"] then
			local s = ""
			if writeLang(tArg["language"]) then
				s = s .. writeLang(tArg["language"])
			end
			if tArg["format"] then
				s = s .. (s == "" and "" or ", ") .. tArg["format"]
			end
			vR = vR .. " (" .. s .. ")"
		end
		if writeDate(tArg["date"]) then
			vR = vR .. " " .. writeDate(tArg["date"], "") .. "."
		end
		vR = vR .. " Sarbidea " .. writeDate(tArg["accessdate"], "n") .. "n."
		if tArg["archiveurl"] then
			local s = ""
			if writeDate(tArg["archivedate"]) then
				s = s .. " " .. writeDate(tArg["archivedate"], "n") .. "n"
			end
			vR = vR .. s .." [" .. tArg["archiveurl"] .. (
				(s == "") and " A" or " a"
			) .. "rtxibatua]."
		end
		if tArg["quote"] then
			vR = vR .. " «" .. tArg["quote"] .. "»"
		end
		if tArg["comment"] then
			vR = vR .. " " .. tArg["comment"]
		end
		return vR
	else
		return getError(
			"[[" .. getInfo().modulename .. "]]",
			"Parametroak gaizki dituzten artikuluak"
		)
	end
end

function p.inflectCase(frame)
	local tArg = getArgs(frame)
	local t = inflectCase(tArg[1], tArg[2], tArg[3])
	if not t.infix then
		return getError(
			tArg[2] .. "?",
			"Parametroak gaizki dituzten artikuluak"
		)
	end
	tArg["part"] = tArg["part"] or tArg["zati"]
	tArg["part"] = tonumber(tArg["part"]) or tArg["part"]
	if findInTable(tArg["part"], {1; "root"; "erro"; "erroa";}) then
		return t.root
	elseif findInTable(tArg["part"], {2; "infix"; "artizki"; "artizkia";}) then
		return t.infix
	elseif findInTable(tArg["part"], {3; "suffix"; "atzizki"; "atzizkia";}) then
		return t.suffix
	end
	return t.root .. t.infix .. t.suffix
end

function p.supplyWebRef(frame)
	local tArg = getArgs(frame)
	local t = {
		sub = getInfo().modulename .. "/Refs";
	}
	if tArg[1] then
		repeat
			t.subname = t.sub
			t.sub = require(t.sub)
			t.sub = (type(t.sub) == type(type)) and t.sub
			if t.sub then
				if (type(t.sub(tArg[1])) == type({})) then
					return getWebRef({args = t.sub(tArg[1])})
				end
				t.sub = (type(t.sub(tArg[1])) == type("")) and t.sub(tArg[1])
			end
		until not t.sub
	end
	t.sub = t.subname or t.sub
	return getError(
		"[[" .. t.sub .. "]]",
		"Parametroak gaizki dituzten artikuluak"
	)
end

function p.unstripNoWikiRow(frame)
	local tArg = getArgs(frame)
	local s = ""
	if table.maxn(tArg) > 0 then
		local i = 0
		tArg["from"] = tArg["from"] or tArg["handik"]
		tArg["from"] = tonumber(tArg["from"]) or 1
		tArg["from"] = math.max(1, math.min(math.floor(
			tArg["from"]
		), table.maxn(tArg)))
		tArg["to"] = tArg["to"] or tArg["hara"]
		tArg["to"] = tonumber(tArg["to"]) or 1
		tArg["to"] = math.max(0, math.min(math.floor(
			tArg["to"]
		), table.maxn(tArg)))
		s = "|-"
		repeat
			if i > 0 then
				tArg[i] = tArg[i] or ""
				s = s .. "\n| " .. (
					(i == tArg["from"]) and
					mw.text.tag("tt", {}, tArg[i]) or
					tArg[i]
				) .. " "
			end
			if i == tArg["to"] then
				s = s .. "\n| " .. mw.getCurrentFrame():preprocess(
					mw.text.unstripNoWiki(tArg[tArg["from"]] or "")
				) .. " "
			end
			i = i + 1
		until i > table.maxn(tArg)
	end
	return s
end

return p