Moduuli:Cs:Wikidata/lib

Wikipediasta
Siirry navigaatioon Siirry hakuun

require "Module:No globals"

local p = {
	common = require "Module:cs:Functions",
	props = {
		begin = { 'P569', 'P580' },
		ending = { 'P570', 'P582' },
		lang = { 'P364', 'P407' },
		point = { 'P571', 'P577', 'P585' },
	},
	datatypeToValueType = {
		['commonsMedia'] = 'string',
		['external-id'] = 'string',
		['geo-shape'] = 'string',
		['globe-coordinate'] = 'globecoordinate',
		['math'] = 'string',
		['monolingualtext'] = 'monolingualtext',
		['quantity'] = 'quantity',
		['string'] = 'string',
		['tabular-data'] = 'string',
		['time'] = 'time',
		['url'] = 'string',
		['wikibase-item'] = 'wikibase-entityid',
		['wikibase-property'] = 'wikibase-entityid',
	},
}

local i18n = mw.loadData("Module:cs:Wikidata/i18n")

function p.addWdClass(str)
	return '<span class="wd">' .. str .. '</span>'
end

function p.formatTextInLanguage(text, language)
	return mw.text.tag('span', { lang = language }, text)
end

function p.IsSnakValue(snak)
	return snak.snaktype == 'value'
end

function p.getLabelInLanguage(entityId, langs)
	langs = p.textToTable(langs)
	local label, lang = mw.wikibase.getLabelWithLang(entityId)
	if label then
		for _, lg in ipairs(langs) do
			if lg == lang then
				return label, lang
			end
		end
	end
	return nil, nil
end

function p.IsOptionTrue(options, key)
	if options[key] then
		if tostring(options[key]) == 'true' or tostring(options[key]) == 'yes' or tostring(options[key]) == '1' then
			return true
		end
	end
	return false
end

function p.textToTable(something, options)
	if type(something) ~= "table" then
		local options = options or {}
		local split_pattern = options.split_pattern or "%s*,%s*"
		something = mw.text.split(something, split_pattern)
	end

	return p.common.cleanArgs(something)
end

function p.getItemIdFromURI(uri)
	return mw.ustring.match(uri, '(Q%d+)')
end

function p.isPropertyId(value)
	return mw.ustring.match(value, '^[Pp][1-9]%d-$') and true
end

function p.simpleCompare(first, second)
	if first == second then
		return 0
	end
	if first < second then
		return -1
	else
		return 1
	end
end

-- @deprecated
function p.getEntityIdFromValue(value)
	local entityType = value['entity-type']
	if entityType == 'item' then
		return 'Q' .. value['numeric-id']
	elseif entityType == 'property' then
		return 'P' .. value['numeric-id']
	else
		return error(p.formatError('unknown-entity-type', entityType))
	end
end

function p.category(key, ...)
	local Category = require 'Module:cs:Kategorie'
	local title = mw.title.getCurrentTitle()
	return Category.makeCategory(mw.ustring.format(i18n.categories[key], ...), '0,14', title.text)
end

function p.formatFromPattern(str, pattern)
	return mw.ustring.gsub(pattern, '$1', str) .. '' --Hack to get only the first result of the function
end

function p.formatError(key, ...)
	return mw.ustring.format(i18n.errors[key], ...)
end

function p.raiseInvalidDatatype(method, allowed, provided)
	if type(allowed) ~= 'table' then
		allowed = { allowed }
	end
	return p.formatError('invalid-datatype2', method, mw.text.listToText(allowed, '“, „', '“ nebo „'), provided)
end

return p