Modulo:Problemoj/provejo

El Vikipedio, la libera enciklopedio
Dokumentado Dokumentado



Se vi havas demandon pri ĉi tiu Lua-modulo, tiam vi povas demandi en la diskutejo pri Lua-moduloj. La Intervikiaj ligiloj estu metataj al Vikidatumoj. (Vidu Helpopaĝon pri tio.)
--[==[ Version from 2015-07-10
-- This module is a Lua implementation of the old {{Pluraj problemoj}} template.
-- Please take care when updating it! It outputs tree functions: p.problem, 
-- which generates a table of problems, p.text, which
-- produces the text for an individual problem message, and p.category, which produces
-- the category for an individual problem.

-- The problem text data is kept in [[Modulo:Problemoj/tekstoj]]
-- The aliases for problem text data are kept in [[Modulo:Problemoj/tekstoj/alternativaj nomoj]]
-- The category data is kept in [[Modulo:Problemoj/kategorioj]]
-- The stup link data is kept in [[Modulo:Problemoj/ĝermoligiloj]]
]==]

local htmlBuilder = require('Modulo:HtmlBuilder')

local p = {}

-- for textes
local function matchTextPage(s)
	-- Finds the appropriate text subpage given a lower-case
	-- text message of that problem.
	if type(s) ~= 'string' or #s < 1 then return end
	local textPage
	textPage = 'Modulo:Problemoj/tekstoj'
	local textes = mw.loadData(textPage)
	local text = textes[s]
	if text then
		return text
	end
end

local function getAlias(s)
	-- Gets an alias from the text alias data page.
	local aliasData = mw.loadData('Modulo:Problemoj/tekstoj/alternativaj nomoj')
	for problem, aliases in pairs(aliasData) do
		for _, alias in ipairs(aliases) do
			if alias == s then
				return problem
			end
		end
	end
end

local function getTextName(s)
	-- Gets the text for a given string.
	if type(s) ~= 'string' or #s < 1 then
		return 'Plibonigenda artikolo'
	end
	s = mw.ustring.lower(s)
	local text = matchTextPage(s)
	if text then
		return text
	else
		local alias = getAlias(s)
		text = matchTextPage(alias) -- If no alias was found this returns nil.
		if text then
			return text
		else
			return 'Plibonigenda artikolo'
		end
	end
end

-- for categories
local function matchCategoryPage(s)
	-- Finds the appropriate category subpage given a lower-case
	-- category of that problem.
	if type(s) ~= 'string' or #s < 1 then return end
	local categoryPage
	categoryPage = 'Modulo:Problemoj/kategorioj'
	local categories = mw.loadData(categoryPage)
	local category = categories[s]
	if category then
		return category
	end
end

local function getCategoryName(s)
	-- Gets the category name for a given string.
	if type(s) ~= 'string' or #s < 1 then
		return 'Plibonigenda artikolo'
	end
	s = mw.ustring.lower(s)
	local category = matchCategoryPage(s)
	if category then
		return category
	else
		local alias = getAlias(s)
		category = matchCategoryPage(alias) -- If no alias was found this returns nil.
		if category then
			return category
		else
			return 'Plibonigenda artikolo'
		end
	end
end

-- for stup links
local function matchStuplinkPage(s)
	-- Finds the appropriate stup link subpage given a lower-case
	-- stup link of that problem.
	if type(s) ~= 'string' or #s < 1 then return end
	local stuplinkPage
	stuplinkPage = 'Modulo:Problemoj/ĝermoligiloj'
	local stuplinks = mw.loadData(stuplinkPage)
	local stuplink = stuplinks[s]
	if stuplink then
		return stuplink
	end
end

local function getStuplinkName(s)
	-- Gets the stup link name for a given string.
	if type(s) ~= 'string' or #s < 1 then
		return s
	end
--	s = mw.ustring.lower(s)
	local stuplink = matchStuplinkPage(s)
	if stuplink then
		return stuplink
	else
		return s
	end
end

-- for stup categories
local function matchStupcategoryPage(s)
	-- Finds the appropriate stup category subpage given a lower-case
	-- stup category of that problem.
	if type(s) ~= 'string' or #s < 1 then return end
	local stupcategoryPage
	stupcategoryPage = 'Modulo:Problemoj/ĝermokategorioj'
	local stupcategories = mw.loadData(stupcategoryPage)
	local stupcategory = stupcategories[s]
	if stupcategory then
		return stupcategory
	end
end

local function getStupcategoryName(s)
	-- Gets the stup category name for a given string.
	if type(s) ~= 'string' or #s < 1 then
		return s
	end
--	s = mw.ustring.lower(s)
	local stupcategory = matchStupcategoryPage(s)
	if stupcategory then
		return stupcategory
	else
		return s
	end
end

