MediaWiki:Common.js

From wikibase-docker
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/* Any JavaScript here will be loaded for all users on every page load. */

// 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 );
				} );
			}
		} );
	} );
}


//Script pour ajouter une description automatique dans l'espace de recherche, Autodesc cf. http://magnusmanske.de/wordpress/?p=64 et https://en.wikipedia.org/w/index.php?title=MediaWiki:Wdsearch-autodesc.js ---> il faudrait l'adapter, système de règles...

// Script pour générer une fiche EAC-cpf :

// var ext_link = "http://eac-cpf.herokuapp.com/eac/"; // Base URL for external service developed during #hackAN2018
// var Qid = $("body").attr("class").split(" ").pop().replace("wb-itempage-", ""); // Get the Q-id for the current page
// $("#p-views>ul").append('<li id="pers-1" class="collapsible"><span><a href="' + ext_link + Qid + '" title="export-ext" target="_blank">Convert to EAC</a></span></li>');


var ext_link = "https://eac-cpf.herokuapp.com/eac/"; // Base URL for external service developed during #hackAN2018
var Qid = $("body").attr("class").split(" ").pop().replace("wb-itempage-", ""); // Get the Q-id for the current page
$("#p-views>ul").append('<li id="pers-1" class="collapsible"><span><a href="' + ext_link + Qid + '" title="export-ext" target="_blank">Convert to EAC</a></span></li>');