Vés al contingut

Mòdul:Llistawikidatacompletesa

De la Viquipèdia, l'enciclopèdia lliure
-- Copied from meta:Module:BBC100Women2019Table, builds a list of articles based on information from Wikidata
-- Sample use: {{#invoke:Llistawikidatacompletesa|table|Q1|Q2|Q3|Q4|Q5}}
-- Original from https://meta.wikimedia.org/wiki/Module:WikimediaCEETable.

local langTable = {'ca'}
local WD = require('Module:Wikidades')

local p = {}

function p.table(frame)
	-- header/init
	--local resultTable = '{| class="wikitable sortable" width=100%\n|-\n! № !! style="width:18%"|Article !! class="sortable" | Extensió en català !! Σ <br> !! Element a Wikidata !! <span title="number of main statements on item">st.</span> !! \n|-'
	local resultTable = '{| class="wikitable sortable" width=100%\n|-\n! № !! style="width:18%"|Article !! class="sortable" | Extensió en català !! Σ <br> !! Element a Wikidata !! Conté multimèdia \n|-'
	local index = 1
	local maxindex = 300
	local extradata = 0
	local ctt = {}
	local statementst = 0
	local coords = 0
	local images = 0
	local wqsitems = ""
	local qids = ""
	local timeline = 0
	local mylang = frame:preprocess('{{int:lang}}')
	local mylang_obj = mw.language.new(mylang) 
	for langCount = 1, #langTable do
		ctt[langCount] = 0
	end
	
	-- rows
	local idinternal = "P373, P948, P935, P460, P856, P910, P213, P1343, P973, P345, P227, P244, P1612, P1472, P1325"
	local commonsp = 'P18 P10 P14 P15 P41 P51 P94 P109 P117 P154 P158 P181 P207 P242 P367 P373 P443 P491 P692 P935 P948 P989 P990 P996 P1442 P1472 P1543 P1612 P1621 P1766 P1801 P1846 P1943 P1944'
	
	while frame.args[index] do
		if index + (extradata / 2) > maxindex then
			resultTable = resultTable .. '\n|-\n|colspan=2|<b>Listed only first ' .. (index - 1) .. ' rows</b>\n'
			break
		end
		
		local Id = frame.args[index]
		
		if not mw.wikibase.entityExists(Id) then
			return '<b>Entity ' .. Id .. ' not found</b>'
		end
		local Label = mw.wikibase.getLabelByLang(Id, 'ca')
		local ensitelink = mw.wikibase.getSitelink(Id, 'enwiki')
		if not Label then
			if ensitelink then
				Label = ensitelink
			else
				Label = '(anglès)'
			end
		end
		local result2 = ''
		local ct = 0
		wqsitems = wqsitems .. "wd%3A" .. Id .. "%20"
		qids = qids .. string.sub(Id, 2) .. ","
		
		-- columns per row
		for langCount = 1, #langTable do
			local iw = langTable[langCount]
			if iw == "be_x_old" then iw = "be-tarask" end
			local sitelink = mw.wikibase.getSitelink(Id, iw .. 'wiki' )
			if sitelink then
				if mw.wikibase.getGlobalSiteId() == iw .. 'wiki' then
					local bytes = string.len(mw.title.new(sitelink):getContent() or '')
					result2 = result2 .. '\n| style="color: #202122; background-color: #cfc;" data-sort-value=' .. bytes .. '|' .. '[[' .. sitelink .. '| ✔️]]'
					result2 = result2 .. ' ' .. '[[:' .. sitelink .. '|' .. mylang_obj:formatNum(bytes) .. ']]'
				else
					result2 = result2 .. '\n| style="color: #202122; background-color: #cfc;"|[[:' .. iw .. ':' .. sitelink .. '| ✔️]]'
				end
				ct = ct + 1
				ctt[langCount] = ctt[langCount] + 1
				if Label == '' then
					Label = sitelink
				end
			else
				result2 = result2 .. '\n| [[:' .. iw .. ':' .. Label .. '| −]]'
			end
		end
		
		-- first cells of row
		local result1 = '|-\n|align=right|' .. index .. '\n|'
		if ensitelink then
			result1 = result1 .. '[[:en:' .. ensitelink .. '|' .. Label ..']]'
		else
			result1 = result1 .. Label
		end
		
		-- for humans (Q5)
		local p31 = WD.claim{item=Id, property='P31', formatting='raw'}
		if p31 == 'Q5' then
			extradata = extradata + 1
			-- icon for females
			local p21 = WD.claim{item=Id, property='P21', formatting='raw'}
			if p21 == 'Q6581072' or p21 == 'Q1052281' then
				result1 = result1 .. ' [[File:Female Icon.svg|18px]]'
			end
			-- years for humans
			local p569 = WD.claim{item=Id, property='P569', formatting='Y', list=false, editicon=false}
			if p569 then
				local p570 = WD.claim{item=Id, property='P570', formatting='Y', list=false, editicon=false}
				if p570 then
					result1 = result1 .. ' <span style="font-size: smaller; font-weight: 100;">(' .. p569 .. '-' .. p570 .. ')</span> '
				else
					result1 = result1 .. ' <span style="font-size: smaller; font-weight: 100;">(b. ' .. p569 .. ')</span>'
				end
			else
				result1 = result1 .. ' <span style="font-size: smaller; font-weight: 100;">(?)</span>'
			end
		elseif WD.validProperty{'P625', from=Id} then
			local p625 = WD.claim{item=Id, property='P625', formatting='$lat_N_$lon_E', list=false, editicon=false}
			extradata = extradata + 0.75 -- heuristically
			result1 = result1 .. "&nbsp;[[File:Geographylogo.svg|18px|view on maps|link=https://tools.wmflabs.org/geohack/geohack.php?language=en&pagename=" .. mw.uri.encode( Label, "PATH" ) .. "&params=" .. p625 .. "]]"
			coords = coords + 1
		end
		if timeline <= 2 then
			if WD.claim{item=Id, property='P580 P569 P571 P582 P576 P577'} then
			end
		end
		
		if images == 0 then
			if WD.validProperty{item=Id, 'P18'} then
				images = images + 1
			end
		end
		
		-- last cells of row
		index = index + 1

		local commons = false
		local statements = 0
		--if WD.claim{item=Id, property=idinternal} == nil then
		--	statements = statements + 1
		--end
		if WD.claim{item=Id, property=commonsp} then
			commons = true
		end
		
		-- todo : check for Commons sitelinks
		local commonsstr = ""
		if commons then
			commonsstr = '<span title="Wikidata item includes Commons resources">+</span>'
			-- commonsstr = "[https://commons.wikimedia.org/w/index.php?fulltext=1&search=".. mw.uri.encode( Label, "PATH" ) .. " +]"
		end
		statementst = statementst + statements
		if statements == 0 then
			bgcolor = ''
		else
			bgcolor = 'color: #202122; background-color: #cfc;'
		end
		--resultTable = resultTable .. result1 .. result2 ..'\n| align=right style="font-size: smaller;"| ' .. ct .. '\n| style="color: #202122; background-color: #cfc;" data-sort-value="' .. string.sub(Id, 2) .. '"|[[d:' .. Id .. '|' .. Id .. ']] \n|style="' .. bgcolor .. 'font-size:smaller;" align=right title="number of main statements on Wikidata item"|'.. statements .. '\n|style="color: #202122; background-color: #cfc;"|' .. commonsstr .. '\n'
		resultTable = resultTable .. result1 .. result2 ..'\n| align=right style="font-size: smaller;"| ' .. ct .. '\n| style="color: #202122; background-color: #cfc;" data-sort-value="' .. string.sub(Id, 2) .. '"|[[d:' .. Id .. '|' .. Id .. ']] \n|style="color: #202122; background-color: #cfc;"|' .. commonsstr .. '\n'
	end
	
	-- footer
	tline = ""
	if timeline > 2 then
		tline = "[https://tools.wmflabs.org/wikidata-timeline/#/timeline?query=" .. mw.uri.encode("items[" .. qids .. "]", "PATH" ) .. " →&nbsp;timeline]"
	end	
	local mapme = ""
	if coords > 0 or images > 0 then
		mapme = "[https://query.wikidata.org/#%23%20click%20%22Execute%22%20to%20run%20the%20query%2C%20then%20%28on%20the%20right%20side%29%20below%20%22Display%22%20select%20the%20link%20%22Map%22%20or%20%22Image%20Grid%22%0ASELECT%20%0A%09%3Fitem%20%0A%09%3FitemLabel%20%0A%09%28GROUP_CONCAT%28DISTINCT%28%3FinstanceLabel%29%3B%20separator%3D%22%2C%20%22%29%20as%20%3Finstance_of%29%0A%09%28SAMPLE%28%3Fcoord%29%20as%20%3Fcoordinates%29%0A%09%28SAMPLE%28%3Fimg%29%20as%20%3Fimage%29%0AWHERE%0A{%0A%09VALUES%20%3Fitem%20{%20" .. wqsitems .. "%20}%09OPTIONAL%20{%3Fitem%20wdt%3AP625%20%3Fcoord1%20}%20.%0A%20%20%09OPTIONAL%20{%3Fitem%20wdt%3AP131%2Fwdt%3AP625%20%3Fcoord2%20}%20.%0A%20%20%09OPTIONAL%20{%3Fitem%20wdt%3AP276%2Fwdt%3AP625%20%3Fcoord3%20}%20.%0A%09BIND%28IF%28!BOUND%28%3Fcoord1%29%2C%28IF%28!BOUND%28%3Fcoord2%29%2C%3Fcoord3%2C%3Fcoord2%29%29%2C%3Fcoord1%29%20as%20%3Fcoord%29%0A%20.%20%20%0A%09OPTIONAL%20{%3Fitem%20wdt%3AP31%20%3Finstance%20}%20.%20%20%20%0A%20%20%09OPTIONAL%20{%3Fitem%20wdt%3AP18%20%3Fimg%20}%20.%20%20%20%0A%09SERVICE%20wikibase%3Alabel%20{%20bd%3AserviceParam%20wikibase%3Alanguage%20%22" .. mylang .. "%2Cen%22%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.%20%3Fitem%20rdfs%3Alabel%20%3FitemLabel%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.%20%3Finstance%20rdfs%3Alabel%20%3FinstanceLabel%20}%0A}%0AGROUP%20BY%20%3Fitem%20%3FitemLabel%20 →"
		if coords > 0 then
			mapme = mapme .. "&nbsp;map"
		end
		if images > 0 then
			mapme = mapme .. "&nbsp;gallery"
		end
		mapme = mapme .. "]"
	end
	local autolist = "[https://tools.wmflabs.org/autolist/?language=" .. mylang .. "&project=wikipedia&wdq=" .. mw.uri.encode("items[" .. qids .. "]", "PATH" ) .. "&run=Run&mode_manual=or&mode_cat=or&mode_wdq=not&mode_wdqs=or&mode_find=or&chunk_size=10000 →&nbsp;Autolist]"
	result1 = '|- align=right style="font-size:smaller" class="sortbottom"\n! Σ \n|'  .. autolist .. mapme .. tline
	result2 = ''
	ct = 0
	for langCount = 1, #langTable do
		result2 = result2 .. '\n |' .. ctt[langCount]
		ct = ct + ctt[langCount]
	end
	--resultTable = resultTable .. result1 .. result2 ..'\n| ' .. ct .. '\n| data-sort-value="999999999999"| avg:&nbsp;<span title="average number of language versions (of ' .. #langTable .. ') per article">' .. math.floor(ct/(index-1)+0.5) .. '</span>\\<span title="average number of articles (of ' .. (index-1) .. ') per language">' .. math.floor(ct / #langTable + 0.5) .. '</span>\\<span title="overall in percent (all: '..  (#langTable * (index-1)) .. ')">' .. math.floor( ct / #langTable / (index-1)*100 + 0.5) .. '%</span>\n|' .. statementst .. '\n| [[File:Commons-logo.svg|16px|Wikimedia Commons]]'
	resultTable = resultTable .. result1 .. result2 ..'\n| ' .. ct .. '\n| data-sort-value="999999999999"| avg:&nbsp;<span title="average number of language versions (of ' .. #langTable .. ') per article">' .. math.floor(ct/(index-1)+0.5) .. '</span>\\<span title="average number of articles (of ' .. (index-1) .. ') per language">' .. math.floor(ct / #langTable + 0.5) .. '</span>\\<span title="overall in percent (all: '..  (#langTable * (index-1)) .. ')">' .. math.floor( ct / #langTable / (index-1)*100 + 0.5) .. '%</span>\n| [[File:Commons-logo.svg|16px|Wikimedia Commons]]'

	resultTable = resultTable .. '\n|}\n'
	return resultTable
end

-- WikimediaCEETable.count: counts articles from the list in certain wiki
-- Sample use: {{#invoke:WikimediaCEETable|count|lang|Q1|Q2|Q3|Q4|Q5}}
-- based on previous "table" function stripped from all the table formatting.

function p.count(frame)
	resultText = ''
	anum = 2
	wikianum = 0
	ctt = {}

	for langCount = 1, #langTable do
		ctt[langCount] = 0          
	end
	
	while frame.args[anum] do          
		Id = frame.args[anum]
		anum = anum + 1
		local entity = mw.wikibase.getEntityObject(Id)

		-- if not entity or not entity.sitelinks then   
		if not entity then
			return '<b>Entity ' .. Id .. ' not found</b>'
		end

		for langCount = 1, #langTable do
			local sitelink = entity:getSitelink( langTable[langCount] .. 'wiki' )
			if sitelink then
				iw = langTable[langCount]
				if iw == "be_x_old" then iw = "be-tarask" end
				ctt[langCount] = ctt[langCount] + 1
			end
		end
	end

	anum = anum - 2                        

	-- number of articles in certain wiki. Straight...
	for langCount = 1, #langTable do
		if langTable[langCount] == frame.args[1] then wikianum = ctt[langCount] end
	end

	resultText = wikianum .. '/' .. anum              -- result is two numbers with '/' separator

	return resultText
end

return p