User:Hsarrazin/LangSwitch.js

Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
$( document ).ready( function() {
  if( mw.config.get( 'wgNamespaceNumber' ) == 100 ) {
    mw.util.addPortletLink( 'p-views', 'http://creatorlinks.wmflabs.org/index.php?site=commons&title=' + mw.config.get( 'wgPageName' ), 'creatorLinks' );
  }
} ); 



( function ( mw, $ ) {

	/**
	 * Return the language code from the ID of the Wiki
	 */
	function getLanguageCodeFromWikiId( wiki ) {
		var splited = wiki.match( /^(.*)wiki$/i );
		if( !splited[1] ) {
			return '';
		} else {
			return splited[1].replace(/_/g, '-');
		}
	}
 
	/**
	 * Create the dialog and add a link in toolBox
	 */
	function init() {
		mw.loader.using( 'ext.wikiEditor', function () {
			$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
				section: 'advanced',
				group: 'insert',
				'tools': {
					'LangSwitch': {
						label: 'Insertion du LangSwitch',
						type: 'button',
						icon: 'http://upload.wikimedia.org/wikipedia/commons/thumb/3/36/Tennis_ball_3.svg/22px-Tennis_ball_3.svg.png',
						action: { 
							type: 'callback',
							execute: function(context) {
								show(context);
							}
						}
					}
				}
			} );
		} );
	}
 
	/**
	 * Show the dialog
	 */
	function show(context) {
		getCreatorLinks( mw.config.get( 'wgPageName' ), function( links ) {
			if( links.wikidata ) {
				var wikidataId = links.wikidata.title.toLowerCase();
			} else {
				var wikidataId = 'q' + prompt( 'Identifiant Wikidata (sans "Q")' );
			}
			getItemLinks( wikidataId, function( links ) {
				var text = '{{LangSwitch\n';
				for( var i in links ) {
					var languageCode = getLanguageCodeFromWikiId( links[i].site );
					if( languageCode !== '' ) {
						if( languageCode == 'simple' ) {
							continue;
						}
						var link = links[i].title;
						var title = link.replace( /\(.*\)/g, '' );
						text += '  | ' + languageCode + '= [[:' + languageCode + ':' + link + '|';
						if( title != link ) {
							text += title;
						}
						text += ']]\n';
					}
				}
				alert( text + ' }}' );
			} );
		} );
	}

	/**
	 * Called on API error
	 */
	function onApiError( jqXHR, textStatus, errorThrown ) {
		alert( 'error-api' );
	}
 
	/**
	 * Return the item
	 * @param success function called on success
	 */
	function getItem( itemId, success ) {
		$.ajax( {
			url: '//wikidata.org/w/api.php',
			data: {
				'format': 'json',
				'action': 'wbgetentities',
				'ids': itemId
			},
			dataType: 'jsonp'
		} )
		.done(function( data ) {
			if( data.success && data.entities[itemId] ) {
				success( data.entities[itemId] );
			} else {
				onApiError();
			}
		} )
		.fail(onApiError);
	}
 
	function getCreatorLinks( pageName, success ) {
		$.ajax( {
			url: 'http://creatorlinks.wmflabs.org/api.php',
			data: {
				'action': 'query',
				'format': 'json',
				'sites': 'commons',
				'titles': pageName
			},
			dataType: 'jsonp'
		} )
		.done(function( data ) {
			var links = [];
			if( data.entries ) {
				for( var i in data.entries ) {
					links = data.entries[i].links;
				}
			}
			success( links );
		} )
		.fail( function( jqXHR, textStatus, errorThrown ) {
			success( [] );
		} );
	}

	/**
	 * Return the existings links of a wiki
	 * @param success function called on success
	 */
	function getItemLinks( itemId, success ) {
		getItem( itemId, function( item ) {
			if( item.sitelinks ) {
				success( item.sitelinks );
			} else {
				success( {} );
			}
		} );
	}
 
	$( document ).ready( init );
} ( mediaWiki, jQuery ) );