Module:ResolveEntityId/doc: Difference between revisions

Content deleted Content added
Usage: passing the frame is no longer required
 
(13 intermediate revisions by 3 users not shown)
Line 5:
Functions similarly to [[:mw:Extension:Wikibase_Client/Lua#mw.wikibase.resolvePropertyId|mw.wikibase.resolvePropertyId]], but for [[:wikidata:|Wikidata]] [[:mw:Extension:Wikibase_Client/Lua#mw.wikibase.entity|entities]] instead of properties.
 
Returns an entity id for the given label or id. This allows using the entity's labels instead of ids in all places. If no entitiyentity was found for the label or id, or if the label is ambiguous, a nil value is returned.
 
When attempting to resolve a label, only entities with English Wikipedia sitelinks are considered in the search. If a label exists in Wikidata, but does not have the requisite language sitelink, a nil value is returned.
=== Call from within a module ===
An example call within a module might look like the following:
 
=== Call from within a module (_id) ===
<source lang="lua">local resolveEntity = require( "Module:ResolveEntityId" )
AnExample example callcalls within a module might look like the following:
id = resolveEntity._id('Q42')</source>
 
{| class="wikitable"
which would result in <code>id</code> = <code>{{#invoke:ResolveEntityId|entityid|Q42|'''nil'''}}</code>
|+ style="text-align:left"|
!Code !! Result !! Notes
|-
|<sourcesyntaxhighlight lang="lua">local resolveEntityresolveEntityId = require( "Module:ResolveEntityId" )._id
id = resolveEntityId('Q42')</syntaxhighlight>
which would result in|style="vertical-align: mid;"|<code>id</code> = <code>{{#invoke:ResolveEntityId|entityid|Q42|'''nil'''}}</code>
| "Q42" is a valid Wikidata ID, and a Wikidata items exists with that Id, so it is therefore returned intact
|-
|<sourcesyntaxhighlight lang="lua">local resolveEntityresolveEntityId = require( "Module:ResolveEntityId" )._id
id = resolveEntityId('Q0')</syntaxhighlight>
which would result in|style="vertical-align: mid;"|<code>id</code> = <code>{{#invoke:ResolveEntityId|entityid|Q0|'''nil'''}}</code>
| "Q0" is not a valid Wikidata ID, and while the Wikipedia article [[Q0]] exists, it is a disambiguation page, so <code>'''nil'''</code> is returned
|-
|<sourcesyntaxhighlight lang="lua">local resolveEntityresolveEntityId = require( "Module:ResolveEntityId" )._id
id = resolveEntityId('Q404')</syntaxhighlight>
which would result in|style="vertical-align: mid;"|<code>id</code> = <code>{{#invoke:ResolveEntityId|entityid|Q404|'''nil'''}}</code>
| "Q404" is a Wikidata redirect to Q395, so the latter is returned
|-
|<sourcesyntaxhighlight lang="lua">local resolveEntityresolveEntityId = require( "Module:ResolveEntityId" )._id
id = resolveEntityId('Q2147483647')</syntaxhighlight>
which would result in|style="vertical-align: mid;"|<code>id</code> = <code>{{#invoke:ResolveEntityId|entityid|Q151384451555Q2147483647|'''nil'''}}</code>
| Q2147483647 is a valid Wikidata ID, but no entity exists with that ID, so <code>'''nil'''</code> is returned
|-
|<sourcesyntaxhighlight lang="lua">local resolveEntityresolveEntityId = require( "Module:ResolveEntityId" )._id
id = resolveEntity._idresolveEntityId('Douglas Adams')</sourcesyntaxhighlight>
which would result in|style="vertical-align: mid;"|<code>id</code> = <code>{{#invoke:ResolveEntityId|entityid|Douglas Adams|'''nil'''}}</code>
| The article [[Douglas Adams]] exists and has the Wikidata ID "Q42", so that is returned.
|-
|<sourcesyntaxhighlight lang="lua">local resolveEntityresolveEntityId = require( "Module:ResolveEntityId" )._id
id = resolveEntity._idresolveEntityId('ThisIsNotARealWikidataItem')</sourcesyntaxhighlight>
which would result in|style="vertical-align: mid;"|<code>id</code> = <code>{{#invoke:ResolveEntityId|entityid|ThisIsNotARealWikidataItem|'''nil'''}}</code>
| "ThisIsNotARealWikidataItem" is not a valid Wikidata ID, and no Wikipedia article exists at [[ThisIsNotARealWikidataItem]], so <code>'''nil'''</code> is returned
|-
|<sourcesyntaxhighlight lang="lua">local resolveEntityresolveEntityId = require( "Module:ResolveEntityId" )._id
id = resolveEntity._idresolveEntityId('ThisIsNotARealWikidataItem', 'Wikidata ID not found!')</sourcesyntaxhighlight>
which would result in|style="vertical-align: mid;"|<code>id</code> = <code>{{#invoke:ResolveEntityId|entityid|ThisIsNotARealWikidataItem|'Wikidata ID not found!'}}</code>
| Same as above, but the custom error message <code>Wikidata ID not found!</code> is returned
|-
|<syntaxhighlight lang="lua">local resolveEntityId = require( "Module:ResolveEntityId" )._id
id = resolveEntityId('Douglas adams')</syntaxhighlight>
|style="vertical-align: mid;"|<code>id</code> = <code>{{#invoke:ResolveEntityId|entityid|Douglas adams|'''nil'''}}</code>
| "Douglas adams" is not a valid Wikidata ID, and while [[Douglas adams]] (with a lower-case "a") exists, it is a redirect to [[Douglas Adams]]. Therefore the Wikidata ID for the latter page is returned
|-
|<syntaxhighlight lang="lua">local resolveEntityId = require( "Module:ResolveEntityId" )._id
id = resolveEntityId('Wikipedia:Village pump (technical)/Archive 1')</syntaxhighlight>
|style="vertical-align: mid;"|<code>id</code> = <code>{{#invoke:ResolveEntityId|entityid|Wikipedia:Village pump (technical)/Archive 1|'''nil'''}}</code>
| "Wikipedia:Village pump (technical)/Archive 1" is not a valid Wikidata ID, and while [[Wikipedia:Village pump (technical)/Archive 1]] exists, it does not have a Wikidata ID, so <code>'''nil'''</code> is returned
|}
 
=== Use from within a template (entityid) ===
<source lang="lua">local resolveEntity = require( "Module:ResolveEntityId" )
The following will return the entity id (or <code>'''nil'''</code>nothing if the ID doesn't exist):
id = resolveEntity._id('Q0')</source>
 
which would result in <code>id</code> = <code>{{#invoke:ResolveEntityId|entityid|Q0|'''nil'''}}</code>
 
<source lang="lua">local resolveEntity = require( "Module:ResolveEntityId" )
id = resolveEntity._id('Q404')</source>
 
which would result in <code>id</code> = <code>{{#invoke:ResolveEntityId|entityid|Q404|'''nil'''}}</code>
 
<source lang="lua">local resolveEntity = require( "Module:ResolveEntityId" )
id = resolveEntity._id('Q151384451555')</source>
 
which would result in <code>id</code> = <code>{{#invoke:ResolveEntityId|entityid|Q151384451555|'''nil'''}}</code>
 
<source lang="lua">local resolveEntity = require( "Module:ResolveEntityId" )
id = resolveEntity._id('Douglas Adams')</source>
 
which would result in <code>id</code> = <code>{{#invoke:ResolveEntityId|entityid|Douglas Adams|'''nil'''}}</code>
 
<source lang="lua">local resolveEntity = require( "Module:ResolveEntityId" )
id = resolveEntity._id('ThisIsNotARealWikidataItem')</source>
 
which would result in <code>id</code> = <code>{{#invoke:ResolveEntityId|entityid|ThisIsNotARealWikidataItem|'''nil'''}}</code>
 
<source lang="lua">local resolveEntity = require( "Module:ResolveEntityId" )
id = resolveEntity._id('ThisIsNotARealWikidataItem', 'Wikidata ID not found!')</source>
 
which would result in <code>id</code> = <code>{{#invoke:ResolveEntityId|entityid|ThisIsNotARealWikidataItem|'Wikidata ID not found!'}}</code>
 
=== Use from within a template ===
The following will return the entity id (or <code>'''nil'''</code> if the ID doesn't exist):
<code><nowiki>{{</nowiki>#invoke:{{BASEPAGENAME}}|entityid|''id''}}</code>
 
The following will return the entity id (or the alternate text if the ID doesn't exist):
<code><nowiki>{{</nowiki>#invoke:{{BASEPAGENAME}}|entityid|''id''|''alternate text if ''id'' is '''nil'''''}}</code>
 
=== Former _entityid function===
The <code>_entityid</code> function, which required that the frame be passed as the first argument, has been removed from the module because workarounds are no longer needed for [[:phab:T143970]].
 
<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox | |