Jump to content

User:DannyS712 test/remind bot.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
(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;
}
})();