Difference between revisions of "Module:Wikibase"

From blackwiki
Jump to navigation Jump to search
blackwiki>MusikAnimal
m (Protected "Module:Wikibase": High-risk module ([Edit=Require template editor access] (indefinite)))
m (14 revisions imported)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
---------- Module:Wikibase ----------------
 
---------- Module:Wikibase ----------------
 +
require('Module:No globals')
 
local p = {}
 
local p = {}
  
−
-- Return the item ID of the item linked to the current page.
+
-- Return the entity ID of the item linked to the current page.
 
function p.id(frame)
 
function p.id(frame)
 
if not mw.wikibase then
 
if not mw.wikibase then
 
return "no mw.wikibase"
 
return "no mw.wikibase"
 
end
 
end
−
entity = mw.wikibase.getEntityObject()
+
return mw.wikibase.getEntityIdForCurrentPage() or "no entity"
−
 
 
−
if entity == nil then
 
−
return "no entity"
 
−
end
 
−
return entity.id
 
 
end
 
end
  
−
-- Return the WD entity URL of a given data item, or of connected page
+
-- Return the URL of an entity given its entity ID
−
-- if no argument is provided to this method.
+
-- or the item linked to the current page if no argument is provided.
 
function p.wdurl(frame)
 
function p.wdurl(frame)
−
if frame.args[1] == nil then
+
return mw.wikibase.getEntityUrl(frame.args[1] and mw.text.trim(frame.args[1])) -- defaults to entity URL of the item linked to the current page
−
entity = mw.wikibase.getEntityObject()
 
−
if not entity then return nil end
 
−
id = entity.id
 
−
else
 
−
id = mw.text.trim(frame.args[1])
 
−
end
 
−
return mw.wikibase.getEntityUrl( id )
 
 
end
 
end
  
−
-- Return the label of a given data item, or of connected page
+
-- Return the label of an entity given its entity ID
−
-- if no argument is provided to this method.
+
-- or the item linked to the current page if no argument is provided.
 
function p.label(frame)
 
function p.label(frame)
−
if frame.args[1] == nil then
+
return mw.wikibase.getLabel(frame.args[1] and mw.text.trim(frame.args[1])) -- defaults to label of the item linked to the current page
−
entity = mw.wikibase.getEntityObject()
 
−
if not entity then return nil end
 
−
id = entity.id
 
−
else
 
−
id = mw.text.trim(frame.args[1])
 
−
end
 
−
return mw.wikibase.label( id )
 
 
end
 
end
  
−
-- Return the description of a given data item, or of connected page
+
-- Return the description of an entity given its entity ID
−
-- if no argument is provided to this method.
+
-- or the item linked to the current page if no argument is provided.
 
function p.description(frame)
 
function p.description(frame)
−
if frame.args[1] == nil then
+
return mw.wikibase.getDescription(frame.args[1] and mw.text.trim(frame.args[1])) -- defaults to description of the item linked to the current page
−
entity = mw.wikibase.getEntityObject()
 
−
if not entity then return nil end
 
−
id = entity.id
 
−
else
 
−
id = mw.text.trim(frame.args[1])
 
−
end
 
−
return mw.wikibase.description( id )
 
 
end
 
end
  
−
-- Return the local page about a given data item, or of connected page
+
-- Return the local title of an item given its entity ID
−
-- if id is not specified.
+
-- or the item linked to the current page if no argument is provided.
 
function p.page(frame)
 
function p.page(frame)
−
if frame.args[1] == nil then
+
local qid = frame.args[1] and mw.text.trim(frame.args[1])
−
entity = mw.wikibase.getEntityObject()
+
if not qid or qid == '' then
−
if not entity then return nil end
+
qid = mw.wikibase.getEntityIdForCurrentPage() -- default the item connected to the current page
−
id = entity.id
 
−
else
 
−
id = mw.text.trim(frame.args[1])
 
 
end
 
end
−
return mw.wikibase.sitelink( id )
+
return mw.wikibase.getSitelink(qid or '') -- requires one string arg
 
end
 
end
  
−
-- Return the data type of a property
+
-- Return the data type of a property given its entity ID.
 
function p.datatype(frame)
 
function p.datatype(frame)
−
if frame.args[1] and string.find(frame.args[1], "Property:P") then
+
local prop = mw.wikibase.getEntity(frame.args[1] and mw.text.trim(frame.args[1]):upper():gsub('PROPERTY:P', 'P')) -- trim and remove any "Property:" prefix
−
if mw.wikibase.getEntityObject(string.gsub(frame.args[1], "Property:P", "P"))  then
+
return prop and prop.datatype
−
return mw.wikibase.getEntityObject(string.gsub(frame.args[1], "Property:P", "P") ).datatype
 
−
end
 
−
elseif frame.args[1] and string.find(frame.args[1], "P") then
 
−
if mw.wikibase.getEntityObject(frame.args[1])  then
 
−
return mw.wikibase.getEntityObject(frame.args[1]).datatype
 
−
end
 
−
end
 
 
end
 
end
  
 
return p
 
return p

Latest revision as of 05:18, 30 September 2020


This module is copied from the example from the module documentation at mw:Extension:Wikibase Client/Lua. It should be kept consistent with that documentation, rather than being adapted further for use on Wikipedia.

Usage

