Module:WikimediaIranUzbekistan

Module documentation
-- WikimediaCEETable: builds a list of articles based on information from Wikidata 
-- Sample use: {{#invoke:WikimediaCEETable|table|Q1|Q2|Q3|Q4|Q5}}
-- Can be used to list 400+ articles on 1 page
-- by User:Voll, with additions by Halibutt, Braveheart, Jura1, Strainu, Yupik, Iketsi
-- Original at https://meta.wikimedia.org/wiki/Module:WikimediaCEETable. Please contribute amendments there and keep copies in sync with that version.

local langTable = { 'fa','en', 'uz'}


local mylang = mw.getCurrentFrame():callParserFunction('int', 'lang')
local mylangObj = mw.language.new( mylang )
local i18nPage = 'Template:WikimediaIranUzbekistan/i18n'
local i18nSubpage = mw.title.new(i18nPage .. '/' .. mylang).exists and (i18nPage .. '/' .. mylang) or (i18nPage .. '/en')

local p = {}

local function def(bla)
  local zdef = {}
  for _, l in ipairs(bla) do zdef[l] = true end
  return zdef
end

local function formatnum(number)
	return mylangObj:formatNum(number)
end

local function formatnumcell(number, extraclasses)
	return string.format(
		'| lang="%s" class="%s" | %s',
		mylangObj:getCode(),
		'wikimedia-cee-table-numeric' .. (extraclasses and (' ' .. extraclasses) or ''),
		formatnum(number)
	)
end

local function i18n(args)
	return mw.getCurrentFrame():expandTemplate{ title = i18nSubpage, args = args }
end

local function getmainsnakid(entity, property)
	local stmt = entity:getBestStatements(property)[1]
	return stmt and stmt.mainsnak.snaktype == 'value' and stmt.mainsnak.datavalue.type == 'wikibase-entityid' and stmt.mainsnak.datavalue.value['id'] or nil
end

local function renderDate(entity, property)
	local stmt = entity:getBestStatements(property)[1]
	if stmt and stmt.mainsnak.snaktype == 'value' and stmt.mainsnak.datavalue.type == 'time' then
		local snak = stmt.mainsnak
		if snak.datavalue.value.precision > 9 then
			-- more precise than year, make it year-precise
			-- make a deep copy to make sure we don't change anything outside of this function
			snak = mw.clone(snak)
			snak.datavalue.value.precision = 9
		end
		return mw.wikibase.renderSnak(snak)
	else
		return nil
	end
end

function p.table(frame)
	local resultTable = {'{| class="wikitable sortable"\n! # !! article !! fa !! en !! uz'}
	index = 1
	ctt = {}
	coords = 0
	images = 0
	timeline = 0
	for langCount = 1, #langTable do
		ctt[langCount] = 0
	end

	-- rows
	while frame.args[index] do
		Id = frame.args[index]
		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
		local Label, LabelLang = entity:getLabelWithLang()
		if not Label then
			Label = ''
			LabelLang = ''
		end
   		local ensitelink = entity:getSitelink( 'enwiki' )
		if ensitelink then
			if Label == '' then
				Label = ensitelink
				LabelLang = 'en'
			end
		end
		local cells = {
			formatnumcell(index),
			'| ', -- the label cell, to be filled later
		}
    	ct = 0
    	
		-- occupation (P106)
		if frame.args['occupation'] then
			local occupationid = getmainsnakid(entity, 'P106')
			if occupationid then
				local occupation, occupationLang = mw.wikibase.getLabelWithLang(occupationid)
				table.insert(cells, '| lang="' .. occupationLang .. '" dir="auto" | ' .. occupation)
			else
				table.insert(cells, '\n| - ')
			end
		end
    	
    	-- columns per row
    	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
				if iw == "fiu_vro" then iw = "fiu-vro" end
				if iw == "roa_rup" then iw = "roa-rup" end
				table.insert(cells, string.format('| class="wikimedia-cee-table-good" | [[:%s:%s| +]]', iw, sitelink))
				ct = ct + 1
				ctt[langCount] = ctt[langCount] + 1
				if Label == '' then
					Label = sitelink
					LabelLang = langTable[langCount]:gsub('_', '-')
				end
			else
				table.insert(cells, '| –')
			end
		end

		if Label ~= '' then
			cells[2] = string.format(
				'| <bdi lang="%s">%s</bdi>',
				LabelLang,
				ensitelink and ('[[:en:' .. ensitelink .. '|' .. Label ..']]') or Label
			)
		end

		-- icon for females (Q6581072 & Q1052281)
		local genderid = getmainsnakid(entity, 'P21')
		if genderid == 'Q6581072' or genderid == 'Q1052281' then
			cells[2] = cells[2] .. ' [[File:Female Icon.svg|link=|alt=♀|18px]]'
		end

		-- years for humans (Q5)
		if getmainsnakid(entity, 'P31') == 'Q5' then
			local p569 = renderDate(entity, 'P569')
			local p570 = nil
			if p569 then
				p570 = renderDate(entity, 'P570')
			end
			cells[2] = cells[2] .. ' ' .. i18n{ 'lifespan', ['birth'] = p569, ['death'] = p570 }
		end
		if (
			entity:getBestStatements( 'P580' )[1] or
			entity:getBestStatements( 'P569' )[1] or
			entity:getBestStatements( 'P571' )[1] or
			entity:getBestStatements( 'P582' )[1] or
			entity:getBestStatements( 'P576' )[1] or
			entity:getBestStatements( 'P577' )[1]
		) then
			timeline = timeline + 1
		end

		local p625 = entity:getBestStatements( 'P625' )[1]
		if p625 then
			local label = i18n{ 'view on maps' }
			local url = "https://geohack.toolforge.org/geohack.php?language=en&pagename=" .. mw.uri.encode( Label, "PATH" ) .. "&params=" .. p625.mainsnak.datavalue.value.latitude .. "_N_" .. p625.mainsnak.datavalue.value.longitude .. "_E"
			cells[2] = cells[2] .. string.format("&nbsp;[[File:Geographylogo.svg|18px|%s|alt=%s|link=%s]]", label, label, url)
			coords = coords + 1
 		end
		if entity:getBestStatements( 'P18' )[1] then
			images = images + 1
		end
		index = index + 1
		
		table.insert(resultTable, '|-\n' .. table.concat(cells, '\n'))
	end

	table.insert(resultTable, '|}\n')
	return table.concat(resultTable, '\n')
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
				if iw == "fiu_vro" then iw = "fiu-vro" 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
Category:Pages with script errors