Roovet Articles

Module:EditAtWikidata

Template:Lua sidebar

Module to display an icon with a tooltip such as "Edit this at Wikidata"

  • Icon will be linked to the Wikidata entry for the article where this is placed.
  • This message is only displayed if a local_parameter is not supplied, i.e. when called from a template, it can be coded not to display the message when a local parameter is in use, preventing the value form Wikidata being fetched.
  • The qid of a Wikidata entry can optionally be supplied for testing outside the article.

Usage

To always display the icon:

  • {{#invoke:EditAtWikidata|showMessage}}

To display the icon depending on a local parameter not existing:

  • {{#invoke:EditAtWikidata|showMessage|local_parameter}}

To test the functionality outside of an article:

  • {{#invoke:EditAtWikidata|showMessage|qid=<ArticleID>|local_parameter}}

To make the link point to the given property at the Wikidata entry:

  • {{#invoke:EditAtWikidata|showMessage|pid=<PropertyID>|local_parameter}}

See also


-- Module:EditAtWikidata (Wikibase-optional)
-- Returns a link to the item's page on Wikidata, or "" when not available.

local p = {}

local function getArgs(frame)
	local parent = frame and frame:getParent() or nil
	local out = {}
	if frame and frame.args then
		for k,v in pairs(frame.args) do if type(v)=="string" and v~="" then out[k]=v end end
	end
	if parent and parent.args then
		for k,v in pairs(parent.args) do if type(v)=="string" and v~="" then out[k]=v end end
	end
	return out
end

local function hasWikibase()
	return mw and mw.wikibase and type(mw.wikibase.getEntityIdForCurrentPage)=="function"
end

local function normQid(id)
	if not id then return nil end
	id = mw.ustring.upper(mw.text.trim(id))
	if id:match("^Q%d+$") then return id end
	if id:match("^%d+$") then return "Q"..id end
	return nil
end

local function getEntityId(args)
	-- priority: explicit id -> title -> current page (if Wikibase present)
	local id = normQid(args.qid or args.id or args.item)
	if id then return id end
	if hasWikibase() then
		if args.title and type(mw.wikibase.getEntityIdForTitle)=="function" then
			local eid = mw.wikibase.getEntityIdForTitle(mw.text.trim(args.title))
			if eid then return eid end
		end
		return mw.wikibase.getEntityIdForCurrentPage()
	end
	return nil
end

local function buildUrl(id, property)
	local url = "https://www.wikidata.org/wiki/"..id
	if property and property~="" then
		local pnum = mw.text.trim(property):gsub("^P","")
		url = url .. "#P" .. pnum
	end
	return url
end

local function makeLink(url, label, icon)
	label = (label and mw.text.trim(label) ~= "" and label) or "Edit at Wikidata"
	if icon then label = "✎ " .. label end
	return string.format("[%s %s]", url, label)
end

function p.main(frame)
	local args = getArgs(frame)
	local id = getEntityId(args)
	if not id then
		-- No Wikibase and no explicit item: do nothing rather than error.
		return ""
	end
	local wantIcon = false
	if args.icon then
		local v = mw.ustring.lower(args.icon)
		wantIcon = (v=="yes" or v=="true" or v=="1" or v=="y")
	end
	local url = buildUrl(id, args.property or args.P)
	return makeLink(url, args.text or args.label, wantIcon)
end

-- Back-compat aliases for older templates that call different entrypoints
p.showMessage = p.showMessage or p.main
p.show        = p.show        or p.main
p.render      = p.render      or p.main
p.link        = p.link        or p.main

return p