MediaWiki:Gadget-C helper util.js
Apparence
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) ;
Firefox (sur GNU/Linux) / Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.C.util = {
config: {
'time-before-refresh': 1000,
'notify-network-issue': 'Problème de réseau, rechargez la page et réessayez.',
'notify-gadget-cant-edit': 'Le gadget ne peut pas modifier la page demandée. Merci de faire la modification manuellement.',
help_icon_src: '/media/wikipedia/commons/a/ab/PL_Wiki_CWNJ_ikona.svg',
help_icon_width: 12,
help_icon_class: 'C-help-icon',
array_to_text_separator: ', ',
array_to_text_conjunction: ' et ',
},
reload: function() {
setTimeout(function() {
location.reload();
}, this.config['time-before-refresh']);
},
'new_section': function(page, section_title, section_content, summary, callback) {
if(page === null) {
page = mw.config.get('wgPageName');
}
var params = {
action: 'edit',
title: page,
summary: summary,
text: section_content,
section: 'new',
sectiontitle: section_title,
tags: 'C-helper',
};
new mw.Api().postWithEditToken(params)
.done(function(data) {
callback();
})
.fail(function(jqXHR) {
if (jqXHR === 'no-direct-editing') {
mw.notify(C.util.config['notify-gadget-cant-edit'], {title:'C-helper', type:'error'});
} else {
mw.notify(C.util.config['notify-network-issue'], {title:'C-helper', type:'error'});
}
});
},
prepend: function(page, content, summary, callback) {
this.edit(page, content, "prepend", summary, callback, false);
},
safe_prepend: function(page, content, summary, callback) {
this.edit(page, content, "prepend", summary, callback, true);
},
append: function(page, content, summary, callback) {
this.edit(page, content, "append", summary, callback, false);
},
replace: function(page, content, summary, callback) {
this.edit(page, content, "replace", summary, callback, false);
},
edit: function(page, content, method, summary, callback, nocreate) {
nocreate = nocreate || false;
if(page === null) {
page = mw.config.get('wgPageName');
}
var params = {
action: 'edit',
title: page,
summary: summary,
tags: 'C-helper',
};
if(method == "append")
params.appendtext = content;
else if(method == "prepend")
params.prependtext = content;
else
params.text = content;
if(nocreate)
params.nocreate = 1;
new mw.Api().postWithEditToken(params)
.done(function(data) {
callback();
})
.fail(function(jqXHR) {
mw.notify(C.util.config['notify-network-issue'], {title:'C-helper', type:'error'});
});
},
'construct_help_icon': function(title) {
return $('<span>').append(" ").append($('<img>', {src: this.config.help_icon_src, width: this.config.help_icon_width, title: title}).tooltip({position: {my: "left top", at: "right+8 top-12"}}));
},
'parse_wikicode': function(string) {
var prev;
var reg = new RegExp("\\[\\[([^\\[\\]]+)\\|([^\\[\\]]+)\\]\\]");
do {
prev = string;
string = string.replace(reg, '<a href="'+mw.config.get('wgServer')+mw.config.get('wgArticlePath')+'">$2</a>');
} while(string != prev);
reg = new RegExp("\\[\\[([^\\[\\]]+)\\]\\]");
do {
prev = string;
string = string.replace(reg, '<a href="'+mw.config.get('wgServer')+mw.config.get('wgArticlePath')+'">$1</a>');
} while(string != prev);
return string;
},
'escape_edit_summary': function(summary) {
return summary.replace(/{/g, '{').replace(/}/g, '}').replace(/</g, '<').replace(/\[\[([a-z-]+):/ig, '[[:$1:');
},
message: function(template, $1, $2, $3, $4) {
if (typeof template === 'string') {
var result = template;
if ($1 !== undefined && $1 !== null) {
result = result.replace(/\$1/g, $1);
}
if ($2 !== undefined && $2 !== null) {
result = result.replace(/\$2/g, $2);
}
if ($3 !== undefined && $3 !== null) {
result = result.replace(/\$3/g, $3);
}
if ($4 !== undefined && $4 !== null) {
result = result.replace(/\$4/g, $4);
}
return result;
} else if (typeof template === 'function') {
return template($1, $2, $3, $4);
}
},
// https://stackoverflow.com/questions/16251822/array-to-comma-separated-string-and-for-last-tag-use-the-and-instead-of-comma/56590273#56590273
'array_to_text': function(a) {
var separator = this.config.array_to_text_separator; // ', '
var conjunction = this.config.array_to_text_conjunction; // ' et '
if(a.length <= 2) {
return a.join(conjunction);
} else {
return a.slice(0, -1).join(separator) + conjunction + a[a.length-1];
}
}
};