לדלג לתוכן

יחידה:PropertyLink: הבדלים בין גרסאות בדף

מתוך צפונות ויקי
מאין תקציר עריכה
מאין תקציר עריכה
שורה 161: שורה 161:
return '[[קטגוריה:ויקינתונים:ערכים_חסרי_תווית_בעברית: ' .. (mw.wikibase.label( propertyName) or propertyName ) .. ']][[קטגוריה:ויקינתונים:ערכים_חסרי_תווית_בעברית]]'
return '[[קטגוריה:ויקינתונים:ערכים_חסרי_תווית_בעברית: ' .. (mw.wikibase.label( propertyName) or propertyName ) .. ']][[קטגוריה:ויקינתונים:ערכים_חסרי_תווית_בעברית]]'
end
end
return table.concat( resTable, multiSeperator )
-- special case * - listify
if multiSeperator == '*' and #resTable>1 then
return '*' .. table.concat( resTable, '\n*' )
else
return table.concat( resTable, multiSeperator )
end
end
end



גרסה מ־20:58, 28 באפריל 2017

יחידה זו משמשת כדי לקשר בוויקיפדיה לפי ויקינתונים. היחידה מיועדת לשימוש בעיקר בתבניות וכוללת את הפונקציות הבאות:

  • property או מאפיין - לקבלת קישור לפי "קביעה" מוויקינתונים
    • פרמטרים: שם המאפיין (property) המתאים (בצורה של P123). למשל בערך דמויי כלב:
      • {{#invoke:PropertyLink|property|p171}} - יחזיר "[[טורפים (סדרת יונקים)|טורפים]]" (טורפים), להבדיל מ־[[{{#property:p70}}]], שיחזיר "[[טורפים]]" (טורפים), שהוא דף פירושונים.
  • label או תווית: דומה לפונקציה הקודמת, רק שלא מחזיר קישור אלא ללא קישור - מחזיר את התווית (label) וללא תלות בקישור כלשהו שקיים או לא קיים בוויקיפדיה בעברית
    • פרמטרים: שם המאפיין (property) המתאים (בצורה של P123)
  • imageLink או תמונה: פונקציה שמחזירה תמונה שמתאימה לשילוב בתבנית מידע בערך.
    • פרמטרים:
      • פרמטר ראשון - (אופציונלי) אם לא מקבל פרמטר בכלל משתמש ב־d:Property:p18 (מאפיין כללי של תמונה), אחרת משתמש בפרמטר המאפיין המכיל את התמונה
      • width - גודל התמונה (אחרת 220px)
  • hasEntry או יש פריט: פונקציה שמחזירה "1" אם לערך יש פריט ויקינתונים; או "" אם אין.
    • פרמטרים: אין

הערות נוספות:

ייתכן שבעתיד תהיה תמיכה מובנית לפחות בחלק מהאפשרויות הנ"ל ראו בmeta:Wikidata/Notes/Inclusion_syntax#Items

--[[ 
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

local function formatQuantity(property) 
	local propValue = property.mainsnak and property.mainsnak.datavalue
    local lang = mw.getContentLanguage()
	local asOfDate = asOfDateQualifier(property)
	local localAmount = lang:formatNum( tonumber(propValue.value.amount) )
	local unit = ''
	if propValue.value.unit and string.match( propValue.value.unit, 'http://www.wikidata.org/entity/' ) then
		local unitEntityId = string.gsub( propValue.value.unit, 'http://www.wikidata.org/entity/', '' );
		local unitEntity = mw.wikibase.getEntity( unitEntityId );
		if unitEntity then
			-- name from label
			unit =  unitEntity:getLabel();
		
			--  name from properties
			if unitEntity.claims and unitEntity.claims.P558 then
				local writingSystemElementId = 'Q33513'; -- hebrew writing system
				local langElementId = 'Q9288'; -- hebrew
				local labelFilter = 'P558[P282:' .. writingSystemElementId .. ', P407:' .. langElementId .. ']';
				local WDS = require( 'Module:WikidataSelectors' );
				local labelClaims = WDS.filter( unitEntity.claims, labelFilter );
				for r, claim in pairs( labelClaims ) do
					if claim.mainsnak
							and claim.mainsnak.datavalue
							and claim.mainsnak.datavalue.type == 'string'
							and claim.mainsnak.datavalue.value ~= '' then
						unit = claim.mainsnak.datavalue.value;
						break;
					end
				end
			end
		
			unit = ' ' .. unit;
		end
    end	
	return localAmount .. unit .. asOfDate
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 and format it:
* if the entity or the claim doesn't exist - nil
* Formating rules:
	- 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
* Multivalues:
	Multivalue is supported with allowMulti. seperator between values defined by multiSeperator
]]
function getProperty( propertyName, allowMulti, allowNA, entityId, multiSeperator, optionalQualifier )
    local entity = nil
    if not multiSeperator then
        multiSeperator = ', '
    end
    if entityId then
    	entity = mw.wikibase.getEntityObject( entityId )
    else
    	entity = mw.wikibase.getEntityObject()
    end

    if not entity or not entity.claims then return end --the entity doesnt exist or have no claims

    local propertyVals = entity:getBestStatements(propertyName)
    
    if (not propertyVals) or (#propertyVals==0) then return end --no such property for this item
    local resTable = {}
    local missingTranslation = false
    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 'לא ידוע'
	    	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 localLabel = mw.wikibase.label( "Q" .. propValue.value['numeric-id'] )
	        local isLocalLabel = localLabel and (mw.wikibase.getEntityObject( "Q" .. propValue.value['numeric-id'] ).labels['he']['language']=='he')
	        if linkTarget and not isLocalLabel then
	        	localLabel = linkTarget
	        	isLocalLabel = true
	        end
	        if localLabel and isLocalLabel then
	        		local formattedValue = linkTarget and localLabel and mw.ustring.format( "[[%s|%s]]", linkTarget, localLabel ) or localLabel
	        		if formattedValue and property.qualifiers and property.qualifiers[optionalQualifier] then
	        			local optionalQualifierVal = mw.wikibase.formatValues(property.qualifiers[optionalQualifier])
	        			local isQualLocalLabel = true
	        			if optionalQualifierVal and property.qualifiers[optionalQualifier][1] and property.qualifiers[optionalQualifier][1]['datatype']=='wikibase-item' then 
	        				for i, optionalQValues in ipairs(property.qualifiers[optionalQualifier]) do
	        					isQualLocalLabel = isQualLocalLabel and (mw.wikibase.getEntityObject( optionalQValues['datavalue']['value']['id'] ).labels['he']['language']=='he')
	        				end
	        			end
	        			if optionalQualifierVal then
	        				if isQualLocalLabel then
	        					formattedValue = formattedValue .. ' (<span style="font-size:0.9em;" title="'.. mw.wikibase.label(optionalQualifier)..'">'..mw.wikibase.formatValues(property.qualifiers[optionalQualifier]) ..'</span>)'
	        				else
	        					formattedValue = formattedValue.. '[[קטגוריה:ויקינתונים:ערכים_חסרי_תווית_בעברית: ' .. (mw.wikibase.label( optionalQualifier) or optionalQualifier) .. ']][[קטגוריה:ויקינתונים:ערכים_חסרי_תווית_בעברית]]'
							end
	        			end
	        		end
	        		table.insert(resTable, formattedValue)
	        else
	        	local foreignLabel = (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']
	        	if foreignLabel then
	        		missingTranslation = true
	        	end
	        end
	    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
	    	table.insert(resTable, formatQuantity(property))
	    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
	
	if missingTranslation then
		return '[[קטגוריה:ויקינתונים:ערכים_חסרי_תווית_בעברית: ' .. (mw.wikibase.label( propertyName) or propertyName ) .. ']][[קטגוריה:ויקינתונים:ערכים_חסרי_תווית_בעברית]]'
	end
	
	-- special case * - listify 
	if multiSeperator == '*' and #resTable>1 then
		return '*' .. table.concat( resTable, '\n*' )
	else
		return table.concat( resTable, multiSeperator )
	end
end

function property( frame )
    return getProperty(string.upper(frame.args[1]), (frame.args[2] and string.len(frame.args[2])>0) or false, true, frame.args['entity'], ', ')
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
        local label, lang = mw.wikibase.label( "Q" ..propValue.value['numeric-id'] )
        return label
    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["גבול"])
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,
    label = label,
    ['תווית'] = label,
    property = property,
    ['מאפיין'] = property,
    getProperty = getProperty,
    getPropertyQualifier = getPropertyQualifier,
    getImageLink = getImageLink,
    getLabel = getLabel,
    hasEntry = hasEntry,
    ['יש פריט'] = hasEntry
}