Модуль:Wikidata/redLink
[прагляд] [правіць] [гісторыя] [падстаронкі] [абнавіць]
Дакументацыя
Для рэалізацыі {{няма артыкула}}, прастаноўкі чырвоных спасылак модулем Wikidata і часткі шаблона {{iw}}.
Каб пазбегнуць паломак старонак, дзе ўжыты гэты модуль, пажадана эксперыментаваць у пясочніцы для модуляў.
local p = {};
function p.formatRedLink(title, text, entityId, infobox)
if infobox == nil or infobox == '' then
infobox = 'Універсальная картка'
end
return mw.ustring.format(
'<span class="plainlinks">[//be.wikipedia.org/w/index.php?title=%s&action=edit&editintro=Шаблон:Няма_артыкула/editintro&preload=Шаблон:Няма_артыкула/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 'Універсальная картка'
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