Questa pagina definisce alcuni parametri di aspetto e comportamento generale di tutte le pagine. Per personalizzarli vedi Aiuto:Stile utente.


Nota: dopo aver salvato è necessario pulire la cache del proprio browser per vedere i cambiamenti (per le pagine globali è comunque necessario attendere qualche minuto). Per Mozilla / Firefox / Safari: fare clic su Ricarica tenendo premuto il tasto delle maiuscole, oppure premere Ctrl-F5 o Ctrl-R (Command-R su Mac); per Chrome: premere Ctrl-Shift-R (Command-Shift-R su un Mac); per Konqueror: premere il pulsante Ricarica o il tasto F5; per Opera può essere necessario svuotare completamente la cache dal menù Strumenti → Preferenze; per Internet Explorer: mantenere premuto il tasto Ctrl mentre si preme il pulsante Aggiorna o premere Ctrl-F5.

/*
 * Inserisce il template {{tradotto da}} ricavando automaticamente
 * i dati da wikidata e dalla wiki di origine. La lingua viene ricavata dagli
 * ultimi due caratteri che precedono il cursore.
 * 
 * Shortcut: Ctrl + y
 * 
 * @author [[Utente:Tino]]
 */
(function (mw, $) {
    'use strict';
    
    var inputArea = $('#wpTextbox1');
    
    var keystrokeDefault = true;
    
    $(function() {
        inputArea.keydown(function(e) {
            if (!e.altKey && !e.shiftKey && !e.metaKey && 
                    e.ctrlKey && e.keyCode === 89 /* "y" key */ &&
                    mw.config.get('wgNamespaceNumber') === 1 /* talkpage only */) {
                
                // get language from the input in the textarea (last 2 chars)
                var cursorPosition = $(e.target).textSelection('getCaretPosition'),
                    lang = inputArea.val().substring(cursorPosition - 2, cursorPosition);                               
            
                var wiki = lang + 'wiki';
                
                keystrokeDefault = false;
            
                var page = mw.config.get('wgTitle');
                
                // query wikidata to get the title in the other language
                $.ajax({
                    url: '//www.wikidata.org/w/api.php',
                    data: {
                        'format': 'json',
                        'action': 'wbgetentities',
                        'sites': mw.config.get('wgDBname'),
                        'titles': page,
                        'props': 'sitelinks',
                        'languages': lang
                    },
                    dataType: 'jsonp',
                    success: function(data) {
                        
                        var index;
                        
                        for (index in data.entities) {
                            var title = data.entities[index].sitelinks[wiki].title;
                            
                            // query the other wikipedia to get revision info
                            $.ajax({
                                url: '//' + lang + '.wikipedia.org/w/api.php',
                                data: {
                                    'format': 'json',
                                    'action': 'query',
                                    'titles': title,
                                    'prop': 'revisions'
                                },
                                dataType: 'jsonp',
                                success: function(voiceData) {
                                    
                                    // regex to extract the date from a timestamp
                                    var dateRegex = /([0-9]{4})-([0-9]{2})-([0-9]{2}).*/;
                                    
                                    var monthNames = [
                                        'gennaio',   'febbraio', 'marzo',    'aprile',
                                        'maggio',    'giugno',   'luglio',   'agosto',
                                        'settembre', 'ottobre',  'novembre', 'dicembre'
                                    ];                                    
                                    
                                    // text to be added after the cursor position
                                    var text = lang + '|';
                                    
                                    for (index in voiceData.query.pages) {
                                        var page = voiceData.query.pages[index];
                                        
                                        // extract date fields
                                        var match = dateRegex.exec(page.revisions[0].timestamp);
                                        
                                        // add voice title
                                        text += page.title + '|';
                                        // add date as: day monthname year
                                        text +=   match[3] + ' ' 
                                                + monthNames[match[2] - 1] + ' '
                                                + match[1] + '|';
                                        // add revid
                                        text += page.revisions[0].revid;
                                        // close template
                                        text += '}}'
                                        
                                        // insert template completion
                                        inputArea.textSelection('setSelection', {
                                            start: cursorPosition - 2,
                                            end: cursorPosition
                                        });
                                        inputArea.textSelection('encapsulateSelection', {
                                            peri: text,
                                            replace: true
                                        });
                                    }
                                }   
                            });
                        }
                    }
                });
            }
        });
        
        // prevent default browser action only when needed
        inputArea.keypress(function(e) {
            if (!e.altKey && !e.shiftKey && !e.metaKey && 
                    e.ctrlKey && e.keyCode === 89 /* "y" key */ ) {
                return keystrokeDefault;
            }
        });
    });
}(mediaWiki, jQuery));