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.
importScript( 'User:Magnus Manske/sdc tool.js' );


mw.loader.using('mediawiki.user', function() { importScript('User:Rillke/RenameLink.js'); });

/*$( function() {
   if(mw.config.get('wgNamespaceNumber') == 6) {
      if(wgTitle.match(/(.*)\.(jpg|jpeg)/gi)) {
          var croplink = "//toolserver.org/~luxo/cropbot/cropbot.php?img="+wgTitle;
          mw.util.addPortletLink("p-tb", croplink, "Crop", "p-crop", "Crop this image");
      }
   }
});*/
/*
(function(mw, $){
    if (mw.config.get( 'wgNamespaceNumber' ) !== 6) return;
    if (mw.config.get( 'wgAction' ) != 'view') return;
 
    $( function () {
    	var title = mw.config.get( 'wgTitle' ).replace(/ /g, '_'),
            croplink = '//tools.wmflabs.org/croptool/?title=' + encodeURIComponent(title);
        mw.util.addPortletLink(
            "p-tb",
            croplink,
            "CropTool",
            "t-crop",
            "Crop this image"
        );
    });
})(mediaWiki, jQuery);
*/

// From: http://www.wikidata.org/wiki/User_talk:Tpt#liens_Wikidata_et_Commons
( 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': {
                                        'Langtext': {
                                                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) {
                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 + '={{w|' + link + '|';
                                        if( title != link ) {
                                                text += title;
                                        }
                                        text += '|' + languageCode + '}}\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);
        }
 
        /**
         * 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 ) );