Jump to content

Module:Import table: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
add label, description and common statements for items being created
no import if label blank
Line 49: Line 49:
table.remove(columns,1) -- remove content before the first \n| character
table.remove(columns,1) -- remove content before the first \n| character
local label = columns[1]
local label = columns[1]
label = mw.text.trim(label)
if label then
label = mw.text.trim(label)
local qid = resolveqid(label) -- resolve qid if first column is link
local qid = resolveqid(label) -- resolve qid if first column is link
if not qid then
if not qid then
v2 = v2 .. "CREATE" .. eol
v2 = v2 .. "CREATE" .. eol
qid = "LAST" -- creating new item, so use LAST
qid = "LAST" -- creating new item, so use LAST
v2 = v2 .. qid .. tab .. 'Len' .. tab .. '"' .. label .. '"' .. eol
v2 = v2 .. qid .. tab .. 'Len' .. tab .. '"' .. label .. '"' .. eol
if common["P31"] then -- create auto-description
if common["P31"] then -- create auto-description
v2 = v2 .. qid .. tab .. 'Den' .. tab .. '"' .. mw.wikibase.getLabel(common["P31"])
v2 = v2 .. qid .. tab .. 'Den' .. tab .. '"' .. mw.wikibase.getLabel(common["P31"])
if common["P17"] then -- add country
if common["P17"] then -- add country
v2 = v2 .. ' in ' .. mw.wikibase.getLabel(common["P17"])
v2 = v2 .. ' in ' .. mw.wikibase.getLabel(common["P17"])
end
v2 = v2 .. '"' .. eol
end
for c = 1,#common do
v2 = v2 .. qid .. tab .. common[c][1] .. tab .. common[c][2] .. eol
end
end
v2 = v2 .. '"' .. eol
end
end
local function addtov2(prop,val)
for c = 1,#common do
v2 = v2 .. qid .. tab .. common[c][1] .. tab .. common[c][2] .. eol
v2 = v2 .. qid .. tab .. prop .. tab .. val .. tab .. "S143" .. tab .. wikiqid .. eol
return v2
end
end
for cn,col in ipairs(columns) do
end
if config[cn] then
local function addtov2(prop,val)
for ref in mw.ustring.gmatch(col,"%<ref.+%<%/ref%>") do
v2 = v2 .. qid .. tab .. prop .. tab .. val .. tab .. "S143" .. tab .. wikiqid .. eol
col = mw.ustring.gsub(col,ref,"")
return v2
end
for cn,col in ipairs(columns) do
if config[cn] then
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
v2 = addtov2(config[cn][2],val)
end
end
col = mw.text.trim(col)
elseif config[cn][1] == "year" then
if config[cn][1] == "wikilink" then
local val = string.match(col,"%d%d%d%d")
if val then
local val = resolveqid(col)
if val then
v2 = addtov2(config[cn][2],"+" ..val .. "-00-00T00:00:00Z/9")
v2 = addtov2(config[cn][2],val)
end
elseif config[cn][1] == "quantity" then
local val = string.gsub(col,",","") -- remove any commas
val = string.match(val,"%d+%.?%d*") -- extract value, possibly from inside convert template
if val then
if string.upper(string.sub(config[cn][3],1,1)) == "Q" then
config[cn][3] = string.sub(config[cn][3],2)
end
end
v2 = addtov2(config[cn][2],val .. "U" .. config[cn][3])
elseif config[cn][1] == "year" then
local val = string.match(col,"%d%d%d%d")
end
elseif config[cn][1] == "text" then
if val then
v2 = addtov2(config[cn][2],"+" ..val .. "-00-00T00:00:00Z/9")
if col ~= "" then
end
v2 = addtov2(config[cn][2],'"' .. col .. '"')
elseif config[cn][1] == "quantity" then
local val = string.gsub(col,",","") -- remove any commas
val = string.match(val,"%d+%.?%d*") -- extract value, possibly from inside convert template
if val then
if string.upper(string.sub(config[cn][3],1,1)) == "Q" then
config[cn][3] = string.sub(config[cn][3],2)
end
v2 = addtov2(config[cn][2],val .. "U" .. config[cn][3])
end
elseif config[cn][1] == "text" then
if col ~= "" then
v2 = addtov2(config[cn][2],'"' .. col .. '"')
end
--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
--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
end
end

Revision as of 12:55, 1 November 2022

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 and rawarticle ~= "" then
			qid = resolveEntity(rawarticle)
		end
	end
	return qid
end

function p.import(frame)
	local tab = "|"
	local wikiqid = "Q328" -- QID for English Wikipedia
	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 common = {}
	if args.common then
		for c1,prop in ipairs(mw.text.split(args.common,",")) do
			common[c1] = mw.text.split(prop,"-")
			common[common[c1][1]] = common[c1][2] -- create index, e.g. common["P31"]=Q39715
		end
	end
	local content = mw.title.new(args.page):getContent() -- read page specified
	content = string.match(content,"%{%|(.+)%|%}") -- keep table only
	content = string.gsub(content,"||","\n|") -- use \n| for column breaks
	local rows = mw.text.split(content,"|-",true) -- split table into rows
	table.remove(rows,1) -- remove table definition
	table.remove(rows,1) -- remove heading row
	local output = ""
	local v2 = ""
	for rn,row in ipairs(rows) do
		local columns = mw.text.split(row,"\n|") -- split table rom into columns
		table.remove(columns,1) -- remove content before the first \n| character
		local label = columns[1]
		if label then
			label = mw.text.trim(label)
			local qid = resolveqid(label) -- resolve qid if first column is link
			if not qid then
				v2 = v2 .. "CREATE" .. eol
				qid = "LAST" -- creating new item, so use LAST
				v2 = v2 .. qid .. tab .. 'Len' .. tab .. '"' .. label .. '"' .. eol
				if common["P31"] then -- create auto-description
					v2 = v2 .. qid .. tab .. 'Den' .. tab .. '"' .. mw.wikibase.getLabel(common["P31"])
					if common["P17"] then -- add country
						v2 = v2 .. ' in ' .. mw.wikibase.getLabel(common["P17"])
					end
					v2 = v2  .. '"' .. eol
				end
				for c = 1,#common do
					v2 = v2 .. qid .. tab .. common[c][1] .. tab .. common[c][2] .. eol
				end
			end
			local function addtov2(prop,val)
				v2 = v2 .. qid .. tab .. prop .. tab .. val .. tab .. "S143" .. tab .. wikiqid .. eol
				return v2
			end
			for cn,col in ipairs(columns) do
				if config[cn] then
					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
							v2 = addtov2(config[cn][2],val)
						end
					elseif config[cn][1] == "year" then
						local val = string.match(col,"%d%d%d%d")
						if val then
							v2 = addtov2(config[cn][2],"+" ..val .. "-00-00T00:00:00Z/9")
						end
					elseif config[cn][1] == "quantity" then
						local val = string.gsub(col,",","") -- remove any commas
						val = string.match(val,"%d+%.?%d*") -- extract value, possibly from inside convert template
						if val then
							if string.upper(string.sub(config[cn][3],1,1)) == "Q" then
								config[cn][3] = string.sub(config[cn][3],2)
							end
							v2 = addtov2(config[cn][2],val .. "U" .. config[cn][3])
						end
					elseif config[cn][1] == "text" then
						if col ~= "" then
							v2 = addtov2(config[cn][2],'"' .. col .. '"')
						end
					--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
			end
		end
	end
	return v2
end

return p