Module:ItemDisplay

Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules

This module is used by {{Q*|Q42}} to display human : common name of Homo sapiens, unique extant species of the genus HomoDouglas Adams (Q42): English science fiction writer and humourist (1952–2001) and {{Q!|Q42}} to display human : common name of Homo sapiens, unique extant species of the genus HomoDouglas Adams (Q42).

Code

local p = {}

local i18nmessages = mw.loadData('Module:i18n/itemdisplay')
local fb = require('Module:Fallback')

function i18n(str, lang)
	local message = i18nmessages[str]
	if type(message) == 'string' then
		return message
	end
	return fb._langSwitch(message, lang) .. ''
end

function p.icon_for_instance(instance_id, label, description, url, lang)
	local icon = mw.wikibase.getBestStatements( instance_id, 'P8972' )
    if icon[1]~= nil and icon[1].mainsnak.datavalue.value~= nil then
    	local image_name = icon[1].mainsnak.datavalue.value
    	local wikitext = '[[File:' .. image_name
    	wikitext = wikitext .. '|text-top|16x16px|link=' 
    	if url ~= nil then
    		wikitext = wikitext .. url 
    	end
    	wikitext = wikitext .. "|"
    	if label~= nil then 
    		wikitext = wikitext .. label
    	end
    	if description~= nil then 
    		wikitext = wikitext .. ' : '
    		wikitext = wikitext .. description
    	end
    	wikitext = wikitext .. ']]'
    	return wikitext
    else 
    	local superclass = mw.wikibase.getBestStatements( instance_id, 'P279' )
    	if superclass[1]== nil	or superclass[1].mainsnak.datavalue==nil then
    		return ''
    	else
    		local superclass_id = superclass[1].mainsnak.datavalue.value.id
    	    return p.icon_for_instance(superclass_id, label, description, url, lang)
    	end
   	end
end

function p.wikicode(instance_id, lang)
	local instance = mw.wikibase.getEntity(instance_id)
	local label = mw.wikibase.getLabelByLang( instance_id, lang ) 
    local description = mw.wikibase.getDescriptionWithLang( instance_id, lang ) 
    local url = mw.wikibase.getEntityUrl(instance_id)

    return p.icon_for_instance(instance_id, label, description, url, lang)
end

function p.icons_for_item(qid, lang)
    local instances = mw.wikibase.getAllStatements(qid, 'P31' ) 
    local return_text = ''
    
    if instances[1]== nil then
    	return_text = "[[File:OOjs_UI_icon_cancel.svg|text-top|16x16px|link=''|"
    	local label = mw.wikibase.getLabelWithLang(qid, lang)
    	if label ~= nil then
    		return_text = return_text .. label
    	else
    		return_text = return_text .. i18n('item', lang)
    	end

    	return_text = return_text .. " "
        return_text = return_text .. i18n('has no instance_of statements', lang)
        return_text = return_text .. "]]"
        return_text = return_text .. " "
    else 
	    for key,value in pairs(instances) do
	    	if value.rank ~= 'deprecated' then
	    		local instance_id = value.mainsnak.datavalue.value.id
	    		return_text = return_text .. p.wikicode(instance_id, lang)
	    	end
	    end    
	end
    return return_text 
end

function p.tablelength(T)
  if T== nil then
  	return 0
  end
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end

function p.item_hover(qid, lang, show_description)
	local hover_text = ''
	
	if show_description then
		local description = mw.wikibase.getDescriptionWithLang(qid, lang)
		if description ~= nil then
			hover_text = hover_text .. description
		end
	end
	return hover_text
end

function p.label_text(qid, lang, show_description)
	local text = '<span class="plainlinks" title="'
    text = text .. p.item_hover(qid, lang, show_description)
    text = text .. '">['
    text = text .. mw.wikibase.getEntityUrl(qid)
    text = text .. " "
    local label = mw.wikibase.getLabelWithLang(qid, lang)
    if label==nil then
    	text = text .. qid
	else
    	text = text .. label
    	text = text .. " <small>("
    	text = text .. qid
    	text = text .. ")</small>"
    end

    text = text .. "]</span>"
    return text
end

function p.description_text(qid, lang)
    local text = ":<small> "
    local description = mw.wikibase.getDescriptionWithLang(qid, lang)
    if description==nil then
    	text = text .. i18n('no description', lang)
	else
    	text = text .. description
    end
    text = text .. "</small>"
    return text
end

function p.create_text(qid, lang, icons, label, description)
	if qid:sub(1,1) ~= "Q" and qid:sub(1,1) ~= "q" then
		qid = "Q" .. qid
	end

	if not mw.wikibase.entityExists(qid) then
		local text = "[["
		text = text .. qid
		text = text .. "]]"
		return text
	else
	    local return_text = ""
	    
	    if icons then 
	    	return_text = return_text .. p.icons_for_item(qid, lang)
		end
	    
	    if label then 
	    	return_text = return_text .. p.label_text(qid, lang, not description)
		end
	    
	    if description then 
		    return_text = return_text .. p.description_text(qid, lang)
		end
	    
	    return return_text 
	end
end

function p.icons(frame)
    local config = frame.args
 
    local qid = config.qid
    local lang = config.lang

    return p.create_text(qid, lang, true, false, false) 
end

function p.label_only(frame)
	local config = frame.args
    local qid = config.qid
    local lang = config.lang
    
    return p.create_text(qid, lang, false, true, false) 
end

function p.icon_and_label(frame)
	local config = frame.args
    local qid = config.qid
    local lang = config.lang
    
    return p.create_text(qid, lang, true, true, false) 
end

function p.full_info(frame)
	local config = frame.args
 
    local qid = config.qid
    local lang = config.lang
    
    return p.create_text(qid, lang, true, true, true) 
end

return p