Модуль:Wikidata/redLink

Википедий — эрыкан энциклопедий гыч материал

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

local p = {};

function p.formatRedLink(title, text, entityId, infobox)
	if infobox == nil or infobox == '' then
		infobox = 'Universal infocard'
	end
	return mw.ustring.format(
		'<span class="plainlinks">[//ru.wikipedia.org/w/index.php?title=%s&action=edit&editintro=T:Нет_статьи/editintro&preload=T:Нет_статьи/preload&preloadparams%%5B%%5D=%s&preloadparams%%5B%%5D=%s&preloadparams%%5B%%5D=%s <span style="color: #ba0000; text-decoration: inherit; -moz-text-decoration-color: #ba0000; text-decoration-color: #ba0000;">%s</span>]</span>',
		mw.uri.encode(title), entityId, mw.uri.encode(title), string.gsub(infobox, ' ', '+') , mw.text.nowiki(text)
	)
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 'Universal infocard'
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