Difference between revisions of "User:BecaBot/common.js"
(Cleanup. Use custom array method to iterate over the pages.) |
m (I have this as ES6 in my drafts...) |
||
Line 33: | Line 33: | ||
} | } | ||
const self = this.slice(); | const self = this.slice(); | ||
− | + | var remaining = self.length; | |
− | + | var index = 0; | |
const execute = function( item, interval ) { | const execute = function( item, interval ) { | ||
if ( remaining-- ) { | if ( remaining-- ) { | ||
− | callback.call( thisArg, item, index++, | + | callback.call( thisArg, item, index++, self ); |
} else { | } else { | ||
clearInterval(interval); | clearInterval(interval); |
Revision as of 11:43, 13 March 2018
/**
* Redirect script.
* @description:
* Redirect Template:Set list card numbers to the respective page.
* @author:
* [[User:Becasita]]
*/
(function(window, $, mw, console) {
"use strict";
/**
* Global objects.
*/
var DELAYTIME;
const LAST_LOG = '00:00, 13 March 2018 (UTC)';
const conf = mw.config.get([
'wgNamespaceNumber',
'wgPageName'
]);
conf.API = function(query, callbacks, method) {
return new mw.Api()[method || 'get'](query)
.done(callbacks.done)
.fail(callbacks.fail)
.always(callbacks.always);
};
const $rightNavUl = $('#right-navigation').find('#p-views').find('ul');
/** Extend Array prototype: */
Array.prototype.delayedEach = function ( delay, callback, thisArg ) {
if ( typeof delay === 'function' ) {
[ delay, callback, thisArg ] = [ 10, delay, callback ];
}
const self = this.slice();
var remaining = self.length;
var index = 0;
const execute = function( item, interval ) {
if ( remaining-- ) {
callback.call( thisArg, item, index++, self );
} else {
clearInterval(interval);
}
};
execute( self[index] );
const interval = window.setInterval( function() {
execute( self[index], interval );
}, delay );
};
/**
* Create button.
*/
const $redirectButton = $('<li>', {
id: 'ca-redirect',
class: 'collapsible',
html: $('<span>', {
html: $('<a>', {
href: '#',
title: 'Create redirects.',
text: 'Redirect'
})
})
});
// Append:
if(conf.wgNamespaceNumber === 3006) {
$rightNavUl.append($redirectButton);
}
/**
* Functions.
*/
// Edit function:
function edit(pageName, redirectTo) {
const description = 'Redirected page to [[' + redirectTo + ']].';
const text = '#REDIRECT [[' + redirectTo + ']] {{R from card number}}';
console.log('%cEdit start!', 'color: blue;');
console.log('\tEditing:', pageName);
const query = {
action: 'edit',
title: pageName,
createonly: true,
text: text,
summary: 'Creating redirects: ' + description,
bot: true,
token: mw.user.tokens.get('editToken')
};
const callbacks = {
done: function() {
console.log('%cEdit success!', 'color: green;');
console.log('\tDescription:', description);
console.log('\tContent:', text);
},
fail: function(message, errorObject) {
console.log('%cEdit error!', 'color: red;', '(message, errorObject)');
console.log(message);
console.log(errorObject);
},
always: function() {
console.log('%cEdit finished!', 'color: blue;');
console.log('\tEdited:', pageName);
console.log('%c--------------', 'color: blue;');
}
};
const method = 'post';
conf.API(query, callbacks, method);
}
// Gets a map (JSON-like) with the pages to be redirected
// and to which pages they should be redirected.
function getRedirectsMap(rawContent) {
const regex = /^ *([\d\-\w]{5,10}) *; *(.*?)(;|$)/gm;
const redirectsMap = {};
rawContent.replace(regex, function(match, $1, $2) {
redirectsMap[$1.trim()] = $2.trim();
});
return redirectsMap;
}
// Create the redirects:
function createRedirects(redirectsMap) {
Object.keys(redirectsMap).delayedEach(DELAYTIME, function(page) {
console.log('%c--------------', 'color: blue;');
edit(page, redirectsMap[page]);
});
}
// Get content function:
function getContent() {
const query = {
action: 'query',
redirects: true,
prop: 'revisions',
rvprop: 'content',
format: 'json',
formatversion: 2,
titles: conf.wgPageName
};
const callbacks = {
done: function(data) {
createRedirects(getRedirectsMap(data.query.pages[0].revisions[0].content));
},
fail: function(message, errorObject) {
console.log('%cContent fetch error!', 'color: red;', '(message, errorObject)');
console.log(message);
console.log(errorObject);
},
always: function() {
console.log('Content fetched!');
}
};
conf.API(query, callbacks);
}
// Init:
function init() {
DELAYTIME = prompt('Delay time between edits:') || 2000;
console.log('----------------------------');
console.log('Starting creating redirects!');
getContent();
}
/**
* Events.
*/
// Click:
$redirectButton.click(function(event) {
event.preventDefault();
init();
});
/**
* Return.
*/
console.log('%cRedirect script loaded: ' + LAST_LOG, 'color: blue;');
})(window, window.jQuery, window.mediaWiki, window.console);