יחידה:אתר רשמי

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:אתר רשמי/תיעוד

function extendedConcat(t, f_sep, m_sep, l_sep)
	local concated = ''
	local f_sep = f_sep or ''
	local m_sep = m_sep or ''
	local l_sep = l_sep or ''
	for idx,val in ipairs(t) do
		if idx == 1 then
			concated = concated .. f_sep .. val
		elseif next(t,idx) ~= nil then
			concated = concated .. m_sep .. val
		elseif next(t,idx) == nil then
			concated = concated .. l_sep .. val
		end
	end
	return concated
end

function formatWebsite(url, name, languages, countries)
	local icon = '[[file:Green globe.svg|15px]]'
	local urlTemplate = '[' .. url ..' אתר האינטרנט הרשמי] של'
	local result = '* ' .. icon .. ' ' .. urlTemplate .. ' ' .. name
	
	if (#languages == 1 and languages[1] == 'עברית') then
		
	else
		if next(languages) ~= nil then
			local language = '<span dir="rtl" class="languageicon" style="font-size:0.95em; font-weight:bold; color:#555;">('..extendedConcat(languages, 'ב', ', ב', ' וב')..')</span>'
			result = result .. ' ' .. language
		elseif next(countries) ~= nil then
			local country = '<span dir="rtl" style="font-size:0.95em; font-weight:bold; color:#555;">('..extendedConcat(countries, nil, ', ', ' ו')..')</span>'
			result = result .. ' ' .. country
		end
	end

	return result
end

function extractQualifier(claim, qualifier, ignoredValue)
	local values = {}
	if claim.qualifiers and claim.qualifiers[qualifier] then 
		for _,v in pairs(claim.qualifiers[qualifier]) do
			if v.datavalue == nil then
				table.insert(values,  'skip')
			elseif mw.wikibase.label(v.datavalue.value.id) ~= ignoredValue then
				table.insert(values,  mw.wikibase.label(v.datavalue.value.id))
			end
		end
	end
	return values
end

function isHebrewWebsite(claim)
	if claim.qualifiers and claim.qualifiers['P407'] then 
		for _,v in pairs(claim.qualifiers['P407']) do
			if mw.wikibase.label(v.datavalue.value.id) == 'עברית' then
				return true
			end
		end
	end
	return false
end

-- generate websites from wikidata
function websites(frame)
	local name = frame.args["שם"] or frame.args["name"]
	local entityId = frame.args["entityId"]
	local websiteProperty = 'P856'
	local languageQualifier = 'P407'
	local countryQualifier = 'P17'
	local endTimeQualifier = 'P582'
	local earliestEndDateQualifier = 'P8554'
	local maintenanceCategory = '[[קטגוריה: אתר רשמי – עודף אתרים רשמיים]]'
	
	if not name or name == '' then 
		name = mw.title.getCurrentTitle().text:gsub('%b()', '')
	end

	local result = {}
	if not mw.wikibase.getEntityObject(entityId) then return nil end
	local ws = mw.wikibase.getEntityObject(entityId).claims[websiteProperty]
	if not ws then return nil end
	
	local preferredOnly = false
	for k,v in pairs(ws) do
		if v.mainsnak.datavalue then
			if not preferredOnly and v.rank == 'normal' or v.rank == 'preferred' then 
				-- TODO: this condition fails when we get normal ranked values prior to a preferred ranked value
				preferredOnly = preferredOnly or (v.rank == 'preferred')
				local lang = extractQualifier(v, languageQualifier)
				local countries = extractQualifier(v, countryQualifier)
				local isThereEndTime = extractQualifier(v, endTimeQualifier)
				local isThereEarliestEndDate = extractQualifier(v, earliestEndDateQualifier)
				local url = v.mainsnak.datavalue.value
				if (#isThereEndTime == 0 and #isThereEarliestEndDate == 0) then
					if isHebrewWebsite(v) then
						table.insert(result, 1, formatWebsite(url, name, lang, countries))
					else
						table.insert(result, formatWebsite(url, name, lang, countries))
					end
				end
			end
		end
	end
	local concated = table.concat(result, "\n")
	if #result>2 then
		concated = concated .. maintenanceCategory
	end
	return concated
end

return {
	formatWebsite = formatWebsite,
	websites = websites
}