Aller au contenu

Module:ArchivéPar

De Wreck

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

local p = {}

local Outils = require 'Module:Outils'
local wikidata = require 'Module:Wikidata'
local linguistic = require 'Module:Linguistique'
--local reference = require 'Module:Wikidata/Références'
local cite = require 'Module:Biblio'
local yesno = require('Module:Yesno')


local function getReferences(statement, args) -- basé sur wikidata.getReferences dans [[Module:Wikidata]]
	local refdata = statement.references
	local cats = ''
	if not refdata then
		return nil
	end
	local expand = args and args.expand and #refdata > 1
	local compact = args and args.compact

	local refs = {}
	local hashes = {}
	for i, ref in pairs(refdata) do
		local s
		local function hasValue(prop) -- checks that the prop is here with valid value
			if ref.snaks[prop] and ref.snaks[prop][1].snaktype == 'value' then
				return true
			end
			return false
		end

		if hasValue('P8091') or hasValue('P854') then -- cas lorsque P8091 (Archival Resource Key) ou P854 (URL de la référence) est utilisé
			local title, arkKey, url, author, publisher, accessdate, publishdate, publishlang, quotation
			local description = ''
			local hasTitle = false

			if hasValue('P8091') then
				arkKey = wikidata.formatSnak(ref.snaks.P8091[1], {text = "-"})
				url = 'https://n2t.net/' .. arkKey
				if hasValue('P1476') then -- si un titre est présent
					title = wikidata.formatSnak(ref.snaks.P1476[1])
					hasTitle = true
				end
			elseif hasValue('P854') then
				url = wikidata.formatSnak(ref.snaks.P854[1], {text = "-"})
				if hasValue('P1476') then -- si un titre est présent
					title = wikidata.formatSnak(ref.snaks.P1476[1])
					hasTitle = true
				end
			end

			if not hasTitle then -- si pas de titre prendre la dernière section de l'url
				-- local tmp = url:gmatch("[^/]+$")
				local pos = url:match'^.*()/'
				if pos then
					if pos == string.len(url) then
						pos = url:match'^.*()/.'
						title = url:sub(pos + 1, -2)
					else
						title = url:sub(pos + 1, -1)
					end
				end
			end

			if expand then
				if hasValue('P1810') then -- sous le nom
					if description ~= '' then description = description .. ', ' end
					description = 'sous le nom ' .. wikidata.formatSnak(ref.snaks.P1810[1])
				end
				if hasValue('P813') then -- date de consultation
					accessdate = wikidata.formatSnak(ref.snaks.P813[1])
				end
				if hasValue('P217') then -- numéro d'inventaire
					if description ~= '' then description = description .. ', ' end
					description = description .. wikidata.formatSnak(ref.snaks.P217[1])
				end
				if hasValue('P50') then -- author (item type)
					author = wikidata.formatSnak(ref.snaks.P50[1])
				elseif hasValue('P2093') then -- author (string type)
					author = wikidata.formatSnak(ref.snaks.P2093[1])
				end
				if hasValue('P123') then -- éditeur
					publisher = wikidata.formatSnak(ref.snaks.P123[1])
				end
				if hasValue('P577') then -- date de publication
					publishdate = wikidata.formatSnak(ref.snaks.P577[1])
				end
				if hasValue('P1683') then -- citation
					quotation = wikidata.formatSnak(ref.snaks.P1683[1])
				end
				if hasValue('P407') then -- langue de l'œuvre
					local id = wikidata.getId(ref.snaks.P407[1])
					publishlang = getLangCode(id)
				end

				s = cite.lienWeb{titre = title, url = url, auteur = author, editeur = publisher, langue = publishlang, ['en ligne le'] = publishdate, ['consulté le'] = accessdate, ['citation'] = quotation, ['description'] = description}
			elseif compact then
				if not hasTitle and hasValue('P1810') then -- sous le nom
					title = 'chercher le nom ' .. wikidata.formatSnak(ref.snaks.P1810[1])
					hasTitle = true
				end

				s = '[' .. url .. ' ' .. title .. ']'
			else
				if hasValue('P1810') then -- sous le nom
					description = description .. 'chercher le nom ' .. wikidata.formatSnak(ref.snaks.P1810[1])
				end

				s = '[' .. url .. ' ' .. title .. ']'
				if description and description ~= '' then
					s = s .. ' ' .. description
				end
			end

			if not hasTitle and description == '' then
				-- cats = cats .. '[[Catégorie:Page ayant une référence sans titre depuis wikidata]]'
			end

			table.insert(hashes, ref.hash)
			table.insert(refs, s)
--[[
		elseif ref.snaks.P248 then -- cas lorsque P248 (affirmé dans) est utilisé
			for j, source in pairs(ref.snaks.P248) do
				if source.snaktype == 'value' then
					local page, accessdate, quotation
					if hasValue('P304') then -- page
						page = wikidata.formatSnak(ref.snaks.P304[1])
					end
					if hasValue('P813') then -- date de consultation
						accessdate = wikidata.formatSnak(ref.snaks.P813[1])
					end
					if hasValue('P1683') then -- citation
						quotation = wikidata.formatSnak(ref.snaks.P1683[1])
					end
					local sourceId = wikidata.getId(source)
					s = reference.citeitem(sourceId, {['page'] = page, ['accessdate'] = accessdate, ['citation'] = quotation})
					table.insert(refs, s)
					table.insert(hashes, ref.hash .. sourceId)
				end
			end
	--]]
		elseif ref.snaks.P854 and ref.snaks.P854[1].snaktype == 'value' then
			s = wikidata.formatSnak(ref.snaks.P854[1], {text = "-"})
			table.insert(hashes, ref.snaks.P854[1].hash)
			table.insert(refs, s)
		end
	end
	if #refs > 0 then
		if #hashes == #refs then
			return refs, hashes, cats
		end
		return refs, nil, cats
	end
