Jump to content

Module:EditAtWikidata: Difference between revisions

From Wikigama
PS (talk | contribs)
No edit summary
Tag: Reverted
PS (talk | contribs)
No edit summary
Tag: Reverted
Line 1: Line 1:
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)
function p._showMessage(args)
local local_parm = trimToNil(args[1])
local local_parm = trimToNil(args[1])
if local_parm then return "" end
if local_parm then return "" end


-- Only access mw.wikibase if it exists
local qid
local qid
if mw and mw.wikibase and mw.wikibase.getEntityIdForCurrentPage then
if mw and mw.wikibase and mw.wikibase.getEntityIdForCurrentPage then
Line 25: Line 10:
end
end


-- Only check entity existence if mw.wikibase is available
if not qid then
-- Show a message when no Wikidata entity exists, or return empty string to show nothing
return "[No Wikidata entity found for this page]"
end
 
if qid and mw and mw.wikibase and mw.wikibase.entityExists and mw.wikibase.entityExists(qid) then
if qid and mw and mw.wikibase and mw.wikibase.entityExists and mw.wikibase.entityExists(qid) then
local anchor = trimToNil(args.pid)
local anchor = trimToNil(args.pid)
Line 39: Line 28:
"]]"
"]]"
end
end
return ""
return ""
end
end
function p.showMessage(frame)
return p._showMessage(frame.args)
end
return p

Revision as of 17:51, 30 May 2025

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

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

	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

	if not qid then
		-- Show a message when no Wikidata entity exists, or return empty string to show nothing
		return "[No Wikidata entity found for this page]"
	end

	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