Модуль:Wikidata/Interproject: различия между версиями
Перейти к навигации
Перейти к поиску
p>Dima st bk (проброс from из родителя (взято с https://ru.wikipedia.org/w/index.php?oldid=130779586#L-1462)) |
In.wiki (комментарии | вклад) м (1 версия импортирована: Импорт из Википедии) |
||
(не показаны 2 промежуточные версии 2 участников) | |||
Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
+ | |||
+ | function p.getWikivoyageLink( context ) | ||
+ | return p.getSisterProjectLink( 'voy', 'wikivoyage', context ) | ||
+ | end | ||
function p.getWikibooksLink( context ) | function p.getWikibooksLink( context ) | ||
Строка 27: | Строка 31: | ||
while p_context do | while p_context do | ||
if p_context.args and p_context.args.from and p_context.args.from ~= '' then | if p_context.args and p_context.args.from and p_context.args.from ~= '' then | ||
+ | if p_context.args.from == '-' then | ||
+ | return '' | ||
+ | end | ||
entityId = p_context.args.from | entityId = p_context.args.from | ||
else | else |
Текущая версия от 09:53, 10 февраля 2025
Для документации этого модуля может быть создана страница Модуль:Wikidata/Interproject/doc
local p = {}
function p.getWikivoyageLink( context )
return p.getSisterProjectLink( 'voy', 'wikivoyage', context )
end
function p.getWikibooksLink( context )
return p.getSisterProjectLink( 'b', 'wikibooks', context )
end
function p.getWikiquoteLink( context )
return p.getSisterProjectLink( 'q', 'wikiquote', context )
end
function p.getWikisourceLink( context )
return p.getSisterProjectLink( 's', 'wikisource', context )
end
function p.getWikipediaLink( context )
return p.getSisterProjectLink( 'w', 'wiki', context )
end
function p.getWiktionaryLink( context )
return p.getSisterProjectLink( 'wikt', 'wiktionary', context )
end
function p.getSisterProjectLink( projectInterwikiPrefix, projectName, context )
-- проброс параметра from откуда угодно
local entityId = nil
local p_context = context
while p_context do
if p_context.args and p_context.args.from and p_context.args.from ~= '' then
if p_context.args.from == '-' then
return ''
end
entityId = p_context.args.from
else
entityId = mw.wikibase.getEntityIdForCurrentPage()
end
p_context = p_context:getParent()
end
-- По умолчанию старается достать ссылку на русском, иначе - на родном (P103)
local wbStatus, sitelink = pcall( mw.wikibase.getSitelink, entityId, 'ru' .. projectName )
if wbStatus and sitelink then
return projectInterwikiPrefix .. ':' .. sitelink
end
local wbStatus, langClaims = pcall( mw.wikibase.getBestStatements, entityId, 'P103' )
if wbStatus ~= true or not langClaims then
return ''
end
local codes = mw.loadData( 'Module:Wikidata:Dictionary/P424' )
for _, claim in ipairs( langClaims ) do
if claim.mainsnak and
claim.mainsnak.datavalue and
claim.mainsnak.datavalue.value and
claim.mainsnak.datavalue.value.id
then
local codesById = codes[ claim.mainsnak.datavalue.value.id ]
if codesById then
for _, code in ipairs( codesById ) do
wbStatus, sitelink = pcall( mw.wikibase.getSitelink, entityId, code .. projectName )
if wbStatus and sitelink then
return projectInterwikiPrefix .. ':' .. code .. ':' .. sitelink
end
end
end
end
end
return ''
end
return p