Module:PastWikithons

From BASAsulselWiki

Documentation for this module may be created at Module:PastWikithons/doc

local p = {}
local competitions = {}
local competitionsInfo = {}
local shelves = {
	'Biography',
	'Book',
	'Environmental Initiative',
	'Children Book',
	'ScholarsRoom',
	'Intercultural',
	'Folktale',
	'Place',
	'Lontar',
	'Literature',
	'WomenSpirit',
	'Holiday',
	'Plant',
	'Comics',
	'VisualArt',
	'Music',
	'Covid',
	'Podcast',
	'Cuisine',
	'Government'
}

function table_contains(tbl, x)
    found = false
    for _, v in pairs(tbl) do
        if v == x or _ == x then 
            found = true 
        end
    end
    return found
end

function addToSet(set, key)
    set[key] = true
end

function removeFromSet(set, key)
    set[key] = nil
end

function setContains(set, key)
    return set[key] ~= nil
end

function p.display( frame )
	local div = mw.html.create( 'div' )
		div:addClass('wikithon-list-legacy')
		div:addClass('bali-wikiton-list')
	-- https://github.com/SemanticMediaWiki/SemanticScribunto/blob/master/docs/README.md
	-- build list of unique wikithons names
	
	local result = mw.smw.ask {
    	'[[Category:Article]]',
    	'[[Competition::+]]',
    	'[[Question::+]]',
    	'[[Topic::+]]',
    	'?Competition',
    	'?Question',
    	'?Question id',
    	'?Question ban',
    	'?Question bug',
    	'?Question mak',
    	'?Topic',
    	'?Categories',
    	'?Modification date',
    	limit = 10000,
    	mainlabel = '-',
    	link = 'none',
    	sort = 'Modification date',
    	order = 'desc'
	}
	
	if result and #result then
		for num, entityData in pairs( result ) do
			local c = entityData['Competition']
			if not setContains(competitions, c) then
				addToSet(competitions, c)
				local cats = entityData['Category']
				local s = ''
				for ci, cv in ipairs( cats ) do
					local catnameraw = mw.ustring.gsub(mw.text.split( cv, '|', True )[2], '%p', '')
					if table_contains(shelves, catnameraw) then
						s = catnameraw
					end
				end
				if type(entityData['Topic']) == "table" then
					entityData['Topic'] = entityData['Topic'][1]
				end
				local q = ''
				if entityData['Question'] ~= nil then
					q = entityData['Question']
				end
				local q_id = ''
				if entityData['Question id'] ~= nil then
					q_id = entityData['Question id']
				end
				local q_ban = ''
				if entityData['Question ban'] ~= nil then
					q_ban = entityData['Question ban']
				end
				local q_bug = ''
				if entityData['Question bug'] ~= nil then
					q_bug = entityData['Question bug']
				end
				local q_mak = ''
				if entityData['Question mak'] ~= nil then
					q_mak = entityData['Question mak']
				end
				competitionsInfo[c] = {
					question = q,
					question_id = q_id,
					question_ban = q_ban,
					question_mak = q_mak,
					question_bug = q_bug,
					topic = entityData['Topic'],
					shelv = s
				}
			end
		end
	end
	
	for k, v in pairs( competitions ) do
		local processingText = ''
		if #competitionsInfo[k].question > 1 then
			processingText = processingText .. '<en>'..competitionsInfo[k].question..'</en>'
		end
		if #competitionsInfo[k].question_id > 1 then
			processingText = processingText .. '<id>'..competitionsInfo[k].question_id..'</id>'
		end
		if #competitionsInfo[k].question_ban > 1 then
			processingText = processingText .. '<ban>'..competitionsInfo[k].question_ban..'</ban>'
		end
		if #competitionsInfo[k].question_bug > 1 then
			processingText = processingText .. '<bug>'..competitionsInfo[k].question_bug..'</bug>'
		end
		if #competitionsInfo[k].question_mak > 1 then
			processingText = processingText .. '<mak>'..competitionsInfo[k].question_mak..'</mak>'
		end
		local questionText = frame:preprocess( processingText )
		local topicImage = mw.title.makeTitle( 'File', 'Topic-'..competitionsInfo[k].topic..'.jpg')
		local formString = 
			'Article_search[Type]='..competitionsInfo[k].shelv
			..'&shelf='..competitionsInfo[k].shelv
			..'&Article_search[Topic]='..competitionsInfo[k].topic
			..'&Article_search[Competition]='..k
			..'&_run'
		local wikithonLink = frame:callParserFunction(
	    	'#queryformlink',
		    	{
		    		'form=Article_search',
		    		'link text=' .. questionText,
		    		'link type=link',
		    		'new window',
		    		'query string=' .. formString
				}
			)
		local wikithonLinkBtn = frame:callParserFunction(
	    	'#queryformlink',
		    	{
		    		'form=Article_search',
		    		'link text=View articles',
		    		'link type=link',
		    		'new window',
		    		'query string=' .. formString
				}
			)
		local wikithonDiv = div:tag('div')
			wikithonDiv:addClass('bali-wikiton')
			if topicImage.file.exists then
				local url = frame:preprocess( '{{filepath:' .. topicImage.text .. '|800px}}' )
				wikithonDiv:attr('data-background-image', url)
			end
			--wikithonDiv:cssText('background-image: url('..topicImageUrl..')')
		local wikithonTags = wikithonDiv:tag('div')
			wikithonTags:addClass('bali-tags')
			if competitionsInfo[k].topic ~= nil and #competitionsInfo[k].topic then
				local tagTopic = wikithonTags:tag('span')
					tagTopic:wikitext('#'..competitionsInfo[k].topic)
			end
			if competitionsInfo[k].shelv ~= nil and #competitionsInfo[k].shelv then
				local tagSpan = wikithonTags:tag('span')
					tagSpan:wikitext('#'..competitionsInfo[k].shelv)
			end
			local codeSpan = wikithonTags:tag('span')
					codeSpan:wikitext('#'..k)
		local wikithonRow = wikithonDiv:tag('div')
			wikithonRow:addClass('bali-wikiton__row')
		local rowText = wikithonRow:tag('div')
			rowText:addClass('bali-wikiton__text')
			rowText:wikitext(wikithonLink)
		local rowBtn = wikithonRow:tag('span')
			rowBtn:addClass('bali-wikiton__button')
			rowBtn:wikitext(wikithonLinkBtn)
	end
	
	-- fetch wikithons artiles per wikithon
	--[=====[ 
	for k, v in pairs( competitions ) do
		local competition = k
		local ulWikithon = div:tag('ul')
			ulWikithon:addClass('wikithon-list-legacy--item')
			ulWikithon:wikitext(competition)
		local result = mw.smw.ask {
			'[[Category:Article]]',
			'[[Competition::' .. competition ..']]',
			limit = 10,
			mainlabel = 'origin'
		}
		if result and #result then
			for num, entityData in pairs( result ) do
				local liPage = ulWikithon:tag('li')
					liPage:addClass('wikithon-list-legacy--page')
					liPage:wikitext(entityData.origin)
			end
		end
	end
	--]=====]
    return div
end

return p