User:DannyS712 test/Draft cleaner bot.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:DannyS712 test/Draft cleaner bot. |
//<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, '"');
content2 = content2.replace( /\\/g, /\\\\/);
if (DC_bot_config.debug) console.log( content2 );
var content3 = content2.replace( /Categories that contain pages.*\n\n{\| class="wikitable sortable"\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>