MediaWiki:Gadget-WikidataInfo.js

Da Wikivoyage.

Nota: dopo aver salvato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.

  • Firefox / Safari: tenere premuto il tasto delle maiuscole e fare clic su Ricarica, oppure premere Ctrl-F5 o Ctrl-R (⌘-R su Mac)
  • Google Chrome: premere Ctrl-Shift-R (⌘-Shift-R su un Mac)
  • Internet Explorer: tenere premuto il tasto Ctrl mentre si fa clic su Refresh, oppure premere Ctrl-F5
  • Opera: svuotare completamente la cache dal menu Strumenti → Preferenze
location.search.indexOf( 'printable=yes' ) === -1 && mw.config.get( 'wgPageContentModel' ) !== 'wikibase-item' && mw.loader.using( [ 'mediawiki.jqueryMsg', 'mediawiki.user' ] ).done( function () {
	var translate = {
		'en': {
			notfound: "Wikidata item not found.",
			intro: "Wikidata: ",
			nolabel: "(No label)",
			nodescription: "no description given",
			aliases: "{{PLURAL:$1|Alias|Aliases}}: ",
			noaliases: "None"
		},
		'it': {
			notfound: "Elemento di Wikidata non trovato.",
			intro: "Wikidata: ",
			nolabel: "(Nessuna etichetta)",
			nodescription: "Nessuna descrizione fornita",
			aliases: "Alias: ",
			noaliases: "Nessuno"
		}
	};
	var namespace = mw.config.get( 'wgNamespaceNumber' ),
		lang = mw.config.get( 'wgContentLanguage' ),
		page = ( namespace -= ( namespace > 0 && namespace % 2 ) ) === 0 ? mw.config.get( 'wgTitle' ) : mw.config.get( 'wgFormattedNamespaces' )[ namespace ] + ':' + mw.config.get( 'wgTitle' );

	namespace !== 2 && namespace !== -1 && $.ajax( {
		url: '//www.wikidata.org/w/api.php',
		data: {
			'format': 'json',
			'action': 'wbgetentities',
			'sites': mw.config.get( 'wgDBname' ),
			'titles': page,
			'props': 'info|labels|descriptions|aliases',
			'languages': lang,
			'maxage': mw.config.get("wgUserName") === null ? 900 : 30,
			'smaxage': mw.config.get("wgUserName") === null ? 900 : 30
		},
		dataType: 'jsonp',
		cache: true
	} )
	.done( function ( data ) {
		if ( data.success ) {
			$( function () {
				var $d = $( '<div>' ).addClass( 'WDI-box' ),
					userLang = mw.config.get( 'wgUserLanguage' ),
					m = Object.prototype.hasOwnProperty.call( translate, userLang ) ? translate[ userLang ] : translate.en;
				mw.messages.set( 'WDI-aliases', m.aliases );
				$.each( data.entities, function ( entity, item ) {
					if ( entity == -1 ) {
						$d.append( $( '<a>' ).attr( 'href',
							'//www.wikidata.org/wiki/Special:CreateItem?site=' +
							mw.config.get( 'wgDBname' ) +
							'&page=' + encodeURIComponent( page ) +
							'&label=' + encodeURIComponent( page )
						).text( m.notfound ) );
					} else {
						var label = item.labels[ lang ] && item.labels[ lang ].value,
							description = item.descriptions[ lang ] && item.descriptions[ lang ].value,
							aliases = $.map( item.aliases[ lang ] || [], function ( alias ) {
								return alias.value;
							} );
						if ( window.WDIlimitaliases && WDIlimitaliases < aliases.length ){
							for( ; WDIlimitaliases < aliases.length; aliases.pop() ) ;
							aliases.push( '...' );
						}
						$d.append(
							$( '<a>' )
								.attr( 'href', '//www.wikidata.org/wiki/' + item.title )
								.text( m.intro + ( label || m.nolabel ) ),
							' (' + item.title + ')',
							$( '<span>' ).text( ', ' ),
							$( '<i>' )
								.text( description || m.nodescription )
								.addClass( description ? 'WDI-desc' : 'WDI-desc WDI-nodesc' ),
							//$( '<br>' ),
							$( '<span>' ).text( " - " + mw.msg( 'WDI-aliases', aliases.length ) + ( aliases.join( ', ' ) || m.noaliases ) )
						);
					}
				} );
				if ( $( ".ext-wpb-pagebanner" ).length )
					$d.prependTo( ".ext-wpb-pagebanner" );
				else
					$d.appendTo( '#contentSub' );
			} );
		}
	} );
} );