Jump to content

User:DannyS712 test/Abuse filters.js

From Wikipedia, the free encyclopedia
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.
//<nowiki>
$(function (){
var AFQ_config = {
	name: '[[User:DannyS712/Abuse filters query|Abuse filters query.js]]',
	version: 2.0,
	debug: true
};
mw.loader.using( 'mediawiki.util', function () {
    $(document).ready( function () { 
    	mw.util.addPortletLink ( 'p-cactions', 'javascript:void(0)', 'Get abuse filters', 'ca-getFilters');
    	$('#ca-getFilters').on('click', function() {
        	getFilters();
    	} );
    } );
} );
async function getFilters(){
	var initial = await doQuery();
	var data = initial.query.abusefilters;
	var offset, shouldRun;
	if ( initial && initial.continue && initial.continue.abfstartid ) {
		offset = parseInt(iter.continue.abfstartid);
		shouldRun = true;
	} else {
		shouldRun = false;
	}
	var iter = {};
	while ( shouldRun ){
		console.log( data );
		iter = await doQuery( offset );
		console.log( iter );
		data = data.concat( iter.query.abusefilters );
		console.log( data );
		if ( iter && iter.continue && iter.continue.abfstartid ) {
			offset = parseInt(iter.continue.abfstartid);
		} else {
			console.log( "Done" );
			shouldRun = false;
		}
	}
	if (AFQ_config.debug) console.log( data );
	
	parseFilters( data );
}
function doQuery( offset ){
	return new Promise((resolve) => {
		var to_send = {
			action: 'query',
			list: 'abusefilters',
			abfprop: 'id|description|pattern|actions|hits|comments|lasteditor|lastedittime|status|private',
			abfshow: 'enabled',
			abflimit: 500,
			format: 'json',
		};
		if (offset || false ) to_send.abfstartid = offset;
		if (AFQ_config.debug) console.log( to_send );
		$.post( mw.config.get( 'wgScriptPath' ) + '/api.php', to_send, function( full_data ){ 
			if (AFQ_config.debug) console.log( full_data );
			resolve( full_data );
		});
	});
}
function parseFilters( data ){
	console.log( data );
	var filters, conds, usesDeprecated;
	var skip = [];
	var withDeprecated = [];
	var shouldUseEqualsAny = [];
	for ( var iii = 0; iii < data.length; iii++ ){
		filter = data[iii];
		console.log( filter, typeof filter.pattern );
		if ( typeof filter.pattern === 'undefined' ){
			skip = skip.concat( filter );
		} else {
			conds = filter.pattern;
			console.log( conds );
			if ( testDeprecated( conds ) ){
				withDeprecated = withDeprecated.concat( filter );
			}
			if ( testEqualAny( conds ) ) {
				shouldUseEqualsAny = shouldUseEqualsAny.concat( filter );
			}
		}
	}
	console.log( 'Skipped due to not being able to view the pattern: ', skip );
	console.log( 'Using deprecated parameters: ', withDeprecated );
	console.log( 'Should use equals to any: ', shouldUseEqualsAny)
	
	report( skip, withDeprecated, shouldUseEqualsAny );
}
function report( skip, withDeprecated, shouldUseEqualsAny ){
	var skipList = skip.map( function( filter ) { return filter.id; } );
	var withDeprecatedList = withDeprecated.map( function( filter ) { return filter.id; } );
	var shouldUseEqualsAnyList = shouldUseEqualsAny.map( function( filter ) { return filter.id; } );
	
	console.log( 'Skipped due to not being able to view the pattern: ', skipList.join( ', ' ) );
	console.log( 'Using deprecated parameters: ', withDeprecatedList.join( ', ' ) );
	console.log( 'Should use equals to any: ', shouldUseEqualsAnyList.join( ', ' ) );
}
function testDeprecated( conds ){
	var deprecated = [
		"article_text",
		"article_prefixedtext",
		"article_namespace",
		"article_articleid",
		"article_restrictions_edit",
		"article_restrictions_move",
		"article_restrictions_create",
		"article_restrictions_upload",
		"article_recent_contributors",
		"article_first_contributor",
		"moved_from_text",
		"moved_from_prefixedtext",
		"moved_from_articleid",
		"moved_to_text",
		"moved_to_prefixedtext",
		"moved_to_articleid",
		"board_articleid", // From flow
		"board_text", // From flow
		"board_prefixedtext", // From flow
		"article_views", // From hitcounters
	];
	for ( var jjj = 0; jjj < deprecated.length; jjj++ ){
		if ( conds.includes( deprecated[jjj] ) ){
			console.log( conds, deprecated[jjj] );
			return true;
		}
	}
	return false;
}
function testEqualAny( conds ) {
	if ( conds.match( /\^\(\d+(\|\d+)+\)\$/ ) === null ) {
		return false;
	}
	return true;
}
});
//</nowiki>