Chronos chronos left.png
Rusmed logo color.png
Chronos chronos right.png

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Différences entre les versions de « Module:Fonctions »

De Commission Historique
m
m
Ligne 44 : Ligne 44 :


function p.afficheDateMod(frame)
function p.afficheDateMod(frame)
return afficheDate(frame.args['langue'],frame.args['date'],frame.args['precision'])
return p.afficheDate(frame.args['langue'],frame.args['date'],frame.args['precision'])
end
end



Version du 8 septembre 2022 à 09:29

La documentation pour ce module peut être créée à Module:Fonctions/doc

local p = {}
--local bib = mw.ext.cargo.query('Zotero', '_pageName, ZoteroKey, TitleItem, Author, Editor, Date, Date__precision, Place, ItemType, Publisher, PublicationTitle, Volume, PublicationYear, Pages, Url, FilingDate, AccessDate, Type, Number, IssuingAuthority',{groupBy = 'ZoteroKey', orderBy = 'ZoteroKey', limit = 4000})
--local bbib={}
local pagetitle = tostring(mw.title.getCurrentTitle())
--awk -F ';' '{print "[\""$1"\"] = {[\"de\"] = \""$2"\", [\"en\"] = \""$3"\", [\"fr\"] = \""$4"\"},"}' tr.csv

	local langue = 'fr'
	local lang = mw.getLanguage( langue )

--for _,r in ipairs(bib) do
--	bbib[r['ZoteroKey']] = r
--	if r['Date__precision'] == '3' then
--		bbib[r['ZoteroKey']]['Date'] = mw.text.truncate(r['Date'],4,'')
--	end
--end

function p.tableRef(Zref,result) -- Place la référence Zref dans la table result dont les clefs sont les champs (ex. Nom, Prénom, Date de naissance...)(Ref[3]) et les valeurs des tables de tables contenant clef Zotero et précision.
	local Ref = mw.text.split(Zref,'+')
	if Ref[3] then
		if not result[Ref[3]] then result[Ref[3]] = {} end
		table.insert(result[Ref[3]],{['Note']=Ref[1],['Precisions']=Ref[2] or nil})
	end
end	

function p.ligne(field,value,ref)
	local result = ''
	if value then
			result = '!' .. field .. '\n|height="25"|' .. value .. ref .. '\n|-\n'
	end
	return result
end

function p.afficheDate(lang,dat,precision)
	if not dat then return ' ' end
	if precision == '1' then return lang:formatDate('j F Y',dat)
		elseif precision == '3' then return lang:formatDate('Y', dat)
		elseif precision == '2' then return lang:formatDate('F Y', dat)
		elseif #dat == 4 then return lang:formatDate('Y',dat)
		elseif #dat == 7 then return lang:formatDate('F Y', dat)
		elseif #dat == 10 then return lang:formatDate('j F Y', dat)
		else return dat
	end
end

function p.afficheDateMod(frame)
	return p.afficheDate(frame.args['langue'],frame.args['date'],frame.args['precision'])
end

function p.dateFrise(dat)
	if not dat then return ' ' end
	if #dat == 4 then return dat .. '-01-01'
		elseif #dat == 7 then return dat .. '-01'
		else return dat
	end
end

function p.dateAdd(dat, nbYear)
	if #dat > 3 then return os.date("%Y-%m-%d",os.time({year=mw.text.truncate(dat,4,'') + nbYear,month=01,day=01}))
	else return dat
	end
end	
	
function p.copy(object)
    local lookup_table = {}
    local function _copy(object)
        if type(object) ~= "table" then
            return object
        elseif lookup_table[object] then
            return lookup_table[object]
        end
        local new_table = {}
        lookup_table[object] = new_table
        for index, value in pairs(object) do
            new_table[_copy(index)] = _copy(value)
        end
        return setmetatable(new_table, getmetatable(object))
    end
    return _copy(object)
end

function p.map(func, array)
  local new_array = {}
  for i,v in ipairs(array) do
    new_array[v] = func(v)
  end
  return new_array
end

function table.contains_idx(table, element)
  for index, value in pairs(table) do
    if index == element then
      return true
    end
  end
  return false
end

