יחידה:ארגז חול/זאב קטן

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:ארגז חול/זאב קטן/תיעוד

local Date = require('Module:תאריך')
local Arguments = require( "Module:Arguments" )
local hebrewDateModule = require('Module:תאריך עברי')

local function WikidataSimpleFill(parameter, entityId, property) 
	-- this doesnt handle unkown values, multi value etc
	if parameter~=nil then return parameter, false end
	local wikidataVal = mw.wikibase.getBestStatements( entityId, property )
	if #wikidataVal > 0 then
		local propValue = wikidataVal[1].mainsnak
		return mw.wikibase.renderSnak( propValue ) , true
	end
	return nil, false
end

function render(frame)
	local args = Arguments.getArgs(frame, { ['trim'] = true, ['removeBlanks'] = true })
	local res = ''
	
	local entityId = args['qid'] or mw.wikibase.getEntityIdForCurrentPage()
	if (entityId == '-' or entityId=='') then entityId = nil end
	local pointInTime = args['נקודת זמן']
	local startDate = args['תאריך התחלה']
	local endDate = args['תאריך סיום']
	
	local wikidataStartDate = args['תאריך התחלה-ויקינתונים'] or 'P580'
	local wikidataEndDate = args['תאריך סיום-ויקינתונים'] or 'P582'
	local showEditWikidata = true

	local startDateforCalculation = nil
	local endDateforCalculation = nil
	local diffFormat = 'auto' -- TODO: better handling for different scales
	
	local showErrorstart = false
	local showErrorend = false
	
	
	-- text to show if we have only end date. Example: "Last even - %s" where %s is time. nil - will not show end date
	local onlyEndDateText = args['טקסט סיום']
	
	local duration = args['משך'] --P2047
	-- Whether to show time diff between start and end day when applicable. Example: כן.
	local showTimeDiff = args['הצגת הפרשת זמנים']
	local dateRangeDiffFormat = args['פורמט הצגה'] or '%s (%s)'

	local usingWikidata = false
	
	if  entityId~=nil then
		-- todo: support different fallbacks for properties
		local usingWikidataStartDate = false
		local usingWikidataEndDate = false
		local usingWikidataPointInTime = false
		local usingWikidataDuration = false
		
		wikidataStartDate = mw.text.split( wikidataStartDate, ' *, *', false )
		wikidataEndDate = mw.text.split( wikidataEndDate, ' *, *', false )
		for _, wikidataStartProp in pairs(wikidataStartDate) do
			startDate, usingWikidataStartDate = WikidataSimpleFill(startDate, entityId, wikidataStartProp )
		end
		for _, wikidataEndProp in pairs(wikidataEndDate) do
			endDate, usingWikidataEndDate = WikidataSimpleFill(endDate, entityId, wikidataEndProp )
		end
		
		usingWikidata = usingWikidata or usingWikidataStartDate or usingWikidataEndDate
		if startDate == nil and endDate == nil then
			pointInTime, usingWikidataPointInTime  = WikidataSimpleFill(pointInTime, entityId, 'P585' )
			usingWikidata = usingWikidata or usingWikidataPointInTime
		end
		
		if showTimeDiff == 'כן' then
			duration, usingWikidataDuration = WikidataSimpleFill(duration, entityId, 'P2047' )
			if usingWikidataDuration then 
				-- workaround for T261543
				duration = mw.ustring.gsub( duration, '(%d+) יממה', '%1 ימים' )
			end
			usingWikidata = usingWikidata or usingWikidataDuration
		end
	end
	
	
	-- Checking the date of converting a Hebrew date to a gregorian Date. Example: לא
	local success = pcall(Date.parseDateRange, startDate, diffFormat, true)
	if success then
		startDateforCalculation = startDate
	else
		local success, sepcDate = pcall(hebrewDateModule.pick, mw.getCurrentFrame():newChild{ args = {hebrew=startDate, world='', exact='כן' } })
		if not success or sepcDate=='' then
			if startDate~=nil and startDate~='' then
				showErrorstart = true
			end
		else
			startDateforCalculation = sepcDate
		end
	end
	
	local success = pcall(Date.parseDateRange, endDate, diffFormat, true)
	if success then
		endDateforCalculation = endDate
	else
		local success, sepcDate = pcall(hebrewDateModule.pick, mw.getCurrentFrame():newChild{ args = {hebrew=endDate, world='', exact='כן' } })
		if not success or sepcDate=='' then
			if endDate~=nil and endDate~='' then
				showErrorend = true
			end
		else
			endDateforCalculation = sepcDate
		end
	end
	
	
	-- Whether the event still continues. this will show <START>-present. Example: כן
	local defaultOngoingEvent = args['נמשך'] == 'כן'
	local ongoingEvent = defaultOngoingEvent
	local endDateOngoingEvent = args['הווה']

	if  endDate~=nil and endDate~='' then
		if endDateOngoingEvent~=nil then
			-- set default ongoingEvent as false
			ongoingEvent = false
			for v in string.gmatch(endDateOngoingEvent, "[^,%s]+") do
				if v == endDate then
					-- set as true if end date specific that match endDateOngoingEvent
					ongoingEvent = true
					showErrorend = false
				end
			end
		else
			-- explicity end date was specified, hence it is not ongoing
			ongoingEvent = false
		end
	end
	
	
	-- simple case of single point in time
	if pointInTime~=nil  or  startDate==endDate then
		if pointInTime == nil and startDate==endDate  then
			pointInTime = startDate
		end
		res = pointInTime
	else -- date ranges
		if startDate == nil and endDate~=nil  then
			if onlyEndDateText == nil then return end -- ignore only end date
			res = mw.ustring.format(onlyEndDateText, endDate)
		elseif startDate ~= nil and (endDate==nil or (ongoingEvent and endDate~=nil))  then
			if ongoingEvent then
				res = mw.ustring.format('%s–%s', startDate, endDate or 'הווה')
				-- try to fallback to automatic duration if duration is not available
				if showTimeDiff == 'כן' and duration==nil and startDateforCalculation~=nil then
					success, duration = pcall(Date.parseDateRange, startDateforCalculation, diffFormat, true)
				end
			else
				res = startDate
			end
		elseif startDate~=nil and endDate~=nil  then
			res = mw.ustring.format('%s–%s', startDate, endDate)
			if showTimeDiff == 'כן' and duration==nil and startDateforCalculation~=nil and endDateforCalculation~=nil then
				success, duration = pcall(Date.parseDateRange, mw.ustring.format('%s–%s', startDateforCalculation, endDateforCalculation), diffFormat, true)
			end
		end
	end
	
	-- append the duration
	if showTimeDiff == 'כן' and duration~=nil and duration~='' then
		if res == nil then
			res = duration
		else
			res = mw.ustring.format(dateRangeDiffFormat, res, duration)
		end
	end
	
	if showEditWikidata and usingWikidata and res~=nil and res~='' and entityId~=nil then
		local link = mw.title.makeTitle( 0, entityId, '', 'wikidata' ):fullUrl('uselang=he')
		res = res .. mw.ustring.format(' [[File:Blue pencil RTL.svg|15px|link=%s|עריכת הנתון בוויקינתונים]]', link)
	end
	
	if showErrorend or showErrorstart then
		res = frame.args['error'] or '<span class="scribunto-error">שגיאת תאריך: '..res..'</span>[[קטגוריה:דפים עם שגיאות בתאריך]]'	
	end
	
	return res
end

return 	{
	['טווח זמנים']=render
}