Module:ړنګېدو ټولپوښتنې/خونديځ

لاسوند لپاره ددې موډيول کېدای سی په Module:ړنګېدو ټولپوښتنې/خونديځ/لاسوند کي وي

require('strict')

local p = {}

local numConv = require('Module:Numeral converter').convert

local showPercentage = function (all, thisCount, frame)
	if all == 0 then
		return "'''۰'''"
	else
		return frame:expandTemplate{ title = 'شمېر او سلنه', args = {thisCount, all, 1, size = 'small', disp = 'br()', bold = 'yes'} } 
	end
end

local frame = mw.getCurrentFrame()
local col_begin = frame:expandTemplate{title = 'Col-begin'}
local col_break = frame:expandTemplate{title = 'Col-break'}
local col_end = frame:expandTemplate{title = 'Col-end'}

local langPs = mw.language.new('ps')
local langEn = mw.language.new('en')
local monthNames = {
	'جنوري',
	'فېبروري',
	'مارچ',
	'اپرېل',
	'مۍ',
	'جون',
	'جولای',
	'اګست',
	'سېپټمبر',
	'اکتوبر',
	'نومبر',
	'ډېسمبر',
}

local gCount = {
	d = 0,
	k = 0,
	m = 0,
	years = {
		d = {},
		k = {},
		m = {}
	}
}

function p.makeGeneralGraphs (frame, countTable)
	local date = os.date('*t')
	local iyear = 2006
	local currentYear = date.year
	
	local function allClosures (tbl, year)
		local all = tbl.years.k[year] +
					tbl.years.d[year] +
					tbl.years.m[year]
		return all
	end
	
	local function makeString (c)
		local dGroup, kGroup, mGroup = '', '', ''
		local barChartWidth = 45 * ((currentYear - iyear) + 1)
		
		local s  = col_begin .. '\n' .. col_break .. '\n'
		
		s = s .. '{{#invoke:Chart|pie chart|radius=150|percent=true' ..
			'|slice 1=' .. numConv('en', c.d) .. ':ړنګېدل:#d33' ..
			'|slice 2=' .. numConv('en', c.k) .. ':پرېښودل:#00af89' ..
			'|slice 3=' .. numConv('en', c.m) .. ':اخږل:#36c' ..
			'}}' .. '\n' .. col_break
		
		s = s .. '{{#invoke:Chart|bar chart|stack=1|height=400' ..
		'|colors=#d33:#00af89:#36c|group names=ړنګېدل:پرېښودل:اخږل' ..
		'|width=' .. barChartWidth
		
		repeat
			dGroup = dGroup .. c.years.d[numConv('ps', iyear)] .. ':'
			iyear = iyear + 1
		until iyear > currentYear
		s = s .. '|group 1=' .. mw.ustring.sub(dGroup, 1, -2)
		
		iyear = 2006
		repeat
			kGroup = kGroup .. c.years.k[numConv('ps', iyear)] .. ':'
			iyear = iyear + 1
		until iyear > currentYear
		s = s .. '|group 2=' .. mw.ustring.sub(kGroup, 1, -2)
		
		iyear = 2006
		repeat
			mGroup = mGroup .. c.years.m[numConv('ps', iyear)] .. ':'
			iyear = iyear + 1
		until iyear > currentYear
		s = s .. '|group 3=' .. mw.ustring.sub(mGroup, 1, -2)
		
		iyear = 2006
		s = s .. '|x legends='
		repeat
			s = s .. '[[#' .. numConv('ps', iyear) .. '|' .. numConv('ps', iyear) .. ']]:'
			iyear = iyear + 1
		until iyear > currentYear
		s = mw.ustring.sub(s, 1, -2)
		
		s = s .. '}}' .. '\n' .. col_end
		
		iyear = 2006
		local comparisonKeep, comparisonDelete, ratio, ryear
		local baseRatio = c.years.d['۲۰۰۶'] / c.years.k['۲۰۰۶']
		repeat
			comparisonKeep = c.years.k[numConv('ps', iyear)]
			comparisonDelete = c.years.d[numConv('ps', iyear)]
			ratio = comparisonDelete / comparisonKeep
			if ratio < baseRatio then 
				baseRatio = math.floor(ratio * 100) / 100
				ryear = numConv('ps', iyear)
			end
			iyear = iyear + 1
		until iyear > currentYear
		
		s = s .. '\n* تا این لحظه سال ' .. ryear .. ' با ' .. numConv('ps', baseRatio) ..
			' حذف به ازای هر جمع‌بندی منجر به ماندن (نسبت ۱ به ' .. numConv('fa', baseRatio) ..
			') دارای بالاترین نسبت ماندن به حذف بوده است.'
		
		iyear = 2006
		comparisonDelete = c.years.d['۲۰۰۶']
		local thisAll = 0
		local basePerc = (comparisonDelete * 100) / allClosures(c, '۲۰۰۶')
			  basePerc = math.floor(basePerc * 100) / 100
		local perc
		local deletePercentageTopYear = ''
		repeat
			thisAll = allClosures(c, numConv('fa', iyear))
			comparisonDelete = c.years.d[numConv('fa', iyear)]
			perc = (comparisonDelete * 100) / thisAll
			if perc > basePerc then
				basePerc = math.floor(perc * 10) / 10
				deletePercentageTopYear = numConv('fa', iyear)
			end
			iyear = iyear + 1
		until iyear > currentYear
		
		s = s .. '\n* در حال حاضر سال ' .. deletePercentageTopYear ..
			' نسبت به سایر سال‌ها دارای بالاترین درصد حذف (٪' ..
			numConv('fa', basePerc) .. ') در مقابل کل جمع‌بندی‌های آن سال بوده است.'
	
		iyear = 2006
		local allC = allClosures(c, '۲۰۰۶')
		local allYear
		thisAll = 0
		repeat
			thisAll = allClosures(c, numConv('fa', iyear))
			if thisAll > allC then
				allC = thisAll
				allYear = numConv('fa', iyear)
			end
			iyear = iyear + 1
		until iyear > currentYear
		
		s = s .. '\n* سال ' .. allYear .. ' با مجموع ' .. numConv('ps', allC) ..
			' جمع‌بندی رکورددار تعداد جمع‌بندی از ابتدا تاکنون بوده است.'
		
		return frame:preprocess(s)
	end
	return frame:expandTemplate{
		title = 'پټ',
		args = {
			'دد ړنګولو لپاره ټولپوښتنې ټوليزه احصايوي ډېټا ',
			makeString(countTable),
			background = '#E5E4E2'
		}
	}
