Jump to content

User:DannyS712 test/Outline 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 Outline_bot_config = {
	name: '[[User:DannyS712/Outline bot|Outline bot.js]]',
	testing: false,
	disclaimer: "Bot in trial ([[Wikipedia:Bots/Requests for approval/DannyS712 bot 20|BRFA]])",
	version: 1.3,
	debug: false
};

var Outline_bot_summary = 'Task 20: Add a short description with ' + Outline_bot_config.name + ' (version ' + Outline_bot_config.version + ')';
if (Outline_bot_config.testing){
	Outline_bot_summary = Outline_bot_config.disclaimer + ": " + Outline_bot_summary;
}

mw.loader.using( 'mediawiki.util', function () {
	importScript( 'User:DannyS712 test/page.js' );
    $(document).ready( function () { 
    	mw.util.addPortletLink ( 'p-cactions', 'javascript:void(0)', 'Outline bot', 'ca-outlineBot', 'Add a short description of this outline');
    	$('#ca-outlineBot').on('click', function() {
        	Outline_bot_run();
    	} );
    } );
} );
function Outline_bot_run(){
	var ar_of_pages = get_page_list( 'Category:Wikipedia_outlines', 0);
	if( Outline_bot_config.debug) console.log( ar_of_pages );
	for (var jjj = 0; jjj < ar_of_pages.length; jjj++){
		//if (jjj < 40)
		outline_run( ar_of_pages[jjj]);
	}
}
function outline_run( title ){
	var regex = /Outline of (.*)/i;
	var match = regex.exec( title );
	console.log( title, match );
	if (match !== null){
		var content = get_page( title );
		if( Outline_bot_config.debug) console.log( content );
		var already_has = false;
		if (content.toLowerCase().indexOf('{{short description\|') > -1) already_has = true;
		console.log( already_has );
		if (!(already_has)){
			if( Outline_bot_config.debug) console.log ( match );
			var short_desc = "{{Short description|1=Overview of and topical guide to " + match[1] + "}}";
			console.log ( short_desc );
			var new_content = short_desc + "\n" + content;
			if( Outline_bot_config.debug) console.log( new_content );
			set_new( title, new_content );
		}
	}
}
function get_page_list( category, current_depth ){
	var get_pages = {
        action: 'query',
        list: 'categorymembers',
        cmlimit: 'max',
        cmtitle: category,
        cmprop: 'title',
        format: 'json'
    };
    var result = null;
	$.ajax({
		url: scriptUrl,
		type: 'get',
		data: get_pages,
		dataType: 'json',
		async: false,
		success: function(catResponse) {
	    	if( Outline_bot_config.debug) console.log( catResponse );
			var pages = catResponse.query.categorymembers;
			if( Outline_bot_config.debug) console.log( pages );
			var good_pages = [];
			var sub_cats = [];
			for (var i = 0; i < pages.length; i++) {
				if ( pages[i].ns === 0 ) {
					var regex = /Outline of (.*)/i;
					var match = regex.exec( pages[i].title);
					if (match !== null) good_pages.push(pages[i].title);
				}
				else if (pages[i].ns === 14) {
					sub_cats.push(pages[i].title);
				}
			}
			if( Outline_bot_config.debug) console.log( good_pages );
			if( Outline_bot_config.debug) console.log( sub_cats );
			if (current_depth < 4){
				for (var j = 0; j < sub_cats.length; j++){
					good_pages = good_pages.concat( get_page_list(sub_cats[j], current_depth + 1));
				}
			}
			result = good_pages;
			if( Outline_bot_config.debug) console.log( result );
		} 
	});
	return result;
}
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( Outline_bot_config.debug) console.log( page );
	    	result = page.query.pages["0"].revisions["0"].content;
	    	if( Outline_bot_config.debug) console.log( result );
		} 
	});
	return result;
}
function set_new ( page, new_content ){
	if( Outline_bot_config.debug) console.log( page, new_content );
    var to_send = {
        action: 'edit',
        title: page,
        text: new_content,
        notminor: true,
        bot: true,
        summary: Outline_bot_summary,
        token: mw.user.tokens.get( 'csrfToken' )
    };
    console.log( to_send );
    $.when(
        $.post( scriptUrl, to_send, function( response ){ } )
    ).done( function() {
        if( Outline_bot_config.debug) console.log( response );
    } );
}
});
//</nowiki>