Module:Index data

La bibliothèque libre.
Aller à la navigation Aller à la recherche

La documentation pour ce module peut être créée à Module:Index data/Documentation

--[[Récupère les données de la page d'index et enrichie les avec Wikidata]]--

local wikidataTypeToIndexType = {
	['Q3331189'] = 'book',
	['Q1238720'] = 'journal',
	['Q28869365'] = 'journal',
	['Q191067'] = 'journal',
	['Q23622'] = 'dictionary',
	['Q187685'] = 'phdthesis'
}
local indexToWikidata = {
	-- type, titre, image et BNF_ARK sont gérés spécialement
    ['sous_titre'] = 'P1680',
    ['volume'] = 'P478',
    ['auteur'] = 'P50',
    ['traducteur'] = 'P655',
    ['editeur_scientifique'] = 'P98',
    ['illustrateur'] = 'P110',
    ['editeur'] = 'P123',
    -- TODO ['School'] = 'PXXX',
    ['lieu'] = 'P291',
    ['annee'] = 'P577',
    ['epigraphe'] = 'P7150',
    -- TODO ['publication'] = 'PXX',
    -- TODO ['bibliotheque'] = 'PXX',
    -- TODO ['clef'] = 'PXX',
    -- TODO ['source'] = 'PXX',
}

function wdeditlink(item, propertyId)
	return '[[File:OOjs UI icon edit-ltr.svg|Voir et modifier les données sur Wikidata|10px|baseline|class=noviewer|link=d:' .. item.id .. '#' .. propertyId .. ']]'
end

function indexDataWithWikidata(frame)
	local args = frame.args
	local item = nil
	if args.wikidata_item and args.wikidata_item ~= '' then
		item = mw.wikibase.getEntity(args.wikidata_item)
		if item == nil then
			mw.addWarning('L\'identifiant d\'entité Wikidata [[d:' .. args.wikidata_item .. '|' .. args.wikidata_item .. ']] mise dans le paramètre "entité Wikidata" de la page Livre: ne semble pas valide.') 
		end
	end
	
	local argsWithMeta = {}
	local argsCache = {}
	-- we lazily load attributes (in order to avoid extra costly functions if the data is actually not used)
	setmetatable(argsWithMeta, {
		__index = function (_, arg)
			if not argsCache[arg] then
				-- we load in cache
				argsCache[arg] = '' -- dummy value to say we already looked for the value
				if args[arg] and args[arg] ~= '' then
					if args[arg] ~= '-' or arg == 'from' or arg == 'to' then -- we ignore the value "-" except for "from" and "to" 
						argsCache[arg] = args[arg]
					end
				elseif item then
					-- we load from Wikidata
					argsCache[arg] = ''
					if arg == 'type' then
						-- type depuis Wikidata
						for _, statement in pairs(item:getBestStatements('P31')) do
							if statement.mainsnak.datavalue ~= nil then
								local typeId = statement.mainsnak.datavalue.value
								if wikidataTypeToIndexType[typeId] then
									argsCache.type = wikidataTypeToIndexType[typeId] 
								end
							end
						end
					elseif arg == 'image' then
						-- image depuis Wikidata
						for _, statement in pairs(item:getBestStatements('P18')) do
							if statement.mainsnak.datavalue.value ~= nil then
								argsCache.image = statement.mainsnak.datavalue.value
							end
						end
					elseif arg == 'BNF_ARK' and argsCache.bibliotheque == nil then
						-- identifant BNF Gallica depuis Wikidata
						for _, statement in pairs(item:getBestStatements('P4258')) do
							if statement.mainsnak.datavalue.value ~= nil then
								argsCache.BNF_ARK = statement.mainsnak.datavalue.value
							end
						end
					elseif arg == 'titre' then
						-- titre depuis Wikidata
						local value = item:formatStatements('P1476')['value']
						if value == '' then
							value = item:getLabel() or ''
						end
						if value ~= '' then
							local siteLink = item:getSitelink()
							if siteLink then
								value = '[[' .. siteLink .. '|' .. value .. ']]'
							end
							argsCache.titre = value .. ' ' .. wdeditlink(item, "P1476")
						end
					elseif indexToWikidata[arg] then
						local propertyId = indexToWikidata[arg]
						local statements = item:getBestStatements(propertyId)
						if statements[1] then
							local valueString = ''
							if statements[1].mainsnak.datatype == 'wikibase-item' then
								local itemLinks = {}
								for _, statement in pairs(statements) do
									if statement.mainsnak and statement.mainsnak.datavalue and statement.mainsnak.datavalue.value then
										local valueItemId = statement.mainsnak.datavalue.value.id
										local val = ''
										if statement.qualifiers and statement.qualifiers.P1932 then
											-- Nom tel qu'affiché dans l'édition (ex. pseudonyme)
											val = statement.qualifiers.P1932[1].datavalue.value
										else
											val = mw.wikibase.getLabel(valueItemId)
										end
										if val ~= '' then
											local sitelink = mw.wikibase.getSitelink(valueItemId)
											if sitelink then
												table.insert(itemLinks, '[[' .. sitelink .. '|' .. val .. ']]')
											else
												table.insert(itemLinks, val)
											end
										end
									end
								end
								valueString = table.concat(itemLinks, ', ')
							else
								valueString = item:formatStatements(propertyId)["value"]
							end
							if valueString ~= '' then
								argsCache[arg] = valueString .. ' ' .. wdeditlink(item, propertyId)
							end
						end
					end
				end
			end
			if argsCache[arg] == '' then
				return nil
			else
				return argsCache[arg]
			end
		end
	})

	return {
		['args'] = argsWithMeta,
		['item'] = item
	}
end

return {
	['indexDataWithWikidata'] = indexDataWithWikidata
}