end

function p.getDate (frmt, obj, l)
	if l == 'en' then
		return langEn:formatDate(frmt, obj or 'now')
	else
		return langFa:formatDate(frmt, obj or 'now')
	end
end

function p.count (arc)
	local c = {
		a = 0,	-- all
		d = 0,	-- delete
		k = 0,	-- keep
		m = 0	-- merge
	}
	local titleObj = mw.title.new('ويکيپېډيا:ړنګېدو ټولپوښتنې/خونديځ/' .. arc)
	local content = titleObj:getContent()
	
	if (titleObj.exists) then
		for i in string.gfind(content, 'ړنګېدل') do
			c.d = c.d + 1
		end
		
		for i in string.gfind(content, 'پرېښودل') do
			c.k = c.k + 1
		end
		
		for i in string.gfind(content, 'اخږل') do
			c.m = c.m + 1
		end
		
		c.a = c.d + c.k + c.m
	end
	
	return c
end

function p.makeGraph(frame, year, months, count, scale, style)
	
	local columns = #months
	local targs = {}
	
	local i = 1
	repeat
		if count[months[i]].a > 0 then
			table.insert(targs, months[i])
		else
			columns = columns - 1
		end
		i = i + 1
	until i > columns
	
	table.insert(targs, '#36c')
	table.insert(targs, 'ادغام')
	
	for _, month in pairs(months) do
		if count[month].a > 0 then
			table.insert(targs, count[month].m)
		end
	end
	
	table.insert(targs, '#00af89')
	table.insert(targs, 'پرېښودل')
	
	for _, month in pairs(months) do
		if count[month].a > 0 then
			table.insert(targs, count[month].k)
		end
	end
	
	table.insert(targs, '#d33')
	table.insert(targs, 'ړنګېدل')
	
	for _, month in pairs(months) do
		if count[month].a > 0 then
			table.insert(targs, count[month].d)
		end
	end
	
	local cur_year_style = (columns > 8) and 5 or 6
	local cur_year_scale = (cur_year_style % 2 == 0) and 1.5 or columns / 3
	
	targs.style   = style or cur_year_style
	targs.scale   = scale or cur_year_scale
	targs.title   = 'د ړټپ کلنۍ ډلبندي ' .. tostring(year)
	targs.columns = columns
	targs.align   = 'center'
	
	 
	return frame:expandTemplate{ title = 'مالوماتي بارونه', args = targs }
