Module:ResolveEntityId/sandbox: Difference between revisions

Content deleted Content added
Fix handling of missing sitelink in code introduced by User:Snævar
add check that page is valid
 
(11 intermediate revisions by 3 users not shown)
Line 1:
local p = {}
 
function p._id(ididOrTitle, alt)
local function resolveIdcheckId(sid)
if mw.wikibase.getSitelink(s)id and mw.wikibase.getEntityIdForTitle(mw.wikibase.getSitelinkentityExists(s)id) then
returnlocal sitelink = mw.wikibase.getEntityIdForTitle(mw.wikibase.getSitelink(s)id)
if sitelink then
return mw.wikibase.getEntityIdForTitle(sitelink) or mw.wikibase.getEntity(id).id
end
ifreturn mw.wikibase.entityExistsgetEntity(id) then.id
else
return mw.wikibase.getEntity(s).idalt
end
end
 
if type(ididOrTitle) == 'string' then
ididOrTitle = mw.ustring.upper(mw.ustring.sub(ididOrTitle, 1, 1)) .. mw.ustring.sub(ididOrTitle, 2)
if mw.ustringwikibase.matchisValidEntityId(id,'^Q%d+$'idOrTitle) then
-- ididOrTitle is in the proper format for a Wikidata entity ID
return checkId(idOrTitle)
if mw.wikibase.isValidEntityId(id) then
-- id is valid
if mw.wikibase.entityExists(id) then
-- entity exists
return resolveId(id)
end
end
else
local eid = mw.wikibase.getEntityIdForTitle(ididOrTitle)
if eid then
-- entity 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') or (instanceOf == nil and mw.wikibase.entityExists(eid)) then
-- notinstance-of disambiguation,value oris existsmissing butor is missingnot an instance-of value"disambiguation"
localreturn rtarget = mw.title.newcheckId(eid).redirectTarget
if rtarget ~= false then
-- title is a Wikipedia redirect
return p._id(rtarget.fullText,alt)
else
-- title is neither disambiguation or redirect
return resolveId(id)
end
end
else
-- idOrTitle is a title, but no wikidata item exists for that title
local rtargetpage = mw.title.new(ididOrTitle).redirectTarget
if rtargetpage ~=then false-- thenvalid title
local rtarget = page.redirectTarget
-- title is a Wikipedia redirect
returnif p._id(rtarget.fullText,alt) then -- title is a Wikipedia redirect
return p._id(rtarget.fullText, alt)
end
end
end
end
end
return alt or nil
end