Module:Import table/sandbox

{{#invoke:Import table|import |page= |config= |common= }} This module is designed to import data from tables in Wikipedia articles into Wikidata. The first colu

Module:Import table/sandbox
require("strict")
local p = {}

local function resolveqid(label)
	local resolveEntity = require("Module:ResolveEntityId")._id
	local qid
	local rawlabel
	if label then
		rawlabel = mw.ustring.match(label,'%[%[([^%|%]]+)%|') or mw.ustring.match(label,'%[%[([^%|%]]+)%]%]')
		if rawlabel then
			qid = resolveEntity(rawlabel)
		else
			rawlabel = label
		end
	end
	return qid,rawlabel
end

local function tidystring(string)
	local tidy = mw.ustring.gsub(string,"%<ref.+%<%/ref%>","") -- remove any references with <ref .. </ref>
	tidy = mw.ustring.gsub(tidy,"%<ref.+%/%>","") -- remove any references with <ref .. />
	tidy = mw.ustring.gsub(tidy,"^%s*%|","") -- remove | character at start
	tidy = mw.text.trim(tidy) -- trim spaces
	return tidy
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 = mw.ustring.match(content,"%{%|(.+)%|%}") -- keep table only
	content = mw.ustring.gsub(content,"||","\n|") -- use \n| for column breaks
	local rows = mw.text.split(content,"%|%-\n") -- split table into rows, prev %|%-[^\n]*\n
	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 = tidystring(label)
			local qid = mw.ustring.match(label,"Q%d+") -- check if QID is specified in first column
			if not qid then
				qid,label = resolveqid(label) -- resolve qid if first column is link
			end
			local entity
			if qid then
				entity = mw.wikibase.getEntity(qid)
			else
				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 "
						if common["P131"] then -- add region too
							v2 = v2 .. mw.wikibase.getLabel(common["P131"]) .. ", "
						end
						v2 = v2 .. mw.wikibase.getLabel(common["P17"])
					elseif common["P376"] then -- add planet
						v2 = v2 .. " on " .. mw.wikibase.getLabel(common["P376"])
					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
					local import = true
					if entity then
						if config[cn][2] then
							if entity:getAllStatements(config[cn][2])[1] then -- statement already present for this property
								import = false
							end
						else -- no property specified to import to
							import = false
						end
					end
					if import then
						col = tidystring(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 = mw.ustring.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 = mw.ustring.gsub(col,",","") -- remove any commas
							val = mw.ustring.match(val,"%d+%.?%d*") -- extract value, possibly from inside convert template
							if val then
								if mw.ustring.upper(mw.ustring.sub(config[cn][3],1,1)) == "Q" then
									config[cn][3] = mw.ustring.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
							local val = mw.ustring.match(col,"%{%{[Cc]oord.*%}%}")
							if val then
								val = mw.getCurrentFrame():preprocess(col)
								v2 = addtov2(config[cn][2],"@" .. coord2text(val,"lat") .. "/" .. coord2text(val,"long"))
							end
						elseif config[cn][1] == "image" then
							local val = mw.ustring.match(col,"%[%[File%:([^%|%]]+).*%]%]")
							if val then
								v2 = addtov2(config[cn][2],'"' .. val .. '"')
							end
						end
					end
				end
			end
		end
	end
	return v2
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.