Jump to content

Module:Sandbox/Frietjes

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Frietjes (talk | contribs) at 15:32, 27 November 2020. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

local function getBestStatement(item_id, property_id)
	if not(item_id) or not(mw.wikibase.isValidEntityId(item_id)) or not(mw.wikibase.entityExists(item_id)) then
		return false
	end
	local statements = mw.wikibase.getBestStatements(item_id, property_id)
	if not statements or #statements == 0 then
		return false
	end
	local hasNoValue = ( statements[1].mainsnak and statements[1].mainsnak.snaktype == 'novalue' )
	if hasNoValue then
		return false
	end
	return statements[1]
end

function p.hasOSM(frame)
	return getBestStatement(mw.wikibase.getEntityIdForCurrentPage(), 'P402') and 'yes' or 'no'
end

function p.main(frame)
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame, {parentFirst = true,
	valueFunc = function (key, val)
		if key == 'text_IPS' then
			return nil
		end
		if type(val) == 'string' then
			val = val:match('^%s*(.-)%s*$')
			if val == '' then
				return nil
			else
				return val
			end
		else
			return val
		end
	end
	})
	local team_list = {}
	local ii = 1
	while args['team'..ii] ~= nil do
		team_list[args['team'..ii]] = ii
		ii = ii + 1
	end
	local max_team = ii - 1
	
	local first_team, last_team = 1, max_team
	if args['showteam'] and team_list[args['showteam']] then
		first_team = team_list[args['showteam']] - 2
		last_team = first_team + 4
		if first_team < 1 then
			first_team = 1
			last_team = first_team + 4
		end
		if last_team > max_team then
			last_team = max_team
			first_team = max_team - 4
		end
		if first_team < 1 then first_team = 1 end
	end
		
	local hasnotes = false
	
	local ii = first_team
	local res = '{| class="wikitable"\n'
	res = res .. '! Pos. !! Team !! Result\n'
	while args['team'..ii] ~= nil and (ii <= last_team) do
		res = res .. '|-\n'
		res = res .. '| ' .. ii .. '\n'
		res = res .. '| ' .. (args['name_'..args['team' .. ii]] or '') .. '\n'
		local text_result = args['result'..ii] and args['text_'..args['result'..ii]] or ''
		local style_text = ''
		if text_result:match('fbmulticompefn') then
			hasnotes = true
			style_text = style_text .. 'padding:0;'
		end
		style_text = style_text .. (args['result'..ii] and ('background:' .. args['col_'..args['result'..ii]]) or '')
		res = res .. '| style="' .. style_text .. '" | ' .. text_result .. '\n'
		ii = ii + 1
	end
	res = res .. '|}'
	
	if hasnotes == true then
		res = res .. '<b>Table notes:</b>' .. frame:expandTemplate{ title = 'notelist'}
	end
	
	-- Generate tracking
	if not args['notracking'] then
		-- Step 1: Generate a team and result list
		for k,v in pairs(args) do
			-- nothing
		end
	end

	return res
end

-- This module performs validation checks for [[WP:DYK]] hooks
local validationPatternGroups = {
	{
		-- Check that hooks start with three periods, followed by an acceptable
		-- follow-on word.
		"^ *%.%.%. *that",
		"^ *%.%.%. *about",	
	},
	{
		-- Check that hooks end with a question mark, or another acceptable
		-- phrase.
		[[.%?%]*'*"? *$]],
		[[.&#63;</span>%]*'*"? *$]],
		"[Yy]ou probably did%.+ *$",
	}
}

function p._isValidHook(hook)
	-- Whether the given hook is valid.
	-- We use the patterns in the validationPatternGroups table to find whether
	-- a hook is valid or not. Hooks are treated as valid if they match at least
	-- one pattern from each group.
	for _, patternGroup in ipairs(validationPatternGroups) do
		local found = false
		for _, pattern in ipairs(patternGroup) do
			if mw.ustring.find(hook, pattern) then
				found = true
				break
			end
		end
		if not found then
			return false
		end
	end
	return true
end

function p.isValidHook(frame)
	local hook = frame.args[1] or frame.args['1']
	if not hook then
		error("No hook specified")
	end
	hook = hook:match("^%s*(.*?)%s*$") -- Trim whitespace
	return hook
--	if p._isValidHook(hook) then
--		return "yes"
--	else
--		return ""
--	end
end

return p