Module:ResolveEntityId: Difference between revisions

Content deleted Content added
No reason to pass the frame any more now that we're not using the hacky workaround
add check that page is valid
 
(4 intermediate revisions by 2 users not shown)
Line 1:
local p = {}
 
function p._entityid_id(_,ididOrTitle, alt)
local function p._idcheckId(id,alt)
return p._id(id,alt)
if id and mw.wikibase.isValidEntityIdentityExists(id) then
end
idlocal sitelink = mw.wikibase.getEntitygetSitelink(id)
 
if idsitelink then
function p._id(id,alt)
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 mw.wikibase.isValidEntityId(id) then
-- id is valid
id = mw.wikibase.getEntity(id)
if id then
-- entity exists
return id.id
end
end
return mw.wikibase.getEntity(id).id
else
return id.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] --instance of
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
returnif mw.wikibase.getEntityisValidEntityId(ididOrTitle).id then
-- ididOrTitle is in the proper format for a Wikidata entity ID
elseif instanceOf == nil then
return checkId(idOrTitle)
-- id is a title, but is missing an instance-of value
else
return mw.wikibase.getEntity(id).id
local eid = mw.wikibase.getEntityIdForTitle(idOrTitle)
if ideid then
-- ididOrTitle is a title that matches a Wikidata entity
local instanceOf = mw.wikibase.getBestStatements(ideid, '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 checkId(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(idrtarget.fullText, alt)
end
end
end
end
end
return alt or nil
end