Jump to content

Module:EditAtWikidata

From Wikigama
Revision as of 17:50, 30 May 2025 by PS (talk | contribs)

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

local p = {}

local i18n =
{
	["message"] = "Edit this at Wikidata"
}

local function trimToNil(text)
	-- Return trimmed non-empty text, or nil.
	if type(text) == 'string' then
		return text:match('(%S.-)%s*$')
	end
end

function p._showMessage(args)
	local local_parm = trimToNil(args[1])
	if local_parm then return "" end

	-- Only access mw.wikibase if it exists
	local qid
	if mw and mw.wikibase and mw.wikibase.getEntityIdForCurrentPage then
		qid = trimToNil(args.qid) or mw.wikibase.getEntityIdForCurrentPage()
	else
		qid = trimToNil(args.qid)
	end

	-- Only check entity existence if mw.wikibase is available
	if qid and mw and mw.wikibase and mw.wikibase.entityExists and mw.wikibase.entityExists(qid) then
		local anchor = trimToNil(args.pid)
		local space = trimToNil(args.nbsp) and " " or " "
		return
			space ..
			"[[File:OOjs UI icon edit-ltr-progressive.svg|frameless|text-top|10px" ..
			"|alt=" .. i18n.message ..
			"|link=https://www.wikidata.org/wiki/" .. qid ..
			(anchor and ("#" .. anchor) or "") ..
			"|class=noprint" ..
			"|" .. i18n.message ..
			"]]"
	end
	return ""
end

function p.showMessage(frame)
	return p._showMessage(frame.args)
end

return p