Модуль:WikimediaCEESpringMMXVII-Row

Матеріал з Вікіпедії — вільної енциклопедії.
Перейти до навігації Перейти до пошуку
{{i}} Документація модуля[створити]
local p = {};

-- from [[:en:Special:Permalink/712321332|:en:Module:Math]]
local function makeArgArray(...)
	-- Makes an array of arguments from a list of arguments that might include nils.
	local args = {...} -- Table of arguments. It might contain nils or non-number values, so we can't use ipairs.
	local nums = {} -- Stores the numbers of valid numerical arguments.
	local ret = {}
	for k, v in pairs(args) do
		v = p._cleanNumber(v)
		if v then
			nums[#nums + 1] = k
			args[k] = v
		end
	end
	table.sort(nums)
	for i, num in ipairs(nums) do
		ret[#ret + 1] = args[num]
	end
	return ret
end


-- from [[:en:Special:Permalink/712321332|:en:Module:Math]]
local function fold(func, ...)
	-- Use a function on all supplied arguments, and return the result. The function must accept two numbers as parameters,
	-- and must return a number as an output. This number is then supplied as input to the next function call.
	local vals = makeArgArray(...)	
	local count = #vals -- The number of valid arguments
	if count == 0 then return
		-- Exit if we have no valid args, otherwise removing the first arg would cause an error.
		nil, 0
	end 
	local ret = table.remove(vals, 1)
	for _, val in ipairs(vals) do
		ret = func(ret, val)
	end
	return ret, count
end

-- from [[:en:Special:Permalink/712321332|:en:Module:Math]]
local function _average(...)
	local sum, count = fold((function(a, b) return a + b end), ...)
	if not sum then
		return 0
	else
		return sum / count
	end
end

-- порожні значення мають стати nil щоб стало використовувати в умовах
local function init(vari)
	local returnie;
	if vari == "" then return nil
	elseif vari == nill then return nil;
	else return vari;
	end
end

function p.row(frame)
	local parent = frame:getParent();
	local args = parent.args;

	local disqualified = tonumber(init(args["дискваліфіковано"]));
	local counter = init(args["лічильник"]);
	local article = init(args["стаття"]);
	local week_oldid =  tonumber(init(args["версія_тиждень"]));
	local contest_oldid =  tonumber(init(args["версія_конкурс"]));
	local badges = init(args["бейджі"]);
	local wikidata_entity = init(args["елемент_Вікіданих"]);
	local author = init(args["автор"]);
	local week_added = tonumber(init(args["додано_в_тижні"]));
	local contest_added = tonumber(init(args["додано_остаточно"]));
	local notes = init(args["зауваження"]);
	local highlight = init(args["підсвітка"]);
	
	local Mcoffsky = init(args["Mcoffsky"]);
	if Mcoffsky then
		Mcoffsky, _ = string.gsub(Mcoffsky, ",", ".");
		Mcoffsky = tonumber(Mcoffsky); 
	end
	
	local Stefanko = init(args["Стефанко1982"]);
	if Stefanko then
		Stefanko, _ = string.gsub(Stefanko, ",", ".");
		Stefanko = tonumber(Stefanko);
	end
	
	local Zheliba = init(args["Zheliba"]);
	if Zheliba then
		Zheliba , _ = string.gsub(Zheliba, ",", ".");
		Zheliba = tonumber(Zheliba);
	end
	
	local Zwiri = init(args["Звірі"]);
	if Zwiri then
		Zwiri, _ = string.gsub(Zwiri, ",", ".");
		Zwiri= tonumber(Zwiri);
	end
	
	local Dim_Grits= init(args["Dim_Grits"]);
	if Dim_Grits then
		Dim_Grits, _ = string.gsub(Dim_Grits, ",", ".");
		Dim_Grits= tonumber(Dim_Grits);
	end

	local fromlists = tonumber(init(args["зі_списків"]));
	local isalist = init(args["є_списком"]);

    local wrt = "|-";


	local added = 0;
	if contest_added 
	then 
		added = contest_added;
	elseif 
		week_added
	then
		added = week_added;
	end

	--Робимо рядок жовтим якщо одиниця, червоним, якщо будь-що — учаснику є, або було над чим працювати
	if disqualified or added < 3500 then
		if disqualified == 1 or added < 3500 then
			wrt = wrt .. " style=\"background:LemonChiffon\"";
		else
			wrt = wrt .. " style=\"background:MistyRose\"";
		end
	end


	local has_been_changed = contest_oldid > week_oldid;
	local has_marks = Mcoffsky or Stefanko or Zheliba or Zwiri or Dim_Grits;
    -- Стилі для оргкому (класи)
    wrt = wrt .. " class=\"";
    -- чи змінено статтю на кінець конкурсу порівняно з на кінець тижня автором
    if not has_been_changed then
     	wrt = wrt .. "ceespring-article-unchanged "
    else
    	wrt = wrt .. "ceespring-article-changed "
    end
    
    if not (has_marks) then
    	wrt = wrt .. "ceespring-article-unrated "
    end
    
    wrt = wrt .. "\"";
    -- кінець класів

	wrt = wrt .. "\n|";
	
	if counter 
	then
		wrt = wrt .. counter;
	end
		
	wrt = wrt .. "\n| ";

	
	if article and week_oldid and wikidata_entity
	then
		wrt = wrt .. "[[" .. article .. "]]<br />( [[Special:Permalink/" .. week_oldid .. "|на кінець тижня]] &bull; [[:d:" .. wikidata_entity .. "|ВД]] )"
	end
	
	local isGA = false;
	local isFA = false;
	local isFL = false;
	
	if badges
	then
		wrt = wrt .. badges;
		if badges == "GA"
		then
			isGA = true;
		elseif badges == "FA"
		then
			isFA = true;
		elseif badges == "FL"
		then
			isFL = true;
		end
	end
	
	wrt = wrt .. "\n| ";
	
	if author
	then
		wrt = wrt .. "[[user:" .. author .. "|" .. author .. "]]";
	end
		
	wrt = wrt .. "\n| ";

	
	
	local quantity = 0;
	if added >= 3500 and (isalist or added < 5000) 
	then
		quantity = 0.8;
	elseif added >= 5000 and added < 10000
	then
		quantity = 1;
		elseif added >= 10000
	then	
		quantity = 1.1;
	end;
	
	if week_added
	then
		wrt = wrt .. "align=\"center\" | " .. week_added
	end
	
	wrt = wrt .. "\n| ";
	
	if contest_added and contest_oldid and week_oldid and week_added 
	then
		wrt = wrt .. "align=\"center\" | " .. contest_added;
		wrt = wrt ..  "<br /><span style=\"color:";
		local diff_added = contest_added-week_added;
		if has_been_changed then
			if diff_added >=0 then
				wrt = wrt .. "darkgreen"
			else wrt = wrt .. "darkred"
			end
		else
			wrt = wrt .. "black"
		end
		wrt = wrt .. "\">";
		wrt = wrt .. "( " .. diff_added .. " )</span>";
	end
	
	wrt = wrt .. "\n| ";

	if notes
	then
		wrt = wrt .. notes
	end
	
	if highlight
	then
		highlight = mw.ustring.lower( highlight );
	end

	wrt = wrt .. "\n| ";
	if highlight == "mcoffsky"
	then
		wrt = wrt .. "bgcolor=\"#ccffcc\"|";
	end
	if Mcoffsky
	then
		wrt = wrt .. Mcoffsky;
	end
	
	wrt = wrt .. "\n| ";
	if highlight == "стефанко1982"
	then
		wrt = wrt .. "bgcolor=\"#ccffcc\"|";
	end
	if Stefanko
	then
		wrt = wrt .. Stefanko;
	end
	
	wrt = wrt .. "\n| ";
	if highlight == "zheliba"
	then
		wrt = wrt .. "bgcolor=\"#ccffcc\"|";
	end
	if Zheliba
	then
		wrt = wrt .. Zheliba;
	end
	
	wrt = wrt .. "\n| ";
	if highlight == "звірі"
	then
		wrt = wrt .. "bgcolor=\"#ccffcc\"|";
	end
	if Zwiri
	then
		wrt = wrt .. Zwiri;
	end
	
	wrt = wrt .. "\n| ";
	if highlight == "dim_grits" or highlight == "dim grits"
	then
		wrt = wrt .. "bgcolor=\"#ccffcc\"|";
	end
	if Dim_Grits
	then
		wrt = wrt .. Dim_Grits;
	end

	wrt = wrt .. "\n| ";
	
	local extra = 1;
	if fromlists and fromlists~=0
	then
		wrt = wrt .. "<span style=\"display:none;\">1</span>[[File:Commons-emblem-success.svg|24px|link=]]";
		extra = 2;
	end
	
	wrt = wrt .. "\n| ";
	
	if isalist
	then
		wrt = wrt .. "<span style=\"display:none;\">1</span>[[File:Commons-emblem-success.svg|24px|link=]]";
	end
	
	wrt = wrt .. "\n| ";
	local total=0;
	
	--Р/1000 * Я * Kр * Кд + Б, де
	
	local quality= _average(Mcoffsky, Stefanko, Zheliba, Zwiri, Dim_Grits);
	
	
	local bonus = 0;
	if isFL
	then
		bonus = 0;
	elseif isGA
	then
		bonus = 0;
	elseif isFA
	then
		bonus = 0;
	end
	
	total = (added/1000) * quality * quantity * extra + bonus;
	
	
	if has_marks
	then
		wrt = wrt .. total;
	else
		-- Категоризація списків з неоціненими більшими за мінімальним розмір статтями в службову категорію
		if quantity > 0 and (not disqualified) and 
			(
				not(
					author == "Nickispeaki" or
					author == "NickK" or
					author == "Dim_Grits" or
					author == "Base" or
					author == "Ата"
				)
			)
			then
			wrt = wrt .. "[[Категорія:Списки статей CEE Spring 2017 з неоціненими статтями]]"
		end
	end
	
	return wrt;
end

--[=[
	
	from [[:en:Special:Permalink/712321332|:en:Module:Math]]
	
Helper function that interprets the input numerically.  If the 
input does not appear to be a number, attempts evaluating it as
a parser functions expression.
]=]

function p._cleanNumber(number_string)
	if type(number_string) == 'number' then
		-- We were passed a number, so we don't need to do any processing.
		return number_string, tostring(number_string)
	elseif type(number_string) ~= 'string' or not number_string:find('%S') then
		-- We were passed a non-string or a blank string, so exit.
		return nil, nil;
	end

	-- Attempt basic conversion
	local number = tonumber(number_string)

	-- If failed, attempt to evaluate input as an expression
	if number == nil then
		local success, result = pcall(mw.ext.ParserFunctions.expr, number_string)
		if success then
			number = tonumber(result)
			number_string = tostring(number)
		else
			number = nil
			number_string = nil
		end
	else
		number_string = number_string:match("^%s*(.-)%s*$") -- String is valid but may contain padding, clean it.
		number_string = number_string:match("^%+(.*)$") or number_string -- Trim any leading + signs.
		if number_string:find('^%-?0[xX]') then
			-- Number is using 0xnnn notation to indicate base 16; use the number that Lua detected instead.
			number_string = tostring(number)
		end
	end

	return number, number_string
end

return p;