Modul:NumberOf

QIRIMTATAR VİKİPEDİYASINIÑ MALÜMATI

Documentation for this module may be created at Modul:NumberOf/doc

-- Модуль для шаблонов серии NUMBEROF и страницы [[Википедия:Список Википедий]]
local p = {}

-- Важнейшие переменные
local mwlang = mw.getContentLanguage()
local langs = mw.loadData('Module:NumberOf/lang')

-- Разделы Википедии, закрытые от редактирования
local readOnly = {
	aa = true,
	cho = true,
	ho = true,
	hz = true,
	ii = true,
	kj = true,
	kr = true,
	mh = true,
	mo = true,
	mus = true,
	ng = true
}

-- Проверка пустоты параметра
local function isEmpty(s)
	return s == nil or s == ''
end

-- Округление до сотых
local function round(n)
	return math.floor(n * 100) / 100
end

-- Форматирование даты
local function formatDate(val)
	return mwlang:formatDate('j xg Y, G:i',val) .. ' (UTC)'
end

-- Длина таблицы
local function tableLength(t)
	local count = 0
	for _ in pairs(t) do count = count + 1 end
	return count
end

-- Рендеринг необходимого параметра из страницы с данными
local function renderNum(f,info)
	-- Парсинг параметров шаблона
	local wiki = f.wiki
	local param = f.param
	local fmt = f.fmt
	
	-- Если нет обязательных параметров, выводится ноль
	local result
	if isEmpty(wiki) or isEmpty(param) then
		result = 0
	else
		if param == 'date' then
			result = formatDate(info['total']['date'])
			return result
		end
		
		-- Расчёты для общего числа разделов
		if wiki == 'total' then
			if param == 'all' then
				result = tableLength(info) - 1
			end
			if param == 'active' then
				result = tableLength(info) - 1 - tableLength(readOnly)
			end
		end
		
		local obj = info[wiki]
		if obj ~= nil then
			-- Убираем NUMBEROF из легаси-кода параметров
			param = param:lower():gsub('numberof','')
			
			if param ~= nil and info[wiki][param] ~= nil then
				result = info[wiki][param]
				
				-- Форматируем значение, если задан параметр
				if not isEmpty(fmt) and type(result) == 'number' then
					if param == 'depth' then
						result = math.floor(result * 100) / 100
					end
					result = mwlang:formatNum(result)
				end
			end
		else
			result = 0
		end
	end
	return tostring(result)
end

-- Функция для вывода в {{NUMBEROF}}
function p.Now(frame)
	local data = mw.loadData('Модуль:NumberOf/data')
	
	return renderNum(frame.args,data)
end

-- Функция для вывода в {{TODAYNUMBEROF}}
function p.Today(frame)
	local data = mw.loadData('Модуль:NumberOf/today')
	
	return renderNum(frame.args,data)
end

-- Вывод ссылки на языковой раздел
local function renderLink(val,text)
	local text = (isEmpty(text) and val or text)
	
	local result = ''
	if val ~= 'ru' then
		result = result .. '[[:' .. val ..  ':|'
		
		if readOnly[val] == true then
			result = result .. '<span style="color:#222; text-decoration:inherit; -moz-text-decoration-color:#222; text-decoration-color:#222;"><s title="Данный раздел закрыт и доступен только в режиме для чтения">' .. text .. '</s></span>'
		else
			result = result .. text
		end
		
		result = result .. ']]'
	else
		result = result .. text
	end
	return result
end

