Przejdź do zawartości

Moduł:Wikidane/format/P39

Z Wikibooks, biblioteki wolnych podręczników.
 Dokumentacja modułu [stwórz] [odśwież]
local moduleData = mw.loadData("Moduł:Wikidane/data")
local infobox = mw.loadData("Moduł:Infobox/resources")

local resources = {
	timeLabel = "Okres",
	timeStart = "od ",
	timeEnd   = "do ",
	timeSep   = "<br />",
	prevLabel = "Poprzednik",
	nextLabel = "Następca",
	
	titleOptions = {
		linkItem = true,
		separator = moduleData.defaultSeparator,
		lastSeparator = moduleData.defaultSeparator,
	},

	nextPrevOptions = {
		linkItem = true,
		novalue = "—",
		separator = moduleData.defaultSeparator,
		lastSeparator = moduleData.defaultSeparator,
	},
	
	timeOptions = {
		linkDate = true,
	},

	nakedOptions = {
		naked = true,
	},
}

local function formatSimple(props, options)
	if #props <= 0 then
		return
	end
	
	local formatSnak = require("Moduł:Wikidane/format/snak").format
	local items = {}
	for k, v in ipairs(props) do
		if (v.type == "statement") and ((v.rank == "normal") or (v.rank == "preferred")) then
			if v.mainsnak and (v.mainsnak.snaktype == "value") then
				table.insert(items, formatSnak(v.mainsnak, resources.titleOptions))
			end
		end
	end

	if #items <= 0 then
		return
	end
	
	return mw.text.listToText(items, options.separator, options.lastSeparator)
end

local function formatInfobox(props, options)
	if #props <= 0 then
		return
	end
	
	local formatSnak = require("Moduł:Wikidane/format/snak").format
	local formatQualifiers = require("Moduł:Wikidane/format/qualifiers")
	local items = {}
	for k, v in ipairs(props) do
		local item = {}
		if (v.type == "statement") and ((v.rank == "normal") or (v.rank == "preferred")) then
			if v.mainsnak and (v.mainsnak.snaktype == "value") then
				local title = formatSnak(v.mainsnak, resources.titleOptions)
				item.title = formatSnak(v.mainsnak, resources.titleOptions)
				if item.title then
					local of = formatQualifiers.AND(v, "", "P642")
					if of then
						item.title = of.."<br />"..item.title
					end
					
					table.insert(items, item)
					local no = formatQualifiers.AND(v, resources.titleOptions, "P1545")
					if no and mw.ustring.match(no,"^%d+$") then
						item.title = no..". "..item.title
					end
					
					item.prev = formatQualifiers.AND(v, resources.nextPrevOptions, "P1365")
					item.next = formatQualifiers.AND(v, resources.nextPrevOptions, "P1366")
					item.s = formatQualifiers.MINTIME(v, resources.nakedOptions, "P580")
					item.stime = item.s and (item.s.snaktype == "value") and item.s.datavalue.value.time
					item.stext = item.s and formatSnak(item.s, resources.timeOptions)
					item.e = formatQualifiers.MAXTIME(v, resources.nakedOptions, "P582")
					item.etime = item.e and (item.e.snaktype == "value") and item.e.datavalue.value.time
					item.etext = item.e and formatSnak(item.e, resources.timeOptions)
				end
			end
		end
	end
	
	if #items <= 0 then
		return
	end
	
	if #items > 1 then
		local function compare(a, b)
			if a and b and a.stime and b.stime then return a.stime > b.stime end
			if a and b and a.etime and b.etime then return a.etime > b.etime end
			if a and b and a.s and not b.s then return true end
			if a and b and a.e and not b.e then return true end
			return false
		end
		
		table.sort(items, compare)
	end
	
	local result = {}
	for _, v in ipairs(items) do
		table.insert(result, '|-class="')
		table.insert(result, infobox.classSpanHeader)
		table.insert(result, '"\n!colspan="2"|')
		table.insert(result, v.title)
		table.insert(result, "\n")
		if v.stext or v.etext then
			table.insert(result, '|-\n!class="')
			table.insert(result, infobox.classRowHeader)
			table.insert(result, '"|')
			table.insert(result, resources.timeLabel)
			table.insert(result, "\n|")
			if v.stext == v.etext then
				table.insert(result, v.stext)
			else
				if v.stext then
					table.insert(result, resources.timeStart)
					table.insert(result, v.stext)
				end
				if v.stext and v.etext then
					table.insert(result, resources.timeSep)
				end
				if v.etext then
					table.insert(result, resources.timeEnd)
					table.insert(result, v.etext)
				end
			end
			table.insert(result, "\n")
		end
		if v.prev then
			table.insert(result, '|-\n!class="')
			table.insert(result, infobox.classRowHeader)
			table.insert(result, '"|')
			table.insert(result, resources.prevLabel)
			table.insert(result, "\n|")
			table.insert(result, v.prev)
			table.insert(result, "\n")
		end
		if v.next then
			table.insert(result, '|-\n!class="')
			table.insert(result, infobox.classRowHeader)
			table.insert(result, '"|')
			table.insert(result, resources.nextLabel)
			table.insert(result, "\n|")
			table.insert(result, v.next)
			table.insert(result, "\n")
		end
	end
	
	table.insert(result, "|-")
	return table.concat(result)
end

return {

scope = "props",

format = function(props, options)
	return options.format == "infobox" and formatInfobox(props, options) or formatSimple(props, options)
end,
 
}