Difference between revisions of "Module:Wikibase/sandbox"

From blackwiki
Jump to navigation Jump to search
(optimize expensive and extraneous calls)
blackwiki>Uzume
(update)
Line 7: Line 7:
 
return "no mw.wikibase"
 
return "no mw.wikibase"
 
end
 
end
eid = mw.wikibase.getEntityIdForCurrentPage()
+
return mw.wikibase.getEntityIdForCurrentPage() or "no entity"
return eid or "no entity"
 
 
end
 
end
  
-- Return the WD entity URL of a given entity ID, or item for current page
+
-- Return the entity URL of a given 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)
eid = frame.args[1]
+
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
eid = eid and mw.text.trim( eid )
 
return mw.wikibase.getEntityUrl( eid ) -- defaults to URL of the item connected to the current page
 
 
end
 
end
  
-- Return the label of a given entity ID, or item for current page
+
-- Return the label of a given 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)
eid = frame.args[1]
+
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
eid = eid and mw.text.trim( eid )
 
return mw.wikibase.getLabel( eid ) -- defaults to label of the item connected to the current page
 
 
end
 
end
  
-- Return the description of a given entity ID, or item for current page
+
-- Return the description of a given 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)
eid = frame.args[1]
+
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
eid = eid and mw.text.trim( eid )
 
return mw.wikibase.getDescription( eid ) -- defaults to description of the item connected to the current page
 
 
end
 
end
  
-- Return the local sitelink of a given entity ID, or item for current page
+
-- Return the local sitelink of a an item 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.page(frame)
 
function p.page(frame)
eid = frame.args[1] or mw.wikibase.getEntityIdForCurrentPage()
+
qid = frame.args[1] and mw.text.trim(frame.args[1])
eid = eid and mw.text.trim( eid )
+
if not qid or qid == '' then
if eid == '' then
+
qid = mw.wikibase.getEntityIdForCurrentPage() -- default the item connected to the current page
eid = nil
 
 
end
 
end
return eid and mw.wikibase.getSitelink( eid )
+
return mw.wikibase.getSitelink(qid or '') -- requires one string arg
 
end
 
end
  
 
-- Return the data type of a property given its entity ID
 
-- Return the data type of a property given its entity ID
 
function p.datatype(frame)
 
function p.datatype(frame)
pid = frame.args[1]
+
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
pid = pid and mw.text.trim( eid )
+
return prop and prop.datatype
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
 
end
  
 
return p
 
return p

Revision as of 20:57, 24 April 2020

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

-- Return the entity URL of a given 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 a given 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 a given 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 sitelink of a an item given its entity ID
-- or the item linked to the current page if no argument is provided.
function p.page(frame)
	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)
	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