Jump to content

Module:FormatStatement

Mai Wikipedia

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

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

local p = {}

function p.formatExample(subj, prop, obj, lang, datatype)
	if not prop or (not subj and not obj) then
		return ''
	end
	if not datatype then
		datatype = prop.datatype
	end
	local entity = prop
	prop = d._getLabel(prop, lang)
	if subj then
		subj = d.formatEntity(subj, {lang = lang})
	else
		subj = ''
	end
	if obj then
		if datatype == 'wikibase-item' or datatype == 'wikibase-property' then
			obj = d.formatEntity(obj, { lang = lang })
		elseif datatype == 'commonsMedia' then
			obj = '[[:File:' .. obj .. '|' .. obj .. ']]'
		else
			obj = obj
			if datatype == 'external-id' or datatype == 'string' then
				if not string.find(obj, "%[http") then
					if type(entity) ~= 'table' then
						formatters = mw.wikibase.getBestStatements(entity, 'P1630')
					else
						formatters = entity:getBestStatements('P1630')
					end
					if #formatters > 0 then
						if formatters[1].mainsnak.snaktype == 'value' then
							obj = '[' .. mw.ustring.gsub(formatters[1].mainsnak.datavalue.value,
								'$1', string.gsub(mw.uri.encode(obj, 'PATH'),'%%','%%%%')) .. ' ' .. obj .. ']'
						end
					end
				end
			end
		end
	else
		obj = ''
	end
	return subj .. ' <i>&lt;' .. prop .. '&gt;</i> ' .. obj
end

function p.formatExampleStatement(frame)
	return p.formatExample(frame.args[1], frame.args[2], frame.args[3], frame:preprocess("{{int:lang}}"));
end

return p