יחידה:טווח זמנים

היחידה מיועדת לבנייה נוחה של תבניות עם טווחי זמנים.

טווח זמנים יהיה מהצורה של <תאריך התחלה> - <תאריך סיום> או <נקודת זמן>

הצגת משך זמן עריכה

כאשר עוסקים בפרט חשוב דיו לציון משך הזמן בין תאריך ההתחלה לסיום ניתן לציין "הצגת הפרשת זמנים=כן" שיחשב את משך הזמן מהתחלה לסיום או יסתמך על ויקינתונים לצורך זה. ניתן לציין בצורה מפורשת "משך" לציון ידני של משך הזמן.

דוגמאות שימוש עריכה

פורמט דוגמה תוצאה הערה
טווח פשוט {{#invoke:טווח זמנים|טווח זמנים|תאריך התחלה=26 באוגוסט 2020|תאריך סיום=26 באוגוסט 2021}} 26 באוגוסט 2020 – 26 באוגוסט 2021
אירוע פשוט {{#invoke:טווח זמנים|טווח זמנים|נקודת זמן=26 באוגוסט 2020}} 26 באוגוסט 2020
משך - פשוט יחד עם משך מפורש {{#invoke:טווח זמנים|טווח זמנים|תאריך התחלה=26 באוגוסט 2020|תאריך סיום=26 באוגוסט 2021|הצגת הפרשת זמנים=כן|משך=שנה}} 26 באוגוסט 2020 – 26 באוגוסט 2021 (שנה)
הצגת הפרשת זמנים - פשוט יחד עם משך אוטומטי {{#invoke:טווח זמנים|טווח זמנים|תאריך התחלה=26 באוגוסט 2020|תאריך סיום=26 באוגוסט 2021|הצגת הפרשת זמנים=כן}} 26 באוגוסט 2020 – 26 באוגוסט 2021 (שנה)
פשוט יחד עם משך אוטומטי (דוגמה נוספת) {{#invoke:טווח זמנים|טווח זמנים|תאריך התחלה=10 בינואר 2005|תאריך סיום=23 בנובמבר 2005|הצגת הפרשת זמנים=כן}} 10 בינואר 2005 – 23 בנובמבר 2005 (318 ימים)
פורמט הצגה - כיצד להציג את משך הזמן {{#invoke:טווח זמנים|טווח זמנים|תאריך התחלה=26 באוגוסט 2020|תאריך סיום=26 באוגוסט 2021|הצגת הפרשת זמנים=כן|פורמט הצגה=%s<br>%s}} 26 באוגוסט 2020 – 26 באוגוסט 2021
(משך הזמן: שנה)
שימוש בוויקינתונים {{#invoke:טווח זמנים|טווח זמנים}} 1 בינואר 2011‏-2 בינואר 2012 בהנחה שהוזנו הפרטים בוויקינתונים
תאריך התחלה-ויקינתונים, תאריך סיום-ויקינתונים - הגדרת המזהים אשר ימשכו מוויקינתונים {{#invoke:טווח זמנים|טווח זמנים|תאריך התחלה-ויקינתונים=P2031|תאריך סיום-ויקינתונים=P2032}} 1 בינואר 2011‏-2 בינואר 2012 בהנחה שהוזנו הפרטים בוויקינתונים
qid - הגדרת הדף בויקינתונים ממנו נמשכים התאריכים {{#invoke:טווח זמנים|טווח זמנים|qid=Q27020128}} 1 בינואר 2011‏-2 בינואר 2012 בהנחה שהוזנו הפרטים בוויקינתונים. ניתן להזין |qid=- על מנת למנוע משיכה מוויקינתונים
הווה (ציון אירוע מתמשך) {{#invoke:טווח זמנים|טווח זמנים|תאריך התחלה=21 בינואר 2020|תאריך סיום=מכהנת|הצגת הפרשת זמנים=כן|הווה=מולך,מולכת,מכהן,מכהנת,לא כיהן,נוכחי,נוכחית,הווה,ואילך}} 21 בינואר 2020 – מכהנת (4 שנים) הווה מאפשר לציין שהאירוע לא הסתיים והוא נמשך בהווה, ומאפשר להכניס ערכים שונים אשר היחידה תקרא כהווה
נמשך - הגדרת ברירת מחדל של תאריך סיום כהווה {{#invoke:טווח זמנים|טווח זמנים|תאריך התחלה=26 ביולי 2020|נמשך=כן|הצגת הפרשת זמנים=כן}} 26 ביולי 2020 – הווה (3 שנים) באם יוזן תאריך סיום היחידה תתחשב בו
טקסט התחלה - טקסט להציג במקרה שבו ידוע רק תאריך התחלה ולא תאריך סיום. {{#invoke:טווח זמנים|טווח זמנים|תאריך התחלה=26 ביולי 2020|טקסט התחלה= מ-%s}} מ-26 ביולי 2020
טקסט סיום - הצגת תאריך הסיום במקרה בו לא יוזן תאריך התחלה (במקום ברירת המחדל של ?) {{#invoke:טווח זמנים|טווח זמנים|תאריך סיום=26 ביולי 2020|טקסט סיום= נגמר ב-%s}} נגמר ב-26 ביולי 2020

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

local function WikidataSimpleFill(parameter, entityId, property) 
	-- this doesnt handle unkown values, multi value etc
	if parameter~=nil and parameter~="" 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

--[[
This function fills missing parameter using wikidata
]]
function crossValidateWikidata(missingParam, templateParams, templateArg, frame, entityId)
	local WikidataCrossValidation = require('Module:WikidataCrossValidation')
	local matching = nil
	local propertyName

	if entityId == nil or missingParam==nil  then
		return nil
	end
	if templateParams[missingParam..'-ויקינתונים'] then
		propertyName = templateParams[missingParam..'-ויקינתונים']
		if mw.ustring.find(propertyName, ',')~=nil then return end
		matching = WikidataCrossValidation.crossValidate(templateArg, propertyName, entityId)
	end
	
	if matching then
		return WikidataCrossValidation.maintainceCategory(matching, propertyName)
	end
end

function render(frame)
	local args = Arguments.getArgs(frame, { ['trim'] = true, ['removeBlanks'] = true })
	local res = ''
	local maintainceCategory = ''
	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 wikidataEndDateLimit = args['גבול תאריך סיום-ויקינתונים']
	local showEditWikidata = true

	-- 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['טקסט סיום']
	-- text to show if we have only start date. Example: "First even - %s" where %s is time. nil - will not show end date
	local onlyStartDateText = 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
		local crossValBegin = crossValidateWikidata('תאריך התחלה', frame.args, startDate, frame, entityId)
		if crossValBegin~=nil then maintainceCategory = maintainceCategory ..crossValBegin end
		local crossvalEnd = crossValidateWikidata('תאריך סיום', frame.args, endDate, frame, entityId)
		if crossvalEnd~=nil then maintainceCategory= maintainceCategory ..crossvalEnd end
		
		
		-- 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

-- if only start date available, we will add prefix prefixStartOnly otherwise will bound the end
		if onlyStartDateText==nil and (endDate == '' or endDate == nil) and wikidataEndDateLimit~=nil and wikidataEndDateLimit~='' then
			-- fallback
			endDate, usingWikidataEndDate = WikidataSimpleFill(endDate, entityId, wikidataEndDateLimit )
		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

	-- Whether the event still continues. this will show <START>-present. Example: כן
	local defaultOngoingEvent = args['נמשך'] == 'כן'
	local ongoingEvent = defaultOngoingEvent
	local endDateOngoingEvent = args['הווה']
	local presentText = args['טקסט הווה']
	local diffFormat = 'auto' -- TODO: better handling for different scales
	
	if  endDate~=nil and endDate~='' then
		if endDateOngoingEvent~=nil then
			-- set default ongoingEvent as false
			ongoingEvent = false
			for v in string.gmatch(endDateOngoingEvent, "[^,]+") do
				if v:match('^%s*(.-)%s*$') == endDate then
					-- set as true if end date specific that match endDateOngoingEvent
					ongoingEvent = true
					if presentText then endDate=presentText end
				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
				dateFormat = '%s–%s'
				if mw.ustring.match(endDate, ' ') then
					dateFormat = '%s – %s'
				end
				res = mw.ustring.format(dateFormat, '?', endDate)
			else
				res = mw.ustring.format(onlyEndDateText, endDate)
			end
		elseif startDate ~= nil and (endDate==nil or (ongoingEvent and endDate~=nil))  then
			if onlyStartDateText then
				res = mw.ustring.format(onlyStartDateText, startDate)
			else
				if ongoingEvent then
					dateFormat = '%s–%s'
					if mw.ustring.match(startDate, ' ') or (endDate and mw.ustring.match(endDate, ' ')) then
						dateFormat = '%s – %s'
					end
					res = mw.ustring.format(dateFormat, startDate, endDate or 'הווה')
					-- try to fallback to automatic duration if duration is not available
					if showTimeDiff == 'כן' and duration==nil then
						local success, automaicDuration = pcall(Date.parseDateRange, startDate, diffFormat, true)
						if success then duration = automaicDuration end
					end
				else
					res = startDate
				end
			end
		elseif startDate~=nil and endDate~=nil  then
			dateFormat = '%s–%s'
			if mw.ustring.match(startDate, ' ') or mw.ustring.match(endDate, ' ') then
				dateFormat = '%s – %s'
			end
			res = mw.ustring.format(dateFormat, startDate, endDate)
			if showTimeDiff == 'כן' and duration==nil then
				local success, automaicDuration = pcall(Date.parseDateRange, res, diffFormat, true)
				if success then duration = automaicDuration end
			end
		end
	end
	
	-- append the duration
	if showTimeDiff == 'כן' and duration~=nil and duration~='' and res~=nil then
		res = mw.ustring.format(dateRangeDiffFormat, res, duration)
	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 maintainceCategory~=nil and res~=nil then
		res = res..maintainceCategory
	end
	return res

end

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