end

local function stringTable(args) -- basé sur wikidata.stringTable dans [[Module:Wikidata]]
	local claims = args.claims
	local cats = ''

	if not claims then
		claims = wikidata.getClaims(args)
	end
	if not claims or claims == {} then
		return {}, {}, cats
	end
	if args.removedupesdate and (args.removedupesdate ~= '-') then
		claims, cats = removeDupesDate(claims, args.removedupesdate)
	end
	local props = {} -- liste des propriétés associété à chaque string pour catégorisation et linkback
	for i, j in pairs(claims) do
		claims[i] = wikidata.formatStatement(j, args)
		if args.expand then
			local sources, hashes, cat = getReferences(j, {expand = args.expand}) -- affichage complet si expand
			if sources and #sources > 1 then
				cats = cats .. cat
				local elementsHtml = mw.html.create('ul')
				for _, source in pairs(sources) do
					elementsHtml:tag('li'):wikitext(source):done():wikitext(' ')
				end
				claims[i] = claims[i].. tostring(elementsHtml:allDone())
			end
		end
		table.insert(props, j.mainsnak.property)
	end
	if args.removedupes and (args.removedupes ~= '-') then
		claims = wikidata.addNewValues({}, claims) -- devrait aussi supprimer de props celles qui ne sont pas utilisées
	end
	return claims, props, cats
end

local function formatQualifiers(statement, qualifs, params) -- basé sur wikidata.getFormattedQualifiers dans [[Module:Wikidata]]
	local str, cats = '', ''
	if not params then params = {} end
	local qualiftable = wikidata.getQualifiers(statement, qualifs)
	if not qualiftable then
		qualiftable = {}
	end
	qualiftable = wikidata.filterClaims(qualiftable, params) or {}
	for i, snak in pairs(qualiftable) do
		if (snak.datatype == 'time' and snak.property == 'P7104') then -- fin de période couverte
			str = str .. '-'
		elseif i > 1 then
			str = str .. ', '
		end
		qualiftable[i] = wikidata.formatSnak(snak, params)
		if snak.datatype == 'quantity' and tonumber(snak.datavalue.value.amount) > 1 then -- pluriel
			qualiftable[i] = qualiftable[i] .. 's'
		end
		str = str .. qualiftable[i]
	end

	if params.showsource and params.showsource ~= '-' then
		local sources, hashes, cat = getReferences(statement, {compact = params.compact}) -- risque d'appel en double
		if sources and not (params.expand and #sources > 1) then
			if #sources > 0 then
				if str ~= '' then
					str = str .. ', '
				end
				str = str .. table.concat(sources, ', ')
			end
			cats = cats .. cat
		end
	end
	return str .. cats
end

function p.main(frame)
	local params = Outils.extractArgs( frame )
	local showsource = yesno(params.showsource) or true
	local compact = (yesno(params.compact) or false) and params.compact ~= '-'
	local expand = (yesno(params.expand) or true) and params.expand ~= '-' and not compact
	local id = params.id or params.entity or params.wikidata
	local entity = wikidata.getEntity(id)
	local cats = ''

	local data = {
		entity = entity,
		property = 'P485', conjtype = 'new line', defaultlinkquery = {property = {'P361','P749','P127'}},
		showqualifiers = {'P217','P7328','P585','P7103','P7104'}, qualifprecision = 'year',
		qualifformat = formatQualifiers, qualifargs = {expand = expand, compact = compact, showsource = showsource},
		expand = expand, compact = compact,
	}
	local result, props, cat = stringTable(data)
	cats = cats .. cat
	if props and #props > 0 then
		cats = cats .. wikidata.addTrackingCat(props)
	end

	if #result > 0 then

		-- Ajout éventuel d'une incise "de ..."
		local ofname = params.dunom
		-- Utilise par défaut le label si l'id wikidata a été précisé manuellement
		if (not ofname) and params.wikidata and params.wikidata ~= "" and entity then --Has "entity" parameter, Wikidata item was found
			local label = entity:getLabel()
			local link = wikidata.siteLink(entity)
			local namestring
			if label then
				if link then
					namestring = '[[' .. link .. '|' .. label .. ']]'
				else
					namestring = label
				end
			elseif link then
				namestring = '[[' .. link .. ']]'
			end
			if namestring then
				ofname = linguistic.of(namestring,nil,nil,nil,label or link)
			end
		end
		if ofname then ofname = ofname .. ' ' end

		local title = "Archives " .. (ofname or '') .. "conservées par"

		if params.linkback ~= '-' then
			title = wikidata.addLinkBack(title , entity, 'P485')
		end

		local elementsHtml
		if compact then
			-- XXX: c'est invalide de mettre un <ul> (block-level) dans un <span> (inline-level)
			elementsHtml = mw.html.create('span')
				:addClass('liste-horizontale')
				:wikitext(title .. ' : ')
				:tag('ul')
			for _, val in pairs(result ) do
				elementsHtml:tag('li'):wikitext(val):done():wikitext(' ')
			end
		elseif #result > 1 then
			elementsHtml = mw.html.create('div')
				:wikitext(title .. ' : ')
				:tag('ul')
			for _, val in pairs(result ) do
				elementsHtml:tag('li'):wikitext(val):done():wikitext(' ')
			end
		elseif #result == 1 then
			elementsHtml = mw.html.create('span')
				:wikitext(title .. ' : ')
				:wikitext(result[1])
		end

		return tostring(elementsHtml:allDone()) .. cats
	end

	return cats
end

return p