User:DannyS712 test/remind bot.js
Appearance
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. |
![]() | Documentation for this user script can be added at User:DannyS712 test/remind bot. |
(function() {
RemindBot_config = {
name: "[[User:DannyS712/Remind Bot.js|Remind Bot]]",
version: 1.1,
debug: false
};
mw.loader.using( 'mediawiki.util', function () {
importScript('User:DannyS712 test/JSON.js');
$(document).ready( function () {
mw.util.addPortletLink( 'p-cactions', null, 'Remind BOT', 'ca-BOT-reminder', 'Check reminders');
$('#ca-BOT-reminder').click( function ( event ) {
event.preventDefault();
check_reminders();
} );
} );
} );
function check_reminders(){
var current_reminders = get_JSON( 'User:DannyS712 test/reminders.json' );
if (RemindBot_config.debug) console.log( current_reminders );
var pages = get_page_list();
var new_reminders = all_reminders( pages );
var actual_current = remove_old( current_reminders, new_reminders );
new_reminders = remove_current( actual_current, new_reminders );
var as_string = JSON.stringify( actual_current.concat( new_reminders ) , null, 2 );
if (RemindBot_config.debug) console.log( as_string );
set_JSON( 'User:DannyS712 test/reminders.json', as_string, 'update', 'Reminders updated' );
}
function get_page_list(){
/**
var get_pages = {
action: 'query',
list: 'categorymembers',
cmlimit: 'max',
cmtitle: 'Category:Wikipedians recieving reminders',
cmprop: 'title',
format: 'json'
};
var result = null;
var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';
$.ajax({
url: scriptUrl,
type: 'get',
data: get_pages,
dataType: 'json',
async: false,
success: function(catResponse) {
console.log( catResponse );
var pages = catResponse.query.categorymembers;
var listed = [];
for (var i = 0; i < pages.length; i++) {
if ( pages[i].ns === 2 ) {
listed.push(pages[i].title + "/remind.json");
}
}
console.log( listed );
if ( listed.length <= 0 ) {
alert( "There are no users recieving reminders." );
}
else {
result = listed;
}
}
});***/
var result = (['User:DannyS712 test/remind.json', 'User:DannyS712 test/remind 2.json']);
if (RemindBot_config.debug) console.log( result );
return result;
}
function all_reminders( pages ){
var all_reminders = [];
for (var iii = 0; iii < pages.length; iii++){
var user_reminders = get_JSON(pages[iii]);
if (RemindBot_config.debug) console.log( user_reminders );
for (var jjj = 0; jjj < user_reminders.length; jjj++){
var reminder = user_reminders[jjj];
if (reminder.page == "demo") continue;
else {
if (RemindBot_config.debug) console.log( reminder );
all_reminders.push( {user: pages[iii], start: reminder.start, id: reminder.id, reminded: false} );
}
}
}
if (RemindBot_config.debug) console.log( all_reminders );
return all_reminders;
}
function remove_current( actual_current, new_reminders ){
console.log( "current:", actual_current);
console.log( "new:", new_reminders);
for (var iii = 0; iii < new_reminders.length; iii++){
for (var jjj = 0; jjj < actual_current.length; jjj++){
if (actual_current[jjj].id == new_reminders[iii].id){
if (RemindBot_config.debug) console.log( actual_current[jjj], new_reminders[iii] );
new_reminders.splice(iii, 1);
iii--;
break;
}
}
}
if (RemindBot_config.debug) console.log( new_reminders );
return new_reminders;
}
function remove_old( current, new_reminders ){
console.log( current, new_reminders );
var is_old = true;
for (var mmm = 1; mmm < current.length; mmm++){
is_old = true;
for (var nnn = 0; nnn < new_reminders.length; nnn++){
if (current[mmm].id == new_reminders[nnn].id){
is_old = false;
break;
}
}
if (is_old){
if (RemindBot_config.debug) console.log( current[mmm]);
current.splice(mmm, 1);
mmm--;
}
}
if (RemindBot_config.debug) console.log(current);
return current;
}
})();