Hopp til innhold

Modul:Wikibase signature

Fra Wikipedia, den frie encyklopedi

--- Get Wikibase items signatures
-- @license (CC-BY-SA-3.0)
-- @copyright John Erling Blad <[email protected]>
-- @provenance https://en.wikipedia.org/w/index.php?title=Module:Wikibase&oldid=722062922

local _sites = mw.loadData('Modul:Wikibase signature/conf')

-- @var The table holding this modules exported members
local p = {}

function p.fetchEntity( str, group, code )
	str = (type( str ) == 'string') and mw.text.trim( str ) or str
	group = (type( group ) == 'string') and mw.text.trim( group ) or group
	code = (type( code ) == 'string') and mw.text.trim( code ) or code
	
	if type( str ) == 'nil' or str == '' then
		return mw.wikibase.getEntityObject(), 'nowiki', '', str
	end
	
	if mw.ustring.find( str, "^[FLMPQS]%d+$" ) then
		local initial = mw.ustring.sub( str, 1, 1 )
		if initial == 'Q' then
			return mw.wikibase.getEntityObject( str ), 'wikidatawiki', 'd:', str
		elseif initial == 'P' then
			return mw.wikibase.getEntityObject( str ), 'wikidatawiki', 'd:property:', str
		else
			error( "''Not implemented!''" )
			return
		end
	end

	local fullStr = str
	local first, last = mw.ustring.find( str, '^[^:]*:')
	if first then
		group = mw.ustring.sub( str, first, last-1 )
		if _sites[group] then
			str = mw.ustring.sub( str, last+1 )
			if type( _sites[group] ) == 'string' then
				group = _sites[group]
			end
			if type( _sites[group] ) == 'table' then
				first, last = mw.ustring.find( str, '^[^:]*:')
				if first then
					code = mw.ustring.sub( str, first, last-1 )
					if _sites[group][code] then
						str = mw.ustring.sub( str, last+1 )
					end
				end
			end
		end
	end

	local site = _sites[group or 'wikipedia']
	local siteId = (type(site) == 'table' and site[code or 'no']) or site

	local id = ( mw.ustring.find( str, "^[PQ]%d+$" ) and str )
		or ( str and not(siteId) and mw.wikibase.getEntityIdForTitle( str ) )
		or ( str and siteId and mw.wikibase.getEntityIdForTitle( str, siteId ) )
		or nil

	local entity = ( id and mw.wikibase.getEntityObject( id ) )
		or ( not(str) and mw.wikibase.getEntityObject() )
		or nil
	
	local prefix = mw.ustring.sub( fullStr, 1, -(mw.ustring.len( str )+1) )

	return entity, siteId, prefix, str
end

-- Wikipedia should always have mw.wikibase, but the code can be used outside wm-wikis
assert( mw.wikibase, "can not find mw.wikibase" )

--- get the item ID of the item linked to the current page
-- @param table frame from the call
-- @return string id of the item
function p.id(frame)
	frame = frame or mw.getCurrentFrame()

	local str = frame.args[1]

	local entity = p.fetchEntity( str )
	if not entity then
		return ''
	end

	return entity.id
end

--- get the label of a given item, title, or of the connected page
-- if no argument is provided
-- @param table frame from the call
-- @return string label from the item
function p.label(frame)
	frame = frame or mw.getCurrentFrame()

	local str = frame.args[1]
	local langCode = frame.args[2]
	
	local entity = p.fetchEntity( str )
	if not entity then
		return ''
	end

	return langCode and entity:getLabel( langCode )
		or entity:getLabel()
end

--- get the description of a given item, local title, or the connected page
-- if no argument is provided
-- @param table frame from the call
-- @return string description from the item
function p.description(frame)
	frame = frame or mw.getCurrentFrame()

	local str = frame.args[1]
	local langCode = frame.args[2]
	
	local entity = p.fetchEntity( str )
	if not entity then
		return ''
	end

	return langCode and entity:getDescription( langCode )
		or entity:getDescription()
end

--- get the local page for a given item, local title, or the connected page
-- if no argument is provided
-- @param table frame from the call
-- @return string page title from the item
function p.page(frame)
	frame = frame or mw.getCurrentFrame()

	local str = frame.args[1]
	local langCode = frame.args[2]
	langCode = (type( langCode ) == 'string') and mw.text.trim( langCode ) or langCode
	langCode = langCode ~= '' and langCode or nil

	local entity, siteId, prefix, str = p.fetchEntity( str )
	if entity then
		local title = ( siteId and ( entity and entity:getSitelink( siteId ) ) )
			or ( not(siteId) and ( entity and entity:getSitelink() ) )
			or ( (siteId == 'wikidatawiki') and (str) )
			or nil
		if title then
			local text = ( langCode and ( entity and entity:getLabel( langCode ) ) )
				or ( not(langCode) and ( entity and entity:getLabel() ) )
				or title
			return mw.ustring.format( '<span class="extiw exist">[[%s%s|%s]]</span>', prefix, title, title )
				..( (text ~= title) and mw.ustring.format( ' (%s)', text ) or '' )
		end
	end
	
	--local text = mw.ustring.sub( str, prefixLength+1 )
	return mw.ustring.format( '<span class="extiw">[[%s%s|%s]]</span>', prefix, str, str )
end

return p