Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;

Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.
mw.loader.using('mediawiki.util', function () {

/*
===Defaultsort===
*/
function defaultsort() {
    var txt = document.editform.wpTextbox1;
    var sortkey = '';
    if (txt.value.match(/\{\{\s*DEFAULTSORT/)) {
        alert('There\'s already a DEFAULTSORT statement!');
        return;
    } 
    // create an array of all category links
    var catlinks = txt.value.match(/\[\[\s*[Cc]at[eé]gor(y|ie)\s*:.*\]\]/ig);
    if (!catlinks) {
        alert('There are no category links!');
        return;
    } 

    for (i=0; i< catlinks.length; i++) {
        // extract the name of the category
        var catname = catlinks[i].replace(/.+\s*:\s*([^|]*[^|\s])\s*(\|.*)?\]\]/,"$1");
        // extract the sort key 
        var match = /\|[^\]]+/.exec(catlinks[i]);
        if (match == null) {
            alert('Category '+catname+' doesn\'t include a sort key!'); 
            return;
        }
        // strip the '|' and any trailing spaces
        sk = match[0].replace(/\|(.+)/, "$1").replace(/(\S) +$/,"$1");
        if (i == 0) {
            sortkey = sk;
        } else {
            if (sortkey != sk) {
                alert('Not all sort keys are identical!\nThe sort key for category '+catname+' is different.');
                return;
            }
        }
    }
    // Now that we know that everything is OK, we can proceed with modifying the content of the editbox
    txt.value=txt.value.replace(catlinks[0], "{{DEFAULTSORT:"+sortkey+"}}\n"+catlinks[0]);
    for (i=0; i< catlinks.length; i++) {
        subst = catlinks[i].replace(/\|[^\]]+/,"");
        txt.value=txt.value.replace(catlinks[i], subst);
    }
}

$(function () {
  if(document.forms.editform) {
    mw.util.addPortletLink('p-cactions', 'javascript:defaultsort()', 'defaultsort', 'ca-defaultsort', 'Edits the category links to make use of the DEFAULTSORT keyword', '', document.getElementById('ca-purge'));
  }
});

});