Модуль:Wikidata/redLink

Материал из in.wiki
Перейти к навигации Перейти к поиску

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

local p = {};

local _wikidataLabel = '[вд]'

function p.formatText(label, entityId, addLink)
	local sup = ''
	if addLink then
		sup = string.format(
			'<sup class="noprint">[[:d:%s#sitelinks-wikipedia|<span>%s</span>]]</sup>',
			entityId,
			_wikidataLabel
		)
	end
	
	return label .. sup
end

function p.formatRedLink(title, text, entityId, infobox)
	if infobox == nil or infobox == '' then
		infobox = 'Универсальная карточка'
	end
	local qPreloadparams = 'preloadparams%%5B%%5D'
	local url = mw.title.new( title ):fullUrl( string.format(
		'action=edit&editintro=T:Нет_статьи/editintro&preload=T:Нет_статьи/preload&' .. qPreloadparams .. '=%s&' .. qPreloadparams .. '=%s&' .. qPreloadparams .. '=%s',
		entityId,
		mw.uri.encode( title ),
		mw.uri.encode( infobox )
	) )
	local templatestyles = mw.getCurrentFrame():extensionTag{
		name = 'templatestyles',
		args = {
			src = 'Module:Wikidata/redLink/styles.css',
		}
	}
	return templatestyles .. string.format(
		'<span class="ts-Wikidata-redLink plainlinks">[%s %s]</span><sup class="noprint">[[:d:%s#sitelinks-wikipedia|<span>%s</span>]]</sup>',
		url,
		mw.text.nowiki( text ),
		entityId,
		_wikidataLabel
	)
end

function p.formatRedLinkWithInfobox(title, text, entityId, defaultInfobox)
	return p.formatRedLink(title, text, entityId, p.getInfobox(entityId, defaultInfobox))
end

function p.getInfobox(entityId, defaultInfobox)
	if defaultInfobox then
		return defaultInfobox
	end
	if entityId then
		local result = mw.wikibase.getBestStatements(entityId, 'P31')
		for _, statement in pairs(result) do
			if statement.mainsnak.datavalue then
				local type = statement.mainsnak.datavalue.value.id
				if		type == 'Q5'	then return p.getBioInfobox(entityId)
				elseif	type == 'Q523'	then return 'Звезда'
				elseif	type == 'Q318'	then return 'Галактика'
				end
			end
		end
	end
	return 'Универсальная карточка'
end

function p.getBioInfobox(entityId)
	if entityId then
		local result = mw.wikibase.getBestStatements(entityId, 'P106')
		for _, statement in pairs(result) do
			if statement.mainsnak.datavalue then
				local occupation = statement.mainsnak.datavalue.value.id
				if occupation == 'Q901' then return 'Учёный'
				end
			end
		end
	end
	return 'Персона'
end

function p.formatRedLinkFromTemplate(frame)
	local args = frame['args']
	if not args[1] then -- may be arguments are passed from the parent template?
		args = frame:getParent().args
	end
	
	if not args[1] then
		return '<span class="error">Не указан элемент Викиданных</span>'
	end
	local entityId = mw.text.trim(args[1])

	local title = mw.wikibase.label(entityId)
	if not title then
		return mw.ustring.format('<span class="error">Нет метки у элемента %s</span>', entityId)
	elseif mw.ustring.match(title, '[%[%]:]') then -- cannot create page with this name
		return title
	end
	local text = title
	if args[2] then
		text = mw.text.trim(args[2])
	end
	
	local sitelink = mw.wikibase.sitelink(entityId)
	if sitelink then
		if text == sitelink then
			return '[[' .. sitelink .. ']]'
		else
			return '[[' .. sitelink .. '|' .. text .. ']]'
		end
	end	

	return p.formatRedLinkWithInfobox(title, text, entityId, args[3])
end

return p