Module:Import table
Appearance
![]() | This module is rated as alpha. It is ready for third-party input, and may be used on a few pages to see if problems arise, but should be watched. Suggestions for new features or changes in their input and output mechanisms are welcome. |
Usage
{{#invoke:Import table|import |page= |config= |common= }}
This module is designed to import data from tables in Wikipedia articles into Wikidata. The first column of the table must be the name of the item.
- If this is a link (or a redirect) to an existing article which has a corresponding Wikidata item, then the data will be imported into this item.
- If the first column contains a valid identifier for a Wikidata item (e.g. Q123456) then the data will be imported into this item.
- If this is plain text or a redlink, then the data will be imported into a new Wikidata item.
Parameters
- page - the name of the article/page to parse, e.g.
|page=List of dams in South Africa
- config - details about what type of data is held in each column of the table formatted as type-property-option. Currently recognised types are:
- label - the name of the article
- wikilink - a link to an article which should be the value of the property, e.g.
wikilink-P131
- quantity - specify the unit in the third parameter, e.g.
quantity-P2048-Q11573
- year - a 4-digit year, e.g.
year-P571
- text - any text to import into a string datatype, e.g.
text-P3562
- coord - coordinate position, inside the {{coord}} template, e.g.
coord-P625
- donotuse (or any other unrecognised type) - indicates a column that will not be imported
- common - a set of values that every item in the list should have, formatted in pairs as property-value, e.g.
|common=P31-Q12323,P17-Q258
Notes
- The module will not import any claim if there is already a statement for that property in Wikidata, no matter if the value is the same or different to that being imported, and even if the value is marked as deprecated.
require("strict")
local p = {}
function p.import(frame)
local wikiqid = "Q328"
local resolveEntity = require("Module:ResolveEntityId")._id
local coord2text = require("Module:Coordinates/sandbox")._coord2text
local args = frame.args or frame:getParent().args
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
if not args.page then
return "No page specified."
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
local output = ""
for rn,row in ipairs(rows) do
local import = {}
local qid
local columns = mw.text.split(row,"|") -- split table rom into columns
table.remove(columns,1) -- remove content before the first | character
--table.remove(columns,1) -- remove heading row
for cn,col in ipairs(columns) do
col = mw.text.trim(col)
if config[cn] then
if config[cn][1] == "label" then
local rawarticle = string.match(col,'%[%[(.+)%|') or string.match(col,'%[%[(.+)%]%]')
if rawarticle then
qid = resolveEntity(rawarticle)
end
elseif config[cn][1] == "wikilink" then
local rawarticle = string.match(col,'%[%[(.+)%|') or string.match(col,'%[%[(.+)%]%]')
local val
if rawarticle then
val = resolveEntity(rawarticle)
if val then
import[#import+1] = {}
import[#import].property = config[cn][2]
import[#import].value = val
end
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"
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]
-- 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
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 output
end
return p