MediaWiki:Gadget-edit-conflict-prevention.js

De Wikimedica

Note : après avoir enregistré vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

// Code pour l'Extension pour éviter les conflits d'édition ici (Gestion:Tâches/Liste/291).
// Ce code sera déplacé dans l'extension une fois finalisé.
console.log("Loading edit-conflict-prevention");

// Checking is page is editable
//if(!mw.config("wgIsProbablyEditable")) { return; } // User cannot edit page, don't bother with the rest of the logic.

// Function to check whether a page is currently being edited by another user
//function isEditing(){}

// Function to alert the current editor that he is not the sole current editor anymore 
function alertEditor(){
	console.log("Other editor(s) have been notified there is a new editor!");
	// API request to send message to other editor(s)
	ve.init.target.getSurface().getModel().off('undoStackChange', alertEditor);
	// Send periodical updates to backend
	setInterval(sendUpdates(false), 120000);
}

// Function to get relevant edition parameters
function getParameters(saved){
	const d = new Date();
	let time = 42; //d.getTime();
	let userId = mw.config.get('wgUserId'); // ALTERNATIVELY: mw.user.getId();
	let userName = mw.config.get('wgUserName');
	let articleId = mw.config.get('wgArticleId');
	let pageName = mw.config.get('wgPageName');
	let params = {saved: saved, time: time, userId: userId, userName: userName, articleId: articleId, pageName: pageName};
	
	console.log(saved);	
	console.log(d.toTimeString());
	console.log(userName);
	console.log(pageName);
	
	return params;
}

// Function to updates to backend
function sendUpdates(saved){
	let params = getParameters(saved);
	// Send updates to backend
}

// Function to save updates to backend
function sendSaveUpdate(){
	console.log('Changes has been saved!');
	sendUpdates(true);
}

// Hook checking is visual editor is being used
mw.hook( 've.activationComplete' ).add( function () {
    console.log("Activated!");
    
    // Backend API request checking if page is being edited
    // response = isEditing(page);
    // status = response.status;
    // user = response.user;
    
    var status = true;
    var user = "Un utilisateur";
    if (status ===  true){
    	// If page is being edited, prompt user with modal
    	var options = {title: user + 'modifie déjà cette page!'};
    	OO.ui.confirm('Vos modifications pourraient créer des conflits. Êtes-vous sûr de vouloir continuer?', options  ).done( function ( confirmed ) { // SUGGESTION: Use ve alert system instead?
			if ( confirmed ) {
				console.log( 'User clicked "OK"!' );
				// Disable saving;
				ve.init.articleTarget.toolbarSaveButton.disabled = true;
				$('.oo-ui-tool-name-showSave').removeClass('oo-ui-widget-enabled');
				$('.oo-ui-tool-name-showSave').addClass('oo-ui-widget-disabled'); 
				ve.init.articleTarget.saveDialogIsOpening = true; // Prevents Alt-Maj-S
				
				// OR?
				
				// Alert current editor(s) there is a new editor if changes are being made
				ve.init.target.getSurface().getModel().on('undoStackChange', alertEditor);
				
				// Add callback to alert backend when changes has been saved
				ve.init.articleTarget.on('save', sendSaveUpdate);
				
				// Listen to API to know if new users are editing or are done editing
				// IF USER ARE EDITING
					var concurrentEditors = ["Un autre utilisateur"];
					var alertMessage;
					if (concurrentEditors.length === 1){
						alertMessage = 'Attention! ' + concurrentEditors + 'est également en train de modifier cette page.';
					} else {
						alertMessage = 'Attention! ' + concurrentEditors.join([separator = ', ']) + 'sont également en train de modifier cette page.';
					}
					if (false){ // REPLACE FALSE WITH concurrentEditos.length > 0 
						var options = {title: alertMessage};
						OO.ui.alert( 'Vos modifications pourraient générer des conflits.' ).done( function () {
						    console.log( 'User closed the dialog.' );
						} );
					}
						
				
			} else {
				console.log( 'User clicked "Cancel" or closed the dialog.' );
				// Return to regular page
				window.location.replace('https://wikimedi.ca/wiki/' + encodeURI(mw.config.get("wgPageName"))); //ALTERNATIVELY: history.back(); 
			}
    	});
	}
});

// TO DO:
// Hook check if regular editor is being used
// Hook checking if discussion is being updated
// Hook checking is page is being edited with a form