Модуль:Wikidata/Interproject: различия между версиями
Перейти к навигации
Перейти к поиску
импортировано>Dima st bk (проброс from из родителя (взято с https://ru.wikipedia.org/w/index.php?oldid=130779586#L-1462)) |
Kirdik (обсуждение | вклад) м (1 версия импортирована) |
(нет различий)
|
Текущая версия от 10:18, 28 сентября 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