end

function p.currentYear (frame)
	local date = os.date('*t')
	local monthIndex = date.month
	local i = tonumber(monthIndex) - 1
	local n = '\n'
	local fayear = numConv('fa', date.year)
	local text = '==' .. fayear .. '==' .. n
	local months = {}
	local countForGraph = {}
	
	gCount.years.d[fayear] = 0
	gCount.years.k[fayear] = 0
	gCount.years.m[fayear] = 0
	
	repeat
		local month = monthNames[monthIndex - i]
		table.insert(months, month)
		i = i - 1
	until i < tonumber(monthIndex) - tonumber(monthIndex)
	
	local tbl = mw.html.create('table')
	tbl
		:attr('class', 'wikitable sortable jquery-tablesorter')
			:tag('tr')
				:tag('th')
					:attr('rowspan', '2')
					:wikitext('مياشت')
					:css('width', '70px')
				:tag('th')
					:attr('rowspan', '2')
					:wikitext('ټول<br/><small>(باقي)</small>')
					:css('width', '32px')
				:tag('th')
					:attr('colspan', '3')
					:wikitext('پايلې')
				:done()
			:tag('tr')
				:tag('th')
					:wikitext('ړنګېدل<br/><small>(٪ له ټولو)</small>')
					:css('width', '44px')
				:tag('th')
					:wikitext('پرېښودل<br/><small>(٪ له ټولو)</small>')
					:css('width', '44px')
				:tag('th')
					:wikitext('اخږل<br/><small>(٪ له ټولو)</small>')
					:css('width', '44px')
				:done()
	
	local length = table.maxn(months)
	local j = 0
	local sortkey = length
	repeat
		local monthname = monthNames[monthIndex - j]
		local ttl = monthname .. ' ' .. numConv('ps', date.year)
		local cnt = p.count(ttl)
		
		-- populate general counts table
		gCount.d = gCount.d + cnt.d
		gCount.k = gCount.k + cnt.k
		gCount.m = gCount.m + cnt.m
		
		gCount.years.d[fayear] = gCount.years.d[fayear] + cnt.d
		gCount.years.k[fayear] = gCount.years.k[fayear] + cnt.k
		gCount.years.m[fayear] = gCount.years.m[fayear] + cnt.m
		
		-- "ncy" for "not closed yet";
		-- will be populated by "pages in current month's category minus total closed discussions
		local ncy = 0
		if ttl == monthNames[date.month] .. ' ' .. numConv('fa', date.year) or	-- current or previous month
		ttl == monthNames[date.month - 1] .. ' ' .. numConv('fa', date.year) then
			ncy = frame:callParserFunction( 'PAGESINCATEGORY', { 'ړنګېدو ټولپوښتنې په ' .. ttl, 'R' } ) - cnt.a
		end
		tbl
			:tag('tr')
				:tag('td')
				:attr('data-sort-value', sortkey)
					:wikitext('[' .. '[ويکيپېډيا:ړنګېدو ټولپوښتنې/خونديځ/' .. ttl .. '|' .. ttl .. ']]')
				
				:tag('td')
					:css('text-align', 'center')
					:wikitext(
						"'''" ..
						frame:expandTemplate{
							title = 'Category link if exists 2',
							args= {
								'ړنګېدو ټولپوښتنه په ' .. ttl,
								numConv('fa', cnt.a)
							}
						} ..
						"'''" ..
						(tonumber(ncy) > 0 and '<br/><small>(' .. numConv('fa', ncy) .. ')</small>' or '')
						)
				:tag('td')
					:css('text-align', 'center')
					:wikitext(showPercentage(cnt.a, cnt.d, frame))
				:tag('td')
					:css('text-align', 'center')
					:wikitext(showPercentage(cnt.a, cnt.k, frame))
				:tag('td')
					:css('text-align', 'center')
					:wikitext(showPercentage(cnt.a, cnt.m, frame))
				:done()
		j = j + 1
		countForGraph[monthname] = cnt
		sortkey = sortkey - 1
	until  j == length
	
	-- تا زمانی که دست کم ۵ جمع‌بندی در ماه فوریه انجام نشده باشد، نمودار بدشکل تک‌میله‌ای یا با میلهٔ دوم کوچک نمایش داده نمی‌شود
	local noGraph = false
	if monthNames[monthIndex] == 'فېبروري' then
		if (countForGraph[monthNames[monthIndex]].a + countForGraph[monthNames[monthIndex]].d + countForGraph[monthNames[monthIndex]].k + countForGraph[monthNames[monthIndex]].m == 0) or 
			(countForGraph[monthNames[monthIndex]].a + countForGraph[monthNames[monthIndex]].d + countForGraph[monthNames[monthIndex]].k + countForGraph[monthNames[monthIndex]].m < 5) then
			noGraph = true
		end
	elseif monthNames[monthIndex] == 'جولای' then
		noGraph = true
	end
	
	text = text .. col_begin .. n .. col_break .. n .. tostring(tbl)
	
	if (noGraph) then
		text = text .. n .. col_break .. n ..
		frame:expandTemplate{
			title = 'پاملرنه',
			args = {
				style = 'width:80%',
				content = 'ځکه چې په نوي کال کې جوړ شوي لنډیز د چارټ چمتو کولو لپاره کافي ندي،' ..
				' لنډیز چارټ پدې وخت کې شتون نلري.'
			}
		}
		.. n .. col_end
	else
		text = text .. n .. col_break .. n ..
			   p.makeGraph(frame, numConv('ps', date.year), months, countForGraph)
			   .. n .. col_end
	end
	
	return text
	
