ميدياويكي:Gadget-WikidataInfo.js

من ويكيبيديا، الموسوعة الحرة

ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات.

/**
 * Add a small box under a Wikipedia article's title displaying the label,
 * description, aliases and the identifier of the related Wikidata item
 *
 * You can enable it by adding these lines to your common.js (or global.js) file:
 *
 *  // [[d:User:Yair rand/WikidataInfo.js]]
 *  mw.loader.load( '//www.wikidata.org/w/index.php?title=User:Yair_rand/WikidataInfo.js&action=raw&ctype=text/javascript' );
 *
 * dependencies=mediawiki.jqueryMsg, mediawiki.util
 */
// <nowiki>
location.search.indexOf( 'printable=yes' ) === -1
&& [
	'wikibase-item',
	'wikibase-property',
	'wikibase-lexeme',
	'flow-board',
	'EntitySchema',
	'proofread-index',
	'proofread-page'
].indexOf( mw.config.get( 'wgPageContentModel' ) ) === -1
&& mw.loader.using( [ 'mediawiki.jqueryMsg', 'mediawiki.util' ] ).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"
		},
		'ar': {
			notfound: "لم يعثر على هذه المادة في ويكي بيانات.",
			intro: "ويكي بيانات: ",
			nolabel: "(لا توجد تسمية)",
			nodescription: "لا يوجد توصيف",
			aliases: "أسماء أخرى: ",
			noaliases: "لا توجد أسماء أخرى"
		},
		'de': {
			notfound: "Wikidata-Eintrag nicht gefunden.",
			intro: "Wikidata: ",
			nolabel: "(Keine Bezeichnung)",
			nodescription: "Keine Beschreibung vorhanden.",
			aliases: "{{PLURAL:$1|Alternative Bezeichnung|Alternative Bezeichnungen}}: ",
			noaliases: "keine"
		},
		'es': {
			notfound: "Elemento de Wikidata no encontrado",
			intro: "Wikidata: ",
			nolabel: "(No etiquetado)",
			nodescription: "sin descripción",
			aliases: "Nombres alternativos: ",
			noaliases: "No"
		},
		'fr': {
			notfound: "Élement de Wikidata non trouvé.",
			intro: "Wikidata: ",
			nolabel: "(Aucun label)",
			nodescription: "Aucune description fournie",
			aliases: "Alias : ",
			noaliases: "Aucun"
		},

	};
	var namespace = mw.config.get( 'wgNamespaceNumber' ),
		lang = (translate[mw.config.get( 'wgUserLanguage' )] && mw.config.get( 'wgUserLanguage' )) || mw.config.get( 'wgContentLanguage' ),
		page = ( namespace -= ( namespace > 0 && namespace % 2 ) ) === 0
			? mw.config.get( 'wgTitle' )
			: mw.config.get( 'wgFormattedNamespaces' )[ namespace ] + ':' + mw.config.get( 'wgTitle' );

	[ -1, 2, 6 ].indexOf( 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,
			//'maxlag': 1
		},
		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:NewItem?site=' +
							mw.config.get( 'wgDBname' ) +
							'&page=' + encodeURIComponent( page ) +
							'&label=' + encodeURIComponent( page ) +
							'&lang=' + lang
						).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 ) ),
							' (',
							$( '<a>' )
								.attr( 'href', '//www.wikidata.org/wiki/' + item.title )
								.text( 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 ) )
						);
					}
				} );
				try {
					mw.util.addSubtitle( $d[ 0 ] );
				} catch ( e ) {
					// subtitle doesn't exist - either removed by another gadget or not supported by skin
					// Should we add it somewhere else here, or add a notification to report this problem?
				}
			} );
		}
	} );
} );
// </nowiki>