User:DannyS712 test/patrol.js

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac);
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5;
  • Konqueror: simply click the Reload button, or press F5;
  • Opera users may need to completely clear their cache in Tools→Preferences.
//<nowiki>
$(function (){
var patroller_config = {
	name: '[[User:DannyS712/patrol|patrol.js]]',
	version: 1.1,
	debug: true,
	comment: "BOT: Automatically patrolled"
};
mw.loader.using( [ 'mediawiki.util', 'mediawiki.api' ], function () {
    $(document).ready( function () { 
    	mw.util.addPortletLink ( 'p-cactions', 'javascript:void(0)', 'Get null changes', 'ca-getChanges');
    	mw.util.addPortletLink ( 'p-cactions', 'javascript:void(0)', 'Patrol changes', 'ca-patrolChanges');
    	$('#ca-getChanges').on('click', function() {
        	getChanges();
    	} );
    	$('#ca-patrolChanges').on('click', function() {
        	patrolChanges();
    	} );
    } );
} );
async function getChanges(){
	var initial = await getFiltered();
	var pre = [{
		notes: {
			script: patroller_config.name,
			scriptVersion: patroller_config.version,
			time: parseInt(initial.offset.num),
			listBuild: formatTime(new Date().toISOString()).num,
		}
	}];
	var data = pre.concat( initial.data );
	var offset = initial.offset;
	var iter = {};
	var shouldRun = true;
	var end = getEnd();
	while ( shouldRun ){
		iter = await getFiltered( offset.string );
		console.log( iter );
		data = data.concat( iter.data );
		offset = iter.offset;
		if (offset && offset.num && offset.num < end){
			console.log("All remaining changes have already been analyzed previously");
			shouldRun = false;
		}
		if (!iter.offset) {
			console.log( "Done" );
			shouldRun = false;
		}
	}
	if (patroller_config.debug) console.log( data );
	var stringy = JSON.stringify( data, null, 2 );
	console.log( stringy );
	set_JSON ( 'User:DannyS712 test/patrol.json', stringy, 'Candidates to patrol');
}
async function getFiltered( offset ){
return new Promise((resolve) => {
	var to_send = {
		action: 'query',
		list: 'oldreviewedpages',
		ornamespace: '*',
		orlimit: 500,
		format: 'json',
        formatversion: 2
	};
	if (offset || false ) to_send.orstart = offset;
	if (patroller_config.debug) console.log( to_send );
	$.post( mw.config.get( 'wgScriptPath' ) + '/api.php', to_send, async function( full_data ){ 
		if (patroller_config.debug) console.log( full_data );
		var pages = full_data.query.oldreviewedpages;
		if (patroller_config.debug) console.log( pages );
		var data = [];
		var page, flag;
		for (var iii = 0; iii < pages.length; iii++){
			page = pages[iii];
			if (shouldPatrol( page )) {
				flag = await get_flag( page );
				page.flag = flag;
				data.push( page );
			}
		}
		console.log( data );
		var next_offset = false;
		if (full_data && full_data.continue && full_data.continue.orstart) next_offset = formatTime( full_data.continue.orstart );
		var to_return = {
			data: data,
			offset: next_offset
		};
		resolve(to_return);
	});
});
}
function get_flag( page ){
return new Promise((resolve) => {
	var to_send = {
		action: 'query',
		pageids: page.pageid,
		prop: 'revisions',
		rvlimit: 1,
		rvprop: 'ids|flagged',
		rvstartid: page.stable_revid,
		format: 'json',
        formatversion: 2
	};
	if (patroller_config.debug) console.log( to_send );
	$.post( mw.config.get( 'wgScriptPath' ) + '/api.php', to_send, function( full_data ){ 
		if (patroller_config.debug) console.log( full_data );
		var flag_info = false;
		if (full_data && full_data.query && full_data.query.pages && full_data.query.pages[0] && full_data.query.pages[0].revisions && full_data.query.pages[0].revisions[0] && full_data.query.pages[0].revisions[0].flagged) {
			flag_info = full_data.query.pages[0].revisions[0].flagged;
		}
		resolve(flag_info);
	});
});
}
function shouldPatrol( page ){
	if (page.diff_size === 0){
		if (checkChange( page.stable_revid, page.revid )){
			console.log("Will patrol:", page.title);
			return true;
		}
	}
	return false;
}
function checkChange( oldid, newid ){
	var old_content = get_page( oldid );
	var new_content = get_page( newid );
	//console.log( old_content, new_content );
	if (old_content === new_content) return true;
	return false;
}
function get_page( old_id ){
	var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';
	//console.log( old_id );
	var page_to_get = {
        action: 'parse',
        oldid: old_id,
        prop: 'wikitext',
        format: 'json',
        formatversion: 2
    };
    var result = null;
	$.ajax({
		url: scriptUrl,
		type: 'get',
		data: page_to_get,
		dataType: 'json',
		async: false,
		success: function(page) {
			//console.log( page );
			var text = page.parse.wikitext;
			//console.log( text );
			result = text;
		} 
	});
	return result;
}
function patrolChanges(){
	var list = get_JSON('User:DannyS712 test/patrol.json');
	if (patroller_config.debug) console.log( list );
	var page = {};
	for (var iii = 1; iii < list.length; iii++){ // Start from 1 because 0 is notes
		page.revid = list[iii]["revid"];
		//page.accuracy = list[iii]["flag"]["tags"]["accuracy"];
		page.value = list[iii]["flag"]["tags"]["value"]
		console.log( page );
		doPatrolFromId( page );
	}
}
function doPatrolFromId( page ){
	var to_send = {
		action: 'review',
		revid: page.revid,
		//flag_accuracy: page.accuracy,
		flag_value: page.value,
		comment: patroller_config.comment,
		token: mw.user.tokens.get( 'csrfToken' ),
		format: 'json'
	};
	if (patroller_config.debug) console.log( to_send );
	$.post( mw.config.get( 'wgScriptPath' ) + '/api.php', to_send, function( response ){ 
		console.log( response );
	} );
}
function getEnd(){
	var list = get_JSON('User:DannyS712 test/patrol.json');
	//console.log( list );
	var end = 0;
	if (list && list[0] && list[0].notes && list[0].notes.time) end = parseInt(list[0].notes.time);
	console.log( end );
	return end;
}
JSON_config = {
	name: "[[User:DannyS712/JSON.js|JSON]]",
	version: 1.0,
	debug: false,
	manual: false
};
function get_JSON ( page ){
	if (JSON_config.debug) console.log( location );
	var get_page = {
        action: 'query',
        titles: page,
        prop: 'revisions',
        rvprop: 'content',
        format: 'json',
        formatversion: 2
    };
    var result = null;
	var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';
	$.ajax({
		url: scriptUrl,
		type: 'get',
		data: get_page,
		dataType: 'json',
		async: false,
		success: function(data) {
		    var text = data.query.pages["0"].revisions["0"].content;
	    	if (JSON_config.debug) console.log ( data );
	    	if (JSON_config.debug) console.log( "Text:", text, "End text" );
	    	var JSONed = JSON.parse( text );
	    	var arr_JSONed = [];
	    	for (var reminder in JSONed){
	    		arr_JSONed.push(JSONed[reminder]);
	    	}
	    	if (JSON_config.debug) console.log( arr_JSONed );
	    	result = arr_JSONed;
		} 
	});
	return result;
}
function set_JSON ( page, new_content, edit_summary, on_success ){
	if (JSON_config.debug) console.log( page, new_content, edit_summary );
	new mw.Api().postWithEditToken( {
		action: 'edit',
		title: page,
		text: new_content,
		summary: edit_summary
	}).done( on_success );
}
function formatTime( as_string ){
	var time = {};
	time.string = as_string;
	time.num = parseInt(as_string.replace(/[\-T:]|\.\d{3}Z/g, ''));
	console.log( time );
	return time;
}
});
//</nowiki>