Jump to content

Module:Import table

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by MSGJ (talk | contribs) at 15:46, 27 October 2022 (rewrite to use v2 syntax). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require("strict")
local p = {}

local function resolveqid(title)
	local resolveEntity = require("Module:ResolveEntityId")._id
	local qid
	if title then
		local rawarticle = string.match(title,'%[%[(.+)%|') or string.match(title,'%[%[(.+)%]%]')
		if rawarticle then
			qid = resolveEntity(rawarticle)
		end
	end
	return qid
end

function p.import(frame)
	local tab = "|"
	local wikiqid = "Q328"
	local eol = "<br>" -- end of line string
	local coord2text = require("Module:Coordinates/sandbox")._coord2text
	local args = frame.args or frame:getParent().args
	if not args.page then
		return "No page specified."
	end
	if not args.config then
		return "No configuation."
	end
	local config = {}
	for c1,col in ipairs(mw.text.split(args.config,",")) do
		config[c1] = mw.text.split(col,"-")
	end
	local content = mw.title.new(args.page):getContent() -- read page specified
	content = string.match(content,"%{%|(.+)%|%}") -- keep table only
	local rows = mw.text.split(content,"|-",true) -- split table into rows
	table.remove(rows,1) -- remove heading row
	local output = ""
	local v2 = ""
	for rn,row in ipairs(rows) do
		local import = {}
		local columns = mw.text.split(row,"\n|") -- split table rom into columns
		table.remove(columns,1) -- remove content before the first | character
		local qid = resolveqid(columns[1]) -- resolve qid of link in first column
		local v2qid
		local create = false
		if qid then
			v2qid = qid
		else
			create = true
			v2qid = "LAST"
		end
		local function addtov2(prop,val)
			if create then
				v2 = v2 .. "CREATE" .. eol
				create = false
			end
			v2 = v2 .. v2qid .. tab .. prop .. tab .. val .. tab .. "S143" .. tab .. wikiqid .. eol
			return v2
		end
		for cn,col in ipairs(columns) do
			for ref in mw.ustring.gmatch(col,"%<ref.+%<%/ref%>") do
				col = mw.ustring.gsub(col,ref,"")
			end
			col = mw.text.trim(col)
			if  config[cn][1] == "wikilink" then
				local val = resolveqid(col)
				if val then
					import[#import+1] = {}
					import[#import].property = config[cn][2]
					import[#import].value = val
					v2 = addtov2(config[cn][2],val)
				end
			elseif config[cn][1] == "year" then
				import[#import+1] = {}
				import[#import].property = config[cn][2]
				import[#import].value = "+" .. col .. "-00-00T00:00:00Z/9"
				v2 = addtov2(config[cn][2],"+" .. col .. "-00-00T00:00:00Z/9")
			elseif config[cn][1] == "quantity" then
				import[#import+1] = {}
				col = string.gsub(col,",","") -- remove any commas
				import[#import].property = config[cn][2]
				if string.upper(string.sub(config[cn][3],1,1)) == "Q" then
					config[cn][3] = string.sub(config[cn][3],2)
				end
				import[#import].value = col .. "U" .. config[cn][3]
				v2 = addtov2(config[cn][2],col .. "U" .. config[cn][3])
			--elseif config[cn][1] == "coord" then
				--import[#import+1] = {}
				--import[#import].property = config[cn][2]
				--import[#import].value = coord2text(col,"lat") .. "/" .. coord2text(col,"long")
			end
		end
		if qid then
			local qslink = ""
			for cn = 1,#import do
				qslink = qslink .. qid .. "|" .. import[cn].property .. "|" .. import[cn].value .. "|S143|" .. wikiqid .. "||"
			end
			output = output .. '<span class="qs">[[File:Commons to Wikidata QuickStatements.svg|20px|link=https://quickstatements.toolforge.org/#/v1=' .. mw.uri.encode(qslink,"PATH") .. ']]</span>'
		end
	end
	return v2
end

return p