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 = /\[\[([^\[\]]+)\|([^\[\]]+)\]\]/;
var replacer = function (match, p1, p2) {
return '<a href="'+mw.util.getUrl(p1)+'">' + mw.html.escape(p2) + '</a>';
};
do {
prev = string;
string = string.replace(reg, replacer);
} while(string != prev);
reg = /\[\[([^\[\]]+)\]\]/;
replacer = function (match, p1) {
return '<a href="'+mw.util.getUrl(p1)+'">' + mw.html.escape(p1) + '</a>';
};
do {
prev = string;
string = string.replace(reg, replacer);
} while(string != prev);
return string;
},
'escape_edit_summary': function(summary) {
return summary.replace(/{/g, '{').replace(/}/g, '}').replace(/</g, '<').replace(/>/g, '>').replace(/\[\[([a-z-]+):/ig, '[[:$1:');
},
message: function(template) {
var parameters = Array.prototype.slice.call(arguments, 1);
if (typeof template === 'string') {
return template.replace(/\$(\d+)/g, function (str, match) {
var parameter = parameters[parseInt(match) - 1];
return (parameter !== undefined && parameter !== null) ? parameter : '$' + match;
});
} else if (typeof template === 'function') {
return template.apply(null, parameters);
}
},
// 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];
}
}
};