Module:Wikidata link

From Wikispore
Jump to navigation Jump to search

{feeble first pass documentation, to be improved}

One argument is expected, in the form wikidata=Qxyz where Qxyz is like Q60, a Wikidata item.

Unlinked Wikibase's getEntity() returns the full data structure of the Wikidata item in JSON format, in a structure called 'mainsnak'. 'Snak' is a term of art among Wikibase developers, used in the Wikibase code itself. It's being replicated by the extension and handed back to Lua here. Developers on Wikispore need skills to unpack that but they DO have all the data they need to get to a particular Property or qualifier.

Unlinked Wikibase's function localid finds out which one local page is linked to that Wikidata item.

Unlinked Wikibase reaches the Wikidata information through a simple static URL, for which only the Wikidata item number is needed. {give example static URL here} It does not use the "Wikibase extensions" nor the Web API to Wikibase.

To build Lua here, look at the local Infobox templates. AND Gergo suggests studying en.wp:Module:Wikidata and Module:WikidataIB bring them here or copy the bit that extracts numbers They are extensive and advanced. THey depend on a different Wikibase client extension Tgr says Wikibase also has a getEntity() kind of function Sam's unlinked data extension goes through the URL not API but it's fine for us


local p = {}

-- Get a link to the given Wikidata item's page on the first of the following places:
--  1. This wiki
--  2. Wikipedia
--  3. Wikisource
--  4. Commons
--  5. Wikidata
function p.link( frame )
	-- Check input.
	if frame == nil or frame.args.wikidata == nil or frame.args.wikidata == '' then
		return "<span class='error'>Please specify 'wikidata' parameter.</span>"
	end
	local itemId = frame.args.wikidata
	local item = mw.ext.UnlinkedWikibase.getEntity( itemId )
	if not item then
		return "<span class='error'>" .. itemId .. "' could not be found.</span>"
	end
	
	-- Get label.
	local label = itemId
	local localTitle = mw.ext.UnlinkedWikibase.getLocalTitle( itemId )
	if frame.args.label ~= nil and frame.args.label ~= '' then
		label = frame.args.label
	elseif localTitle then
		label = localTitle.text
	elseif item.labels ~= nil and item.labels.en ~= nil then
		label = item.labels.en.value
	end

	if localTitle then
		return '<span class="mdl-wikidata-link">[[:' .. localTitle.prefixedText .. '|' .. label .. ']]</span>';
	end

	-- Look through the site hierarchy for a matching sitelink, in this order.
	-- The first elements are sitelink IDs and the second are local interwiki prefixes.
	local sitelinks = {
		{ 'enwiki', 'wikipedia' },
		{ 'enwikisource', 'wikisource' },
		{ 'commonswiki', 'commons' },
	}
	for i,v in ipairs( sitelinks ) do
		local sitelink = sitelinks[i][1]
		local interwiki = sitelinks[i][2]
		if item.sitelinks ~= nil and item.sitelinks[ sitelink ] ~= nil then
			return '<span class="mdl-wikidata-link">[[' .. interwiki .. ':' .. item.sitelinks[ sitelink ].title .. '|' .. label .. ']]</span>';
		end
	end

	-- Fall back on Wikidata if no sitelink found.
	return '<span class="mdl-wikidata-link">[[wikidata:' .. itemId .. '|' .. label .. ']]</span>'
end

return p