MediaWiki:Gadget-edit-conflict-prevention.js
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");
var periodicalUpdates;
// Checking is page is editable
if(!mw.config.get("wgIsProbablyEditable")) { throw 'Page cannot be edited.'; } // 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