User:Aseleste/Scripts/Purger.js
Appearance
< User:Aseleste | Scripts
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:Aseleste/Scripts/Purger. |
// <nowiki>
/* Purger */
(function() {
/** Configuration */
var config = {
debug: false,
namespace: 'purger',
portletGroup: 'p-cactions',
portletId: 'ca-purger',
portletAccessKey: null,
i18n: {
portletText: 'Purger',
portletTooltip: 'Purge this page',
specialPage: {
url: 'Special:Purger',
documentTitle: 'Purger - Wikipedia',
title: 'Purger: ',
forcelinkupdate: 'forcelinkupdate',
forcerecursivelinkupdate: 'forcerecursivelinkupdate',
mathpurge: 'mathpurge',
purgeButton: 'Purge',
nullButton: 'Null edit',
},
purgeSucceed: 'Purge succeed',
purgeFailed: 'Purge failed',
nullSucceed: 'Null edit succeed',
nullFailed: 'Null edit failed',
nullSummary: '[[User:Aseleste/Script/Purger|Purger]]: null edit',
nullBug: '(If you see this message, undo this edit, notify the editor, and then report to [[User:Aseleste|Aseleste]].)',
},
}
function namespaced(string) { return config.namespace + '-' + string }
function formatMessage(message) { return '[' + config.namespace + '] ' + message }
/** Page source of special page.
* Identifiers (without namespace):
* - purge
* - null
* - forcelinkupdate
* - forcerecursivelinkupdate
* - mathpurge
* Classes (without namespace):
* - purge-options
*/
config.specialPageSource = (function() {
function generatePurgeOption(name) {
return '<input type="checkbox" id="' + namespaced(name) + '" value="' + name + '" class="' + namespaced('purge-options') + '">' +
'<label for="' + namespaced(name) + '">' + config.i18n.specialPage[name] + '</label><br/>'
}
return generatePurgeOption('forcelinkupdate')
+ generatePurgeOption('forcerecursivelinkupdate')
+ generatePurgeOption('mathpurge')
+ '<button id="' + namespaced('purge') + '">' + config.i18n.specialPage.purgeButton + '</button>'
+ '<button id="' + namespaced('null') + '">' + config.i18n.specialPage.nullButton + '</button>'
})()
/* Loading */
$.when(
mw.loader.using(['mediawiki.util']),
$.ready
).then(function() {
if (mw.config.get('wgPageName').startsWith(config.i18n.specialPage.url)) {
// special page
writeSpecialPage()
} else {
// add portlet link
mw.util.addPortletLink(config.portletGroup, mw.util.getUrl(config.i18n.specialPage.url + '/' + mw.config.get('wgPageName')), config.i18n.portletText, config.portletId, config.i18n.portletTooltip, config.portletAccessKey, null);
}
})
function writeSpecialPage() {
var page = mw.config.get('wgPageName').split(config.i18n.specialPage.url + '/')[1]
if (page === undefined) return // no page specified
/* Initialization */
function initialize() {
document.title = config.i18n.specialPage.documentTitle
$('h1').text(config.i18n.specialPage.title + page);
$('#mw-content-text')
.empty()
.append(config.specialPageSource)
$('#' + namespaced('purge')).on('click', doPurge)
$('#' + namespaced('null')).on('click', doNullEdit)
}
/* Actions */
function doNullEdit(event) {
var parameters = {
action: 'edit',
title: page,
summary: (function(){
if (config.debug) return config.i18n.nullSummary
else return config.i18n.nullSummary + ' ' + config.i18n.nullBug
})(),
minor: true,
nocreate: true,
watchlist: 'nochange',
appendtext: '',
}
console.log(formatMessage('POST with CSRF token'))
console.log(parameters)
new mw.Api()
.postWithToken('csrf', parameters)
.then(function(result) {
console.log(formatMessage('Success result'))
console.log(result)
window.location.href = mw.util.getUrl(page)
}, function(result) {
console.log(formatMessage('Failure result'))
console.log(result)
mw.notify(config.i18n.nullFailed, {type: 'error'});
});
event.preventDefault()
}
function doPurge(event) {
var parameters = {
action: 'purge',
titles: page,
}
$('.' + namespaced('purge-options'))
.each(function(index, element) { parameters[element.value] = element.checked })
console.log(formatMessage('POST'))
console.log(parameters)
new mw.Api()
.post(parameters)
.then(function(result) {
console.log(formatMessage('Success result'))
console.log(result)
window.location.href = mw.util.getUrl(page)
}, function(result) {
console.log(formatMessage('Failure result'))
console.log(result)
mw.notify(config.i18n.purgeFailed, {type: 'error'});
});
event.preventDefault()
}
initialize()
}
})()
// </nowiki>