« MediaWiki:Gadget-edit-conflict-prevention.js » : différence entre les versions

De Wikimedica
(On/Off -> Once)
(Draft conflict prevention for ve, debugged)
Ligne 5 : Ligne 5 :


// Checking is page is editable
// Checking is page is editable
//if(!mw.config("wgIsProbablyEditable")) { return; } // User cannot edit page, don't bother with the rest of the logic.
if(!mw.config.get("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 to check whether a page is currently being edited by another user
//function isEditing(){}
//function isEditing(){}


// Function to alert the current editor that he is not the sole current editor anymore  
// Function to alert the current editor(s) that he/they is/are not the sole current editor(s) anymore  
function alertEditor(){
function alertEditor(){
console.log("Other editor(s) have been notified there is a new editor!");
console.log("Other editor(s) have been notified there is a new editor!");
Ligne 16 : Ligne 16 :
// Send periodical updates to backend
// Send periodical updates to backend
sendEditingUpdates(); // First call is immediate
periodicalUpdates = setInterval(sendEditingUpdates, 120000);
periodicalUpdates = setInterval(sendEditingUpdates, 120000);
}
}


// Function to get relevant edition parameters
// Function to get relevant editing parameters
function getParameters(saved){
function getParameters(saved){
var d = new Date();
var d = new Date();
Ligne 37 : Ligne 38 :
}
}


// Function to updates to backend
// Function to send updates to backend
function sendUpdates(saved){
function sendUpdates(saved){
var params = getParameters(saved);
var params = getParameters(saved);
Ligne 43 : Ligne 44 :
}
}


// Function to save updates to backend
// Function to send editing updates to backend
function sendEditingUpdates(){
function sendEditingUpdates(){
sendUpdates(false);
sendUpdates(false);
}
}


// Function to save updates to backend
// Function to send save updates to backend
function sendSaveUpdate(){
function sendSaveUpdate(){
console.log('Changes has been saved!');
console.log('Changes has been saved!');
Ligne 86 : Ligne 87 :
ve.init.target.getSurface().getModel().once('undoStackChange', alertEditor);
ve.init.target.getSurface().getModel().once('undoStackChange', alertEditor);
// Add callback to alert backend if editor has been closed
mw.hook( 've.deactivationComplete' ).add(sendSaveUpdate);
// NOT NECESSARY (Deactivation takes care of this case)
// Add callback to alert backend when changes has been saved
// Add callback to alert backend when changes has been saved
ve.init.articleTarget.on('save', sendSaveUpdate);
// ve.init.articleTarget.on('save', sendSaveUpdate);
// TO DO: Add callback to alert backend if editor has been closed
mw.hook( 've.deactivationComplete' ).add(sendSaveUpdate);
// Listen to API to know if new users are editing or are done editing
// Listen to API to know if new users are editing or are done editing
Ligne 96 : Ligne 99 :
var concurrentEditors = ["Un autre utilisateur"];
var concurrentEditors = ["Un autre utilisateur"];
var alertMessage;
var alertMessage;
if (concurrentEditors.length === 1){
var options;
alertMessage = 'Attention! ' + concurrentEditors + 'est également en train de modifier cette page.';
if (concurrentEditors.length > 0){
if (concurrentEditors.length === 1){
alertMessage = 'Attention! ' + concurrentEditors + 'est également en train de modifier cette page.';
} else if (concurrentEditors.length > 1) {
alertMessage = 'Attention! ' + concurrentEditors.join([separator = ', ']) + 'sont également en train de modifier cette page.';
}
if (false){ // REMOVE ONCE BACKEND IMPLEMENTED
options = {title: alertMessage};
OO.ui.alert( 'Vos modifications pourraient générer des conflits.', options ).done( function () {
    console.log( 'User closed the dialog.' );
} );
}
} else {
} else {
alertMessage = 'Attention! ' + concurrentEditors.join([separator = ', ']) + 'sont également en train de modifier cette page.';
alertMessage = 'Attention! Vous êtes désormais le seul éditeur en train de modificier cette page.'
}
options = {title: alertMessage};
if (false){ // REPLACE FALSE WITH concurrentEditos.length > 0
var options = {title: alertMessage};
OO.ui.alert( 'Vos modifications pourraient générer des conflits.', options ).done( function () {
OO.ui.alert( 'Vos modifications pourraient générer des conflits.', options ).done( function () {
    console.log( 'User closed the dialog.' );
    console.log( 'User closed the dialog.' );

Version du 21 mai 2022 à 15:00

// 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");
var periodicalUpdates;

// Checking is page is editable
if(!mw.config.get("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(s) that he/they is/are not the sole current editor(s) 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)
	
	// Send periodical updates to backend
	sendEditingUpdates(); // First call is immediate
	periodicalUpdates = setInterval(sendEditingUpdates, 120000);
}

// Function to get relevant editing parameters
function getParameters(saved){
	var d = new Date();
	var time = d.getTime();
	var userId = mw.config.get('wgUserId'); // ALTERNATIVELY: mw.user.getId();
	var userName = mw.config.get('wgUserName');
	var articleId = mw.config.get('wgArticleId');
	var pageName = mw.config.get('wgPageName');
	var 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 send updates to backend
function sendUpdates(saved){
	var params = getParameters(saved);
	// Send updates to backend
}

// Function to send editing updates to backend
function sendEditingUpdates(){
	sendUpdates(false);
}

// Function to send save updates to backend
function sendSaveUpdate(){
	console.log('Changes has been saved!');
	clearInterval(periodicalUpdates);
	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().once('undoStackChange', alertEditor);
				
				// Add callback to alert backend if editor has been closed
				mw.hook( 've.deactivationComplete' ).add(sendSaveUpdate);
				
				// NOT NECESSARY (Deactivation takes care of this case)
				// 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;
					var options;
					if (concurrentEditors.length > 0){
						if (concurrentEditors.length === 1){
							alertMessage = 'Attention! ' + concurrentEditors + 'est également en train de modifier cette page.';
						} else if (concurrentEditors.length > 1) {
							alertMessage = 'Attention! ' + concurrentEditors.join([separator = ', ']) + 'sont également en train de modifier cette page.';
						}
						if (false){ // REMOVE ONCE BACKEND IMPLEMENTED 
							options = {title: alertMessage};
							OO.ui.alert( 'Vos modifications pourraient générer des conflits.', options ).done( function () {
							    console.log( 'User closed the dialog.' );
							} );
						}
					} else {
						alertMessage = 'Attention! Vous êtes désormais le seul éditeur en train de modificier cette page.'
						options = {title: alertMessage};
						OO.ui.alert( 'Vos modifications pourraient générer des conflits.', options ).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