Difference between revisions of "Module:Wikibase/sandbox"

From blackwiki
Jump to navigation Jump to search
(sync)
(optimize expensive and extraneous calls)
Line 2: Line 2:
 
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()
+
eid = mw.wikibase.getEntityIdForCurrentPage()
 
+
return eid 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 WD entity URL of a given entity ID, or item for current page
 
-- if no argument is provided to this method.
 
-- if no argument is provided to this method.
 
function p.wdurl(frame)
 
function p.wdurl(frame)
if frame.args[1] == nil then
+
eid = frame.args[1]
entity = mw.wikibase.getEntityObject()
+
eid = eid and mw.text.trim( eid )
if not entity then return nil end
+
return mw.wikibase.getEntityUrl( eid ) -- defaults to URL of the item connected to the current page
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 a given entity ID, or item for current page
 
-- if no argument is provided to this method.
 
-- if no argument is provided to this method.
 
function p.label(frame)
 
function p.label(frame)
if frame.args[1] == nil then
+
eid = frame.args[1]
entity = mw.wikibase.getEntityObject()
+
eid = eid and mw.text.trim( eid )
if not entity then return nil end
+
return mw.wikibase.getLabel( eid ) -- defaults to label of the item connected to the current page
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 a given entity ID, or item for current page
 
-- if no argument is provided to this method.
 
-- if no argument is provided to this method.
 
function p.description(frame)
 
function p.description(frame)
if frame.args[1] == nil then
+
eid = frame.args[1]
entity = mw.wikibase.getEntityObject()
+
eid = eid and mw.text.trim( eid )
if not entity then return nil end
+
return mw.wikibase.getDescription( eid ) -- defaults to description of the item connected to the current page
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 sitelink of a given entity ID, or item for current page
-- if id is not specified.
+
-- if no argument is provided to this method.
 
function p.page(frame)
 
function p.page(frame)
if frame.args[1] == nil then
+
eid = frame.args[1] or mw.wikibase.getEntityIdForCurrentPage()
entity = mw.wikibase.getEntityObject()
+
eid = eid and mw.text.trim( eid )
if not entity then return nil end
+
if eid == '' then
id = entity.id
+
eid = nil
else
 
id = mw.text.trim(frame.args[1])
 
 
end
 
end
return mw.wikibase.sitelink( id )
+
return eid and mw.wikibase.getSitelink( eid )
 
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
+
pid = frame.args[1]
if mw.wikibase.getEntityObject(string.gsub(frame.args[1], "Property:P", "P"))  then
+
pid = pid and mw.text.trim( eid )
return mw.wikibase.getEntityObject(string.gsub(frame.args[1], "Property:P", "P") ).datatype
+
pid = pid and string.gsub( string.upper(pid), 'PROPERTY:P', 'P') -- capitalize and remove "Property:" prefix
end
+
prop = mw.wikibase.getEntity( pid )
elseif frame.args[1] and string.find(frame.args[1], "P") then
+
if prop and prop.datatype then
if mw.wikibase.getEntityObject(frame.args[1])  then
+
return prop.datatype
return mw.wikibase.getEntityObject(frame.args[1]).datatype
 
end
 
 
end
 
end
 
end
 
end
  
 
return p
 
return p

Revision as of 23:58, 2 August 2019

Documentation for this module may be created at Module:Wikibase/sandbox/doc

---------- Module:Wikibase ----------------
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
	eid = mw.wikibase.getEntityIdForCurrentPage()
	return eid or "no entity"
end

-- Return the WD entity URL of a given entity ID, or item for current page
-- if no argument is provided to this method.
function p.wdurl(frame)
	eid = frame.args[1]
	eid = eid and mw.text.trim( eid )
	return mw.wikibase.getEntityUrl( eid ) -- defaults to URL of the item connected to the current page
end

-- Return the label of a given entity ID, or item for current page
-- if no argument is provided to this method.
function p.label(frame)
	eid = frame.args[1]
	eid = eid and mw.text.trim( eid )
	return mw.wikibase.getLabel( eid ) -- defaults to label of the item connected to the current page
end

-- Return the description of a given entity ID, or item for current page
-- if no argument is provided to this method.
function p.description(frame)
	eid = frame.args[1]
	eid = eid and mw.text.trim( eid )
	return mw.wikibase.getDescription( eid ) -- defaults to description of the item connected to the current page
end

-- Return the local sitelink of a given entity ID, or item for current page
-- if no argument is provided to this method.
function p.page(frame)
	eid = frame.args[1] or mw.wikibase.getEntityIdForCurrentPage()
	eid = eid and mw.text.trim( eid )
	if eid == '' then
		eid = nil
	end
	return eid and mw.wikibase.getSitelink( eid )
end

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

return p