Module:Wikidata/citetest

Από τη Βικιπαίδεια, την ελεύθερη εγκυκλοπαίδεια
Documentation icon Τεκμηρίωση module[δημιουργία]
--require 'Module:No globals'

local p = {}

local Wikidata = require 'Module:Wikidata'

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

local function dataToContent(data)
	local content = {}
	if data.author then
		local authors = {}
		for _, snak in ipairs(data.author) do
			table.insert(authors, Wikidata.getDatavalue(snak, { link = 'wikipedia' }))
		end
		table.insert(content, table.concat(authors, ', ') .. ':')
	end
	if data.title then
		local title = Wikidata.getDatavalue(data.title[1], {})
		if data.archiveurl then
			title = Wikidata.getDatavalue(data.archiveurl[1], { displayformat = 'weblink', showntext = '«' .. title .. '»' }) .. '.'
		elseif data.url then
			title = Wikidata.getDatavalue(data.url[1], { displayformat = 'weblink', showntext = '«' .. title .. '»' }) .. '.'
		elseif data.work then
			local function displayformat(snak, params)
				local id = snak.datavalue.value.id
				return '[[' .. (mw.wikibase.sitelink(id) or ('d:' .. id)) .. '|«' .. title .. '»]]'
			end
			title = Wikidata.getDatavalue(data.work[1], { displayformat = displayformat })
		end
		table.insert(content, title)
	end
		if data.lang then
		local langs = {}
		local displayformat = function(snak, params)
			local id = snak.datavalue.value.id
			return mw.wikibase.label(id)
		end
		for _, lang in ipairs(data.lang) do
			table.insert(langs, Wikidata.getDatavalue(lang, { link = 'wikipedia' }))
		end
		table.insert(content, '(' .. table.concat(langs, ', ') .. ')')
	end
	if data.work and (not data.title or data.archiveurl or data.url) then
		local title = Wikidata.getDatavalue(data.work[1], { link = 'wikipedia' })
		table.insert(content, mw.ustring.format("''%s''.", title))
	end
	if data.publisher then
		local publishers = {}
		for _, snak in ipairs(data.publisher) do
			table.insert(publishers, Wikidata.getDatavalue(snak, { link = 'wikipedia' }))
		end
		table.insert(content, table.concat(publishers, ', ') .. '.')
	end
	if data.place then
		local places = {}
		for _, snak in ipairs(data.place) do
			table.insert(places, Wikidata.getDatavalue(snak, { link = 'wikipedia' }))
		end
		table.insert(content, table.concat(places, ', ') .. '.')
	end
	if data.date then
		local date = Wikidata.getDatavalue(data.date[1], { type = 'dateobject' }) .. '.'
		table.insert(content, date)
	end
	if not data.title and data.url then
		local url = Wikidata.getDatavalue(data.url[1], { displayformat = 'weblink' }) .. '.'
		table.insert(content, url)
	elseif data.external then
		local url = Wikidata.getDatavalue(data.external[1], {
			urlpattern = p._formatStatements{ id = data.external[1].property, property = 'P1630', displayformat = 'raw' }
		}) .. '.'
		table.insert(content, url)
	end
	if data.archivedate and data.url and data.archiveurl then
		local url = Wikidata.getDatavalue(data.url[1], { displayformat = 'weblink', showntext = 'το πρωτότυπο' })
		local date = Wikidata.getDatavalue(data.archivedate[1], { type = 'dateobject' })
		table.insert(content, mw.ustring.format('Αρχειοθετήθηκε από %s στις %s.', url, date))
	end
	if data.accessdate then
		local date = Wikidata.getDatavalue(data.accessdate[1], { type = 'dateobject' })
		table.insert(content, mw.ustring.format('Ανακτήθηκε στις %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 statement.mainsnak.snaktype == 'value' 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 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 snak.snaktype == 'value' 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 = Wikidata.getRawvalue(data.work[1])
			dataFromItem(id, data)
		end
		local ref_content = dataToContent(data)
		if ref_content ~= '' then
			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