Modul:List of wikis by project

local p = {}

local ku_lang = require('Modul:Ziman/lîste')
local data = mw.loadData('Module:NUMBEROF/data')
local getArgs = require('Module:Arguments').getArgs
local pageLanguage = mw.language.new(mw.getCurrentFrame():preprocess('{{PAGELANGUAGE}}')) -- for the current page, not the default site language

local function bold(action, param)
		if action == 'articles' then
			return '<b>' .. param .. '</b>'
		else
			return param
		end
end

local function ku_language(lang)
	if  ku_lang[lang] then
		return  ku_lang[lang].nav
	else
		return mw.language.fetchLanguageName(lang, 'ku')
	end
end

local function english_link(lang)
	title = string.format('zimanê %s', ku_language(lang), ku_language(lang))
	
	if mw.title.new(title, 0).exists == true then
		return ''
	else
		return string.format('[[:en:%s language|%s]]', mw.language.fetchLanguageName(lang, 'en'), ' <small>(en)</small>')
	end

end

local function getLinkedDataCell(interwiki, specialpage, stats, action)
	return string.format('|| [[%s:Special:%s|%s]]', interwiki, specialpage, bold(action, pageLanguage:formatNum(stats[data.map[action]])))
end

local function getDepth(stats)
	-- https://meta.wikimedia.org/wiki/Wikipedia_article_depth
	-- This gives silly results if, for example, the number of articles is small.
	local n = { 'articles', 'edits', 'pages' }
	for i, v in ipairs(n) do
		n[i] = data.map[v]
	end
	for i, v in ipairs(n) do
		n[i] = stats[v] or 0
	end
	local articles, edits, pages = n[1], n[2], n[3]
	if pages == 0 or articles == 0 then
		return 0
	end
	return math.floor((edits/pages) * ((pages - articles)/articles)^2)
end

p.main = function(frame)
	local args = getArgs(frame)
	
	local limit1 = (tonumber(args["lim1"]) or 0)
	local limit2 = (tonumber(args["lim2"]) or 100000000)
	
	local project = args[1]
	local ranks = mw.loadData('Module:NUMBEROF/' .. (project == 'wikipedia' and 'rank' or 'other'))[project]
	if ranks == nil then
		return '<strong class="error">Invalid project: ' .. (project or '{{{1}}}') .. '</span>'
	else
		local rows = {}
		-- mw.logObject(data)
		for i, lang in ipairs(ranks.rankByIndex) do
			local siteData = data.data[lang .. '.' .. project]
			if tonumber(siteData[data.map['articles']]) > limit1 and tonumber(siteData[data.map['articles']]) < limit2 then
				local row = {
					'| ' .. pageLanguage:formatNum(i),
					string.format(
						'|| [[zimanê %s|%s]] %s', 
						ku_language(lang), 
						mw.getContentLanguage():ucfirst(ku_language(lang)),
						english_link(lang)
					),
					'|| ' .. mw.language.fetchLanguageName(lang),
					string.format('|| [[:%s:|%s]]', lang, lang),
				}
				local interwiki = project == 'wikipedia' and (':' .. lang) or (project .. ':' .. lang)
				table.insert(row, getLinkedDataCell(interwiki, 'Statistics', siteData, 'articles'))
				table.insert(row, getLinkedDataCell(interwiki, 'AllPages', siteData, 'pages'))
				table.insert(row, getLinkedDataCell(interwiki, 'RecentChanges', siteData, 'edits'))
				table.insert(row, getLinkedDataCell(interwiki, 'ListUsers/sysop', siteData, 'admins'))
				table.insert(row, getLinkedDataCell(interwiki, 'ListUsers', siteData, 'users'))
				table.insert(row, getLinkedDataCell(interwiki, 'ActiveUsers', siteData, 'activeusers'))
				table.insert(row, getLinkedDataCell(interwiki, 'NewFiles', siteData, 'files'))
				
				if project == 'wikipedia' then
					table.insert(row, '|| ' .. pageLanguage:formatNum(getDepth(siteData)))
				end
				
				if (lang == 'ku') then
					table.insert(rows, '|- id="ku" style="background-color:#FFDEAD"\n' .. table.concat(row, ' '))
				elseif ( lang == 'ckb') then
					table.insert(rows, '|- id="ckb" style="background-color:#FFFACD"\n' .. table.concat(row, ' '))
				elseif ( lang == 'diq') then
					table.insert(rows, '|- id="diq" style="background-color:#FFFACD"\n' .. table.concat(row, ' '))
				else
					table.insert(rows, '|-id="' .. lang .. '" \n' .. table.concat(row, ' '))
				end
				
			elseif not siteData then
				table.insert(row, '|| colspan="8" class="error" | No data available')
				table.insert(rows, table.concat(row, ' '))
			end
		end
		return table.concat(rows, '\n|-\n')
	end
end

function p.topN(frame)
	local args = getArgs(frame)
	local project = args[1] or ''
	local n = tonumber(args[2]) or -1
	local count = 0
	for site, siteData in pairs(data.data) do
		if site:find('%.' .. project .. '$') and not site:find('^total') and siteData[data.map['articles']] > n then
			count = count + 1
		end
	end
	return count
end

function p.rank(frame)
	local args = getArgs(frame)
	local project = args[1]
	local kod = args[2]
	
	local ranks = mw.loadData('Module:NUMBEROF/' .. (project == 'wikipedia' and 'rank' or 'other'))[project]
	
	for i, lang in ipairs(ranks.rankByIndex) do
		local siteData = data.data[lang .. '.' .. project]
		if lang == kod then
			return i
		end
	end
end

return p