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

Module:Wikidata/cite

Από τη Βικιπαίδεια, την ελεύθερη εγκυκλοπαίδεια
--require "strict"

local p = {}

local lib = require 'Module:Wikidata/lib'

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' },
	isbn13 = { 'P212' },
	isbn10 = { 'P957' },
}

local function dataToContent(data)
	if data.wikipedia then
		return '';
	end
	local content = {}
	local Formatters = require 'Module: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 then
		local title = Formatters.getFormattedValue(data.title[1], {})
		if data.archiveurl then
			title = Formatters.getFormattedValue(data.archiveurl[1], { ['value-formatter'] = 'url', text = '«' .. title .. '»' }) .. '.'
		elseif data.url then
			title = Formatters.getFormattedValue(data.url[1], { ['value-formatter'] = 'url', text = '«' .. title .. '»' }) .. '.'
		elseif data.work then
			local id = Formatters.getRawValue(data.work[1])
			title = '[[' .. (mw.wikibase.sitelink(id) or ('d:' .. id)) .. '|«' .. title .. '»]]'
		end
		table.insert(content, title)
	end
	if data.lang then
		local langs = {}
		for _, lang in ipairs(data.lang) do
			table.insert(langs, Formatters.getFormattedValue(lang, {}))
		end
		table.insert(content, '(' .. table.concat(langs, ', ') .. ')')
	end
	if data.work and (not data.title or data.archiveurl or data.url) then
		local title = Formatters.getFormattedValue(data.work[1], {})
		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, Formatters.getFormattedValue(snak, {}))
		end
		table.insert(content, table.concat(publishers, ', ') .. '.')
	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], {}) .. '.'
		table.insert(content, date)
	end
	if not data.title and data.url then
		local url = Formatters.getFormattedValue(data.url[1], { ['value-formatter'] = 'url' }) .. '.'
		table.insert(content, url)
	elseif data.external then
		local url = Formatters.getFormattedValue(data.external[1], { autoformat = true, property = data.external[1].property }) .. '.'
		table.insert(content, url)
	end
	if data.archivedate and data.url and data.archiveurl then
		local url = Formatters.getFormattedValue(data.url[1], { ['value-formatter'] = 'url', text = 'το πρωτότυπο' })
		local date = Formatters.getFormattedValue(data.archivedate[1], {})
		table.insert(content, mw.ustring.format('Αρχειοθετήθηκε από %s στις %s.', url, date))
	end
	if data.accessdate and data.accessdate[1].datavalue.value.time ~= "+42400-00-00T00:00:00Z" then
		local date = Formatters.getFormattedValue(data.accessdate[1], {})
		table.insert(content, mw.ustring.format('Ανακτήθηκε στις %s.', date))
	end
	if data.pages then
		--local pages = Formatters.getFormattedValue(data.pages[1], { pattern = 'σελ. $1.'})
		local pages = mw.ustring.format('σελ. %s.', Formatters.getRawValue(data.pages[1], {}))
		table.insert(content, pages)
	end
	if data.isbn13 then
		local isbn13 = mw.ustring.format('ISBN-13 %s', Formatters.getFormattedValue(data.isbn13[1],
			{ pattern = '[[Special:BookSources/$1|$1]].' }))
		table.insert(content, isbn13)
	end
	if data.isbn10 then
		local isbn10 = mw.ustring.format('ISBN-10 %s', Formatters.getFormattedValue(data.isbn10[1],
			{ pattern = '[[Special:BookSources/$1|$1]].' }))
		table.insert(content, isbn10)
	end
	return table.concat(content, ' ')
end
local function dataFromItem(id, data)
	local entity = mw.wikibase.getEntity(id)
	if entity.claims["P31"] then
	  for _, statement in ipairs(entity.claims["P31"]) do
	  	if statement.mainsnak and statement.mainsnak.datavalue and statement.mainsnak.datavalue.value and statement.mainsnak.datavalue.value.id == 'Q10876391' then -- no references to wikipedia please
		  data.wikipedia = true;
		  return;
		end
      end
    end
	for key, props in pairs(p.props) do
		if not data[key] then
			data[key] = {}
			for _, prop in ipairs(props) do
				for _, statement in ipairs(mw.wikibase.getBestStatements(id, prop)) do
					if lib.IsSnakValue(statement.mainsnak) then
						table.insert(data[key], statement.mainsnak)
					end
				end
				if #data[key] > 0 then
					break
				end
			end
			if #data[key] == 0 then
				data[key] = nil
			end
		end
	end
end

function p.formatReferences(references, options)
	local frame = mw.getCurrentFrame()
	local valid_refs = {}
	local limit = tonumber(options.max_ref) or 3
	local Formatters = require 'Module:Wikidata/Formatters'
	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 = Formatters.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