יחידה:WikidataBind

מתוך ויקיפדיה, האנציקלופדיה החופשית

היחידה מיועדת לקשור בין תבנית פשוטה ללא תמיכה בוויקינתונים לוויקינתונים. התבנית מושכת מידע מוויקינתונים, ועבור כל טענה בוויקינתונים מוסיפה את התבנית המצוינת, וממלאת את הפרמטרים של התבנית על פי פרטים מוויקינתונים.

אופן השימוש[עריכת קוד מקור]

{{#invoke:WikidataBind|חיבור ויקינתונים
|ויקינתונים=P
|תבנית=שם התבנית
|חובה=P
|+=שם התפקיד
|P=שם פרמטר
}}
  • ויקינתונים - שדה מוויקינתונים שהיחידה משתמשת בו כדי למשוך מידע
  • תבנית - שם התבנית שאותה רוצים למלא על סמך ויקינתונים
  • חובה - טענת משנה הכרחית אשר בלעדיה לא תוצג הטענה (P ומספר המאפיין)
  • + - מיפוי של הטענה המצוינת לפרמטר בתבנית
  • מיפוי טענות משנה - טענות משנה (qualifiers) ממופים לשדות בתבנית באמצעות ציון המאפיין (P ומספר המאפיין) = שם הפרמטר בתבנית שמיועד להתמלא בו

local p={}
local Date = require('Module:תאריך')

function substitleSorter(a,b)
	return a[1]<b[1] 
end

function p.bind(frame)
	local templateParams =  frame.args
	local templateArgs = frame:getParent().args
	local warnings = ''
	local propertyLink = require('Module:PropertyLink')
	local template = frame.args['תבנית']
	local property = frame.args['ויקינתונים']
	local requiredQualifier = frame.args['חובה'] or nil
	local qId = templateArgs['qid'] or mw.wikibase.getEntityIdForCurrentPage()
	if qId == nil then
		error('אין יישות בוויקינתונים')
	end
	local mainStatementBind = nil
	local grouping = true
	local statements = mw.wikibase.getBestStatements(qId, property)
	local res = {}
	for _, statement in pairs(statements) do
		if not requiredQualifier or (statement.qualifiers and statement.qualifiers[requiredQualifier]) then
			local templateArgs = {}
			local propValue = statement.mainsnak and statement.mainsnak.datavalue
			local mainStatement = nil
			local missingTranslation = false
			if propValue['type'] == 'wikibase-entityid' then
				templateArgs[templateParams['+']], missingTranslation = propertyLink.formatEntity("Q" .. propValue.value['numeric-id'], qId)
			end
			if statement.qualifiers then
				for pWd, pTemplate in pairs(templateParams) do
					-- retrieve default value
					local defaultVal = mw.ustring.gsub( pTemplate, '([^/]+)(.*)', '%2' )
					-- remove default value from template param name
					pTemplate = mw.ustring.match( pTemplate, '([^/]+)' )
					if string.match( pWd, '^P[0-9]+') and statement.qualifiers[pWd] then
						if pTemplate == templateParams['+'] then
							if statement.qualifiers[pWd][1].snaktype == 'somevalue' then
								templateArgs[pTemplate] = templateArgs[templateParams['+']] .. ' ?'
							elseif statement.qualifiers[pWd][1].snaktype ~= 'novalue' then
								if statement.qualifiers[pWd][1].datatype == "time" then
									templateArgs[pTemplate] = templateArgs[templateParams['+']] .. ' ' .. mw.wikibase.renderSnak(statement.qualifiers[pWd][1])
								else
									templateArgs[pTemplate] = templateArgs[templateParams['+']] .. ' ' .. mw.wikibase.formatValue(statement.qualifiers[pWd][1])
								end
							end
						elseif statement.qualifiers[pWd][1].snaktype == 'somevalue' then
							templateArgs[pTemplate] = "?"
						elseif statement.qualifiers[pWd][1].snaktype ~= 'novalue' then
							if statement.qualifiers[pWd][1].datatype == "time" then
								templateArgs[pTemplate] = Date.newFromWikidataValue(statement.qualifiers[pWd][1].datavalue.value):toHebrewString()
							else
								templateArgs[pTemplate] = mw.wikibase.formatValue(statement.qualifiers[pWd][1])
							end
						elseif defaultVal ~= "" then
							templateArgs[pTemplate] = mw.ustring.sub(defaultVal, 2)
						end
					elseif defaultVal ~= "" then
						templateArgs[pTemplate] = mw.ustring.sub(defaultVal, 2)
					end
				end
			end
			if missingTranslation then
					warnings = '[[קטגוריה:ויקינתונים:ערכים_חסרי_תווית_בעברית: ' .. (mw.wikibase.label(property) or property) .. ']][[קטגוריה:ויקינתונים:ערכים_חסרי_תווית_בעברית]]'
			else
				if grouping then
					local groupName = templateArgs[templateParams['+']]
					if res[groupName] then
						templateArgs[templateParams['+']] = '-'
						table.insert(res[groupName],frame:expandTemplate{ title = template, args = templateArgs })
					else
						res[groupName] = { frame:expandTemplate{ title = template, args = templateArgs } }
					end
				else
					table.insert(res, frame:expandTemplate{ title = template, args = templateArgs })
				end
			end
		end
	end
	
	if grouping then
		local tmpRes = {}
		-- group same subtitle
		for k,v in pairs(res) do
			table.insert(tmpRes, {k, table.concat(v, '\n')})
		end
		-- sort by subtitle
		table.sort(tmpRes, substitleSorter)
		res = {}
		for k,v in pairs(tmpRes) do
			table.insert(res, v[2])
		end
	end
	res = table.concat(res, '\n')
	return res.. warnings
end

p['חיבור ויקינתונים'] = p.bind
return p