-- Вывод названия языка
local function renderLang(val)
	local text = ''
	local result = ''
	if not isEmpty(langs[val]) then
		text = langs[val][1]
		if not isEmpty(langs[val][2]) then
			result = '[[' .. langs[val][2] .. '|'
		else
			result = '[[:' .. val ..  ':|'
		end
	else
		text = mwlang:ucfirst(mw.language.fetchLanguageName(val))
		result = '[[:' .. val ..  ':|'
	end

	if val == 'ru' then
		result = result .. '<span style="color:#222; margin-right:4px; text-decoration:inherit; -moz-text-decoration-color:#222; text-decoration-color:#222;">' .. text .. '</span>]]<span class="nomobile">[[File:Mw-unwatch-icon.svg|16px|text-top|alt=★|link=]]</span>'
	else
		if readOnly[val] == true then
			result = result .. '<span style="color:#222; text-decoration:inherit; -moz-text-decoration-color:#222; text-decoration-color:#222;"><s style="color:#222;" title="Данный раздел закрыт и доступен только в режиме для чтения">' .. text .. '</s></span>'
		else
			result = result .. text
		end
		result = result .. ']]'
	end
	
	return result
end

-- Функция для вывода ячейки
local function createCell(val,style,fmt,stats)
	local result = '|'
	if not isEmpty(fmt) then
		result = result .. 'data-sort-value="' .. val .. '" '
	end
	result = result .. 'style="border:0; ' .. (not isEmpty(style) and style or 'padding:4px 24px 4px 0;') .. '"| '
	
	if not isEmpty(stats) then
		result = result .. '[[:' .. stats .. '|<span style="color:#222; text-decoration:inherit; -moz-text-decoration-color:#222; text-decoration-color:#222;">'
	end
	
	result = result .. (not isEmpty(fmt) and mwlang:formatNum(val) or val) .. ' '
	if not isEmpty(stats) then
		result = result .. '</span>]]'
	end
	
	return result
end

-- Функция для вывода ряда таблицы
local function createRow(key,val)
	local result = '|- style="'
	local style, originalStyle = ''
	if key == 'ru' then
		result = result .. 'font-weight:bold; '
		style = 'background:#d5fdf4; '
		originalStyle = style .. 'padding:4px 24px 4px 0;'
	end
	result = result .. 'text-align:right; vertical-align:top;"\n'
	
	result = result .. createCell(val['pos'],style .. 'font-weight:normal; padding:4px 24px 4px 0;')
					.. '|' .. createCell(renderLink(key),style .. 'font-weight:normal; padding:4px 24px 4px 0; white-space:nowrap;')
					.. '|' .. createCell(renderLang(key),style .. 'padding:4px 24px 4px 0; text-align:left;')
					.. '|' .. createCell(val['articles'],originalStyle,true,key .. ':' .. 'Special:Statistics')
					.. '|' .. createCell(val['pages'],originalStyle,true)
					.. '|' .. createCell(val['edits'],originalStyle,true)
					.. '|' .. createCell(round(val['depth']),originalStyle,true)
					.. '|' .. createCell(val['users'],originalStyle,true,key .. ':' .. 'Special:ListUsers')
					.. '|' .. createCell(val['activeusers'],originalStyle,true,key .. ':' .. 'Special:ActiveUsers')
					.. '|' .. createCell(val['admins'],originalStyle,true,key .. ':' .. 'Special:ListAdmins')
					.. '|' .. createCell(val['files'],originalStyle,true)
	
	return result .. '\n'
end

-- Функция для вывода шапки таблицы
local function createHeader()
	local cell = 'background-color:transparent; padding-bottom:2px; padding-right:24px;'
	local cellLang = 'background-color:transparent; padding-bottom:2px; text-align:left; min-width:25%;'
	
	local result = '\n{| class="mw-datatable sortable" style="border:0; font-feature-settings:\'tnum\' 1; margin:4px 0 0; width:100%;"\n'
				.. '|- style="border-bottom:1px solid #a2a9b1; text-align:right;"\n'
				.. '!style="border:0; ' .. cell .. '"| №'
				.. '|' .. createCell('Код',cell)
				.. '|' .. createCell('Язык',cellLang)
				.. '|' .. createCell('Статей',cell)
				.. '|' .. createCell('Страниц',cell)
				.. '|' .. createCell('Правок',cell)
				.. '|' .. createCell('<abbr title="Глубина">Гл.</abbr>',cell)
				.. '|' .. createCell('Участников',cell)
				.. '|' .. createCell('(<abbr title="Активных участников">акт.</abbr>)',cell)
				.. '|' .. createCell('<abbr title="Администраторов">Адм.</abbr>',cell)
				.. '|' .. createCell('Файлов',cell)
	
	return result .. '\n'