-- for stup text and category
function stupTexts(stup)
		if stup == "" or stup == nil then
			return "", ""
		end
		local stuplink = getStuplinkName(stup)
		if stup ~= stuplink then
		 stuptext = "[[" .. stuplink .. "|" .. stup .. "]]"
		else
		 stuptext = "[[" .. stup .. "]]"		
		end
			stupcategory = getStupcategoryName(stup)
	 	    stupcategory = "[[Kategorio:Ĝermoj pri " .. stupcategory .. "]]"
		return stuptext, stupcategory
end

function p._problem(problems, args)
	-- This function builds the problems box used by the {{Problemoj}} template.

	-- Start the table. This corresponds to the start of the wikitext table in the old [[Template:Pluraj problemoj]].
	-- This formats have to be the same like in the template {{ArtikolaMesaĝokesto}}
	local tableroot = htmlBuilder.create('table')
	    .addClass('noprint')
		.attr('id', 'rq')	    
	    .addClass('plainlinks')
		.css('background', '#E8FFE0')
		.css('font-size', '90%')
		.css('line-height', '110%')
--		.css('width', '80%')
		.css('margin', '0 10%')
		.css('border-collapse', 'collapse')
		.css('border', '1px solid #aaa')
		.css('border-left', '10px solid #f4c430')
		.css('padding', '1ex')
	
	-- If no portals have been specified, display an error and add the page to a tracking category.
	local kat = args.kat -- it is used a few times
	if kat == "ne" then
		errorcat = ""
	else errorcat = "[[Kategorio:Ŝablonoj pri problemoj sen parametro]]"	
	end
	if not problems[1] then
		 heading = '<strong class="error">Ŝablono {{[[Ŝablono:Pluraj problemoj|Pluraj problemoj]]}} ne estas uzata ĝuste, ĉar mankas parametro.</strong>' .. errorcat
		 divtitleid = "rqerror"
		 divcontentid = "nix"
	else
	     heading = "Vi povas plibonigi la jenon:"	
	     use = "good"
	     divtitleid = "rqtitle"
	     divcontentid = "rqcontent"
	end
	
	    local image = "Wiki letter w.svg"

	    
		-- Generate the html for the image and the problem name.
		tableroot
			.newline()
			.tag('tr', {unclosed = true})
				.attr('valign', 'middle')
				.tag('td')
					.css('text-align', 'center')
					.css('padding', '2px 0px 2px 0.5em')
					     .tag('div')
					     .css('padding', '0.25em 0.5em')
					     .css('width', '52px')
					     .wikitext(mw.ustring.format('[[Dosiero:%s|40px|alt=Portala ikono|link=Vikipedio:Kiel redakti paĝon]]', image))
					     .done()	
					.done()
				.tag('td', {unclosed = true})
					-- .css('padding', '0 0.2em')
					.css('padding', '0.25em 0.5em')
					.css('width', '100%')
						.tag('div')
						     .attr('id', divtitleid)
						     .css('font-weight', '800')
						     .wikitext(mw.ustring.format('%s', heading))
						     .done()
						.tag('div', {unclosed = true})     
						     .attr('id', divcontentid)
						          .newline()
						       	  .tag('ul', {unclosed = true})

	-- Display the problems specified in the positional arguments.
	for i, problem in ipairs(problems) do
		local text = getTextName(problem)
		local category = getCategoryName(problem)
	    -- text from the user, per index i
	    local usertext = args['teksto' .. i] or args['text' .. i]
	    if usertext ~= "" and usertext ~= nil then
	    	text = usertext
	    	ut = "yes"
	    end
	    -- text from the user, for problem
	    local usertext2 = args['teksto por ' .. problem]
	    if usertext2 ~= "" and usertext2 ~= nil then
	    	text = usertext2
	    	ut = "yes"
	    end
	    if text == "Plibonigenda artikolo" and ut ~= "yes" then
	    	local format = "color:red;"
	    	text = "<b style='" .. format .. "'>Malĝusta parametro de la ŝablono {{[[Ŝablono:Pluraj problemoj|Pluraj problemoj]]}} — ''" .. problem .. "'' Kontrolu bazan tekston kaj uzu parametrojn laŭ la [[Ŝablono:Pluraj problemoj/dokumentado|dokumentado]].</b>"
	    elseif text == "Plibonigenda artikolo" and ut == "yes" then
	    	local format = "color:red;"	    	
	    	text = "<b style='" .. format .. "'>Tro ĝenerala teksto ''Plibonigenda artikolo'' por la problemo: " .. problem .. "</b>"
	    end
	    if category == "Plibonigenda artikolo" or kat == "ne" then
	    	category = ""
	    else category = "[[Kategorio:" .. category	.. "]]"
	    end
	    
		-- Generate the html for the list item about the problem.
		tableroot
			-- .newline()
										.tag('li')
											.wikitext(mw.ustring.format('%s', text))
											.newline()
											.wikitext(category)
											.done()
	end
	if args["ĝermo"] ~= "" and args["ĝermo"] ~= nil or args["ĝermo1"] ~= "" and args["ĝermo1"] ~= nil then
		local stup = args["ĝermo"] or args["ĝermo1"]
		local stupsentence = "Ĉi tiu artikolo ankoraŭ estas ĝermo pri "
		stuplink1, stupcategory = stupTexts(stup)
		if kat ~= "ne" then
		tableroot
		           .newline()
		           .wikitext(stupcategory)
		           .done()
		end
	if args["ĝermo2"] ~= "" and args["ĝermo2"] ~= nil then
		local stup = args["ĝermo2"]
		stuplink2, stupcategory = stupTexts(stup)
		if kat ~= "ne" then
		tableroot
		           .newline()
		           .wikitext(stupcategory)
		           .done()
		end           
	if args["ĝermo3"] ~= "" and args["ĝermo3"] ~= nil then
		local stup = args["ĝermo3"]
		stuplink3, stupcategory = stupTexts(stup)
		if kat ~= "ne" then
		tableroot
		           .newline()
		           .wikitext(stupcategory)
		           .done()
		end           
	end	-- for "ĝermo3"	
    end	-- for "ĝermo2"	
		local stuptext = stupsentence .. stuplink1
		if stuplink2 ~= "" and stuplink2 ~= nil and stuplink3 ~= "" and stuplink3 ~=nil then
			stuptext = stuptext .. ", " .. stuplink2 .. " kaj " .. stuplink3 .. "."
		elseif stuplink2 ~= "" and stuplink2 ~= nil and (stuplink3 == "" or stuplink3 == nil) then
			stuptext = stuptext .. " kaj " .. stuplink2 .. "."
		else
			stuptext = stuptext .. "."
		end
         tableroot
         		   .tag('li')
		           .wikitext(mw.ustring.format('%s', stuptext))
	end -- for "ĝermo" and "ĝermo1"
	
	    tableroot 
	                .tag('/ul', {unclosed = true})
    if use == "good" then
    	tableroot
	                	.tag('p')
	                	    .css('border', '1px dotted red')
	                	    .css('padding', '0.3em')
	                	    .attr('id', 'rqinfo')
	                	    .wikitext('Se vi korektis unu el la menciitaj mankoj, bonvolu forigi la koncernan parametron de la ŝablono {{[[Ŝablono:Pluraj problemoj|Pluraj problemoj]]}}. Detaloj estas en la [[Ŝablono:Pluraj problemoj/dokumentado|dokumentado]].')
	                	    .done()    	
    end
       tableroot
	                .tag('/div', {unclosed = true})
	                .tag('/td', {unclosed = true})
	                .tag('/tr', {unclosed = true})	
	                .done() -- table   
	
	return tostring(tableroot)
