Jump to content

User:DannyS712 test/Draft cleaner bot.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 DC_bot_config = {
	name: '[[User:DannyS712/Draft cleaner bot|Draft cleaner bot]]',
	version: 1.4,
	debug: true
};

var DC_bot_advert = "Task 27: Disable the categories on this page while it is still a draft, per [[WP:DRAFTNOCAT]], using " + DC_bot_config.name + " (v. " + DC_bot_config.version + ")";

var will_fix = [];
var unable_to_fix = [];

mw.loader.using( 'mediawiki.util', function () {
    $(document).ready( function () {
    	importScript( 'User:DannyS712 test/page.js' );
		if ( mw.config.get( 'wgPageName' ) === "Wikipedia:Database_reports/Polluted_categories_(2)") {
		    $(document).ready( function () { 
		    	mw.util.addPortletLink ( 'p-cactions', 'javascript:void(0)', 'Polluted categories 2', 'ca-pollutedTwo', 'TOOLTIP');
		    	$('#ca-pollutedTwo').on('click', function() {
		        	Categorized_drafts();
		    	} );
		    } );
		}
    } );
} );
function DC_bot ( category_name ) {
	return new Promise((resolve) => {
		console.log( category_name );
		var getDrafts = {
	        action: 'query',
	        list: 'categorymembers',
	        cmnamespace: 118,
	        cmlimit: 'max',
	        cmtitle: category_name,
	        cmprop: 'title',
	        format: 'json'
		};
		$.get( mw.config.get( 'wgScriptPath' ) + '/api.php', getDrafts, function( Drafts ) {
			//console.log(Drafts);
			var pages = Drafts.query.categorymembers;
			for (var i = 0; i < pages.length; i++) {
				if (pages[i].ns === 118){
					var name = pages[i].title;
					console.log("\tWill try:", name);
					will_fix.push( name );
				}
			}
			//location.reload();
			resolve();
		} );
	});
}
async function Categorized_drafts(){
	var content1 = get_page( mw.config.get( 'wgPageName' ) );
	if (DC_bot_config.debug) console.log( content1 );
	var content2 = content1.replace( /"/g, '&quot;');
	content2 = content2.replace( /\\/g, /\\\\/);
	if (DC_bot_config.debug) console.log( content2 );
	var content3 = content2.replace( /Categories that contain pages.*\n\n{\| class=&quot;wikitable sortable&quot;\s+! Category !! Drafts\n/, '{\n"Categories": [\n');
	if (DC_bot_config.debug) console.log( content3 );
	var content4 = content3.replace( /\|-\n\| \[\[:(Category:.*?)\]\].*/g, '"$1",');
	if (DC_bot_config.debug) console.log( content4 );
	var content5 = content4 + 'ENDING_STRING';
	if (DC_bot_config.debug) console.log( content5 );
	var content6 = content5.replace ( /,\n\|}ENDING_STRING/, '\n]}');
	if (DC_bot_config.debug) console.log( content6 );
	var as_JSON = JSON.parse( content6 );
	if (DC_bot_config.debug) console.log( as_JSON );
	var array = as_JSON.Categories;
	console.log( array );
	
	
	var page = "";
	for (var iii = 0; iii < array.length; iii++){
		page = array[iii];
		if (/Wikipedia/i.test( page )) console.log( "\tSkip: Wikipedia");
		else if (/purge/i.test( page )) console.log( "\tSkip: Purge");
		else {
			console.log( page );
			await DC_bot( page );
		}
	}
	var to_try = [...new Set(will_fix)];
	for (var jjj = 0; jjj < to_try.length; jjj++){
		await draft_no_cat(to_try[jjj]);
	}
	console.log("Unable to fix:", unable_to_fix);
	
}
function draft_no_cat( title ){
	return new Promise((resolve) => {
		var send_req = {
		    action: 'query',
		    titles: title,
		    prop: 'revisions',
		    intoken: 'edit',
		    rvprop: 'content',
		    indexpageids: 1,
		    dataType: 'xml',
		    format: 'xml'
		};
		$.get( mw.config.get( 'wgScriptPath' ) + '/api.php', send_req, function( response ) {
		    var text = $( response ).find( 'rev' ).text();
		    var new_text = text.replace( /\[\[Category/gi, '\[\[:Category' ); // Replace categorization with links
		    var submit = {
		        action: 'edit',
		        title: title,
		        text: new_text,
		        summary: DC_bot_advert,
		        minor: true,
		        bot: true,
		        token: mw.user.tokens.get( 'csrfToken' )
		    };
		    if ( text === new_text ) {
	    		console.log('Unable to fix: ' + title);
	    		unable_to_fix.push( '* [[' + title + ']]' );
		    } else {
			    $.when(
			        $.post( mw.config.get( 'wgScriptPath' ) + '/api.php', submit, function( response ){ } )
			    ).done( function() {
			    	console.log("\t\tDone: " + title);
			    } );
		    }
			resolve();
		} );
	} );
}
});
//</nowiki>