Módulo:PropertyLink

Un artíkolo de la Vikipedya, la ansiklopedya líbera
Ir a la navegación Ir a la búsqueda

This module can be used for advance linking, according to Wikidata. It includes the following functions:

  • property - get a link according to wikidata claim
    • Parameters: property (e.g p123)
    • Example: in Lion {{#invoke:PropertyLink|property|p171}} - will give Panthera. It is better to use this function and not [[{{#property:p171}}]] as the property gives the label in Wikidata, while this function will use the correct sitelink.
  • label: gives the label in wikidata (no [[link]])
    • Parameters: property (e.g p123)
  • imageLink: Get a related file to be used in the article
    • Parameters:
      • 1 param (optional) - Property to get commons link. If not specified it uses d:Property:p18 (a generic property for image in wikidata)
      • width - default 220px

--[[ 
Fetch the "as of date" property of a associated with a property
]]
function asOfDateQualifier(claim)
	local AS_OF_PROPERTY = 'P585'
	local wikidataModule = require('Module:Wikidata')
	local value
	local error
	value, error = wikidataModule.getValueOfClaim(claim, AS_OF_PROPERTY, nil)
	if value then
		return ' ('..value ..')'
	end
	return ''
end

function getPropertyQualifier(property, qualifier)
	local wikidataModule = require('Module:Wikidata')
	local entity = mw.wikibase.getEntityObject()
    if not entity or not entity.claims then return end --the entity doesnt exist or have no claims
	
    local propertyVals = entity.claims[property]
    if not propertyVals then return end --no such property for this item
 	value, error = wikidataModule.getValueOfClaim(propertyVals[1], qualifier, nil)
	return value
end

--[[ 
Fetch property from wikidata:
* if the entity or the claim doesn't exist - nil
* if the property exist:
	- for entity reference - returns link to entity (using sitelink) with label as text, otherwise wikidata label as text
	- for string - returns the string
	- for quantity - returns the amount
	- for time - returns the time as string
	- for image - returns image inclusion with 250px size
]]
function getProperty( propertyName, allowMulti, allowNA )
    local entity = mw.wikibase.getEntityObject()
    if not entity or not entity.claims then return end --the entity doesnt exist or have no claims

    local propertyVals = entity.claims[propertyName]
    if not propertyVals then return end --no such property for this item
    local resTable = {}
    for i, property in ipairs(propertyVals) do
	    local propValue = property.mainsnak and property.mainsnak.datavalue
	    if not propValue then 
	    	if allowNA and (property.mainsnak and property.mainsnak.snaktype)=='somevalue' then
	    		return 'Unknown'
	    	else
	    		--property doesnt exist
	    		return 
			end
	    end 
	    
		local isImage = (property.mainsnak.datatype=='commonsMedia')
	    if propValue['type'] == 'wikibase-entityid' then
	        local linkTarget = mw.wikibase.sitelink( "Q" .. propValue.value['numeric-id'] )
	        -- use Hebrew label,  if it doesn't exist use English and otherwise use Q id
	        local linkTitle = mw.wikibase.label( "Q" .. propValue.value['numeric-id'] ) or (mw.wikibase.getEntityObject( "Q" .. propValue.value['numeric-id'] ).labels['en'] and mw.wikibase.getEntityObject( "Q" .. propValue.value['numeric-id'] ).labels['en'].value) or "Q" .. propValue.value['numeric-id']
	        table.insert(resTable, linkTarget and linkTitle and mw.ustring.format( "[[%s|%s]]", linkTarget, linkTitle )
	            or linkTitle)
	    elseif propValue['type'] == 'string' then
	    	if isImage then
	    		table.insert(resTable, mw.ustring.format( '[[File:%s|250px]]', propValue.value ))
	    	else
	    		table.insert(resTable, propValue.value)
	    	end
    	elseif propValue['type'] == 'monolingualtext' then
    		table.insert(resTable, propValue.value.text)
	    elseif propValue['type'] == 'quantity' then
	    	local lang = mw.getContentLanguage()
	    	local asOfDate = asOfDateQualifier(property)
	    	table.insert(resTable, lang:formatNum( tonumber(propValue.value.amount) ) .. asOfDate)
	    elseif propValue['type'] == 'time' then
	    	local timeValue = entity:formatPropertyValues( propertyName ).value
	    	timeValue = mw.ustring.gsub(timeValue, '^(%d+ %a+) (%d+)$', '[[%1]] [[%2]]') 
	    	table.insert(resTable, timeValue)
	    end
	    if not allowMulti then
	    	break
	    end
	end
	return table.concat( resTable, ', ' )
end

function property( frame )
    return getProperty(string.upper(frame.args[1]), (frame.args[2] and string.len(frame.args[2])>0) or false, true)
end

function getLabel( propertyName )
    local entity = mw.wikibase.getEntityObject()
    if not entity or not entity.claims then return end--the entity doesnt exist or have no claims
    local property = entity.claims[propertyName]
    if not property then return end --no such property for this item
    
    property = property[1]
    local propValue = property.mainsnak.datavalue
    if not propValue then return '' end --property doesnt exist

    if propValue['type'] == 'wikibase-entityid' then
        return mw.wikibase.label( "Q" ..propValue.value['numeric-id'] )
    elseif propValue['type'] == 'string' then
        return propValue.value
    end
end

-- Return the label for property, or the label of the linked entiy of that property
function label( frame )
    return getLabel( string.upper(frame.args[1] ))
end

function getImageLink( propName, width, align, description, border)
    local entity = mw.wikibase.getEntityObject()
    if not entity or not entity.claims then return end --the entity doesnt exist or have no claims
    local property = entity.claims[propName or "P18"]
    if property then
        local width = width or "220"
        local extraParameters = width..'px'
        if align then extraParameters = extraParameters .. '|' .. align end
        if description then extraParameters = extraParameters .. '|' .. description end
        if border and (#border > 0) then extraParameters =extraParameters..'|' ..'border' end
        return mw.ustring.format( '[[File:%s|%s]]', property[1].mainsnak.datavalue.value, extraParameters )
    end
end
    
--use this function to get associated image to be used in the article
function imageLink( frame )
    return getImageLink(string.upper(frame.args[1] or 'P18'), frame.args["width"], frame.args["align"], frame.args["description"], frame.args["Border"])
end

-- returns "1" if the page has an associated wikidata entry, "" otherwise
function hasEntry()
    local entity = mw.wikibase.getEntityObject()
    --if not entity or not entity.claims then return end --the entity doesnt exist or have no claims
    if not entity then return end --the entity doesnt exist or have no claims
    return 1
end

return {
    imageLink = imageLink,
    ['imagelink'] = imageLink,
    label = label,
    ['label'] = label,
    property = property,
    ['property'] = property,
    getProperty = getProperty,
    getPropertyQualifier = getPropertyQualifier,
    getImageLink = getImageLink,
    getLabel = getLabel,
    hasEntry = hasEntry,
    ['hasEntry'] = hasEntry
}