Module:LoP row

From Wikidata
Jump to navigation Jump to search

Documentation for this module may be created at Module:LoP row/doc

local d = require('Module:Wikidata')
local fmt = require('Module:FormatStatement')
local datatypemod = require('Module:Datatype')

return { main = function ( frame )

	local args = {}
	for i, j in pairs(frame:getParent().args) do
		if j ~= '' then args[i] = j end
	end

	local lang = frame:preprocess("{{int:lang}}")
	local id = args.id
	local datatype, entity, label, link, pid
	local stat = args.stat or ''
	local bold = args.bold or ''
	local description = args.description
	if id == "new" then
		label = args.title
		subjectitem = args.subjectitem
		datatype = string.lower(args.type)
		local i18n = mw.loadData('Module:i18n/datatype')
		local cdatatype = i18n.keys[datatype] or ''
		link = "''new'' <sup>[https://www.wikidata.org/wiki/Special:NewProperty?lang=en&label=" .. mw.uri.encode(label, PATH)
			.. "&description=" .. mw.uri.encode(description or '', PATH) .. "&datatype=" .. cdatatype .. " &nbsp;create]</sup>"
	elseif not id or not tonumber(id) then
		return "invalid 'id' parameter should be a number or 'new', " .. ((id and "is " .. id) or "none provided")
	elseif args.datatype and args.noexpensivecalls then
		pid = "P" .. id
		label = mw.wikibase.getLabel(pid)
		description = mw.wikibase.getDescription(pid)
		link = '[[Property:' .. pid .. '|' .. pid .. ']]'
		datatype = datatypemod.resolveDatatype(args.datatype)
	else
		pid = "P" .. id
		entity = mw.wikibase.getEntityObject(pid)
		if not entity then return 'property does not exist. Use "id=new" if it\'s to be created.' end
		label = d._getLabel(entity, lang)
		if args["contextualized-qualifier"] and args["example-property"] then
			local property = d._getLabel(mw.wikibase.getEntityObject(args["example-property"]), lang)
			if not property then return 'example-property ' .. args["example-property"] .. ' does not exist' end
			label = property .. ' with qualifier <span style="background-color: #ffffaa; font-size: small;">' .. label .. '</span>'
		end
		link = '[[Property:' .. pid .. '|' .. pid .. ']]'
		description = description or d._getDescription(entity, lang)
		subjectitem = d._formatStatements{entity = entity, lang = lang, property = 'P1629'}
		datatype = entity.datatype
	end

	if description and subjectitem then
		description = subjectitem .. tostring(mw.message.new('colon'):inLanguage(lang)) .. ' ' .. description
	end
	if not description then -- for customized description on topical lists
		description = args.description
	end
	local comment = args.comment
	if comment then
		description = description .. '<br><i>' .. comment .. '</i>'
	end

	local example, inverse
	if id == "new" then
		example = d.formatEntity(args["example-subject"], {lang = lang}) .. ' → ' .. args["example-object"]
	else
		if args.noexpensivecalls then
			example = fmt.formatExample(args["example-subject"], args["example-property"] or pid, args["example-object"], nil, datatype)
		else
			local example_entity = args["example-property"] and mw.wikibase.getEntity(args["example-property"])
			example = fmt.formatExample(args["example-subject"], example_entity or entity, args["example-object"])
			inverse = d._formatStatements{ entity = entity, lang = lang, property = 'P1696' }
		end
		local example_language
		if args["example-language"] then
			local linguistic = require 'Module:Linguistic'
			example_language = linguistic.inparentheses(frame:preprocess(mw.ustring.format('{{#language:%s|%s}}', args["example-language"], lang)), lang)
		end
		if example_language and datatype == "monolingualtext" then
			example = example .. example_language
		end
		if args["example-predicate"] then
			local examplequalif = fmt.formatExample(nil, entity, args["example-predicate"])
			if example_language and example_entity.datatype == "monolingualtext" then
				examplequalif = examplequalif .. example_language
			end
			example = '<span style="opacity: 0.66;">' .. example .. '</span><br/><span style="font-size: small;">' .. examplequalif .. '</span>'
		end
		if (not example or example == '') and not args.noexpensivecalls then
			example = d.formatStatements{
				delimiter = '&#32;<i>&lt;' .. d._getLabel(entity.id, lang) .. '&gt;</i>&#32;',
				entity = entity,
				lang = lang,
				numval = 1,
				property = {'P1855','P2271'},
				rank = 'best',
				showqualifiers = {entity.id}
			}
		end
		example = example or '<small>→ [[Property talk:' .. pid .. ']]</small>'
	end

	if args.pair then
		inverse = d.formatEntity('P' .. args.pair, { lang = lang })
		if args['pair2'] then inverse = inverse .. ' / ' .. args['pair2'] end
	end

	local formatteddatatype = datatypemod.display(datatype, lang)
	if bold == 'y' then
		label = '<b>' .. label .. '</b>'
	end		
	local row = mw.html.create('tr')
		:tag('td')
			:wikitext(label)
		:tag('td')
			:wikitext(link)
		:tag('td')
			:wikitext(formatteddatatype)
		:tag('td')
			:wikitext(description)
		:tag('td')
			:css{['text-align'] = 'center'}
			:wikitext(example)
		:tag('td')
			:css{['text-align'] = 'center'}
			:wikitext(inverse or '-')
			:allDone()
	return tostring(row)
end}