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

Module:Fonctions

De Commission Historique
Révision datée du 2 mars 2021 à 12:33 par L.strappazon (discussion | contributions) (Page créée avec « local p = {} local bib = mw.ext.cargo.query('Zotero', '_pageName, ZoteroKey, TitleItem, Author, Editor, Date, Date__precision, Place, ItemType, Publisher, PublicationTitle… »)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)

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.expRef(frame)
	local Refs, Ref = {}, ''
	for _,Zref in ipairs(mw.text.split(frame.args['Refs'],'#')) do
		Ref = mw.text.split(Zref,'+')
			Refs[#Refs+1] = {['Note'] = Ref[1], ['Precisions'] = Ref[2] or nil}
	end
	return(p.expandRef(frame,Refs,'_'))
end

--[===[
function p.freeRef(frame)
	local result = ''
	if #frame.args['ZKey'] > 1 then
		local ref = mw.text.truncate(frame.args['ZKey'],-8,'')
		if bbib[ref] then
			local templ = 'ZoteroNote'
    		bbib[ref]['Precisions'] = frame.args['Precisions']
			if #frame.args['Type'] > 1 then templ = 'ZoteroPub' end
			result = frame:expandTemplate{title = templ, args = bbib[ref]}
		else
			local note = frame.args['ZKey']
			if #frame.args['Precisions'] > 1 then note = note .. ', ' .. frame.args['Precisions'] end
			result = frame:expandTemplate{title = 'Note', args = {note}}
		end
	end
	return result
end	
--]===]

function p.freeRef(frame)
	local result = ''
	if #frame.args['ZKey'] > 1 then
		local ref = mw.text.truncate(frame.args['ZKey'],-8,'')
		if bbib[ref] then
    		bbib[ref]['Precisions'] = frame.args['Precisions']
			result = frame:expandTemplate{title = 'ZoteroNote', args = bbib[ref]}
		else
			local note = frame.args['ZKey']
			if #frame.args['Precisions'] > 1 then note = note .. ', ' .. frame.args['Precisions'] end
			result = frame:expandTemplate{title = 'Note', args = {note}}
		end
	end
	return result
end	


function p.expandRef(frame,refs,typeRef)
	local result = ''
	if refs then
		for _,ref in ipairs(refs) do
			if #ref['Note'] > 1 then
				local Zkey = mw.text.truncate(ref['Note'],-8,'')
				if bbib[Zkey] then
					local templ = 'ZoteroNote'
					if typeRef then templ = 'ZoteroPub' end
			    	bbib[Zkey]['Precisions'] = ref['Precisions'] or nil
					result = result .. frame:expandTemplate{title = templ, args = bbib[Zkey]}
				else
	 				local note = ref['Note']
					if #ref['Precisions'] > 1 then
						note = note .. ', ' .. ref['Precisions']
					end
					if typeRef then
						result = result .. '\n*' .. note
					else	
						result = result .. frame:expandTemplate{title = 'Note', args = {note}}
					end
				end
			end
		end
	end
	return result
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 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.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_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.afficheTexteLangue(text_fields,langs,P)
	local lngAff
	for _,field in ipairs(text_fields) do
		lngAff = ''
		for _,lng in pairs(langs) do
			if #P[field .. '_' .. lng] > 1 and lngAff == '' then 
				lngAff = lng
			elseif lng ~= lngAff then 
				P[field .. '_' .. lng] = '' 
			end 
		end

	end
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