Modul:Benutzer:Count Count/Wikidata helper

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Die Dokumentation für dieses Modul kann unter Modul:Benutzer:Count Count/Wikidata helper/Doku erstellt werden

p = {}

--[[

This modules include help functionality for Wikidata that are not primarily used in articles directly or in templates, but rather talk and project pages.

]]

function tablesize(T)
   local n = 0
   for _ in pairs(T) do n = n + 1 end
   return n
end

--[[
  wdmatrix, creates a table with links to articles for given wikidata items on given languages
  and the number of languages that has link to the items
  parameters
  ==========
  objects: (mandatory) a comma separated list of qids 
  langs: (mandatory) a comma separated list of language codes
  
  example
  =======
  {{#invoke:Wikidatahelper|wdmatrix|objects=Q210723,Q187140,Q2100893,Q18451251,Q4876106,Q4876107,Q16839444,Q386215|langs=en,es,de,sv}} 
]]

function p.wdmatrix(frame)
	local langs = frame.args['langs'] 
	local langstable = {}
	if langs then langstable = mw.text.split(langs,',') end
	local nlangs = tablesize(langstable)

	local objs = frame.args['objects']
	local objstable = {}
	if objs then objstable = mw.text.split(objs,',') end
	local nobjs = tablesize(objstable)

	local sitelinks = {}
	local res = '{| class="wikitable"\n|-\n'
	res = res .. '!colspan=2| !!colspan=' .. nlangs+1 .. '|Languages\n|-\n!Wikidata items||Label||#'
	for j = 1,nlangs do res = res .. '!!' .. langstable[j] .. 'wiki' end
	for i = 1,nobjs do
		local entity = mw.wikibase.getEntity(objstable[i]);
		if entity then
           	local n = 0
           	if entity.sitelinks then n = tablesize(entity.sitelinks) end
      		res = res .. '\n|-\n'

			res = res .. '| [[:d:' .. objstable[i] .. ']] || ' .. (entity:getLabel() or '-') .. ' || align="right" |' .. n
      		for j = 1,nlangs do
       			local link = '-'
       			if entity.sitelinks then
       				for _, q in pairs(entity.sitelinks) do
	  				 	if q.site == langstable[j]..'wiki' then
	  				 		link = '[[:' .. langstable[j] .. ':' .. entity:getSitelink(q.site) .. '|' .. entity:getSitelink(q.site) .. ']]'
	  					 end
	  				end
	  			end
	  			res = res .. '||' .. link
			end 
	  	end
	end
	res = res .. '\n|}'
	return res
end

return p