Module:Infobox plantage

Moduledocumentatie​[bekijk] [bewerk] [ververs] [geschiedenis]

local p = {}

--Function checking if a value id empty (nil or empty string)
function is_empty(ie_val)
	if(ie_val) then --if not nul, check for empty string
		if(string.len(ie_val)>0 and ie_val~="-") then return false; --length of value not zero and value not a dash, means not empty
		elseif(ie_val=="-") then return true; --dash, means empty
		else return true; end --empty string, means empty
	else return true; end --if nil, then value is empty
end

--Function gets a Wikidata item id and returns link to a corresponding page, or if absent, just text label
function title_for(item_id)
	tf_item = mw.wikibase.getEntity(item_id);
	if(tf_item.sitelinks and tf_item.sitelinks.nlwiki) then --there is a sitelink to nlwiki
		page = tf_item.sitelinks.nlwiki.title;
		return "[["..page.."|"..mw.text.split(page, " %(")[1].."]]";
	elseif(tf_item.labels.nl) then return tf_item.labels.nl.value; --there is a label in dutch
	else return ""; end --if neither, return empty string
end


--Main function
function p.main(frame)
	parent = frame:getParent();
	args = parent.args;

	item = mw.wikibase.getEntity(mw.wikibase.getEntityIdForCurrentPage());
	label = '\n|-\n| style="width:120px" | '; --beginning of a row
	res = {'{| class="infobox" style="width:{{Infobox/breedte}}px; font-size:85%;"'}; --table in which result will be stored

	--Title
	if(is_empty(args["naam"])) then --if argument "naam" not set, it gets page name with text in brackets removed
		args["naam"] = mw.text.split(tostring(mw.title.getCurrentTitle()), " %(")[1]; end
	table.insert(res, '\n|-\n| align="center" colspan="2"  bgcolor="#00aaFF" | '
	  .."<big>'''"..args["naam"].."'''</big>"); --insert argument "naam" into the result
	
	--Image
	if(is_empty(args["afbeelding"]) and item and item.claims["P18"]) then 
		args["afbeelding"] = item:getBestStatements('P18')[1].mainsnak.datavalue.value; end
	if(is_empty(args["afbeeldingbreedte"])) then args["afbeeldingbreedte"] = "290px"; end
	if(is_empty(args["beschrijving_afbeelding"]) and 
	  item and item.claims["P18"] and 
	  item.claims["P18"][1].qualifiers and 
	  item.claims["P18"][1].qualifiers["P2096"]) then 
		desc_text = ""; nl_index = -1;
		desc = item:getBestStatements('P18')[1].qualifiers["P2096"];
		--iterate over P2096 qualifiers, untill there is one with dutch language
		for i=1,#desc,1 do
				if(desc[i].datavalue.value.language=="nl") then
				  nl_index = i; break; end
		end
		if(nl_index~=-1) then args["beschrijving_afbeelding"] = desc[nl_index].datavalue.value.text; end
	end
	
	--insert image and its description, if present, into the result table
	if(not is_empty(args["afbeelding"])) then 
		table.insert(res, '\n|-\n| align="center" colspan="2" | [[Bestand:'..
		  args["afbeelding"].."|"..args["afbeeldingbreedte"].."|center]]"); end
	if(not is_empty(args["beschrijving_afbeelding"])) then 
		table.insert(res, "{{center|1=''"..args["beschrijving_afbeelding"].."''}}"); end
	
	--Country
	if(is_empty(args["land"]) and item and item.claims["P17"]) then 
		args["land"] = title_for(item:getBestStatements('P17')[1].mainsnak.datavalue.value.id); end
	if(is_empty(args["vlag"]) and item and item.claims["P17"]) then 
		--getting the item for country, then retrieving iso-3 code from it to use in Template:Vlagland
		country_item = mw.wikibase.getEntity(item:getBestStatements('P17')[1].mainsnak.datavalue.value.id);
		if(country_item and country_item.claims["P298"]) then
			args["vlag"] = '{{Vlagland|code='..
			  country_item:getBestStatements('P298')[1].mainsnak.datavalue.value..'}}&nbsp;';
		else args["vlag"] = ""; end
	elseif(is_empty(args["vlag"])) then args["vlag"] = ""; end --if there is no iso-3 code, there would be no flag
	if(not is_empty(args["land"])) then 
		table.insert(res, label.."'''Land'''".." || "..args["vlag"]..args["land"]); end
	
	--Place
	if(is_empty(args["plaats"]) and item and item.claims["P131"]) then 
		args["plaats"] = title_for(item:getBestStatements('P131')[1].mainsnak.datavalue.value.id); end
	if(not is_empty(args["plaats"])) then 
		table.insert(res, label.."'''Plaats'''".." || "..args["plaats"]); end
	
	--Date created
	if(is_empty(args["opgericht"]) and item and item.claims["P571"]) then 
		--date in Wikidata is stored in format +YYYY-MM-DD... , only year is needed
		date_string = item:getBestStatements('P571')[1].mainsnak.datavalue.value.time;
		date_string = mw.text.split(date_string, "+")[2];
		date_string = mw.text.split(date_string, "-")[1];
		args["opgericht"] = "[["..date_string.."]]"; end
	if(not is_empty(args["opgericht"])) then 
		table.insert(res, label.."'''Opgericht'''".." || "..args["opgericht"]); end
	
	--Water bodies
	if(is_empty(args["waterlichamen"]) and item and item.claims["P206"]) then 
		water_list = {};
		--there can be multiple values of P206, adding each of them to the table
		for i = 1,#item.claims["P206"],1 do 
			table.insert(water_list, title_for(item.claims["P206"][i].mainsnak.datavalue.value.id)); end
		args["waterlichamen"] = table.concat(water_list, ", <br/>"); --result is a table concatenated with separator being a comma and newline 
	end
	if(not is_empty(args["waterlichamen"])) then 
		table.insert(res, label.."'''Waterlichamen'''".." || "..args["waterlichamen"]); end
	
	--Produces
	if(is_empty(args["produceert"]) and item and item.claims["P1056"]) then 
		prod_list = {};
		--there can be multiple values of P1056, adding each of them to the table
		for i = 1,#item.claims["P1056"],1 do 
			table.insert(prod_list, title_for(item.claims["P1056"][i].mainsnak.datavalue.value.id)); end
		args["produceert"] = table.concat(prod_list, ", <br/>");
	end
	if(not is_empty(args["produceert"])) then 
		table.insert(res, label.."'''Produceert'''".." || "..args["produceert"]); end
	
	--Links
	if(is_empty(args["beschreven_op"]) and item and item.claims["P973"]) then 
		url_list = {};
		--there can be multiple values of P973, adding each of them to the table
		for i = 1,#item.claims["P973"],1 do 
			link = item.claims["P973"][i].mainsnak.datavalue.value;
			--link text will be a part of url between http:// and first /
			link_text = mw.text.split(mw.text.split(link, "//")[2], "/")[1];
			table.insert(url_list, "["..link.." "..link_text.."]"); end
		args["beschreven_op"] = table.concat(url_list, "<br/>");
	end
	if(not is_empty(args["beschreven_op"])) then 
		table.insert(res, label.."'''Beschreven op'''"..
		  " || <small>"..args["beschreven_op"].."</small>"); end
	
	--Coordinates and map
	if((is_empty(args["breedtegraad"]) or is_empty(args["lengtegraad"])) and 
	  item and item.claims["P625"]) then
	    --if arguments "breedtegraad" and "lengtegraad" not set, take value from Wikidata
		coord = item:getBestStatements('P625')[1].mainsnak.datavalue.value;
		args["breedtegraad"] = coord.latitude;
		args["lengtegraad"] = coord.longitude;
	end
	--if both latitude and longtitude are set, add an OSM-map
	if( (not is_empty(args["breedtegraad"])) and (not is_empty(args["lengtegraad"])) ) then 
		if(is_empty(args["mapframe_breedte"])) then 
			args["mapframe_breedte"] = mw.text.split(args["afbeeldingbreedte"], "px")[1];
		else args["mapframe_breedte"] = mw.text.split(args["mapframe_breedte"], "px")[1]; end
		table.insert(res, '\n|-\n| align="center" colspan="2" | '..
		  '<mapframe width='..args["mapframe_breedte"]..' height='..args["mapframe_breedte"]..
		  ' latitude='..args["breedtegraad"]..' longitude='..args["lengtegraad"]..
		  ' zoom=11 frameless align="center">'..
		  '{"type": "Feature","geometry": { "type": "Point", "coordinates": ['..
		  args["lengtegraad"]..','..args["breedtegraad"]..
		  '] },"properties": {"title": "'..args["naam"]..'"'..
		  ',"marker-symbol": "farm","marker-size": "medium","marker-color": "080"'..
		  '}}</mapframe>');
	end

	return frame:preprocess(table.concat(res)..'\n|}'); --the final result id the concatenated result table
end

return p;