User:AramilFeraxa/QuickProtect.js
Appearance
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
var QuickProtect = {
createFieldSet: function () {
document.getElementsByTagName("h1")[0].textContent = "Quick protect/unprotect pages";
document.title = "Quick protect/unprotect pages";
jQuery('<div/>', {
id: 'QP-container',
style: 'max-width: 750px; border: 1px solid #CCC; text-align:center; border-radius: 10px; padding: 15px; margin: 0 auto;'
}).appendTo('#mw-content-text');
var QP_fieldset = new OO.ui.FieldsetLayout({
label: 'Quick Protect',
classes: ['container']
});
var QP_input_pages = new OO.ui.MultilineTextInputWidget({
placeholder: 'Page names (one per line)',
multiline: true,
rows: 15,
"class": "QP-input",
id: 'QP-pages',
required: true,
validate: 'non-empty'
});
var QP_select_action = new OO.ui.DropdownWidget({
menu: {
items: [
new OO.ui.MenuOptionWidget({
data: 'protect',
label: 'Protect'
}),
new OO.ui.MenuOptionWidget({
data: 'unprotect',
label: 'Unprotect'
})
]
}
});
var QP_input_reason = new OO.ui.TextInputWidget({
placeholder: 'Reason',
"class": "QP-input",
id: 'QP-reason',
required: true,
validate: 'non-empty'
});
var QP_input_protection_level = new OO.ui.DropdownWidget({
menu: {
items: [
new OO.ui.MenuOptionWidget({
data: 'sysop',
label: 'Sysop'
}),
new OO.ui.MenuOptionWidget({
data: 'autoconfirmed',
label: 'Autoconfirmed'
}),
new OO.ui.MenuOptionWidget({
data: 'none',
label: 'None'
})
]
}
});
var QP_input_expiry = new OO.ui.DropdownWidget({
menu: {
items: [
new OO.ui.MenuOptionWidget({
data: 'infinite',
label: 'Infinite'
}),
new OO.ui.MenuOptionWidget({
data: '1 day',
label: '1 day'
}),
new OO.ui.MenuOptionWidget({
data: '1 week',
label: '1 week'
}),
new OO.ui.MenuOptionWidget({
data: '1 month',
label: '1 month'
}),
new OO.ui.MenuOptionWidget({
data: '3 months',
label: '3 months'
}),
new OO.ui.MenuOptionWidget({
data: '6 months',
label: '6 months'
}),
new OO.ui.MenuOptionWidget({
data: '1 year',
label: '1 year'
})
]
}
});
var QP_reason_field = new OO.ui.FieldLayout(QP_input_reason, {
label: 'Reason',
align: 'top',
id: 'QP-reason'
});
var QP_level_field = new OO.ui.FieldLayout(QP_input_protection_level, {
label: 'Protection level',
align: 'top',
id: 'QP-protection-level'
});
var QP_expiry_field = new OO.ui.FieldLayout(QP_input_expiry, {
label: 'Expiry',
align: 'top',
id: 'QP-expiry'
});
QP_reason_field.$element.hide();
QP_level_field.$element.hide();
QP_expiry_field.$element.hide();
QP_select_action.getMenu().on('choose', function (item) {
var selectedAction = item?.getData();
if (selectedAction === 'protect') {
QP_reason_field.$element.fadeIn();
QP_level_field.$element.fadeIn();
QP_expiry_field.$element.fadeIn();
} else if (selectedAction === 'unprotect') {
QP_reason_field.$element.fadeIn();
QP_level_field.$element.fadeOut();
QP_expiry_field.$element.fadeOut();
} else {
QP_reason_field.$element.fadeOut();
QP_level_field.$element.fadeOut();
QP_expiry_field.$element.fadeOut();
}
});
QP_fieldset.addItems([
new OO.ui.FieldLayout(QP_input_pages, {
label: 'Page names',
align: 'top',
}),
new OO.ui.FieldLayout(QP_select_action, {
label: 'Action',
align: 'top'
}),
QP_reason_field,
QP_level_field,
QP_expiry_field
]);
$('#QP-container').append(QP_fieldset.$element);
var QP_button_execute = new OO.ui.ButtonInputWidget({
type: 'submit',
useInputTag: true,
id: 'QP-execute-button',
label: 'Execute Action',
flags: [
'primary'
]
});
$('#QP-container').append(QP_button_execute.$element);
jQuery('<div/>', {
id: 'QP-message-container',
style: 'width:100%; margin:5px;'
}).appendTo('#QP-container');
console.log("QP: Utworzono formularz");
$(document).on('click', '#QP-execute-button', function () {
var pages = QP_input_pages.getValue().trim().split('\n');
var action = QP_select_action.getMenu().findSelectedItem()?.getData();
var reason = QP_input_reason.getValue();
var protectionLevel = QP_input_protection_level.getMenu().findSelectedItem()?.getData();
var expiry = QP_input_expiry.getMenu().findSelectedItem()?.getData();
console.log("QP: Rozpoczynam wykonanie akcji: " + action + " dla stron: ", pages);
QuickProtect.executeAction(pages, action, reason, protectionLevel, expiry);
});
},
executeAction: function (pages, action, reason, protectionLevel, expiry) {
$('#QP-message-container').empty();
pages.forEach(function (page) {
if (action === 'protect') {
QuickProtect.protectPage(page, reason, protectionLevel, expiry);
} else if (action === 'unprotect') {
QuickProtect.unprotectPage(page, reason);
}
});
},
protectPage: function (page, reason, protectionLevel, expiry) {
var protectParams = {
action: 'protect',
title: page,
protections: 'edit=' + protectionLevel,
expiry: expiry,
reason: reason,
token: mw.user.tokens.get('csrfToken'),
format: 'json'
};
$.ajax({
url: mw.util.wikiScript('api'),
type: 'POST',
data: protectParams,
success: function (data) {
console.log("QP: Zabezpieczono stronę: " + page);
QuickProtect.displayMsg("Protected: " + page, 'success');
},
error: function (xhr, status, error) {
console.error("QP: Błąd podczas zabezpieczania strony " + page + ": " + error);
QuickProtect.displayMsg("Error while protecting: " + page + ": " + error, 'error');
}
});
},
unprotectPage: function (page, reason) {
var unprotectParams = {
action: 'protect',
title: page,
protections: '',
reason: reason || 'Mass unprotection using [[meta:User:AramilFeraxa/QuickProtect.js|QuickProtect tool]]',
token: mw.user.tokens.get('csrfToken'),
format: 'json'
};
$.ajax({
url: mw.util.wikiScript('api'),
type: 'POST',
data: unprotectParams,
success: function (data) {
console.log("QP: Odbezpieczono stronę: " + page);
QuickProtect.displayMsg("Unprotected: " + page + ": " + reason, 'success');
},
error: function (xhr, status, error) {
console.error("QP: Błąd podczas odbezpieczania strony " + page + ": " + error);
QuickProtect.displayMsg("Error while unprotecting: " + page + ": " + error, 'error');
}
});
},
displayMsg: function (QP_msg, QP_msgtype) {
$('#QP-message-container').css({
'border-top': '1px solid #EEE',
'padding-top': '5px',
'margin-top': '5px'
});
var msgbox = document.createElement('div');
msgbox.style.width = "90%";
msgbox.style.margin = "0 auto";
msgbox.style.marginBottom = "5px";
msgbox.style.padding = "2px 5px 2px 20px";
msgbox.style.borderRadius = "0.2em";
msgbox.style.textAlign = "left";
if (QP_msgtype == 'error') {
msgbox.style.backgroundColor = "#FF9494";
msgbox.style.border = "2px solid #CB4154";
msgbox.innerHTML = "<b>ERROR:</b> ";
}
if (QP_msgtype == 'success') {
msgbox.style.backgroundColor = "#93DB70";
msgbox.style.border = "2px solid #659D32";
msgbox.innerHTML = "<b>OK:</b> ";
}
msgbox.innerHTML += QP_msg;
$(msgbox).appendTo('#QP-message-container');
}
};
$(document).ready(function () {
mw.loader.using(['oojs-ui', 'mediawiki.util']).done(function () {
if (mw.config.get('wgNamespaceNumber') == -1 && mw.config.get('wgTitle').toLowerCase() == "qp" && ((mw.config.get("wgGlobalGroups").indexOf("global-sysop") >= 0) || (mw.config.get("wgGlobalGroups").indexOf("steward") >= 0) || (mw.config.get("wgUserGroups").indexOf("sysop") >= 0))) {
$('#mw-content-text').empty();
var QP_p = document.createElement('p');
QP_p.innerHTML = "QuickProtect (QP) tool allows you to quickly protect or unprotect pages.";
$('#mw-content-text').append(QP_p);
QuickProtect.createFieldSet();
}
});
});