You can edit almost every page by Creating an account. Otherwise, see the FAQ.

Moduł:Sprawdź

Z EverybodyWiki Bios & Wiki
Skocz do:nawigacja, szukaj

Ten moduł zapewnia platformę programistyczną dla tworzenia szablonów budujących przypadki testowe szablonów Wikipedii. Przypadki testowe mogą być tworzone ręcznie, aczkolwiek te tworzone za pomocą szablonów opartych na module Lua, takim jak niniejszy moduł, mają taką zaletę, że argumenty szablonu muszą być wprowadzone jedynie jednokrotnie, co zmniejsza nakład pracy konieczny do tworzenia testów, a także zmniejsza ryzyko występowania błędów w danych wejściowych.

Porównaj[edytuj]

Ta funkcja jest obecnie wykorzystywana przez szablon {{Przypadek testowy}}.

Parametry[edytuj]

Funkcja do wykrywania wywołań szablonów z nierozpoznanymi, pustymi lub przestarzałymi parametrami. Należy ją wywołać ze wszystkimi możliwymi parametrami jakie przyjmuje szablon podlegający sprawdzaniu.

Opis parametrów
pole opis status uwagi
bez nazwy konfiguracja działania wymagany klasa lub klasa odstęp suffix
inny deklaracja pola obsługiwanego w szablonie opcjonalny txt, num, num?, old lub pusty
Konfiguracja
  • klasa – ciąg znaków bez spacji umieszczany jako wartość atrybutu class w tagu <span> z wygenerowanym raportem
  • odstęp – ciąg znaków odstępu oddzielający pozostałą treść konfiguracji, jeśli zawiera znak nowej linii to przed wygenerowanym raportem jest również umieszczany znak nowej linii
  • suffix – treść dołączana po wygenerowanym raporcie, zwykle deklaracja technicznej kategorii
Sprawdzanie parametrów
  • txt – oczekiwany jest dowolna niepusta wartość pola
  • num – sprawdzanie czy przekazana wartość jest liczbą
  • num? – sprawdzanie czy przekazana wartość jest pusta lub jest liczbą
  • old – informuje, że przekazywany parametr jest przestarzały
  • pusty – parametr jest opcjonalny, jest to najczęściej stosowany sposób użycia
