Difference between revisions of "Module:Wikibase/sandbox"
Jump to navigation
Jump to search
(sync) |
(optimize expensive and extraneous calls) |
||
| Line 2: | Line 2: | ||
local p = {} | local p = {} | ||
| − | -- Return the | + | -- 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 | ||
| − | + | eid = mw.wikibase.getEntityIdForCurrentPage() | |
| − | + | return eid or "no entity" | |
| − | |||
| − | |||
| − | |||
| − | |||
end | end | ||
| − | -- Return the WD entity URL of a given | + | -- 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) | ||
| − | + | 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 | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | return mw.wikibase.getEntityUrl( | ||
end | end | ||
| − | -- Return the label of a given | + | -- 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) | ||
| − | + | 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 | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | return mw.wikibase. | ||
end | end | ||
| − | -- Return the description of a given | + | -- 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) | ||
| − | + | 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 | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | return mw.wikibase. | ||
end | end | ||
| − | -- Return the local | + | -- Return the local sitelink of a given entity ID, or item for current page |
| − | -- if | + | -- if no argument is provided to this method. |
function p.page(frame) | 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 | end | ||
| − | return mw.wikibase. | + | 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) | ||
| − | + | 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 | ||
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