پودمان:الگو:زبان‌های ویکی‌پدیا

از ویکی‌پدیا، دانشنامهٔ آزاد
توضیحات پودمان[نمایش] [ویرایش] [تاریخچه] [پاکسازی]

استفاده[ویرایش]

{{#invoke:الگو:زبان‌های ویکی‌پدیا|main}}

نتیجه:

۱٬۰۰۰٬۰۰۰+ نوشتار
۲۵۰٬۰۰۰+ نوشتار
۱۰۰٬۰۰۰+ نوشتار

local p = {}

local function getWikipediaArticlesCount()
	local result = {}
	-- https://commons.wikimedia.org/wiki/Data:Wikipedia_statistics/data.tab
	local statistics = mw.ext.data.get('Wikipedia statistics/data.tab')
	local fields = {}
	for i, v in ipairs(statistics.schema.fields) do fields[v.name] = i end
	for i, v in ipairs(statistics.data) do
		if v[fields.site]:find('.wikipedia$') and not v[fields.site]:find('^total') then
			table.insert(result, { string.gsub(v[fields.site], '.wikipedia', ''), v[fields.articles] })
		end
	end
	table.sort(result, function (k1, k2) return k1[2] > k2[2] end)
	return result
end

local function firstToUpper(str) -- https://stackoverflow.com/a/2421746
	return mw.ustring.gsub(str, '^%l', mw.ustring.upper)
end
		
function p.main()
	local articlesCount = getWikipediaArticlesCount()
	local result = '<div class="wikipedia-languages nourlexpansion">'
	local index = 1
	local contentLanguage = mw.getContentLanguage()
	for i, v in ipairs({ 1000000, 250000, 100000 }) do
		local count = contentLanguage:formatNum(v)
		result = result .. [[
	<div class="wikipedia-languages-count-container">
		<div class="wikipedia-languages-prettybars"></div>
		<div role="heading" class="wikipedia-languages-count">]] .. count .. [[+ نوشتار</div>
		<div class="wikipedia-languages-prettybars"></div>
	</div>
	<ul class="wikipedia-languages-langs hlist hlist-separated inline">
]]
		while articlesCount[index] and articlesCount[index][2] >= v do
			local lang = articlesCount[index][1]
			-- as enwiki, seems only macrolanguages on the top section and let's skip Persian
			if lang ~= 'ceb' and lang ~= 'war' and lang ~= 'fa' then
				local persianName = mw.language.fetchLanguageName(lang, 'fa')
				if lang == 'azb' then persianName = 'ترکی آذربایجانی' end
				if lang == 'lld' then persianName = 'لادینو' end
				local nativeName = firstToUpper(mw.language.fetchLanguageName(lang))
				local languageLink = 'زبان ' .. persianName
				local interwikiCode = lang
				if lang == 'simple' then
					lang = 'en'; persianName = 'انگلیسی ساده'
					languageLink = 'ویکی‌پدیای ' .. persianName
				end
				result = result .. '<li title="ویکی‌پدیای ' .. persianName ..
					' با ' .. contentLanguage:formatNum(articlesCount[index][2]) ..
					' نوشتار"><span lang="' .. lang .. '">[[:' .. interwikiCode .. ':|' ..
					nativeName .. ']]</span>&nbsp;<small>([[' .. languageLink ..
					'|' .. persianName  .. ']])</small></li>'
			end
			index = index + 1
		end
		result = result .. '</ul>'
	end
	return result .. '</div>'
end

return p