296 lines
11 KiB
JavaScript
296 lines
11 KiB
JavaScript
/**
|
|
* P2P Wiki FR Draft Approval Gadget
|
|
*
|
|
* Adds "Approve & Publish" and "Delete Draft" buttons to pages in the Draft: namespace.
|
|
*/
|
|
|
|
(function() {
|
|
'use strict';
|
|
|
|
// Only run on Draft: namespace pages (namespace 118)
|
|
if (mw.config.get('wgNamespaceNumber') !== 118) {
|
|
return;
|
|
}
|
|
|
|
// Authorized users
|
|
var authorizedUsers = ['JavierRgz', 'MaiaDereva', 'Mbauwens', 'JeffEmmett', 'Mbauwens bot'];
|
|
var currentUser = mw.config.get('wgUserName');
|
|
|
|
if (!authorizedUsers.includes(currentUser)) {
|
|
return;
|
|
}
|
|
|
|
// Wait for the page to be ready
|
|
$(document).ready(function() {
|
|
var pageTitle = mw.config.get('wgPageName');
|
|
var displayTitle = mw.config.get('wgTitle');
|
|
var targetTitle = displayTitle;
|
|
|
|
// Create the approval button container
|
|
var $container = $('<div>')
|
|
.attr('id', 'draft-approval-container')
|
|
.css({
|
|
'background': 'linear-gradient(135deg, #e8f5e9 0%, #c8e6c9 100%)',
|
|
'border': '2px solid #4caf50',
|
|
'border-radius': '8px',
|
|
'padding': '15px 20px',
|
|
'margin': '15px 0',
|
|
'display': 'flex',
|
|
'align-items': 'center',
|
|
'justify-content': 'space-between',
|
|
'flex-wrap': 'wrap',
|
|
'gap': '10px'
|
|
});
|
|
|
|
var $info = $('<div>')
|
|
.css({ 'flex': '1', 'min-width': '200px' })
|
|
.html(
|
|
'<strong style="color: #2e7d32; font-size: 1.1em;">📝 Article Brouillon</strong><br>' +
|
|
'<span style="color: #555;">Cet article est en attente de révision. Approuver pour publier ou supprimer pour rejeter.</span>'
|
|
);
|
|
|
|
var $buttonContainer = $('<div>')
|
|
.css({ 'display': 'flex', 'gap': '10px', 'flex-wrap': 'wrap' });
|
|
|
|
var $approveButton = $('<button>')
|
|
.attr('id', 'approve-draft-btn')
|
|
.text('✓ Approuver & Publier')
|
|
.css({
|
|
'background': '#4caf50',
|
|
'color': 'white',
|
|
'border': 'none',
|
|
'border-radius': '6px',
|
|
'padding': '12px 24px',
|
|
'font-size': '1em',
|
|
'font-weight': 'bold',
|
|
'cursor': 'pointer',
|
|
'transition': 'all 0.2s',
|
|
'box-shadow': '0 2px 4px rgba(0,0,0,0.2)'
|
|
})
|
|
.hover(
|
|
function() { $(this).css('background', '#43a047'); },
|
|
function() { $(this).css('background', '#4caf50'); }
|
|
);
|
|
|
|
var $deleteButton = $('<button>')
|
|
.attr('id', 'delete-draft-btn')
|
|
.text('✗ Supprimer Brouillon')
|
|
.css({
|
|
'background': '#f44336',
|
|
'color': 'white',
|
|
'border': 'none',
|
|
'border-radius': '6px',
|
|
'padding': '12px 24px',
|
|
'font-size': '1em',
|
|
'font-weight': 'bold',
|
|
'cursor': 'pointer',
|
|
'transition': 'all 0.2s',
|
|
'box-shadow': '0 2px 4px rgba(0,0,0,0.2)'
|
|
})
|
|
.hover(
|
|
function() { $(this).css('background', '#d32f2f'); },
|
|
function() { $(this).css('background', '#f44336'); }
|
|
);
|
|
|
|
$buttonContainer.append($approveButton, $deleteButton);
|
|
$container.append($info, $buttonContainer);
|
|
|
|
// Insert at the bottom of content
|
|
var $content = $('#mw-content-text');
|
|
$content.append($container);
|
|
|
|
// Handle the approve click
|
|
$approveButton.on('click', function() {
|
|
if (!confirm(
|
|
'Approuver ce brouillon ?\n\n' +
|
|
'Cela va :\n' +
|
|
'• Déplacer "Draft:' + displayTitle + '" → "' + targetTitle + '"\n' +
|
|
'• Supprimer le modèle {{Draft}}\n' +
|
|
'• Rendre l\'article publiquement visible\n\n' +
|
|
'Continuer ?'
|
|
)) {
|
|
return;
|
|
}
|
|
|
|
$approveButton
|
|
.prop('disabled', true)
|
|
.text('Publication en cours...')
|
|
.css('background', '#9e9e9e');
|
|
$deleteButton.prop('disabled', true);
|
|
|
|
new mw.Api().get({
|
|
action: 'query',
|
|
meta: 'tokens',
|
|
type: 'csrf'
|
|
}).then(function(tokenData) {
|
|
var token = tokenData.query.tokens.csrftoken;
|
|
|
|
return new mw.Api().post({
|
|
action: 'move',
|
|
from: pageTitle,
|
|
to: targetTitle,
|
|
reason: 'Brouillon approuvé par administrateur',
|
|
movetalk: 1,
|
|
noredirect: 1,
|
|
token: token
|
|
});
|
|
}).then(function(moveResult) {
|
|
if (moveResult.error) {
|
|
throw new Error(moveResult.error.info);
|
|
}
|
|
|
|
return new mw.Api().get({
|
|
action: 'query',
|
|
titles: targetTitle,
|
|
prop: 'revisions',
|
|
rvprop: 'content',
|
|
rvslots: 'main'
|
|
});
|
|
}).then(function(contentData) {
|
|
var pages = contentData.query.pages;
|
|
var pageId = Object.keys(pages)[0];
|
|
var content = pages[pageId].revisions[0].slots.main['*'];
|
|
|
|
var newContent = content
|
|
.replace(/\{\{Draft\|[^}]*\}\}\s*\n?/gi, '')
|
|
.replace(/\{\{Draft\}\}\s*\n?/gi, '')
|
|
.replace(/\{\{Brouillon\|[^}]*\}\}\s*\n?/gi, '')
|
|
.replace(/\{\{Brouillon\}\}\s*\n?/gi, '');
|
|
|
|
if (newContent === content) {
|
|
return { noChange: true };
|
|
}
|
|
|
|
return new mw.Api().get({
|
|
action: 'query',
|
|
meta: 'tokens',
|
|
type: 'csrf'
|
|
}).then(function(tokenData2) {
|
|
return new mw.Api().post({
|
|
action: 'edit',
|
|
title: targetTitle,
|
|
text: newContent,
|
|
summary: 'Modèle brouillon supprimé après approbation',
|
|
token: tokenData2.query.tokens.csrftoken
|
|
});
|
|
});
|
|
}).then(function() {
|
|
$container
|
|
.css({
|
|
'background': 'linear-gradient(135deg, #c8e6c9 0%, #a5d6a7 100%)',
|
|
'border-color': '#2e7d32'
|
|
});
|
|
|
|
$container.html(
|
|
'<div style="text-align: center; width: 100%;">' +
|
|
'<strong style="color: #2e7d32; font-size: 1.2em;">✓ Brouillon Approuvé !</strong><br>' +
|
|
'<span style="color: #555;">Redirection vers l\'article publié...</span>' +
|
|
'</div>'
|
|
);
|
|
|
|
setTimeout(function() {
|
|
window.location.href = mw.util.getUrl(targetTitle);
|
|
}, 1500);
|
|
|
|
}).catch(function(error) {
|
|
var errorMsg = error.message || error;
|
|
|
|
$approveButton
|
|
.prop('disabled', false)
|
|
.text('✓ Approuver & Publier')
|
|
.css('background', '#4caf50');
|
|
$deleteButton.prop('disabled', false);
|
|
|
|
$container.after(
|
|
$('<div>')
|
|
.css({
|
|
'background': '#ffebee',
|
|
'border': '1px solid #f44336',
|
|
'border-radius': '6px',
|
|
'padding': '10px 15px',
|
|
'margin': '10px 0',
|
|
'color': '#c62828'
|
|
})
|
|
.html('<strong>Erreur:</strong> ' + errorMsg)
|
|
);
|
|
});
|
|
});
|
|
|
|
// Handle the delete click
|
|
$deleteButton.on('click', function() {
|
|
if (!confirm(
|
|
'SUPPRIMER ce brouillon ?\n\n' +
|
|
'Cela supprimera définitivement "Draft:' + displayTitle + '".\n\n' +
|
|
'Cette action est irréversible !\n\n' +
|
|
'Continuer ?'
|
|
)) {
|
|
return;
|
|
}
|
|
|
|
$deleteButton
|
|
.prop('disabled', true)
|
|
.text('Suppression...')
|
|
.css('background', '#9e9e9e');
|
|
$approveButton.prop('disabled', true);
|
|
|
|
new mw.Api().get({
|
|
action: 'query',
|
|
meta: 'tokens',
|
|
type: 'csrf'
|
|
}).then(function(tokenData) {
|
|
var token = tokenData.query.tokens.csrftoken;
|
|
|
|
return new mw.Api().post({
|
|
action: 'delete',
|
|
title: pageTitle,
|
|
reason: 'Brouillon rejeté par administrateur',
|
|
token: token
|
|
});
|
|
}).then(function(deleteResult) {
|
|
if (deleteResult.error) {
|
|
throw new Error(deleteResult.error.info);
|
|
}
|
|
|
|
$container
|
|
.css({
|
|
'background': 'linear-gradient(135deg, #ffebee 0%, #ffcdd2 100%)',
|
|
'border-color': '#f44336'
|
|
});
|
|
|
|
$container.html(
|
|
'<div style="text-align: center; width: 100%;">' +
|
|
'<strong style="color: #c62828; font-size: 1.2em;">✗ Brouillon Supprimé</strong><br>' +
|
|
'<span style="color: #555;">Redirection vers la page d\'accueil...</span>' +
|
|
'</div>'
|
|
);
|
|
|
|
setTimeout(function() {
|
|
window.location.href = mw.util.getUrl('Accueil');
|
|
}, 1500);
|
|
|
|
}).catch(function(error) {
|
|
var errorMsg = error.message || error;
|
|
|
|
$deleteButton
|
|
.prop('disabled', false)
|
|
.text('✗ Supprimer Brouillon')
|
|
.css('background', '#f44336');
|
|
$approveButton.prop('disabled', false);
|
|
|
|
$container.after(
|
|
$('<div>')
|
|
.css({
|
|
'background': '#ffebee',
|
|
'border': '1px solid #f44336',
|
|
'border-radius': '6px',
|
|
'padding': '10px 15px',
|
|
'margin': '10px 0',
|
|
'color': '#c62828'
|
|
})
|
|
.html('<strong>Erreur:</strong> ' + errorMsg)
|
|
);
|
|
});
|
|
});
|
|
});
|
|
})();
|