Przykład
{{#invoke:Sprawdź|Parametry|=problemy-w-szablonie [[Kategoria:Błędy wywołań w szablonie]]|argument1=|argument2=}}

odn[edytuj]

Funkcja sprawdzająca prawidłowość parametrów w {{odn}}.


return {
	["Porównaj"] = function(frame)
		local config = frame:getParent().args[""] or ""
		local options = mw.text.split(config, "|")
		local templateName = mw.text.trim(options[1])
		if #templateName == 0 then
			local title = mw.title.getCurrentTitle()
			if title.namespace == 10 then
				templateName = mw.ustring.match(title.text, "^(.-)/opis")
					or mw.ustring.match(title.text, "^(.-)/test")
					or mw.ustring.match(title.text, "^(.-)/brudnopis")
					or title.text
			end
			if #templateName == 0 then
				mw.log("brak nazwy szablonu")
				return
			end
		end
	
		local templateTitle = mw.title.new(templateName, 10)
		if templateTitle.id == 0 then
			mw.log("szablon '"..templateName.."' nie istnieje")
			return
		end
		
		local sandboxName = templateName.."/brudnopis"
		local sandboxTitle = mw.title.new(sandboxName, 10)
		if sandboxTitle.id == 0 then
			mw.log("brudnopis '"..sandboxName.."' nie istnieje")
			return
		end
		
		local i = 2
		local showparams = true
		local showinfo = true
		local vertical = false
		while i <= #options do
			local option = mw.text.trim(options[i])
			if option == "bez wikikodu" then
				showparams = false
			elseif option == "bez opisu" then
				showinfo = false
			elseif option == "pionowo" then
				vertical = true
			end
			
			i = i + 1
		end
	
		local templateParams = {}
		local params = {}
		for k, v in pairs(frame:getParent().args) do
			if k ~= "" then
				templateParams[k] = v
				table.insert(params, k)
			end
		end
	
		local result = {}
		table.insert(result, '<table style="width: 100%;">')
		
		if showparams and (#params > 0) then
			local compare = function(a, b)
				-- return a < b
				if (type(a) == "number") and (type(b) == "number") then
					return a < b
				end
				
				if (type(a) == "string") and (type(b) == "string") then
					return a < b
				end
				
				if (type(a) == "number") and (type(b) == "string") then
					return true
				end
				
				return false
			end
			
			table.sort(params, compare)
			table.insert(result, "<caption><code>&#x7B;&#x7B;")
			table.insert(result, templateName)
			for i, k in ipairs(params) do
				table.insert(result, " &#x7C; ")
				local p = mw.text.nowiki(tostring(k))
				local v = mw.text.nowiki(templateParams[k])
				table.insert(result, p)
				table.insert(result, " = ")
				table.insert(result, v)
			end
			table.insert(result, "&#x7D;&#x7D;</code></caption>")
		end
		
		local templateResult = frame:expandTemplate{ title=templateName, args=templateParams}
		local sandboxResult = frame:expandTemplate{ title=sandboxName, args=templateParams}
		
		if templateResult and string.match(templateResult, "^{|") then
			templateResult = "\n"..templateResult
		end
		if sandboxResult and string.match(sandboxResult, "^{|") then
			sandboxResult = "\n"..sandboxResult
		end
		
		if vertical and showinfo then
			table.insert(result, '<tr><th style="width: 15em">[[Szablon:')
			table.insert(result, templateName)
			table.insert(result, '|Szablon]]</th><td>')
			table.insert(result, templateResult)
			table.insert(result, '</td></tr><tr><th>[[Szablon:')
			table.insert(result, sandboxName)
			table.insert(result, '|Brudnopis szablonu]]</th><td>')
			table.insert(result, sandboxResult)
			table.insert(result, '</td></tr>')
		elseif vertical then
			table.insert(result, '<tr><td>')
			table.insert(result, templateResult)
			table.insert(result, '</td></tr><tr><td>')
			table.insert(result, sandboxResult)
			table.insert(result, '</td></tr>')
		else
			if showinfo then
				table.insert(result, '<tr><th style="width: 50%;">[[Szablon:')
				table.insert(result, templateName)
				table.insert(result, '|Szablon]]</th><th style="width: 50%;">[[Szablon:')
				table.insert(result, sandboxName)
				table.insert(result, '|Brudnopis szablonu]]</th></tr>')
			end
			
			table.insert(result, '<tr style="vertical-align: top;"><td>')
			table.insert(result, templateResult)
			table.insert(result, '</td><td>')
			table.insert(result, sandboxResult)
			table.insert(result, '</td></tr>')
		end
		
		table.insert(result, "</table>")
		return table.concat(result)
	end,

	["Parametry"] = function(frame)
		local unknown = {}
		local invalid = {}
		local deprecated = {}
		local config = frame.args[""]
		local class, space, category = string.match(config or "", "^%s*(%S+)(%s+)(.-)%s*$")
		local nl = space and string.match(space, "\n") or ""
		class = class or config

		local function argName(arg)
			return type(arg) ~= "string" and tostring(arg) or ('"'..arg..'"')
		end

		local emptyArg = false
		for k, v in pairs(frame:getParent().args) do
			local kind = frame.args[k]
			if k == "" then
				emptyArg = v
			elseif not kind then
				table.insert(unknown, argName(k))
			elseif kind == "num" then
				local n = tonumber(v)
				if not n then table.insert(invalid, argName(k)) end
			elseif kind == "num?" then
				local n = (#v == 0) or tonumber(v)
				if not n then table.insert(invalid, argName(k)) end
			elseif kind == "txt" then
				if #v == 0 then table.insert(invalid, argName(k)) end
			elseif kind == "old" then
				table.insert(deprecated, argName(k))
			end
		end

		if (#unknown == 0) and (#invalid == 0) and (#deprecated == 0) then
			return nil
		end

		local result = mw.html.create("span")
		if class then
			result:addClass(class)
		end

	 	if mw.title.getCurrentTitle().namespace == 0 then
 			result:css("display", "none")
 		else
 			result:css("color", "red")
 		end
 
		if emptyArg then
			result:wikitext("|=", emptyArg, "| ")
		end

		if #invalid > 0 then
			result:wikitext("Nieprawidłowe pola: ", mw.text.listToText(invalid), ". ")
		end

		if #unknown > 0 then
			result:wikitext("Nieznane pola: ", mw.text.listToText(unknown), ". ")
		end

		if #deprecated > 0 then
			result:wikitext("Przestarzałe pola: ", mw.text.listToText(deprecated), ". ")
		end

		if category then
			result:wikitext(category)
		end

		return nl..tostring(result)
	end,

	["odn"] = function(frame)
		local pf = frame:getParent()
		local i = 1
		local problems = false
		local yeardetected = false
		while true do
			local arg = pf.args[i]
			if not arg then
				problems = i == 1 and "brak argumentów" or false
				break
			end
	 
			if (i > 5) or yeardetected then
				problems = "za dużo argumentów pozycyjnych"
				break
			end
	 
			if #arg == 0 then
				problems = "pusty argument"
				break
			end
	 
			if arg ~= mw.text.trim(arg) then
				problems = "nieoczekiwane odstępy na początku lub końcu argumentu"
				break
			end
	 
			if string.match(arg, "^%d+%l?$") then
				yeardetected = true
				if i == 1 then
					problems = "rok musi być ostatnim parametrem po nazwiskach autorów"
					break
				end
			elseif string.match(arg, "^s[%-%.:]%s*%d+") then
				problems = "prawdopodobnie nieprawidłowo podany numer strony"
				break
			elseif string.match(arg, "%s%s") then
				problems = "podwójne odstępy"
				break
			elseif mw.ustring.match(arg, "^%a+%d") then
				if not mw.ustring.match(arg, "^[%u%d]+$") then
					problems = "prawdopodobnie sklejone argumenty (brak pionowej kreski)"
					break
				end
			elseif mw.ustring.match(arg, "^OdeB ") then
				-- [[Ordre de Bataille]]
			elseif mw.ustring.match(arg, "^%u%l+%u") then
				local justification = {
					["De"] = true,
					["Del"] = true,
					["Di"] = true,
					["Le"] = true,
					["Mac"] = true,
					["Mc"] = true,
					["Te"] = true, -- TeSelle
					["Sar"] = true, -- SarDesai
					["Van"] = true, -- VanBuren
				}
				if not justification[mw.ustring.match(arg, "^%u%l+")] then
					problems = "prawdopodobnie sklejone argumenty (brak pionowej kreski)"
					break
				end
			end
	 
			i = i + 1
		end
	 
		if not problems then
			local odn = pf.args.odn
			if odn and ((#odn ~= 1) or (odn < "a") or (odn > "z")) then
				problems = "nieoczekiwany parametr odn"
			end
		end
	 
	 	if not problems then
	 		local s = pf.args.s
	 		if s and string.match(s, "&[a-z]+;") then
	 			problems = "użyto encji HTML w numerze strony"
	 		end
	 	end
	 
		if not problems then
			if pf.args.strona or pf.args.ss or pf.args.strony or pf.args.p or pf.args.page or pf.args.pp or pf.args.pages then
				problems = "przestarzały parametr z numerem strony"
			end
		end
	
	 	if not problems then
	 		return nil
	 	end
	 	
	 	local result = mw.html.create("span"):addClass("problemy-w-odn")
	 	if mw.title.getCurrentTitle().namespace == 0 then
	 		result:css("display", "none")
	 		result:wikitext("[[Kategoria:Szablon odn do sprawdzenia]]")
	 	else
	 		result:css("color", "red")
	 	end
	 	result:wikitext("ODN: ", problems)
	 	return tostring(result)
	end,
	
	["Wikidane"] = function(frame)
		local property = frame.args.cecha
		local field = frame.args.pole
		local value = frame.args[1]
		if not property or not field then
			return
		end
		
		if not value then
			value = frame:getParent().args[field]
			if not value or (#value == 0) then
				return
			end
		end
		
		local entity = mw.wikibase.getEntity()
		if not entity or not entity.claims or not entity.claims[property] then
			return
		end
		
		for i, v in ipairs(entity.claims[property]) do
			if v.mainsnak.snaktype == "value" then
				if value == v.mainsnak.datavalue.value then
					return
				end
			end
		end
	
		local template = frame:getParent():getTitle()
		local infobox = mw.ustring.match(template, "^Szablon:(.- infobox)$")
		return mw.ustring.format("[[Kategoria:%s – niezgodność w Wikidanych – %s – %s]]", infobox and "Infoboksy" or "Szablony", infobox or template, field)
	end,

}

This Moduł "Sprawdź" is from Wikipedia if otherwise notified