end

function p.prevYear(frame)
	local y = p.getDate('Y', '-1 year', 'ps')
	local text = '== ' .. y .. ' ==\n'
	local tbl = mw.html.create('table')
	local ttl, month, cnt
	local sortkey = 12
	local countForGraph = {}
	
	gCount.years.d[y] = 0
	gCount.years.k[y] = 0
	gCount.years.m[y] = 0
	
	tbl
		:attr('class', 'wikitable sortable jquery-tablesorter')
		:tag('tr')
			:tag('th')
				:attr('rowspan', '2')
				:wikitext('ميات')
				:css('width', '70px')
			:tag('th')
				:attr('rowspan', '2')
				:wikitext('ټول')
				:css('width', '32px')
			:tag('th')
				:attr('colspan', '3')
				:wikitext('پايله')
			:done()
		:tag('tr')
			:tag('th')
				:wikitext('ړنګېدل')
				:css('width', '44px')
			:tag('th')
				:wikitext('پرېښودل')
				:css('width', '44px')
			:tag('th')
				:wikitext('اخږل')
				:css('width', '44px')
		:done()
	for i = #monthNames, 1, -1 do
		month = monthNames[i]
		ttl = month .. ' ' .. y
		cnt = p.count(ttl)
		
		-- populate general counts table
		gCount.d = gCount.d + cnt.d
		gCount.k = gCount.k + cnt.k
		gCount.m = gCount.m + cnt.m
		
		gCount.years.d[y] = gCount.years.d[y] + cnt.d
		gCount.years.k[y] = gCount.years.k[y] + cnt.k
		gCount.years.m[y] = gCount.years.m[y] + cnt.m
		
		countForGraph[month] = cnt
		tbl
			:tag('tr')
				:tag('td')
				:attr('data-sort-value', sortkey)
					:wikitext('[' .. '[ويکيپېډيا:ړنګېدو ټولپوښتنې/خونديځ/' .. ttl .. '|' .. ttl .. ']]')
				
				:tag('td')
					:css('text-align', 'center')
					:wikitext(numConv('ps', cnt.a))
				:tag('td')
					:css('text-align', 'center')
					:wikitext(numConv('ps', cnt.d))
				:tag('td')
					:css('text-align', 'center')
					:wikitext(numConv('ps', cnt.k))
				:tag('td')
					:css('text-align', 'center')
					:wikitext(numConv('ps', cnt.m))
				:done()
		sortkey = sortkey - 1
	end
	
	text = text .. col_begin .. '\n' .. col_break .. '\n' .. tostring(tbl) .. '\n' ..
	col_break .. '\n' .. p.makeGraph(frame, y, monthNames, countForGraph, 1.7, 1) .. '\n' .. col_end
	
	return text
	