id
{{#invoke: Wikibase | id }}
wdurl
{{#invoke: Wikibase | wdurl }}
{{#invoke: Wikibase | wdurl | entity id (Q# or P#) }}
label
{{#invoke: Wikibase | label }}
{{#invoke: Wikibase | label | entity id (Q# or P#) }}
description
{{#invoke: Wikibase | description }}
{{#invoke: Wikibase | description | entity id (Q# or P#) }}
page
{{#invoke: Wikibase | page }}
{{#invoke: Wikibase | page | item id (Q#) }}
datatype
{{#invoke: Wikibase | datatype | property id (P#) }}

Examples

id

  • {{#invoke:Wikibase | id }} → no mw.wikibase

wdurl

  • {{#invoke:Wikibase | wdurl }} → Lua error at line 16: attempt to index field 'wikibase' (a nil value).
  • {{#invoke:Wikibase | wdurl | Q132689 }} → Lua error at line 16: attempt to index field 'wikibase' (a nil value).
  • {{#invoke:Wikibase | wdurl | P31 }} → Lua error at line 16: attempt to index field 'wikibase' (a nil value).

label

  • {{#invoke:Wikibase | label }} → Lua error at line 22: attempt to index field 'wikibase' (a nil value).
  • {{#invoke:Wikibase | label | Q132689 }} → Lua error at line 22: attempt to index field 'wikibase' (a nil value).
  • {{#invoke:Wikibase | label | P31 }} → Lua error at line 22: attempt to index field 'wikibase' (a nil value).

description

  • {{#invoke:Wikibase | description }} → Lua error at line 28: attempt to index field 'wikibase' (a nil value).
  • {{#invoke:Wikibase | description | Q132689 }} → Lua error at line 28: attempt to index field 'wikibase' (a nil value).
  • {{#invoke:Wikibase | description | P31 }} → Lua error at line 28: attempt to index field 'wikibase' (a nil value).

page

  • {{#invoke:Wikibase | page }} → Lua error at line 36: attempt to index field 'wikibase' (a nil value).
  • {{#invoke:Wikibase | page | Q132689 }} → Lua error at line 38: attempt to index field 'wikibase' (a nil value).

datatype

⧼datatypes-type-commonsMedia⧽: Template:Wpl

  • {{#invoke:Wikibase | datatype | P18 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).
  • {{#invoke:Wikibase | datatype | Property:P18 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).

⧼datatypes-type-globe-coordinate⧽: Template:Wpl

  • {{#invoke:Wikibase | datatype | P625 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).
  • {{#invoke:Wikibase | datatype | Property:P625 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).

⧼datatypes-type-wikibase-item⧽: Template:Wpl

  • {{#invoke:Wikibase | datatype | P6 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).
  • {{#invoke:Wikibase | datatype | Property:P6 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).

⧼datatypes-type-wikibase-property⧽: Template:Wpl

  • {{#invoke:Wikibase | datatype | P1647 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).
  • {{#invoke:Wikibase | datatype | Property:P1647 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).

⧼datatypes-type-string⧽: Template:Wpl

  • {{#invoke:Wikibase | datatype | P225 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).
  • {{#invoke:Wikibase | datatype | Property:P225 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).

⧼datatypes-type-monolingualtext⧽: Template:Wpl

  • {{#invoke:Wikibase | datatype | P1448 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).
  • {{#invoke:Wikibase | datatype | Property:P1448 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).

⧼datatypes-type-quantity⧽: Template:Wpl

  • {{#invoke:Wikibase | datatype | P1082 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).
  • {{#invoke:Wikibase | datatype | Property:P1082 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).

⧼datatypes-type-time⧽: Template:Wpl

  • {{#invoke:Wikibase | datatype | P569 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).
  • {{#invoke:Wikibase | datatype | Property:P569 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).

⧼datatypes-type-url⧽: Template:Wpl

  • {{#invoke:Wikibase | datatype | P856 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).
  • {{#invoke:Wikibase | datatype | Property:P856 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).

⧼datatypes-type-math⧽: Template:Wpl

  • {{#invoke:Wikibase | datatype | P2534 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).
  • {{#invoke:Wikibase | datatype | Property:P2534 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).

⧼datatypes-type-external-id⧽: Template:Wpl

  • {{#invoke:Wikibase | datatype | P212 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).
  • {{#invoke:Wikibase | datatype | Property:P212 }} → Lua error at line 43: attempt to index field 'wikibase' (a nil value).

---------- Module:Wikibase ----------------
require('Module:No globals')
local p = {}

-- Return the entity ID of the item linked to the current page.
function p.id(frame)
	if not mw.wikibase then
		return "no mw.wikibase"
	end
	return mw.wikibase.getEntityIdForCurrentPage() or "no entity"
end

-- Return the URL of an entity given its entity ID
-- or the item linked to the current page if no argument is provided.
function p.wdurl(frame)
	return mw.wikibase.getEntityUrl(frame.args[1] and mw.text.trim(frame.args[1])) -- defaults to entity URL of the item linked to the current page
end

-- Return the label of an entity given its entity ID
-- or the item linked to the current page if no argument is provided.
function p.label(frame)
	return mw.wikibase.getLabel(frame.args[1] and mw.text.trim(frame.args[1])) -- defaults to label of the item linked to the current page
end

-- Return the description of an entity given its entity ID
-- or the item linked to the current page if no argument is provided.
function p.description(frame)
	return mw.wikibase.getDescription(frame.args[1] and mw.text.trim(frame.args[1])) -- defaults to description of the item linked to the current page
end

-- Return the local title of an item given its entity ID
-- or the item linked to the current page if no argument is provided.
function p.page(frame)
	local qid = frame.args[1] and mw.text.trim(frame.args[1])
	if not qid or qid == '' then
		qid = mw.wikibase.getEntityIdForCurrentPage() -- default the item connected to the current page
	end
	return mw.wikibase.getSitelink(qid or '') -- requires one string arg
end

-- Return the data type of a property given its entity ID.
function p.datatype(frame)
	local prop = mw.wikibase.getEntity(frame.args[1] and mw.text.trim(frame.args[1]):upper():gsub('PROPERTY:P', 'P')) -- trim and remove any "Property:" prefix
	return prop and prop.datatype
end

return p