Module:Interwiki from P46

-- Original at https://gratisdata.miraheze.org/wiki/Module:Interwiki_from_P46 -- in case of changes, please edit https://gratisdata.miraheze.org/wiki/Module:Interwiki_from_P46 then copy changes here.

-- adapted from https://pl.wikipedia.org/wiki/Modu%C5%82:%C5%81atki by w:pl:User:Paweł Ziemian

-- Use with: --       -- uses sitelinks on items used as values with P46 (same as) --       -- uses sitelinks on items used as values with P56 (different from) --       -- uses sitelinks on items used as values with P56 (different from) if qualified with "criterion used" and Q3505

local m = {}

local langlist={"aa","ab","ace","af","ak","als","am","an","ang","ar","arc","arz","as","ast","av","ay","az","ba","bar","bat-ltg","bat-smg","bcl","be","bej","be-tarask","be-x-old","bg","bh","bi","bjn","bm","bn","bo","bpy","br","bs","bug","bxr","ca","cbk-zam","cdo","ce","ceb","ch","cho","chr","chy","ckb","co","cr","crh","crs","cs","csb","cu","cv","cy","da","de","diq","dsb","dv","dz","ee","el","eml","en","enm","eo","es","ess","esu","et","eu","ext","fa","ff","fi","fiu-vro","fj","fo","fon","fr","frp","frr","fur","fy","ga","gaa","gag","gan","gd","gil","gl","glk","gn","got","grc","grc-koi","gu","gv","ha","hak","haw","he","hi","hif","hil","ho","hr","hsb","ht","hu","hy","hz","ia","id","ie","ig","ii","ik","ilo","io","is","it","iu","ja","jbo","jv","ka","kaa","kab","kbd","kg","khw","ki","kj","kk","kl","km","kn","ko","koi","kr","krc","krj","ks","ksh","ku","kv","kw","ky","la","lad","lb","lbe","lez","lfn","lg","li","lij","lmo","ln","lo","lt","ltg","lv","map-bms","mdf","mg","mh","mhr","mi","min","mk","ml","mn","mo","mr","mrj","ms","mt","mus","mwl","my","myv","mzn","na","nah","nan","nap","nb","nds","nds-nl","ne","new","ng","nl","nn","no","non","nov","nrm","nso","nv","ny","oc","om","or","os","pa","pag","pam","pap","paw","pcd","pdc","pdt","peo","pfl","pi","pih","pl","pms","pnb","pnt","ps","pt","pt-br","qu","qya","rm","rmy","rn","ro","roa-rup","roa-tara","ru","rue","rw","sa","sah","sc","scn","sco","sd","se","sg","sh","shi","si","sid","simple","sk","sl","sla","sli","sm","sn","so","sq","sr","srn","ss","st","stq","su","sv","sw","syc","szl","ta","te","tet","tg","th","ti","tk","tl","tn","to","tokipona","tpi","tr","ts","tt","tum","tvl","tw","ty","udm","ug","uk","ur","uz","ve","vec","vep","vi","vls","vo","wa","war","wbl","wo","wuu","wym","xal","xh","xmf","yai","yi","yo","za","zea","zh","zh-classical","zh-cn","zh-min-nan","zh-simple","zh-tw","zh-yue","zu","zun"} -- list from Module:Lang/data

function m.InterwikiP588(frame) local qid = frame.args.id	local v2 = m.completeiw(qid, "P588") return v2 end

function m.InterwikiP56(frame) local qid = frame.args.id	local v2 = m.completeiw(qid, "P56") return v2 end

function m.InterwikiP56fn(frame) local qid = frame.args.id	local v2 = m.completeiw(qid, "P56P201Q3505") return v2 end

function m.Interwiki(frame) local qid = frame.args.id	local v2 = m.completeiw(qid, "P46") return v2 end

function m.completeiw(qid, prop) local data = mw.wikibase.getEntityObject(qid) if not data then return -- brak danych -> kategoria? end local links = {} local appendLink = function(lang, title) if not links[lang] then links[lang] = { title } else table.insert(links[lang], title) end end local knownLanguages = {} for i=1, #langlist do     knownLanguages[langlist[i]] = true end local extractLinks = function(data) if data.sitelinks then for k, v in pairs(data.sitelinks) do				local lang = string.sub(k, 1, -5) local project = string.sub(k, -4) if (project == "gratispaideiawiki") and knownLanguages[lang] then appendLink(lang, v.title) end end end end

extractLinks(data) if prop == "P46" then if data.claims and data.claims.P46 then for _, v in ipairs(data.claims.P46) do				if v.mainsnak.snaktype == "value" then local seeid = v.mainsnak.datavalue.value["id"] local seedata = mw.wikibase.getEntityObject(seeid) if seedata then extractLinks(seedata) end end end end end

if prop == "P588" then if data.claims and data.claims.P588 then for _, v in ipairs(data.claims.P588) do				if v.mainsnak.snaktype == "value" then local seeid = v.mainsnak.datavalue.value["id"] local seedata = mw.wikibase.getEntityObject(seeid) if seedata then extractLinks(seedata) end end end end end

if prop == "P56" then if data.claims and data.claims.P56 then for _, v in ipairs(data.claims.P56) do				if v.mainsnak.snaktype == "value" then local seeid = v.mainsnak.datavalue.value["id"] local seedata = mw.wikibase.getEntityObject(seeid) if seedata then extractLinks(seedata) end end end end end

if prop == "P56P201Q3505" then local passed = false local seeid if data.claims and data.claims.P56 then for _, v in ipairs(data.claims.P56) do				if v.mainsnak.snaktype == "value" then seeid = v.mainsnak.datavalue.value["id"] end if v.qualifiers and v.qualifiers.P201 then for _, w in ipairs(v.qualifiers.P201) do						if w.snaktype == "value" then local qualid = w.datavalue.value["id"] if qualid == "Q3505" then passed = true end end end end end end if passed == true and seeid then local seedata = mw.wikibase.getEntityObject(seeid) if seedata then extractLinks(seedata) end end

end

local result = {} local content = mw.title.getCurrentTitle:getContent for k, v in pairs(links) do		local pattern = "%[%["..mw.ustring.gsub( k, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" )..":[^%[%]|]+%]%]" local interwiki = mw.ustring.match(content, pattern) if not interwiki and not data.sitelinks[k.."gratispaideiawiki"] then table.insert(result, "")			table.insert(result, k)			table.insert(result, ":")			table.insert(result, v[1])			table.insert(result, "") end end

return table.concat(result, "") end

return m