Модуль:Wikidata/Interproject: различия между версиями
Перейти к навигации
Перейти к поиску
м (1 версия импортирована: Импорт из Википедии) |
p>Dima st bk (проброс from из родителя (взято с https://ru.wikipedia.org/w/index.php?oldid=130779586#L-1462)) |
||
Строка 2: | Строка 2: | ||
function p.getWikibooksLink( context ) | function p.getWikibooksLink( context ) | ||
− | return p.getSisterProjectLink( 'b', 'wikibooks', context | + | return p.getSisterProjectLink( 'b', 'wikibooks', context ) |
end | end | ||
function p.getWikiquoteLink( context ) | function p.getWikiquoteLink( context ) | ||
− | return p.getSisterProjectLink( 'q', 'wikiquote', context | + | return p.getSisterProjectLink( 'q', 'wikiquote', context ) |
end | end | ||
function p.getWikisourceLink( context ) | function p.getWikisourceLink( context ) | ||
− | return p.getSisterProjectLink( 's', 'wikisource', context | + | return p.getSisterProjectLink( 's', 'wikisource', context ) |
end | end | ||
function p.getWikipediaLink( context ) | function p.getWikipediaLink( context ) | ||
− | return p.getSisterProjectLink( 'w', 'wiki', context | + | return p.getSisterProjectLink( 'w', 'wiki', context ) |
end | end | ||
function p.getWiktionaryLink( context ) | function p.getWiktionaryLink( context ) | ||
− | return p.getSisterProjectLink( 'wikt', 'wiktionary', context | + | return p.getSisterProjectLink( 'wikt', 'wiktionary', context ) |
end | end | ||
− | function p.getSisterProjectLink( projectInterwikiPrefix, projectName, | + | function p.getSisterProjectLink( projectInterwikiPrefix, projectName, context ) |
− | entityId = entityId | + | -- проброс параметра 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 | ||
+ | entityId = p_context.args.from | ||
+ | else | ||
+ | entityId = mw.wikibase.getEntityIdForCurrentPage() | ||
+ | end | ||
+ | p_context = p_context:getParent() | ||
+ | end | ||
+ | |||
-- По умолчанию старается достать ссылку на русском, иначе - на родном (P103) | -- По умолчанию старается достать ссылку на русском, иначе - на родном (P103) | ||
local wbStatus, sitelink = pcall( mw.wikibase.getSitelink, entityId, 'ru' .. projectName ) | local wbStatus, sitelink = pcall( mw.wikibase.getSitelink, entityId, 'ru' .. projectName ) |
Версия от 20:44, 1 сентября 2023
Для документации этого модуля может быть создана страница Модуль:Wikidata/Interproject/doc
local p = {}
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
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