Jump to content

Module:Attached KML

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Evad37 (talk | contribs) at 16:38, 17 August 2016 (make a start). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

-- Table of available wikis, in the order that they are to be searched for kml files
-- (once a kml file is found, further sites are not checked)
local sites = {}
sites[1] = { "commonswiki", "commons.wikimedia.org" }
sites[2] = { "enwiki", "en.wikipedia.org" }
sites[3] = { "zhwiki", "zh.wikipedia.org" }


local p = {}

function p.main(frame)
	local parent = frame.getParent(frame)
	local url
	if not (frame.args.uselocal) then
		url = getUrlFromWikidata()
	end
	if not (url) then
		local frompage = parent.args.from or mw.title.getCurrentTitle()
		url = "https://en.wikipedia.org/w/index.php?title=Template:Attached_KML/" .. mw.uri.encode(frompage, "WIKI" ) .. "&action=raw"
	end
	return url	-- TODO: pass url through to proposed functions below

	
end

-- TODO: function to make external links from url
-- TODO: _main function to replicate current template (create wikitext for title display, or inline display, or both)


-- TODO: function to make title links
-- TODO: function to make inline (box) links


local function getUrlFromWikidata()				-- Attempts to get url from linked wikidata items, will return nil if it can't
	local entity = mw.wikibase.getEntityObject()
	if not entity then return nil end			

	local kml_claim = entity.claims["P000"]			-- TODO: replace with real P value once property created
	
	if kml_claim then
		-- get the QID of the first value of the property
		if (kml_claim[1] and kml_claim[1].mainsnak.snaktype == "value" and kml_claim[1].mainsnak.datavalue.type == "wikibase-entityid") then
			local kml_qid = "Q" .. claims[1].mainsnak.datavalue.value["numeric-id"]
			local kml_entity = mw.wikibase.getEntity( kml_qid )
			if not kml_entity then return nil end
			local kml_sitelink
			local kml_url
			for i, v in ipairs( sites ) do
				kml_sitelink = kml_entity:getSitelink( v[1] )
				if kml_sitelink then kml_url = "//" .. v[2] .. "/w/index.php?title=" .. mw.uri.encode( kml_sitelink, "WIKI" ) .. "&action=raw" end
				if kml_url then break end
			end
			return kml_url or nil
		else
			return nil
		end
	else
		return nil
	end
end

return p