end

-- Функция для вывода подвала таблицы
local function createFooter(frame,hide)
	-- Параметр hide отвечает за оптическое выравнивание таблиц
	local val = frame['total']
	local cell = 'background:transparent; padding-right:24px; ' .. (isEmpty(hide) and 'padding-top:2px;' or '')
	local cellHide = 'padding-right:24px;'  .. (isEmpty(hide) and ' padding-top:2px;' or '') .. ' visibility:hidden; white-space:nowrap;'
	
	-- aa — закрытый раздел с самой большой глубиной
	local num = tableLength(frame) - 1
	local depth = (isEmpty(hide) and round(val['depth']) or round(frame['aa']['depth']))
	local result = '\n|- style="border-top:1px solid; font-weight:bold; text-align:right; vertical-align:top;'
				.. (isEmpty(hide) and ' border-top-color:#a2a9b1;' or ' line-height:0; visibility:hidden; border-top-color:transparent;') .. '"'
				.. (isEmpty(hide) and '' or ' class="nomobile"') .. '\n'
				.. '!style="border:0; ' .. cellHide .. '"| ' .. num
	
	-- zh-classical — самое длинное название раздела
	result = result .. '|' .. createCell('zh-classical',cellHide)
					.. '|' .. createCell('Всего',cell .. ' text-align:left;')
					.. '|' .. createCell(val['articles'],cell,true)
					.. '|' .. createCell(val['pages'],cell,true)
					.. '|' .. createCell(val['edits'],cell,true)
					.. '|' .. createCell(depth,cell,true)
					.. '|' .. createCell(val['users'],cell,true)
					.. '|' .. createCell(val['activeusers'],cell,true)
					.. '|' .. createCell(val['admins'],cell,true)
					.. '|' .. createCell(val['files'],cell,true)
	
	return result
end

-- Функция для вывода в [[Википедия:Список Википедий]]
function p.Editions(frame)
	local data = mw.loadData('Модуль:NumberOf/today')
	
	-- Таблица для сбора разделов по величине
	local s = {
		[1000000] = {},
		[100000] = {},
		[10000] = {},
		[1000] = {},
		[0] = {}
	}
	
	-- Значения для проверки
	local checks = {}
	local n = 0
	for k, v in pairs(s) do
		table.insert(checks,n + 1,k)
	end
	table.sort(checks)
	
	-- Сортировка разделов по позиции и величине
	for key, val in pairs(data) do
		local curr = data[key]
		if key ~= 'total' then
			local text = createRow(key,curr)
			for i = #checks, 1, -1 do
				if curr['articles'] <= 1 then
					table.insert(s[0],{curr['pos'],text})
					break
				end
				
				local n = checks[i]
				if curr['articles'] / n > 1 then
					table.insert(s[n],{curr['pos'],text})
					break
				end
			end
		end
	end

	-- Вывод таблицы
	local result = ''
	for i = #checks, 1, -1 do
		local n = checks[i]
		if n == 0 then 
			result = result .. '=== Менее 1000 статей ==='
		else
			result = result .. '=== Более ' .. mwlang:formatNum(n) .. ' статей ==='
		end
		
		-- Автоматический скролл для недостаточно широких мониторов
		result = result .. '\n<div style="overflow-x:auto; overflow-y:hidden;">\n' .. createHeader()

		-- Вывод рядов таблицы
		local function compare(a,b)
			return a[1] < b[1]
		end
		table.sort(s[n],compare)
		for k, v in pairs(s[n]) do
			result = result .. v[2]
		end
		
		-- Вывод подвала таблицы
		if n ~= 0 then
			result = result .. createFooter(data,true)
		else
			result = result .. createFooter(data)
		end
		result = result .. '\n|}\n</div>\n'
	end
	
	return result
end

return p