Module:ResolveEntityId: Difference between revisions

m
1 revision imported: Templates and CSS files
(-- backwards compatibility for deprecated _entityid function)
 
m (1 revision imported: Templates and CSS files)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local p = {}
local p = {}


function p._entityid(_,id,alt)
function p._id(idOrTitle, alt)
-- backwards compatibility for deprecated _entityid function
local function checkId(id)
return p._id(id,alt)
if id and mw.wikibase.entityExists(id) then
end
local sitelink = mw.wikibase.getSitelink(id)
if sitelink then
return mw.wikibase.getEntityIdForTitle(sitelink) or mw.wikibase.getEntity(id).id
end
return mw.wikibase.getEntity(id).id
else
return alt
end
end


function p._id(id,alt)
if type(idOrTitle) == 'string' then
if type(id) == 'string' then
idOrTitle = mw.ustring.upper(mw.ustring.sub(idOrTitle, 1, 1)) .. mw.ustring.sub(idOrTitle, 2)
id = mw.ustring.upper(mw.ustring.sub(id,1,1))..mw.ustring.sub(id,2)
if mw.wikibase.isValidEntityId(idOrTitle) then
if mw.ustring.match(id,'^Q%d+$') then
-- idOrTitle is in the proper format for a Wikidata entity ID
-- id is in the proper format for a Wikidata entity
return checkId(idOrTitle)
if mw.wikibase.isValidEntityId(id) then
else
-- id is valid
local eid = mw.wikibase.getEntityIdForTitle(idOrTitle)
id = mw.wikibase.getEntity(id)
if eid then
if id then
-- idOrTitle is a title that matches a Wikidata entity
-- entity exists
local instanceOf = mw.wikibase.getBestStatements(eid, 'P31')[1] --instance of
return id.id
if not instanceOf or instanceOf.mainsnak.datavalue.value.id ~= 'Q4167410' then
-- instance-of value is missing or is not "disambiguation"
return checkId(eid)
end
end
end
else
else
-- idOrTitle is a title, but no wikidata item exists for that title
id = mw.wikibase.getEntityIdForTitle(id)
local page = mw.title.new(idOrTitle)
if id then
if page then -- valid title
-- id is a title that matches a Wikidata entity
local rtarget = page.redirectTarget
local instanceOf = mw.wikibase.getBestStatements(id, 'P31')[1] --instance of
if rtarget then -- title is a Wikipedia redirect
if instanceOf and instanceOf.mainsnak.datavalue.value.id ~= 'Q4167410' then
return p._id(rtarget.fullText, alt)
-- not disambiguation
end
return mw.wikibase.getEntity(id).id
elseif instanceOf == nil then
-- id is a title, but is missing an instance-of value
return mw.wikibase.getEntity(id).id
end
end
end
end
end
end
end
end
return alt or nil
return alt
end
end