Module:PastWikithons

From BASAbaliWiki
Revision as of 15:27, 9 July 2024 by Alexey123 (talk | contribs)

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::+]]',
    	'?Competition',
    	'?Question',
    	'?Question id',
    	'?Question ban',
    	'?Topic',
    	'?Categories',
    	limit = 10,
    	mainlabel = '-',
    	link = 'none'
	}
	
	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
				competitionsInfo[c] = {
					question = entityData['Question'],
					question_id = entityData['Question id'],
					question_ban = entityData['Question ban'],
					topic = entityData['Topic'],
					shelv = s
				}
			end
		end
	end
	
	for k, v in pairs( competitions ) do
		local questionText = frame:preprocess( '<en>'..competitionsInfo[k].question..'</en><id>'..competitionsInfo[k].question_id..'</id><ban>'..competitionsInfo[k].question_ban..'</ban>' )
		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',
		    		'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 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