Aller au contenu

MediaWiki:Gadget-C helper util.js

Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 29 mars 2017 à 21:26 et modifiée en dernier par Orlodrim (discuter | contributions) ("g" manquant dans un remplacement). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.
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.',
	  	help_icon_src: '/media/wikipedia/commons/a/ab/PL_Wiki_CWNJ_ikona.svg',
	  	help_icon_width: 12,
	  	help_icon_class: 'C-help-icon',
	},
	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');
		}
		$.ajax({
			url: mw.util.wikiScript('api'),
			data: {
				action: 'query',
				meta: 'tokens',
				format: 'json',
				type: 'csrf',
			},
			dataType: 'json'
		}).then(function(data) {
			$.ajax({
				url: mw.util.wikiScript('api'),
				method: 'POST',
				data: {
					action: 'edit',
					title: page,
					summary: summary,
					text: section_content,
					section: 'new',
					sectiontitle: section_title,
					tags:'C-helper',
					token: data.query.tokens.csrftoken,
					format: 'json',
				},
				dataType: 'json',
				success: function(data) {
					callback();
				},
				error: function(data) {
    				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');
		}
		$.ajax({
			url: mw.util.wikiScript('api'),
			data: {
				action: 'query',
				meta: 'tokens',
				format: 'json',
				type: 'csrf',
			},
			dataType: 'json'
		}).then(function(data) {
			data = {
				action: 'edit',
				title: page,
				summary: summary,
				tags:'C-helper',
				token: data.query.tokens.csrftoken,
				format: 'json',
			};
			if(method == "append")
				data.appendtext = content;
			else if(method == "prepend")
				data.prependtext = content;
			else
				data.text = content;
			if(nocreate)
				data.nocreate = 1;
			$.ajax({
				url: mw.util.wikiScript('api'),
				method: 'POST',
				data: data,
				dataType: 'json',
				success: function(data) {
					callback();
				},
				error: function(data) {
    				mw.notify(C.util.config['notify-network-issue'], {title:'C-helper', type:'error'});
				},
			});
		});
	},
	'construct_help_icon': function(title) {
		return $('<span>').append("&nbsp;").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 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, '&#123;').replace(/}/g, '&#125;').replace(/</g, '&lt;').replace(/\[\[([a-z-]+):/ig, '[[:$1:');
	}
};