Module:ResolveEntityId: Difference between revisions

Content deleted Content added
No need to get parent args, rename functions
add check that page is valid
 
(10 intermediate revisions by 4 users not shown)
Line 1:
local p = {}
 
function p.getEntity_id(frameidOrTitle, alt)
local function checkId(id)
local z=mw.wikibase.getEntity(frame.args[1])
if id =and mw.wikibase.getEntityIdForTitleentityExists(id) then
end
local instanceOfsitelink = mw.wikibase.getBestStatementsgetSitelink(id, 'P31')[1]
 
if idsitelink then
function p._entityid(frame,id)
return mw.wikibase.getEntityIdForTitle(sitelink) or mw.wikibase.getEntity(id).id
if type(id) == 'string' then
id = mw.ustring.upper(mw.ustring.sub(id,1,1))..mw.ustring.sub(id,2)
if mw.ustring.match(id,'^Q%d+$') then
-- id is in the proper format for a Wikidata entity
if frame:preprocess("{{#iferror:{{#invoke:ResolveEntityId|getEntity|".. id .. "}}|error}}") ~= "error" then
-- getEntity(id) doesn't return an error
return id
end
return local z=mw.wikibase.getEntity(frameid).args[1])id
else
return idalt
id = mw.wikibase.getEntityIdForTitle(id)
end
if id then
end
-- id is a title that matches a Wikidata entity
 
local instanceOf = mw.wikibase.getBestStatements(id, 'P31')[1]
if type(ididOrTitle) == 'string' then
if instanceOf and instanceOf.mainsnak.datavalue.value.id ~= 'Q4167410' then
ididOrTitle = mw.ustring.upper(mw.ustring.sub(ididOrTitle, 1, 1)) .. mw.ustring.sub(ididOrTitle, 2)
-- not disambiguation
if mw.wikibase.isValidEntityId(idOrTitle) then
return id
-- ididOrTitle is in the proper format for a Wikidata entity ID
return checkId(idOrTitle)
else
local eid = mw.wikibase.getEntityIdForTitle(idOrTitle)
if eid then
-- ididOrTitle is a title that matches a Wikidata entity
local instanceOf = mw.wikibase.getBestStatements(eid, 'P31')[1] --instance of
if not instanceOf andor instanceOf.mainsnak.datavalue.value.id ~= 'Q4167410' then
-- instance-of value is missing or is not "disambiguation"
return idcheckId(eid)
end
else
-- idOrTitle is a title, but no wikidata item exists for that title
local page = mw.title.new(idOrTitle)
if page then -- valid title
local rtarget = page.redirectTarget
if rtarget then -- title is a Wikipedia redirect
return p._id(rtarget.fullText, alt)
end
end
end
end
end
return nilalt
end
 
function p.entityid(frame)
return p._entityid_id(frame.args[1], frame.args[12])
end