function p.trie_relations(result,key,langue,sym)
	local relations = {}
	if not sym then
		for _,row in ipairs(result) do
		--	row = p.langue_texte_field(row,"Resume_",langue)
			if not relations[row[key]] then relations[row[key]] = {} end
			table.insert(relations[row[key]],row)
		end
	else
		for _,row in ipairs(result) do
		--	row = p.langue_texte_field(row,"Resume_",langue)
			if row["Objet_2"] == pagetitle then
				if table.contains_idx(sym,row[key]) then
					row[key] = sym[row[key]]
					if not relations[row[key]] then relations[row[key]] = {} end
					row["Objet_1"], row["Objet_2"] = row["Objet_2"], row["Objet_1"]
					table.insert(relations[row[key]],row)
				end
			else
				if not relations[row[key]] then relations[row[key]] = {} end
				table.insert(relations[row[key]],row)
			end
		end
	end	
	return(relations)
end

function p.langue_field(frame)
	langue = frame:preprocess("{{int:lang}}")
	local result = frame.args[langue]
	if string.len(result) == 0 then
		result = frame.args['en']
		if string.len(result) == 0 then
			result = frame.args['fr']
			if string.len(result) == 0 then
				result = frame.args['de']
			end
		end
	end
	return(result)
end

function p.langue_texte_field(line,field,langue)
	if string.len(line[field .. langue]) == 0 then
		if string.len(line[field .. 'en']) ~= 0 then
			line[field .. langue] = line[field .. 'en']
		elseif string.len(line[field .. 'fr']) ~= 0 then
			line[field .. langue] = line[field .. 'fr']
		elseif string.len(line[field .. 'de']) ~= 0 then
			line[field .. langue] = line[field .. 'de']
		end
	end
	return(line)
end

function p.contains_idx(table, element)
  for index, value in pairs(table) do
    if index == element then
      return true
    end
  end
  return false
end

function p.prox(personne,coords,radius,d,f,time_radius)
	local tables = 'Localisations'
	local fields = '_pageName,Coordonnees,Date_debut,Date_fin'
	local dd,ff = d,f
	--if d == f then
		dd = p.addDates(d,-time_radius)
		ff = p.addDates(f,time_radius)
	--end
	local params = {
		where = "_pageName <> '" .. personne .. "' AND Date_debut <= '" .. ff .. "' AND Date_fin >= '" .. dd .. "' AND Coordonnees NEAR (" .. coords .. ",".. radius .. " km)",
		groupBy = '_pageName',
		limit = 3000,
	}
	local result = mw.ext.cargo.query(tables,fields,params)
	mess = ''
	for i, row in ipairs(result) do
		--mess = mess ..  '**' .. row['_pageName'] .. ' ' .. row['Date_debut'] .. ' ' .. row['Date_fin'] .. ' ' .. row['Coordonnees'] .. '\n'
		mess = mess ..  '** [[' .. row['_pageName'] .. ']]\n'
	end
	return mess
end

function p.reqLoc(frame)
	local langue = frame:preprocess("{{int:lang}}")
	local lang = mw.getLanguage( langue )
	local personne, radius, time_radius = frame.args[1],frame.args[2],frame.args[3]
	local tables = 'Localisations'
	local fields = 'Ville,Coordonnees,Date_debut,Date_debut__precision,Date_fin,Date_fin__precision'
	local params = {
		where = '_pageName =' .. '"' .. personne .. '" AND (Date_debut <> "" OR Date_fin <> "")', 
		--groupBy = '_pageName, ',
		limit=3000,
	}
	local table_locs = mw.ext.cargo.query(tables,fields,params)
	mess=''
	for i, row in ipairs(table_locs) do
		--if row['Date_debut'] or row['Date_fin'] then
			mess = mess .. '*' .. p.afficheDate(lang,row['Date_debut'],row['Date_debut__precision']) .. ' - ' .. p.afficheDate(lang,row['Date_fin'],row['Date_fin__precision']) .. ' : ' .. row['Ville'] .. '\n'
			mess = mess .. p.prox(personne,row['Coordonnees'],radius,row['Date_debut'],row['Date_fin'],time_radius)
		--end
	end
	return mess
end 

function p.addDates(date, delai)
	local y, m, d = date:match '(%d+)-(%d+)-(%d+)'
	local date_sec = os.time{ year = y, month = m, day = d } + delai*24*60*60
	return os.date("%Y-%m-%d",date_sec)
end 

function p.test()
	return "test !"
end

return p