Moduuli:Cs:Wikidata/cite

Wikipediasta
Siirry navigaatioon Siirry hakuun

require "Module:No globals"

local p = {}

local lib = require 'Module:cs:Wikidata/lib'

p.props = {
	accessdate = { 'P813' },
	archivedate = { 'P2960' }, -- todo
	archiveurl = { 'P1065' }, -- todo
	author = { 'P50' },
	date = { 'P577' },
	editor = { 'P90' }, -- todo
	ilustration = { 'P110' }, -- todo
	lang = { 'P364', 'P407' }, -- todo
	pages = { 'P304' }, -- todo
	place = { 'P291' },
	publisher = { 'P123' }, -- todo
	title = { 'P1476' },
	type = { 'P31' }, -- todo
	url = { 'P854', 'P953' },
	work = { 'P248' },
}

local function dataToContent(data)
	local content = {}
	local Formatters = require 'Module:cs:Wikidata/Formatters'
	if data.author then
		local authors = {}
		for _, snak in ipairs(data.author) do
			table.insert(authors, Formatters.getFormattedValue(snak, {}))
		end
		table.insert(content, table.concat(authors, ', ') .. ':')
	end
	if data.title or data.work then
		local title
		if data.title then
			title = Formatters.getFormattedValue(data.title[1], {})
		end
		if data.work then
			title = Formatters.getFormattedValue(data.work[1], { text = title }) -- TODO
		end
		table.insert(content, mw.ustring.format("''%s''.", title))
	end
	if data.place then
		local places = {}
		for _, snak in ipairs(data.place) do
			table.insert(places, Formatters.getFormattedValue(snak, {}))
		end
		table.insert(content, table.concat(places, ', ') .. '.')
	end
	if data.date then
		local date = Formatters.getFormattedValue(data.date[1], { nolink = true }) .. '.'
		table.insert(content, date)
	end
	if data.url then
		local url = Formatters.getFormattedValue(data.url[1], { ['value-formatter'] = 'url', text = 'Dostupné online' }) .. '.'
		table.insert(content, url)
	elseif data.external then
		local url = Formatters.getFormattedValue(data.external[1], {
			autoformat = true, property = data.external[1].property, text = 'Dostupné online' -- todo: i18n
		}) .. '.'
		table.insert(content, url)
	end
	if data.accessdate then
		local date = Formatters.getRawValue(data.accessdate[1], {}):toString()
		table.insert(content, mw.ustring.format('[cit. %s]', date))
	end
	return table.concat(content, ' ')
end

local function dataFromItem(id, data)
	local entity = mw.wikibase.getEntity(id)
	if entity and entity.claims then
		for key, props in pairs(p.props) do
			if not data[key] then
				data[key] = {}
				for _, prop in ipairs(props) do
					if entity.claims[prop] then
						for _, statement in ipairs(entity.claims[prop]) do
							if lib.IsSnakValue(statement.mainsnak) then
								table.insert(data[key], statement.mainsnak)
							end
						end
					end
					if #data[key] > 0 then
						break
					end
				end
				if #data[key] == 0 then
					data[key] = nil
				end
			end
		end
	end
end

function p.formatReferences(references, options)
	local frame = mw.getCurrentFrame()
	local Formatters = require 'Module:cs:Wikidata/Formatters'
	local valid_refs = {}
	local limit = tonumber(options.max_ref)
	for _, ref in ipairs(references) do
		local data = {}
		for key, props in pairs(p.props) do
			data[key] = {}
			for _, prop in ipairs(props) do
				if ref.snaks[prop] then
					for _, snak in ipairs(ref.snaks[prop]) do
						if lib.IsSnakValue(snak) then
							table.insert(data[key], snak)
						end
					end
				end
				if #data[key] > 0 then
					break
				end
			end
			if #data[key] == 0 then
				data[key] = nil
			end
		end
		for prop, snaks in pairs(ref.snaks) do
			if snaks[1].datatype == 'external-id' or prop == 'P627' then --fixme
				data.external = { snaks[1] }
				break
			end
		end
		if data.work then -- P248
			local id = Formatters.getRawValue(data.work[1], {})
			dataFromItem(id, data)
		end
		local ref_content = dataToContent(data)
		if ref_content ~= '' then
			if lib.IsOptionTrue(options, 'addclass') then
				ref_content = lib.addWdClass(ref_content)
			end
			if lib.IsOptionTrue(options, 'addlink') then
				-- TODO
			end
			table.insert(valid_refs, frame:extensionTag('ref', ref_content, { name = ref.hash }))
		end
		if limit and #valid_refs == limit then
			break
		end
	end
	return table.concat(valid_refs)
end

return p