« Utilisateur:AdrianoRutz/common.js » : différence entre les versions

De Wikiromandie.org
Aller à la navigation Aller à la recherche
Page créée avec « ( function () { 'use strict'; // Mêmes propriétés que la property_blacklist du module Lua var PROPERTY_BLACKLIST = { P360: 1, P4224: 1, P935: 1, P1472: 1, P1612: 1, P373: 1, P3722: 1, P1151: 1, P1424: 1, P910: 1, P1200: 1, P1792: 1, P1464: 1, P1465: 1, P1791: 1, P1740: 1, P2033: 1, P2517: 1, P4195: 1, P1754: 1, P301: 1, P971: 1, P3876: 1, P1753: 1, P3921: 1, P1204: 1, P1423: 1, P1709: 1, P3950: 1, P2888: 1, P1382: 1, P527: 1, P2670: 1, P3113: 1,... »
 
Page blanchie
Balise : Blanchiment
 
(Une version intermédiaire par le même utilisateur non affichée)
Ligne 1 : Ligne 1 :
( function () {
'use strict';


// Mêmes propriétés que la property_blacklist du module Lua
var PROPERTY_BLACKLIST = {
P360: 1, P4224: 1, P935: 1, P1472: 1, P1612: 1, P373: 1, P3722: 1,
P1151: 1, P1424: 1, P910: 1, P1200: 1, P1792: 1, P1464: 1, P1465: 1,
P1791: 1, P1740: 1, P2033: 1, P2517: 1, P4195: 1, P1754: 1, P301: 1,
P971: 1, P3876: 1, P1753: 1, P3921: 1, P1204: 1, P1423: 1, P1709: 1,
P3950: 1, P2888: 1, P1382: 1, P527: 1, P2670: 1, P3113: 1, P2737: 1,
P2738: 1, P2445: 1, P1963: 1, P3176: 1, P1889: 1, P460: 1, P2959: 1,
P2860: 1, P5125: 1, P5008: 1, P2559: 1, P1343: 1, P972: 1, P1282: 1,
P4839: 1, P6104: 1, P5996: 1, P2357: 1, P31: 1
};
var EXCLUDED_DATATYPES = {
'commonsMedia': 1,
'external-id': 1,
'quantity': 1,
'wikibase-property': 1,
'geo-shape': 1,
'tabular-data': 1
};
var LANG = 'fr'; // adapte si besoin
function wbApi( params ) {
var base = 'https://www.wikidata.org/w/api.php?origin=*&format=json';
var qs = Object.keys( params ).map( function ( k ) {
return k + '=' + encodeURIComponent( params[ k ] );
} ).join( '&' );
return fetch( base + '&' + qs ).then( function ( r ) { return r.json(); } );
}
function formatSnak( snak, labelCache ) {
if ( snak.snaktype === 'novalue' ) return '—';
if ( snak.snaktype === 'somevalue' ) return 'inconnu';
var v = snak.datavalue.value;
switch ( snak.datatype ) {
case 'wikibase-item':
var qid = 'Q' + v[ 'numeric-id' ];
var lbl = ( labelCache[ qid ] && labelCache[ qid ].label ) || qid;
return '<a href="https://www.wikidata.org/wiki/' + qid + '" target="_blank" rel="noopener">' + lbl + '</a>';
case 'time':
var m = /^\+?(-?\d+)-(\d{2})-(\d{2})/.exec( v.time );
return m ? ( m[ 1 ] + ( v.precision >= 10 ? '-' + m[ 2 ] : '' ) + ( v.precision >= 11 ? '-' + m[ 3 ] : '' ) ) : v.time;
case 'url':
return '<a href="' + v + '" target="_blank" rel="noopener">' + v + '</a>';
case 'monolingualtext':
return v.text;
case 'globe-coordinate':
return v.latitude.toFixed( 4 ) + ', ' + v.longitude.toFixed( 4 ) +
' <a href="https://www.openstreetmap.org/?mlat=' + v.latitude + '&mlon=' + v.longitude + '&zoom=12" target="_blank" rel="noopener">🗺️</a>';
default:
return String( v );
}
}
function buildDatabox( entity, propLabels, itemLabels, containerEl ) {
var label = ( entity.labels && entity.labels[ LANG ] && entity.labels[ LANG ].value ) || entity.id;
var desc = ( entity.descriptions && entity.descriptions[ LANG ] && entity.descriptions[ LANG ].value ) || '';
var box = document.createElement( 'div' );
box.className = 'wd-databox';
box.style.cssText = 'float:right;border:1px solid #a2a9b1;max-width:300px;padding:0 0.4em;margin:0 0 0.4em 0.4em;font-size:90%;';
var title = document.createElement( 'div' );
title.style.cssText = 'text-align:center;background:#f9f9f9;padding:0.5em 0;margin:0.5em 0;font-size:120%;font-weight:bold;';
title.textContent = label.charAt( 0 ).toUpperCase() + label.slice( 1 );
box.appendChild( title );
if ( desc ) {
var descEl = document.createElement( 'div' );
descEl.style.cssText = 'text-align:center;font-style:italic;margin-bottom:0.5em;';
descEl.textContent = desc;
box.appendChild( descEl );
}
// Image P18 via Special:FilePath (redirige vers Commons, pas de CORS nécessaire pour <img>)
var claims = entity.claims || {};
if ( claims.P18 && claims.P18[ 0 ] && claims.P18[ 0 ].mainsnak.datavalue ) {
var filename = claims.P18[ 0 ].mainsnak.datavalue.value;
var img = document.createElement( 'img' );
img.src = 'https://commons.wikimedia.org/wiki/Special:FilePath/' + encodeURIComponent( filename ) + '?width=280';
img.style.cssText = 'display:block;margin:0 auto 0.5em;max-width:100%;';
img.alt = filename;
box.appendChild( img );
}
var table = document.createElement( 'table' );
table.style.cssText = 'width:100%;table-layout:fixed;word-break:break-word;';
// Ligne P31 en caption
if ( claims.P31 && claims.P31.length ) {
var caption = document.createElement( 'caption' );
caption.style.cssText = 'background:#f9f9f9;font-weight:bold;margin-top:0.2em;';
caption.innerHTML = claims.P31.slice( 0, 5 ).map( function ( c ) {
return formatSnak( c.mainsnak, itemLabels );
} ).join( ', ' );
table.appendChild( caption );
}
Object.keys( claims ).forEach( function ( pid ) {
if ( pid === 'P18' ) return; // déjà affiché comme image
if ( PROPERTY_BLACKLIST[ pid ] ) return;
var statements = claims[ pid ];
var datatype = statements[ 0 ].mainsnak.datatype;
if ( EXCLUDED_DATATYPES[ datatype ] ) return;
if ( statements.length > 5 ) return;
var tr = document.createElement( 'tr' );
var th = document.createElement( 'th' );
th.style.cssText = 'text-align:left;vertical-align:top;';
var plabel = ( propLabels[ pid ] && propLabels[ pid ].label ) || pid;
th.textContent = plabel.charAt( 0 ).toUpperCase() + plabel.slice( 1 );
var td = document.createElement( 'td' );
td.style.cssText = 'vertical-align:top;';
td.innerHTML = statements.map( function ( s ) {
return formatSnak( s.mainsnak, itemLabels );
} ).join( '; ' ) +
' <a href="https://www.wikidata.org/wiki/' + entity.id + '#' + pid + '" target="_blank" rel="noopener" style="font-size:85%;">✎</a>';
tr.appendChild( th );
tr.appendChild( td );
table.appendChild( tr );
} );
box.appendChild( table );
containerEl.innerHTML = '';
containerEl.appendChild( box );
}
function loadDatabox( containerEl ) {
var qid = containerEl.getAttribute( 'data-qid' );
if ( !qid ) return;
wbApi( { action: 'wbgetentities', ids: qid, languages: LANG, props: 'labels|descriptions|claims' } )
.then( function ( data ) {
var entity = data.entities[ qid ];
if ( !entity || entity.missing !== undefined ) {
containerEl.textContent = 'Élément Wikidata introuvable : ' + qid;
return;
}
// Collecter tous les PID des propriétés utilisées + tous les QID référencés en valeur
var pids = Object.keys( entity.claims || {} );
var refQids = {};
pids.forEach( function ( pid ) {
entity.claims[ pid ].forEach( function ( c ) {
if ( c.mainsnak.datatype === 'wikibase-item' && c.mainsnak.snaktype === 'value' ) {
refQids[ 'Q' + c.mainsnak.datavalue.value[ 'numeric-id' ] ] = 1;
}
} );
} );
var idsToLabel = pids.concat( Object.keys( refQids ) );
if ( !idsToLabel.length ) {
buildDatabox( entity, {}, {}, containerEl );
return;
}
// L'API limite à 50 ids par requête : on découpe si besoin
var chunks = [];
for ( var i = 0; i < idsToLabel.length; i += 50 ) {
chunks.push( idsToLabel.slice( i, i + 50 ) );
}
Promise.all( chunks.map( function ( chunk ) {
return wbApi( { action: 'wbgetentities', ids: chunk.join( '|' ), languages: LANG, props: 'labels' } );
} ) ).then( function ( results ) {
var propLabels = {}, itemLabels = {};
results.forEach( function ( res ) {
Object.keys( res.entities ).forEach( function ( id ) {
var lbl = res.entities[ id ].labels && res.entities[ id ].labels[ LANG ];
var target = id.charAt( 0 ) === 'P' ? propLabels : itemLabels;
target[ id ] = { label: lbl ? lbl.value : id };
} );
} );
buildDatabox( entity, propLabels, itemLabels, containerEl );
} );
} )
.catch( function ( err ) {
containerEl.textContent = 'Erreur de chargement Wikidata : ' + err.message;
} );
}
function init() {
document.querySelectorAll( '.wd-databox-loader' ).forEach( loadDatabox );
}
if ( document.readyState === 'loading' ) {
document.addEventListener( 'DOMContentLoaded', init );
} else {
init();
}
}() );

Dernière version du 14 juillet 2026 à 10:37