From Wikipedia, the free encyclopedia

local mArguments = require('Module:Arguments')



local validFields = {

	type = true,

	scale = true,

	region = true

}



local p = {}



local function err(msg)

	return string.format('<strong class="error">Error: %s</strong>', msg)

end



function p.main(frame)

	local args = mArguments.getArgs(frame)

	return p._main(args)

end



function p._main(args)

	local input = args1

	local field = args2 or args.field

	local showCityData = args.citydata == 'yes'

	

	-- Validate input.

	if not input then

		return err('no input code specified')

	elseif not field then

		return err('no field specified')

	end

	

	-- Parse the input string.

	local substrings = mw.text.split(input, '_')

	local data = {}

	for i, substring in ipairs(substrings) do

		local key, value = mw.ustring.match(substring, '^(.-):(.*)$')

		if key and validFieldskey then

			datakey = value

		end

	end

	

	-- Fetch the result.

	if field == 'error' then

		-- Check for missing data.

		local missingFields = {}

		for validField in pairs(validFields) do

			if not datavalidField then

				table.insert(missingFields, validField)

			end

		end

		if #missingFields > 0 then

			local msg = 'the '

				.. mw.text.listToText(missingFields)

				.. ' fields are missing'

			return err(msg)

		else

			return ''

		end

	else

		-- Return the specified field, or the blank string if it is missing.

		local result = datafield or ''

		if field == 'type' then

			-- Check for type values like "city(20000)".

			local city, population = mw.ustring.match(result, '^(city)%((.-)%)$')

			if city then

				if showCityData then

					return population

				else

					return city

				end

			end

		end

		return result

	end

end



return p