Open main menu

Documentation for this module may be created at Module:Interwiki from P460/doc

-- Original at https://www.wikidata.org/wiki/Module:Interwiki_from_P460
-- To update, please edit there first, then copy to your wiki. 

-- adapted from https://pl.wikipedia.org/wiki/Modu%C5%82:%C5%81atki by [[pl:User:PaweĊ‚ Ziemian]]

-- Use with:
--      {{#invoke:Interwiki from P460|Interwiki}}  -- uses sitelinks on items used as values with P460 (same as)
--      {{#invoke:Interwiki from P460|InterwikiP1889}}  -- uses sitelinks on items used as values with P1889 (different from)
--      {{#invoke:Interwiki from P460|InterwikiP1889fn}}  -- uses sitelinks on items used as values with P1889 (different from) if qualified with "criterion used" and Q27924673

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.InterwikiP1420(frame)
	local qid = frame.args.id
	local v2 = m.completeiw(qid, "P1420")
	return v2
end	

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

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

function m.Interwiki(frame)
	local qid = frame.args.id
	local v2 = m.completeiw(qid, "P460")
	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 == "wiki") and knownLanguages[lang] then
					appendLink(lang, v.title)
				end
			end
		end
	end

	extractLinks(data)
	if prop == "P460" then 
		if data.claims and data.claims.P460 then
			for _, v in ipairs(data.claims.P460) 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 == "P1420" then 
		if data.claims and data.claims.P1420 then
			for _, v in ipairs(data.claims.P1420) 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 == "P1889" then 
		if data.claims and data.claims.P1889 then
			for _, v in ipairs(data.claims.P1889) 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 == "P1889P1013Q27924673" then
		local passed = false
		local seeid
		if data.claims and data.claims.P1889 then
			for _, v in ipairs(data.claims.P1889) do
				if v.mainsnak.snaktype == "value" then
					seeid = v.mainsnak.datavalue.value["id"]
				end
				if v.qualifiers and v.qualifiers.P1013 then
					for _, w in ipairs(v.qualifiers.P1013) do
						if w.snaktype == "value" then
							local qualid = w.datavalue.value["id"]
							if qualid == "Q27924673" 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.."wiki"] 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