Jump to content

User:DannyS712 test/Stub 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 SN_bot_config = {
	name: '[[User:DannyS712/Stub bot|Stub bot]]',
	version: 1.3,
	debug: false
};

var SN_bot_advert = 'Task 14: Correct name parameter with ' + SN_bot_config.name + ' (version ' + SN_bot_config.version + ')';

var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';

mw.loader.using( 'mediawiki.util', function () {
    $(document).ready( function () {
    	if ( mw.config.get( 'wgPageName' ) === "Category:Stub_message_templates_needing_attention") {
        var link = mw.util.addPortletLink( 'p-cactions', 'javascript:void(0)', 'SN bot', 'ca-SNbot', 'SN bot'); 
	        $( link ).click( function ( event ) {
	            event.preventDefault();
	            SN_bot();
	        } );
    	}
    } );
} );
function SN_bot () {
	var getStubs = {
        action: 'query',
        list: 'categorymembers',
        cmlimit: 'max',
        cmtitle: 'Category:Stub message templates needing attention',
        cmprop: 'title',
        format: 'json'
	};
	$.get( mw.config.get( 'wgScriptPath' ) + '/api.php', getStubs, function( Stubs ) {
		console.log(Stubs);
		var pages = Stubs.query.categorymembers;
		console.log(pages);
		for (var i = 0; i < pages.length; i++) {
			stub_name( pages[i].title );
		}
		//location.reload();
	} );
}
function stub_name( name ){
	var page = get_page ( name );
	if (SN_bot_config.debug) console.log ( page );
	var new_page = page.replace(/(\|\s*name\s*=).*/i, "$1 {{subst:FULLPAGENAME}}");
	if (SN_bot_config.debug) console.log ( new_page );
	set_new ( name, new_page );
}
function get_page( name ){
    var page_to_get = {
        action: 'query',
        titles: name,
        prop: 'revisions',
        rvprop: 'content',
        format: 'json',
        formatversion: 2
    };
    var result = null;
	$.ajax({
		url: scriptUrl,
		type: 'get',
		data: page_to_get,
		dataType: 'json',
		async: false,
		success: function(page) {
			if (SN_bot_config.debug) console.log( page );
	    	result = page.query.pages["0"].revisions["0"].content;
	    	if (SN_bot_config.debug) console.log( result );
		} 
	});
	return result;
}
function set_new ( page, new_content ){
	console.log( page, new_content );
    var to_send = {
        action: 'edit',
        title: page,
        text: new_content,
        minor: true,
        bot: true,
        summary: SN_bot_advert,
        token: mw.user.tokens.get( 'csrfToken' )
    };
    console.log( to_send );
    
    $.when(
        $.post( scriptUrl, to_send, function( response ){ } )
    ).done( function() {
		//location.reload();
    } );
    
}
});
//</nowiki>