Jump to content

User:DannyS712 test/DNC 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.
$(function (){
var DNC_bot_config = {
	name: '[[User:DannyS712/DNC bot|DNC bot]]',
	version: 1.3,
//	testing: false,
//	disclaimer: "Bot in trial ([[Wikipedia:Bots/Requests for approval/DannyS712 bot 3|BRFA]])",
	debug: true
};

var DNC_bot_advert = "Task 3: Disable the categories on this page while it is still a draft, per [[WP:DRAFTNOCAT]]/[[WP:USERNOCAT]], using " + DNC_bot_config.name + " (v. " + DNC_bot_config.version + ")";
//if (DNC_bot_config.testing){
//	DNC_bot_advert = DNC_bot_config.disclaimer + ": " + DNC_bot_advert;
//}

mw.loader.using( 'mediawiki.util', function () {
    $(document).ready( function () {
    	//console.log ( mw.config.get( 'wgPageName' ) );
    	if ( mw.config.get( 'wgPageName' ) === "Category:AfC_submissions_with_categories") {
        var link = mw.util.addPortletLink( 'p-cactions', 'javascript:void(0)', 'DNC bot', 'ca-DNCbot', 'DNC bot'); 
	        $( link ).click( function ( event ) {
	            event.preventDefault();
	            DNC_bot();
	        } );
    	}
    } );
} );
function DNC_bot () {
	var getAFCdrafts = {
        action: 'query',
        list: 'categorymembers',
        cmlimit: 'max',
        //cmtitle: mw.config.get( 'wgPageName' ),
        cmtitle: 'Category:AfC submissions with categories',
        cmprop: 'title',
        format: 'json'
	};
	$.get( mw.config.get( 'wgScriptPath' ) + '/api.php', getAFCdrafts, function( AFCdrafts ) {
		//console.log(AFCdrafts);
		var pages = AFCdrafts.query.categorymembers;
		//var listed = [];
		//var links = "";
		for (var i = 0; i < pages.length; i++) {
			if (pages[i].ns === 2 || pages[i].ns === 118){
				var name = pages[i].title;
				draft_no_cat( name );
				/***
				console.log( name );
				if ( name === "User:DannyS712 test/sandbox" || name === "User:DannyS712 test" ){
					console.log(true);
					draft_no_cat( name );
				}***/
				//listed.push('* [[' + pages[i].title + ']]\n');
				//links = links + listed[i];
			}
		}
		//location.reload();
	} );
}
function draft_no_cat( title ){
	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();
	    text = text
	        .replace( /\[\[Category/gi, '\[\[:Category' ); // Replace categorization with links
	    var submit = {
	        action: 'edit',
	        title: title,
	        text: text,
	        summary: DNC_bot_advert,
	        minor: true,
	        bot: true,
	        token: mw.user.tokens.get( 'csrfToken' )
	    };
	    $.when(
	        $.post( mw.config.get( 'wgScriptPath' ) + '/api.php', submit, function( response ){ } )
	    ).done( function() {
	    	location.reload();
	    } );
	} );
}
});