« MediaWiki:Common.js » : différence entre les versions
Aller à la navigation
Aller à la recherche
+Replace Wikidata IDs with their [label, description] |
m re |
||
(21 versions intermédiaires par le même utilisateur non affichées) | |||
Ligne 4 : | Ligne 4 : | ||
mw.loader.using( 'mediawiki.ForeignApi', function() { | mw.loader.using( 'mediawiki.ForeignApi', function() { | ||
$( '.wb-external-id' ).each( function() { | $( '.wb-external-id' ).each( function() { | ||
var href = $( this ).attr( 'href' ); | |||
if ( href && href.lastIndexOf( 'https://www.wikidata.org', 0 ) === 0 ) { | |||
var wikidataApi = new mw.ForeignApi( 'https://www.wikidata.org/w/api.php', { | var wikidataApi = new mw.ForeignApi( 'https://www.wikidata.org/w/api.php', { | ||
anonymous: true, | anonymous: true, | ||
Ligne 17 : | Ligne 18 : | ||
'ids': node.text(), | 'ids': node.text(), | ||
'props': 'labels|descriptions', | 'props': 'labels|descriptions', | ||
'languages': lang, | 'languages': lang + ( lang !== 'fr' ? '|fr' : ''), | ||
'languagefallback': 1, | 'languagefallback': 1, | ||
'origin': '*' | 'origin': '*' | ||
} ).then( function( data ) { | } ).then( function( data ) { | ||
var entity = data.entities[ node.text() ] | var entity = data.entities[ node.text() ]; | ||
lang = entity.labels[ lang ] === undefined && lang !== 'fr' ? 'fr' : lang; | |||
var label = ( entity.labels[ lang ] !== undefined ? entity.labels[ lang ].value + ' <i>(' + node.text() + ')</i>' : node.text() ), | |||
description = ( entity.descriptions[ lang ] !== undefined ? '<small>' + entity.descriptions[ lang ].value + '</small>' : '' ); | |||
node.html( label + '<br>' + description ) | node.html( label + '<br>' + description ); | ||
} ); | } ); | ||
} | } | ||
Ligne 32 : | Ligne 34 : | ||
} ); | } ); | ||
} | } | ||
// Set a CSS class for statements whose a reference is P10 := Q22 (automatic synchronisation := LEGI database), which are statements from the official database | |||
// and on statements where P10 := Q112459 (automatic synchronisation := additional scripted contributions to the LEGI database) | |||
$( '.wikibase-statementview' ).each( function() { | |||
if( $( '.wikibase-statementview-references-container .wikibase-statementview-references .wikibase-snakview-2cdb0dd67364a92757f55f2cb72ac2f90cf6b2b5 .wikibase-snakview-variation-valuesnak a', this ).attr('title') === 'Q22' ) { | |||
$( this ).addClass( 'LEGI_DB' ); | |||
} | |||
if( $( '.wikibase-statementview-references-container .wikibase-statementview-references .wikibase-snakview-3e9e178b363306e7d4fd50b91891d0f28ff20570 .wikibase-snakview-variation-valuesnak a', this ).attr('title') === 'Q112459' ) { | |||
$( this ).addClass( 'scripted_LEGI_DB' ); | |||
} | |||
if( $( '.wikibase-statementview-references-container .wikibase-statementview-references .wikibase-snakview-fb3e74b3b1480db5a454cd5beba11a1124ae9bbe .wikibase-snakview-variation-valuesnak a', this ).attr('title') === 'Q113081' ) { | |||
$( this ).addClass( 'scripted_LEGI_DB' ); | |||
} | |||
} ); | |||
// For LEGI and additional-scripted-LEGI statements, remove the 'edit' button and add a 'mark as error' button | |||
mw.hook( 'wikibase.entityPage.entityView.rendered' ).add( function() { | |||
$( '.wikibase-statementview' ).each( function() { | |||
var type_source = null; | |||
if( $( '.wikibase-statementview-references-container .wikibase-statementview-references .wikibase-snakview-2cdb0dd67364a92757f55f2cb72ac2f90cf6b2b5 .wikibase-snakview-variation-valuesnak a', this ).attr('title') === 'Q22' ) { | |||
type_source = 'official'; | |||
} else if( $( '.wikibase-statementview-references-container .wikibase-statementview-references .wikibase-snakview-3e9e178b363306e7d4fd50b91891d0f28ff20570 .wikibase-snakview-variation-valuesnak a', this ).attr('title') === 'Q112459' ) { | |||
type_source = 'scripted'; | |||
} | |||
if( type_source ) { | |||
var statement = $( this ), | |||
claimid = statement.attr( 'id' ); | |||
// Add the button | |||
$( '.wikibase-edittoolbar-container > .wikibase-toolbar-container', this ).append( | |||
'<span class="wikibase-toolbarbutton wikibase-toolbar-item wikibase-toolbar-button wikibase-toolbar-button-edit wikibase-toolbar-button-save-deprecated" aria-disabled="true"><a href="#" title="" tabindex="-1"><span class="wb-icon"></span>marquer comme erreur</a></span>' | |||
); | |||
// When the button is cliked, retrieve the claim from the API, change only the rank to 'deprecated' and save this claim | |||
// This could be improved with more Wikibase-native functions, to be investigated | |||
$( '.wikibase-toolbar-button-save-deprecated', this ).click( function( event ) { | |||
event.preventDefault(); | |||
var api = new mw.Api(); | |||
api.get( { "action": "wbgetclaims", "claim": claimid } ).done( function( result ) { | |||
var property = ( result && result.claims && Object.keys( result.claims )[0] ) || null, | |||
claim = ( property && result.claims[property] && result.claims[property][0] ) || null; | |||
if( !claim ) return; | |||
if( claim.rank === "deprecated" ) return; | |||
claim.rank = "deprecated"; | |||
api.postWithEditToken( { | |||
"action": "wbsetclaim", | |||
"claim": JSON.stringify( claim ), | |||
"summary": type_source === 'official' ? "Valeur officielle de " + property + " marquée comme « en erreur »" : "Valeur déduite automatiquement de " + property + " marquée comme « en erreur »", | |||
"baserevid": mw.config.get( 'wgCurRevisionId' ), | |||
"bot": 1 | |||
} ).done( function() { | |||
$( 'div.wikibase-statementview-rankselector span.ui-icon-rankselector', statement ) | |||
.removeClass( [ 'wikibase-rankselector-normal', 'wikibase-rankselector-preferred' ] ) | |||
.addClass( 'wikibase-rankselector-deprecated' ) | |||
.attr( 'title', 'Rang désuet' ); | |||
}); | |||
} ); | |||
} ); | |||
} | |||
} ); | |||
} ); |
Dernière version du 15 août 2021 à 21:46
// Replace Wikidata IDs with their [label, description]
// From/depuis: https://lingualibre.fr/wiki/MediaWiki:Common.js
if ( $( '.wb-external-id' ).length > 0 ) {
mw.loader.using( 'mediawiki.ForeignApi', function() {
$( '.wb-external-id' ).each( function() {
var href = $( this ).attr( 'href' );
if ( href && href.lastIndexOf( 'https://www.wikidata.org', 0 ) === 0 ) {
var wikidataApi = new mw.ForeignApi( 'https://www.wikidata.org/w/api.php', {
anonymous: true,
parameters: { 'origin': '*' },
ajax: { timeout: 10000 }
} ),
lang = mw.config.get( 'wgUserLanguage' ),
node = $( this );
wikidataApi.get( {
'action': 'wbgetentities',
'format': 'json',
'ids': node.text(),
'props': 'labels|descriptions',
'languages': lang + ( lang !== 'fr' ? '|fr' : ''),
'languagefallback': 1,
'origin': '*'
} ).then( function( data ) {
var entity = data.entities[ node.text() ];
lang = entity.labels[ lang ] === undefined && lang !== 'fr' ? 'fr' : lang;
var label = ( entity.labels[ lang ] !== undefined ? entity.labels[ lang ].value + ' <i>(' + node.text() + ')</i>' : node.text() ),
description = ( entity.descriptions[ lang ] !== undefined ? '<small>' + entity.descriptions[ lang ].value + '</small>' : '' );
node.html( label + '<br>' + description );
} );
}
} );
} );
}
// Set a CSS class for statements whose a reference is P10 := Q22 (automatic synchronisation := LEGI database), which are statements from the official database
// and on statements where P10 := Q112459 (automatic synchronisation := additional scripted contributions to the LEGI database)
$( '.wikibase-statementview' ).each( function() {
if( $( '.wikibase-statementview-references-container .wikibase-statementview-references .wikibase-snakview-2cdb0dd67364a92757f55f2cb72ac2f90cf6b2b5 .wikibase-snakview-variation-valuesnak a', this ).attr('title') === 'Q22' ) {
$( this ).addClass( 'LEGI_DB' );
}
if( $( '.wikibase-statementview-references-container .wikibase-statementview-references .wikibase-snakview-3e9e178b363306e7d4fd50b91891d0f28ff20570 .wikibase-snakview-variation-valuesnak a', this ).attr('title') === 'Q112459' ) {
$( this ).addClass( 'scripted_LEGI_DB' );
}
if( $( '.wikibase-statementview-references-container .wikibase-statementview-references .wikibase-snakview-fb3e74b3b1480db5a454cd5beba11a1124ae9bbe .wikibase-snakview-variation-valuesnak a', this ).attr('title') === 'Q113081' ) {
$( this ).addClass( 'scripted_LEGI_DB' );
}
} );
// For LEGI and additional-scripted-LEGI statements, remove the 'edit' button and add a 'mark as error' button
mw.hook( 'wikibase.entityPage.entityView.rendered' ).add( function() {
$( '.wikibase-statementview' ).each( function() {
var type_source = null;
if( $( '.wikibase-statementview-references-container .wikibase-statementview-references .wikibase-snakview-2cdb0dd67364a92757f55f2cb72ac2f90cf6b2b5 .wikibase-snakview-variation-valuesnak a', this ).attr('title') === 'Q22' ) {
type_source = 'official';
} else if( $( '.wikibase-statementview-references-container .wikibase-statementview-references .wikibase-snakview-3e9e178b363306e7d4fd50b91891d0f28ff20570 .wikibase-snakview-variation-valuesnak a', this ).attr('title') === 'Q112459' ) {
type_source = 'scripted';
}
if( type_source ) {
var statement = $( this ),
claimid = statement.attr( 'id' );
// Add the button
$( '.wikibase-edittoolbar-container > .wikibase-toolbar-container', this ).append(
'<span class="wikibase-toolbarbutton wikibase-toolbar-item wikibase-toolbar-button wikibase-toolbar-button-edit wikibase-toolbar-button-save-deprecated" aria-disabled="true"><a href="#" title="" tabindex="-1"><span class="wb-icon"></span>marquer comme erreur</a></span>'
);
// When the button is cliked, retrieve the claim from the API, change only the rank to 'deprecated' and save this claim
// This could be improved with more Wikibase-native functions, to be investigated
$( '.wikibase-toolbar-button-save-deprecated', this ).click( function( event ) {
event.preventDefault();
var api = new mw.Api();
api.get( { "action": "wbgetclaims", "claim": claimid } ).done( function( result ) {
var property = ( result && result.claims && Object.keys( result.claims )[0] ) || null,
claim = ( property && result.claims[property] && result.claims[property][0] ) || null;
if( !claim ) return;
if( claim.rank === "deprecated" ) return;
claim.rank = "deprecated";
api.postWithEditToken( {
"action": "wbsetclaim",
"claim": JSON.stringify( claim ),
"summary": type_source === 'official' ? "Valeur officielle de " + property + " marquée comme « en erreur »" : "Valeur déduite automatiquement de " + property + " marquée comme « en erreur »",
"baserevid": mw.config.get( 'wgCurRevisionId' ),
"bot": 1
} ).done( function() {
$( 'div.wikibase-statementview-rankselector span.ui-icon-rankselector', statement )
.removeClass( [ 'wikibase-rankselector-normal', 'wikibase-rankselector-preferred' ] )
.addClass( 'wikibase-rankselector-deprecated' )
.attr( 'title', 'Rang désuet' );
});
} );
} );
}
} );
} );