« Module:Index template » : différence entre les versions

Contenu supprimé Contenu ajouté
améliore le support de Wikidata
ajoute Wikidata pour plus de champ et début du découpage Wikidata du code du modèle (pour pouvoir le partager avec le header)
Ligne 6 :
['Q23622'] = 'dictionary',
['Q187685'] = 'phdthesis'
}
local indexToWikidata = {
-- type est géré spécialement
['titre'] = 'P1476',
['sous_titre'] = 'P1680',
['volume'] = 'P478',
['auteur'] = 'P50',
['traducteur'] = 'P655',
['editeur_scientifique'] = 'P98',
['illustrateur'] = 'P110',
['editeur'] = 'P123',
-- TODO ['School'] = 'PXXX',
['lieu'] = 'P291',
['annee'] = 'P577',
-- TODO ['publication'] = 'PXX',
-- TODO ['bibliotheque'] = 'PXX',
-- TODO ['clef'] = 'PXX',
['BNF_ARK'] = 'P4258',
-- TODO ['source'] = 'PXX',
-- TODO ['image'] = 'PXX',
}
 
function indexDataWithWikidata(frame)
function withWikidataLink(wikitext, item, property, category)
--create a clean table of parameters with blank parameters removed
if wikitext == nil then
local args = {}
if item ~= nil and property ~= nil then
for k,v in pairs(frame.args) do
local links = {}
if v ~= '' then
for _, statement in pairs(item:getBestStatements(property)) do
args[k] = v
if statement.mainsnak.datavalue ~= nil then
end
local value = statement.mainsnak.datavalue.value.id
end
table.insert(links, displayWikidataLink(mw.wikibase.getSitelink(value), mw.wikibase.getLabel(value) or '?', category, item.id .. '#' .. property))
local item = nil
if 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
if not item then
return {
['args'] = args,
['item'] = nil
}
end
 
-- type depuis Wikidata
if not args.type then
for _, statement in pairs(item:getBestStatements('P31')) do
if statement.mainsnak.datavalue ~= nil then
local typeId = statement.mainsnak.datavalue.value.id
if wikidataTypeToIndexType[typeId] then
args.type = wikidataTypeToIndexType[typeId]
end
end
end
if next(links) then
end
return mw.text.listToText(links)
 
else
for arg, propertyId in pairs(indexToWikidata) do
return nil
if not args[arg] then
local values = {}
for _, statement in pairs(item:getBestStatements(propertyId)) do
table.insert(values, mw.wikibase.formatValue(statement.mainsnak))
end
if next(values) then
args[arg] = mw.text.listToText(values) .. ' [[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
elseif arg == 'titre' then
args.titre = withWikidataLink(args.titre, 'Livres avec un lien Wikidata')
else
args[arg] = withWikidataLink(args[arg])
return nil
end
end
-- title
if not args.titre then
args.titre = item:getLabel()
end
 
return {
['args'] = args,
['item'] = item
}
end
 
function withWikidataLink(wikitext, category)
if wikitext == nil then
return nil
end
new_wikitext = mw.ustring.gsub(wikitext, '%[%[([^|%]]*)%]%]', function(page)
Ligne 46 ⟶ 113 :
title = title.redirectTarget
end
return displayWikidataLink(title.fullText, label, category, mw.wikibase.getEntityIdForTitle(title.fullText))
end
 
local tag = mw.html.create('span')
function displayWikidataLink(page, label, category, wikidataEditLink)
local tagitemId = mw.htmlwikibase.creategetEntityIdForTitle('span'title.fullText)
tag:wikitext('[[' .. page .. '|' .. label .. ']]')
if page == nil then
if itemId ~= nil then
tag:wikitext(label)
tag:wikitext(' [[Image:Wikidata.svg|10px|link=d:' .. itemId .. '|Voir l\'entité sur Wikidata]]')
else
tag:wikitext('[[:' .. page .. '|' .. label .. ']]')
end
if wikidataEditLink ~= nil then
tag:wikitext(' [[Image:Blue pencil.svg|10px|link=d:' .. wikidataEditLink .. '|Voir et modifier les données sur Wikidata]]')
if category ~= nil then
tag:wikitext('[[CategoryCatégorie:' .. category .. ']]')
end
end
Ligne 79 ⟶ 140 :
function indexTemplate(frame)
--create a clean table of parameters with blank parameters removed
local argsdata = {}indexDataWithWikidata(frame)
forlocal k,vargs in= pairs(framedata.args) do
local item = data.item
if v ~= '' then
args[k] = v
end
end
local page = mw.title.getCurrentTitle()
local html = mw.html.create()
localif item = nilthen
if 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
html:wikitext('[[Catégorie:Livres avec un identifiant Wikidata]]<indicator name="wikidata">[[File:Wikidata.svg|20px|élément Wikidata|link=d:' .. item.id .. ']]</indicator>')
end
-- type depuis Wikidata
if item and not args.type then
for _, statement in pairs(item:getBestStatements('P31')) do
if statement.mainsnak.datavalue ~= nil then
local typeId = statement.mainsnak.datavalue.value.id
if wikidataTypeToIndexType[typeId] then
args.type = wikidataTypeToIndexType[typeId]
end
end
end
end
 
Ligne 144 ⟶ 185 :
local metadataTable = metadataContainer:tag('table')
 
if item and not args.titre then
args.titre = item:getLabel()
end
if args.titre then
if args.type == 'journal' then
addRow(metadataTable, 'Journal', args.titre)
else
addRow(metadataTable, 'Titre', withWikidataLink(args.titre, item, nil, 'Livres avec un lien Wikidata'))
end
else
Ligne 158 ⟶ 196 :
addRow(metadataTable, 'Sous-titre', args.sous_titre)
addRow(metadataTable, 'Volume', args.volume)
addRow(metadataTable, 'Auteur', withWikidataLink(args.auteur, item, 'P50'))
addRow(metadataTable, 'Traducteur', withWikidataLink(args.traducteur, item, 'P655'))
addRow(metadataTable, 'Éditeur', withWikidataLink(args.editeur_scientifique, item, 'P98'))
addRow(metadataTable, 'Illustrateur', withWikidataLink(args.illustrateur, item, 'P110'))
addRow(metadataTable, 'École', withWikidataLink(args.school, item, nil))
addRow(metadataTable, 'Maison&nbsp;d’édition', withWikidataLink(args.editeur, item, 'P123'))
addRow(metadataTable, 'Lieu&nbsp;d’édition', withWikidataLink(args.lieu, item, 'P291'))
addRow(metadataTable, 'Année&nbsp;d’édition', args.annee)
addRow(metadataTable, 'Publication&nbsp;originale', args.publication)