Jump to content

Module:WD version

From Wikisource

require('strict')

local p = {}

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local small_scan_link = require('Module:Small scan link')._small_scan_link
local WikidataIB = require('Module:WikidataIB')

local function getWDLink(args)
	return WikidataIB.getLink({['args'] = args})
end

local function getWDLabel(args)
	return WikidataIB.getLink({['args'] = args})
end

function p.WDStatement(args)
	local item = args.item
	if not item then
		return nil
	end
	
	local prop = args.prop
	
	local formattedStatement
	if args.getLink then
		local unsorted_statements = item:getBestStatements(prop)
		local statements = {}
		for k, v in pairs(unsorted_statements) do
			if v.rank == 'preferred' then
				table.insert(statements, v)
			end
		end
		for k, v in pairs(unsorted_statements) do
			if v.rank == 'normal' then
				table.insert(statements, v)
			end
		end
		-- mw.logObject('statements:')
		-- mw.logObject(statements)
		
		local statementLinks = {}
		for k, statement in pairs(statements) do
			if statement['mainsnak']['datatype'] == 'wikibase-item' and statement.mainsnak.datavalue and statement.mainsnak.datavalue.value and statement.mainsnak.datavalue.value.id then
				local statementLink = getWDLink({statement.mainsnak.datavalue.value.id})
				if statementLink and statementLink ~= '' then
					table.insert(statementLinks, statementLink)
				end
			else
				local statementValue = mw.wikibase.formatValue(statement.mainsnak)
				if statementValue and statementValue ~= '' then
					table.insert(statementLinks, statementValue)
				end
			end
		end
		-- mw.logObject(statementLinks)
		-- mw.logObject(#statementLinks)
		
		if #statementLinks == 1 then
			-- mw.logObject('#statementLinks = 1')
			formattedStatement = statementLinks[1]
		elseif #statementLinks > 1 then
			-- mw.logObject('#statementLinks > 1')
			formattedStatement = table.concat(statementLinks, ', ', 1, #statementLinks - 1) .. ' and ' .. statementLinks[#statementLinks]
		end
	end
	
	if not formattedStatement or formattedStatement == '' then
		-- mw.logObject('not formattedStatement')
		formattedStatement = item:formatPropertyValues(prop).value
	end
	-- mw.logObject(formattedStatement)
	
	if formattedStatement and formattedStatement ~= '' then
		return (args.prefix or '') .. formattedStatement .. (args.suffix or '')
	else
		return nil
	end
end

function p._WD_version(args)
	local wikidata_id = args[1]
	--mw.logObject(wikidata_id)
	local item = mw.wikibase.getEntity(wikidata_id)
	--mw.logObject(item)
	if not item then
		return nil
	end
	
	local version_info = {}
	
	version_info.link = getWDLink({wikidata_id})
	
	version_info.author = p.WDStatement({
		['item'] = item,
		['prop'] = 'P50',
		['prefix'] = ' by ',
		['getLink'] = true
	})
	version_info.translator = p.WDStatement({
		['item'] = item,
		['prop'] = 'P655',
		['prefix'] = ', translated by ',
		['getLink'] = true
	})
	version_info.editor = p.WDStatement({
		['item'] = item,
		['prop'] = 'P98',
		['prefix'] = ', edited by ',
		['getLink'] = true
	})
	version_info.illustrator = p.WDStatement({
		['item'] = item,
		['prop'] = 'P110',
		['prefix'] = ', illustrated by ',
		['getLink'] = true
	})
	version_info.published = p.WDStatement({
		['item'] = item,
		['prop'] = 'P1433',
		['prefix'] = ", in ''",
		['suffix'] = "''",
		['getLink'] = true
	})
	version_info.pubdate = p.WDStatement({
		['item'] = item,
		['prop'] = 'P577',
		['prefix'] = ' (',
		['suffix'] = ')'
	})
	
	if version_info.link and not yesno(args.useitalics or false) and (version_info.published or yesno(args.usequotes or false)) then
		version_info.link = '"' .. version_info.link .. '"'
	else
		version_info.link = "''" .. version_info.link .. "''"
	end
	
	local scan
	local scan_item = mw.wikibase.getEntity(args[2] or wikidata_id)
	scan = p.WDStatement({
		['item'] = scan_item,
		['prop'] = 'P996'
	})
	if not scan and scan_item ~= item then
		scan = p.WDStatement({
			['item'] = item,
			['prop'] = 'P996'
		})
	end
	-- mw.logObject(scan)
	if scan then
		version_info.scanlink = ' ' .. small_scan_link({scan})
	end
	
	local version_text = {}
	local version_keys = {'link', 'author', 'translator', 'editor', 'illustrator', 'published', 'pubdate', 'scanlink'}
	for i = 1, #version_keys do
		local v = version_keys[i]
		if version_info[v] and not yesno(args['no' .. v] or false) then
			table.insert(version_text, version_info[v])
		end
	end
	return table.concat(version_text)
end

function p.WD_version(frame)
	return p._WD_version(getArgs(frame))
end

return p