end

function p.otherYears()
	local hy = p.getDate('Y', '-2 years', 'en')	-- Highest year
	local ly = 2006								-- lowest year
	local lm = 'مارچ ۲۰۰۶'						-- to break the loop when we reach the oldest archive
	local yi = tonumber(hy)
	local years = {}
	local text = '== ' .. 'څخه دمخه خونديځ ' .. p.getDate('Y', '-1 year', 'ps') .. ' =='
	
	repeat 
		table.insert(years, yi)
		yi = yi - 1
	until yi - ly < 0
	
	local tbl = mw.html.create('table')
	tbl
		:attr('class', 'wikitable collapsible collapsed')
		:css('width', '372px')
		:tag('tr')
			:tag('th')
				:attr('rowspan', '2')
				:wikitext('ماه')
			:tag('th')
				:attr('rowspan', '2')
				:wikitext('کل')
			:tag('th')
				:attr('colspan', '3')
				:wikitext('نتایج')
			:done()
		:tag('tr')
			:tag('th')
				:wikitext('حذف')
			:tag('th')
				:wikitext('بماند')
			:tag('th')
				:wikitext('ادغام')
		:done()
	for k, v in pairs(years) do
		local yr = v
		tbl
			:tag('tr')
				:tag('td')
					:attr('colspan', '5')
					:attr('id', numConv('fa', yr))
					:css('background', '#39F none repeat scroll 0% 0%')
					:css('text-align', 'center')
					:wikitext("'''" .. numConv('fa', yr) .. "'''")
				:done()
			
			for i = #monthNames, 1, -1 do
				local month = monthNames[i]
				local ttl = month .. ' ' .. numConv('fa', yr)
				if ttl == lm then 
					break
				end
				local cn = p.count(ttl)
				
				-- populate general counts table
				gCount.d = gCount.d + cn.d
				gCount.k = gCount.k + cn.k
				gCount.m = gCount.m + cn.m
				
				gCount.years.d[numConv('fa', yr)] = (gCount.years.d[numConv('fa', yr)] or 0) + cn.d
				gCount.years.k[numConv('fa', yr)] = (gCount.years.k[numConv('fa', yr)] or 0) + cn.k
				gCount.years.m[numConv('fa', yr)] = (gCount.years.m[numConv('fa', yr)] or 0) + cn.m
				
				tbl
					:tag('tr')
						:tag('td')
							:wikitext('[' .. '[ویکی‌پدیا:نظرخواهی برای حذف/بایگانی/' .. ttl .. '|' .. ttl .. ']]')
						:tag('td')
							:css('text-align', 'center')
							:wikitext(numConv('fa', cn.a))
						:tag('td')
							:css('text-align', 'center')
							:wikitext(numConv('fa', cn.d))
						:tag('td')
							:css('text-align', 'center')
							:wikitext(numConv('fa', cn.k))
						:tag('td')
							:css('text-align', 'center')
							:wikitext(numConv('fa', cn.m))
						:done()
			end
		
	end
	text = text .. '\n' .. tostring(tbl)
	return text
	
end

function p.main(frame)
	local nsIsValid = mw.title.getCurrentTitle().namespace == 4
	local output = p.currentYear(frame) .. '\n' .. p.prevYear(frame) .. '\n' .. p.otherYears()
		.. (nsIsValid and '[[رده:بحث‌های نبح| بایگانی]]' or '')
	output = frame:expandTemplate{ title = 'ویکی‌پدیا:نظرخواهی برای حذف/بایگانی/بالا'}
		.. '\n' .. p.makeGeneralGraphs(frame, gCount)
		.. '\n' .. output
	return output
end

return p