end

function p._text(problems)

	-- Wrapper function to allow getTextName() to be accessed through #invoke.
	return getTextName(problems[1])
end

function p._category(problems)

	-- Wrapper function to allow getCategoryName() to be accessed through #invoke.
	return getCategoryName(problems[1])
end

local function processProblemArgs(args)
	-- This function processes a table of arguments and returns two tables: an array of portal names for processing by ipairs, and a table of
	-- the named arguments that specify style options, etc. We need to use ipairs because we want to list all the portals in the order
	-- they were passed to the template, but we also want to be able to deal with positional arguments passed explicitly, for example
	-- {{portal|2=Politics}}. The behaviour of ipairs is undefined if nil values are present, so we need to make sure they are all removed.
	args = type(args) == 'table' and args or {}
	local problems = {}
	local namedArgs = {}
	for k, v in pairs(args) do
		if type(k) == 'number' and type(v) == 'string' then -- Make sure we have no non-string problem names.
			table.insert(problems, k)
		elseif type(k) ~= 'number' then
			namedArgs[k] = v
		end
	end
	table.sort(problems)
	for i, v in ipairs(problems) do
		problems[i] = args[v]
	end
	return problems, namedArgs
end

local function makeWrapper(funcName)
	-- Processes external arguments and sends them to the alia functions.
	return function (frame)
		-- If called via #invoke, use the args passed into the invoking
		-- template, or the args passed to #invoke if any exist. Otherwise
		-- assume args are being passed directly in from the debug console
		-- or from another Lua modulo.
		local origArgs
		if frame == mw.getCurrentFrame() then
			origArgs = frame:getParent().args
			for k, v in pairs(frame.args) do
				origArgs = frame.args
				break
			end
		else
			origArgs = frame
		end
		-- Trim whitespace and remove blank arguments.
		local args = {}
		for k, v in pairs(origArgs) do
			if type(v) == 'string' then
				v = mw.text.trim(v)
			end
			if v ~= '' then
				args[k] = v
			end
		end
		return p[funcName](processProblemArgs(args)) -- passes two tables to func: an array of portal names, and a table of named arguments.
	end
end

local funcNames = {'problem', 'text', 'category'}

for _, funcName in ipairs(funcNames) do
	p[funcName] = makeWrapper('_' .. funcName)
end

return p