Модуль:WikidataMagic

Поделись знанием:
Это текущая версия страницы, сохранённая Putnik (обсуждение | вклад) в 10:11, 9 сентября 2016. Вы просматриваете постоянную ссылку на эту версию.

(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Для документации этого модуля может быть создана страница Модуль:WikidataMagic/doc

local p = {}

--[[
	Получение идентификатора свойства по имени параметра шаблона.
]]
function p.propertyIdByName( name )
	local names = require( 'Module:WikidataMagic/names' )
	
	name = string.gsub( name, ' ', ' ' )
	name = string.gsub( name, '%s+', ' ' )
	name = string.gsub( name, '%[%[[^%[%]%|]+%|([^%[%]%|]+)%]%]', '%1' )
	name = string.gsub( name, ':$', '' )
	name = mw.language.getContentLanguage():ucfirst( name )

	if names[ name ] then
		return 'P' .. names[ name ]
	end
	
	mw.log( 'Parameter not mapped with Wikidata property: "' .. name .. '"' )
	return nil
end

--[[
	Получение параметров форматтера, которые обычно используются для вывода свойства.
]]
function p.usualPropertyParams( propertyId )
	propertyId = string.upper( string.gsub( propertyId, '%[.*$', '' ) )

	-- род занятий
	if propertyId == 'P18' then
		return {
			['size'] = '250x350px',
		}
	elseif propertyId == 'P41' then
		return {
			['size'] = '150x200px',
		}
	elseif propertyId == 'P94' then
		return {
			['size'] = '100x200px',
		}
	elseif propertyId == 'P106' then
		return {
			['claim-module'] = 'Wikidata/item',
			['claim-function'] = 'formatEntityWithGenderClaim',
			['conjunction'] = ', ',
		}
	elseif propertyId == 'P109' then
		return {
			['size'] = '150x150px',
		}
	elseif propertyId == 'P117' then
		return {
			['size'] = '290x300px',
		}
	elseif propertyId == 'P154' then
		return {
			['size'] = '220x80px',
		}
	elseif propertyId == 'P242' then
		return {
			['size'] = '300x300px',
		}
	elseif propertyId == 'P1477' then
		return {
			['separator'] = '<br>',
			['conjunction'] = '<br>',
			['monolingualLangTemplate'] = 'lang',
		}
	elseif propertyId == 'P1559' then
		return {
			['separator'] = '<br>',
			['conjunction'] = '<br>',
			['monolingualLangTemplate'] = 'lang',
		}
	elseif propertyId == 'P1621' then
		return {
			['size'] = '300x300px',
		}
	elseif propertyId == 'P2910' then
		return {
			['size'] = '100x80px',
		}
	end

	return {}
end

--[[
	Обёртка над методом formatProperty модуля Wikidata.
]]
function p.formatProperty( frame )
	local Wikidata = require( 'Module:Wikidata' )

	-- если локальное значение передано в параметрах вызова,
	-- сразу вызывается форматтер Wikidata для вывода
    if frame.args.value and frame.args.value ~= '' then
        return Wikidata.formatProperty( frame )
    end

	-- поиск свойства по названию
	-- if not string.match( frame.args.property, '^[Pp]%d' ) then
	-- 	local propertyId = p.propertyIdByName( frame.args.property )
	-- 	if not propertyId then
	-- 		return ''
	-- 	end

	-- 	frame.args.property = propertyId
	-- end

	-- автоматическое форматирование свойства
	local propertyParams = p.usualPropertyParams( frame.args.property )
	for key, value in pairs( propertyParams ) do
		frame.args[ key ] = value
	end

	-- временное решение для использования специальных шаблонов
	if #propertyParams == 0 then
		local specalTemplates = { P17 = 1, P19 = 1, P20 = 1, P27 = 1, P106 = 1,
			P159 = 1, P166 = 1, P212 = 1, P225 = 1, P348 = 1,
			P373 = 1, P421 = 1, P495 = 1, P512 = 1, P551 = 1, P569 = 1,
			P570 = 1, P571 = 1, P577 = 1, P856 = 1, P957 = 1, P1082 = 1,
			P1098 = 1, P1128 = 1, P1195 = 1, P1324 = 1, P1436 = 1, P1532 = 1,
			P2031 = 1, P2032 = 1, P2046 = 1 }
		if specalTemplates[ string.upper( frame.args.property ) ] then
			return frame:expandTemplate{ title = 'Wikidata/' .. string.lower( frame.args.property ) }
		end
	end

	return Wikidata.formatProperty( frame )
end

return p