Модуль:WikimediaCEESpringMMXVI-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 Krutyvuss = init(args["Krutyvuss"]);
	if Krutyvuss then
		Krutyvuss, _ = string.gsub(Krutyvuss, ",", ".");
		Krutyvuss = tonumber(Krutyvuss); 
	end
	
	local Volodymyr_D_k = init(args["Volodymyr_D-k"]);
	if Volodymyr_D_k then
		Volodymyr_D_k, _ = string.gsub(Volodymyr_D_k, ",", ".");
		Volodymyr_D_k = tonumber(Volodymyr_D_k);
	end
	
	local Visem = init(args["Visem"]);
	if Visem then
		Visem, _ = string.gsub(Visem, ",", ".");
		Visem = tonumber(Visem);
	end
	
	local Pidlisniuk = init(args["Максим_Підліснюк"]);
	if Pidlisniuk then
		Pidlisniuk, _ = string.gsub(Pidlisniuk, ",", ".");
		Pidlisniuk = tonumber(Pidlisniuk);
	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 = Krutyvuss or Volodymyr_D_k or Visem or Pidlisniuk;
    -- Стилі для оргкому (класи)
    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 == "krutyvuss"
	then
		wrt = wrt .. "bgcolor=\"#ccffcc\"|";
	end
	if Krutyvuss
	then
		wrt = wrt .. Krutyvuss;
	end
	
	wrt = wrt .. "\n| ";
	if highlight == "volodymyr_d-k"
	then
		wrt = wrt .. "bgcolor=\"#ccffcc\"|";
	end
	if Volodymyr_D_k
	then
		wrt = wrt .. Volodymyr_D_k;
	end
	
	wrt = wrt .. "\n| ";
	if highlight == "visem"
	then
		wrt = wrt .. "bgcolor=\"#ccffcc\"|";
	end
	if Visem
	then
		wrt = wrt .. Visem;
	end
	
	wrt = wrt .. "\n| ";
	if highlight == "максим_підліснюк" or highlight == "максим підліснюк" or highlight == "учитель"
	then
		wrt = wrt .. "bgcolor=\"#ccffcc\"|" ;
	end
	if Pidlisniuk
	then
		wrt = wrt .. Pidlisniuk;
	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(Krutyvuss, Volodymyr_D_k, Visem, Pidlisniuk);
	
	
	local bonus = 0;
	if isFL
	then
		bonus = 15;
	elseif isGA
	then
		bonus = 25;
	elseif isFA
	then
		bonus = 50;
	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 == "Visem" or
					author == "Учитель" or
					author == "Максим Підліснюк" or
					author == "Krutyvuss" or
					author == "Dim Grits" or
					author == "Volodymyr D-k" or
					author == "Base" or
					author == "Ата" or
					author == "Friend" or
					author == "NickK" or
					author == "Magalia" or
					author == "Lingveno" or
					author == "Лорд Бъмбъри"
				)
			)
			then
			wrt = wrt .. "[[Категорія:Списки статей CEE Spring 2016 з неоціненими статтями]]"
		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;