Aller au contenu

Module:Wikimedians map

Depi Wikipedya, ansiklopedi lib

La documentation pour ce module peut être créée à Module:Wikimedians map/doc

--
--
--   ATTENTION: This code is maintained at https://ht.wikipedia.org/wiki/Module:Wikimedians_map
--              Please do not modify it anywhere else, as it may get copied and override your changes.
--              Suggestions can be made at https://ht.wikipedia.org/wiki/Module_talk:Wikimedians_map
--
--


local p = {}

local function filter_wiki(geojson, wikis)
	local filtered_geojson = {};
	wikis = mw.text.split(wikis, ",", true);
	for _, user_infos in pairs(geojson) do
		local data = user_infos.data;
		local should_break = false;
		for _,wiki in pairs(wikis) do
			if data[ wiki ] ~= nil then
				table.insert(filtered_geojson, user_infos);
				break;
			else
				for project,langs in pairs(data) do
					for _,lang in pairs(langs) do
						if wiki == (lang .. project) then
							table.insert(filtered_geojson, user_infos);
							should_break = true;
							break;
						end
					end
					if should_break == true then
						break;
					end
				end
				if should_break == true then
					break;
				end
			end
		end
	end
	return filtered_geojson;
end

local function filter_project(geojson, projects)
	local filtered_geojson = {};
	projects = mw.text.split(projects, ",", true);
	for _, user_infos in pairs(geojson) do
		local data = user_infos.data;
		for _,project in pairs(projects) do
			if data[ project ] ~= nil then
				table.insert(filtered_geojson, user_infos);
				break;
			end
		end
	end
	return filtered_geojson;
end

local function filter_lang(geojson, languages)
	local filtered_geojson = {};
	languages = mw.text.split(languages, ",", true);
	for _, user_infos in pairs(geojson) do
		local data = user_infos.data;
		local should_break = false;
		for _,project in pairs(data) do
			for _,lang in pairs(project) do
				for _,language in pairs(languages) do
					if language == lang then
						table.insert(filtered_geojson, user_infos);
						should_break = true;
						break;
					end
				end
				if should_break == true then
					break;
				end
			end
			if should_break == true then
				break;
			end
		end
	end
	return filtered_geojson;
end


function p.draw(frame)
	local args = frame:getParent().args
	
	-- Choose the tagname
	local tagname = 'mapframe'
	if args.lien ~= nil then
		tagname = 'maplink'
	end
	
	-- Manage the tag params
	local tagArgs = {
		latitude = args.latitude == nil and "25" or args.latitude,
		longitude = args.longitude == nil and "22" or args.longitude,
		zoom = args.zoom == nil and 2 or tonumber(args.zoom),
		height = args.height == nil and 500 or tonumber(args.height),
		width = args.width == nil and 900 or tonumber(args.width),
		align = args.align == nil and 'center' or args.align,
	}
	if args.frameless ~= "no" then
		tagArgs.frameless = ''
	end
	
	-- Fetch the datas from commons
	local geojson = mw.ext.data.get("Wikimedians.map").data.features;
	
	-- Filter the inputs if asked for
	if args.site ~= nil then
		geojson = filter_wiki(geojson, args.site);
	elseif args.project ~= nil then
		geojson = filter_project(geojson, args.project);
	elseif args.lang ~= nil then
		geojson = filter_lang(geojson, args.lang);
	end
	
	geojson = '{"type": "FeatureCollection", "features": ' .. mw.text.jsonEncode(geojson) .. '}';
	
	return frame:extensionTag(tagname, geojson, tagArgs);
end

return p