Moduł:Portal

Z Nonsensopedii, polskiej encyklopedii humoru

local tools = require('Moduł:Narzędzia')
local p = {}

local function makeShowStatus(text_or_nil)
	local text = text_or_nil or ''
	local showstatus = {mobileonly=false, desktoponly=false}
	if text == "mobileonly" then
		showstatus.mobileonly = true
	elseif text == "desktoponly" then
		showstatus.desktoponly = true
	end
	showstatus.tostring = function()
		local final = ""
		if showstatus.mobileonly then final = final .. "mobileonly" end
		if showstatus.desktoponly then final = final .. " desktoponly" end
		final = mw.text.trim(final)
		return final
	end
	return showstatus
end

local function makeInfoSet(args, num, showstatus_maybe_nil)
	local showstatus = makeShowStatus(showstatus_maybe_nil)
	local infoSet = {color="#c7c7c7", text="", title="", showstatus=showstatus}
	infoSet.text = args['panel' .. num] or ''
	infoSet.title = args['ptytuł' .. num] or ''
	if args.kolor ~= nil then
		infoSet.color = args.kolor
	end
	return infoSet
end

local function makePanel(infoSet, stack)
	local panelDiv = mw.html.create( 'div' )
	local h2 = mw.html.create( 'h2' )
	h2:css('border-bottom', '2px solid' .. infoSet.color .. '!important')
		:addClass("portal-panel-header")
		:wikitext(infoSet.title)
	panelDiv:addClass("portal-panel")
		:addClass(infoSet.showstatus.tostring())
		:wikitext(tostring(h2) .. '<div class="portal-panel-content">\n' .. infoSet.text .. '\n</div>')
	table.insert(stack, tostring(panelDiv))	
end

-- Znajduje zbiór stron "należących" do portalu i dodaje odpowiednie
-- właściwości semantyczne do śledzenia tego.
local function findRelevantPages(args)
	local pages = {}
	local categories = {}
	
	-- Artykuły podane explicite
	local arts = mw.text.split( args['artykuły'] or '', '#', true )
	for _, art in ipairs(arts) do
		local art2 = mw.text.trim(art)
		if art2 ~= '' then
			table.insert(pages, mw.text.trim(art))
		end
	end
	
	-- Wyszukiwanie wg kategorii
	if args['kategorie'] then
		local res = mw.smw.getQueryResult{
			args['kategorie'] .. ' [[Modification date::+]]', 
			limit=1000 -- większych portali raczej nie powinno być
		}
		for _, r in ipairs(res.results or {}) do
			if r.exists == '1' then
				table.insert(pages, r.fulltext)
			end
		end
		
		-- znajdź w warunkach wyszukiwania kategorie nadrzędne
		for m in mw.ustring.gmatch(args['kategorie'], '%[%[[^%]]+%]%]') do
			local cond = mw.text.trim(mw.ustring.sub(m, 3, -3))
			if mw.ustring.find(mw.ustring.lower(cond), 'kategoria:') == 1 or
				mw.ustring.find(mw.ustring.lower(cond), 'category:') == 1 then
				cond = mw.ustring.sub(cond, mw.ustring.find(cond, ':', 1, true) + 1)
				for _, cat in ipairs(mw.text.split(cond, '||', true)) do
					table.insert(categories, 'Kategoria:' .. mw.text.trim(cat))
				end
			end
		end
	end
	
	-- Ustawiamy właściwości semantyczne
	mw.smw.set( { 
		['Zawiera artykuł'] = pages,
		['Zawiera kategorię'] = categories,
	} )
end

function p.generate(frame)
	local args = tools.getArgs(frame)
	findRelevantPages(args)
	
	local ilstack, irstack = {}, {}	
	local lstack, rstack = {}, {}
	local curSpecial = 0
	
	
	local function subpanel(num)
		if args['panel' .. num] == '-' or args['panel' .. num] == '' then
			return
		end
		
		if num % 2 == 1 then
			local panelInfo = makeInfoSet(args, num)
			table.insert(ilstack, panelInfo)
			curSpecial = 0
		else
			local lpanelInfo = makeInfoSet(args, num, "mobileonly")
			local rpanelInfo = makeInfoSet(args, num, "desktoponly")
			table.insert(ilstack, lpanelInfo)
			table.insert(irstack, rpanelInfo)
			curSpecial = curSpecial + 1
		end
	end
	
	local function execute()
		if curSpecial > 0 then
			local maxLeft = table.maxn(ilstack)
			local maxRight = table.maxn(irstack)
			for i = 0, (curSpecial - 1) do
				table.remove(ilstack, maxLeft - i)
				irstack[maxRight-i].showstatus.desktoponly = false
			end
		end
				
		for i, v in ipairs(ilstack) do
			makePanel(v, lstack)
		end
		for i, v in ipairs(irstack) do
			makePanel(v, rstack)
		end
	end
	
	for i = 1, 50 do
		if args['panel' .. i] ~= nil then subpanel(i)
		else break end
	end
	
	execute()
	
	lstack = table.concat(lstack, '\n')
	rstack = table.concat(rstack, '\n')
	local column = '<div class="portal-kolumna portal-kolumna-'
	return '<div class="portal-main-content">' .. column .. 'left">' .. lstack .. '</div>' .. 
		column .. 'right">' .. rstack .. '</div></div>'
end

return p