Μετάβαση στο περιεχόμενο

Module:Wikidata/cite

Από τη Βικιπαίδεια, την ελεύθερη εγκυκλοπαίδεια
Αυτή είναι μια παλιά έκδοση της σελίδας, όπως διαμορφώθηκε από τον Xaris333 (συζήτηση | συνεισφορές) στις 17:25, 4 Φεβρουαρίου 2017 (Νέα σελίδα: require "Modul:No globals" local p = {} local lib = require 'Modul:Wikidata/lib' p.props = { accessdate = { 'P813' }, archiveurl = { 'P1065' }, author = { 'P50' }, dat...). Η τρέχουσα διεύθυνση (URL) είναι μόνιμος σύνδεσμος προς αυτή την έκδοση, που μπορεί να διαφέρει σημαντικά από την τρέχουσα έκδοση.
(διαφ.) ← Παλαιότερη έκδοση | Βλέπε τελευταία έκδοση (διαφ.) | Νεότερη έκδοση → (διαφ.)
require "Modul:No globals"

local p = {}

local lib = require 'Modul:Wikidata/lib'

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

local function dataToContent(data)
	local content = {}
	local Formatters = require 'Modul: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], { pattern = '[$1 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'
		}) .. '.'
		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 'Modul: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